blob: 60128c5154782a2722fef5fee2bc30724efca022 [file] [log] [blame]
Ian Zernyb2d27c42019-02-20 09:09:41 +01001// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4package tasks;
5
6import java.io.BufferedReader;
7import java.io.File;
8import java.io.IOException;
9import java.io.InputStreamReader;
10import java.nio.charset.StandardCharsets;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.List;
15import java.util.stream.Collectors;
Jake Whartona138d5b2019-09-17 11:53:52 -040016import javax.inject.Inject;
Ian Zernyb2d27c42019-02-20 09:09:41 +010017import org.gradle.api.DefaultTask;
18import org.gradle.api.tasks.InputFiles;
19import org.gradle.api.tasks.OutputDirectory;
20import org.gradle.api.tasks.TaskAction;
21import org.gradle.internal.os.OperatingSystem;
Jake Whartona138d5b2019-09-17 11:53:52 -040022import org.gradle.workers.IsolationMode;
23import org.gradle.workers.WorkerExecutor;
Ian Zernyb2d27c42019-02-20 09:09:41 +010024
25public class DownloadDependency extends DefaultTask {
26
27 public enum Type {
28 GOOGLE_STORAGE,
29 X20
30 }
31
Jake Whartona138d5b2019-09-17 11:53:52 -040032 private final WorkerExecutor workerExecutor;
33
Ian Zernyb2d27c42019-02-20 09:09:41 +010034 private Type type;
Ian Zernyb2d27c42019-02-20 09:09:41 +010035 private File outputDir;
36 private File tarGzFile;
37 private File sha1File;
38
Jake Whartona138d5b2019-09-17 11:53:52 -040039 @Inject
40 public DownloadDependency(WorkerExecutor workerExecutor) {
41 this.workerExecutor = workerExecutor;
42 }
43
Ian Zernyb2d27c42019-02-20 09:09:41 +010044 public void setType(Type type) {
45 this.type = type;
46 }
47
48 public void setDependency(String dependency) {
Ian Zernyb2d27c42019-02-20 09:09:41 +010049 outputDir = new File(dependency);
50 tarGzFile = new File(dependency + ".tar.gz");
51 sha1File = new File(dependency + ".tar.gz.sha1");
52 }
53
54 @InputFiles
55 public Collection<File> getInputFiles() {
56 return Arrays.asList(sha1File, tarGzFile);
57 }
58
59 @OutputDirectory
60 public File getOutputDir() {
61 return outputDir;
62 }
63
64 public File getSha1File() {
65 return sha1File;
66 }
67
68 public File getTarGzFile() {
69 return tarGzFile;
70 }
71
72 @TaskAction
73 public void execute() throws IOException, InterruptedException {
74 // First run will write the tar.gz file, causing the second run to still be out-of-date.
75 // Check if the modification time of the tar is newer than the sha in which case we are done.
76 // Also, check the contents of the out directory because gradle appears to create it for us...
77 if (outputDir.exists()
78 && outputDir.isDirectory()
79 && outputDir.list().length > 0
80 && tarGzFile.exists()
81 && sha1File.lastModified() <= tarGzFile.lastModified()) {
82 return;
83 }
84 if (outputDir.exists() && outputDir.isDirectory()) {
85 outputDir.delete();
86 }
Jake Whartona138d5b2019-09-17 11:53:52 -040087 workerExecutor.submit(RunDownload.class, config -> {
88 config.setIsolationMode(IsolationMode.NONE);
89 config.params(type, sha1File);
90 });
Ian Zernyb2d27c42019-02-20 09:09:41 +010091 }
92
Jake Whartona138d5b2019-09-17 11:53:52 -040093 public static class RunDownload implements Runnable {
94 private final Type type;
95 private final File sha1File;
96
97 @Inject
98 public RunDownload(Type type, File sha1File) {
99 this.type = type;
100 this.sha1File = sha1File;
101 }
102
103 @Override
104 public void run() {
105 try {
106 if (type == Type.GOOGLE_STORAGE) {
107 downloadFromGoogleStorage();
108 } else if (type == Type.X20) {
109 downloadFromX20();
110 } else {
111 throw new RuntimeException("Unexpected or missing dependency type: " + type);
112 }
113 } catch (Exception e) {
114 throw new RuntimeException(e);
115 }
116 }
117
118 private void downloadFromGoogleStorage() throws IOException, InterruptedException {
119 List<String> args = Arrays.asList("-n", "-b", "r8-deps", "-s", "-u", sha1File.toString());
120 if (OperatingSystem.current().isWindows()) {
121 List<String> command = new ArrayList<>();
122 command.add("download_from_google_storage.bat");
123 command.addAll(args);
124 runProcess(new ProcessBuilder().command(command));
125 } else {
126 runProcess(
127 new ProcessBuilder()
128 .command("bash", "-c", "download_from_google_storage " + String.join(" ", args)));
129 }
130 }
131
132 private void downloadFromX20() throws IOException, InterruptedException {
133 if (OperatingSystem.current().isWindows()) {
134 throw new RuntimeException("Downloading from x20 unsupported on windows");
135 }
Ian Zernyb2d27c42019-02-20 09:09:41 +0100136 runProcess(
137 new ProcessBuilder()
Jake Whartona138d5b2019-09-17 11:53:52 -0400138 .command("bash", "-c", "tools/download_from_x20.py " + sha1File.toString()));
Ian Zernyb2d27c42019-02-20 09:09:41 +0100139 }
Ian Zernyb2d27c42019-02-20 09:09:41 +0100140
Jake Whartona138d5b2019-09-17 11:53:52 -0400141 private static void runProcess(ProcessBuilder builder)
142 throws IOException, InterruptedException {
143 String command = String.join(" ", builder.command());
144 Process p = builder.start();
145 int exit = p.waitFor();
146 if (exit != 0) {
147 throw new IOException(
148 "Process failed for "
149 + command
150 + "\n"
151 + new BufferedReader(
152 new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8))
153 .lines()
154 .collect(Collectors.joining("\n")));
155 }
Ian Zernyb2d27c42019-02-20 09:09:41 +0100156 }
157 }
158}