aboutsummaryrefslogtreecommitdiff
path: root/src/vogar/Run.java
blob: 2725935fa3330b888939b6edaad1cb0a79063a40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package vogar;

import com.google.common.base.Splitter;

import com.google.common.base.Supplier;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import vogar.android.ActivityMode;
import vogar.android.AndroidSdk;
import vogar.android.DeviceRuntime;
import vogar.android.HostRuntime;
import vogar.commands.Mkdir;
import vogar.commands.Rm;
import vogar.tasks.TaskQueue;
import vogar.util.Strings;

public final class Run {
    /**
     * A list of generic names that we avoid when naming generated files.
     */
    private static final Set<String> BANNED_NAMES = new HashSet<String>();

    private static final String JAR_URI_PREFIX = "jar:file:";

    private static final String FILE_URL_PREFIX = "file:";

    private static final String VOGAR_CLASS_RESOURCE_PATH = "/vogar/Vogar.class";

    static {
        BANNED_NAMES.add("classes");
        BANNED_NAMES.add("javalib");
    }

    public final File xmlReportsDirectory;
    public final File resultsDir;
    public final boolean recordResults;
    public final ExpectationStore expectationStore;
    public final Date date;
    public final String invokeWith;
    public final File keystore;
    public final Log log;
    public final Classpath classpath;
    public final Classpath buildClasspath;
    public final Classpath resourceClasspath;
    public final List<File> sourcepath;
    public final Mkdir mkdir;
    public final Rm rm;
    public final int firstMonitorPort;
    public final int timeoutSeconds;
    public final File javaHome;
    public final List<String> excludeFilters;
    public final Integer debugPort;
    public final Language language;
    public final List<String> javacArgs;
    public final boolean multidex;
    public final boolean benchmark;
    public final File runnerDir;
    public final boolean cleanBefore;
    public final boolean cleanAfter;
    public final File localTemp;
    public final int maxConcurrentActions;
    public final File deviceUserHome;
    public final Console console;
    public final int smallTimeoutSeconds;
    public final String vmCommand;
    public final String dalvikCache;
    public final List<String> additionalVmArgs;
    public final List<String> targetArgs;
    public final boolean useBootClasspath;
    public final int largeTimeoutSeconds;
    public final RetrievedFilesFilter retrievedFiles;
    public final Driver driver;
    public final Mode mode;
    public final Target target;
    public final AndroidSdk androidSdk;
    public final XmlReportPrinter reportPrinter;
    public final JarSuggestions jarSuggestions;
    public final ClassFileIndex classFileIndex;
    public final OutcomeStore outcomeStore;
    public final TaskQueue taskQueue;
    public final RunnerType runnerType;
    public final Toolchain toolchain;
    public final boolean checkJni;
    public final boolean debugging;
    public final Integer sdkVersion;
    public final boolean serialDexing;
    public final boolean verboseDexStats;

    public Run(Vogar vogar, Toolchain toolchain, Console console, Mkdir mkdir,
            AndroidSdk androidSdk, Rm rm, Target target, File runnerDir)
            throws IOException {
        this.console = console;

        this.localTemp = new File("/tmp/vogar/" + UUID.randomUUID());
        this.log = console;

        this.target = target;

        this.toolchain = toolchain;
        this.vmCommand = vogar.vmCommand;
        this.dalvikCache = vogar.dalvikCache;
        this.additionalVmArgs = vogar.vmArgs;
        this.benchmark = vogar.benchmark;
        this.cleanBefore = vogar.cleanBefore;
        this.cleanAfter = vogar.cleanAfter;
        this.date = new Date();
        this.debugPort = vogar.debugPort;
        this.runnerDir = runnerDir;
        this.deviceUserHome = new File(runnerDir, "user.home");
        this.mkdir = mkdir;
        this.rm = rm;
        this.firstMonitorPort = vogar.firstMonitorPort;
        this.invokeWith = vogar.invokeWith;
        this.language = vogar.language;
        this.javacArgs = vogar.javacArgs;
        this.multidex = vogar.multidex;
        this.javaHome = vogar.javaHome;
        this.excludeFilters = new ArrayList<>();
        this.excludeFilters.addAll(vogar.excludeFilters);
        this.largeTimeoutSeconds = vogar.timeoutSeconds * Vogar.LARGE_TIMEOUT_MULTIPLIER;
        this.maxConcurrentActions = (vogar.stream || vogar.modeId == ModeId.ACTIVITY)
                    ? 1
                    : Vogar.NUM_PROCESSORS;
        this.timeoutSeconds = vogar.timeoutSeconds;
        this.smallTimeoutSeconds = vogar.timeoutSeconds;
        this.sourcepath = vogar.sourcepath;
        this.resourceClasspath = Classpath.of(vogar.resourceClasspath);
        this.useBootClasspath = vogar.useBootClasspath;
        this.targetArgs = vogar.targetArgs;
        this.xmlReportsDirectory = vogar.xmlReportsDirectory;
        this.recordResults = vogar.recordResults;
        this.resultsDir = vogar.resultsDir == null
                ? new File(vogar.vogarDir, "results")
                : vogar.resultsDir;
        this.keystore = localFile("activity", "vogar.keystore");
        this.classpath = Classpath.of(vogar.classpath);
        this.classpath.addAll(vogarJar());
        this.runnerType = vogar.runnerType;

        this.androidSdk = androidSdk;

        expectationStore = ExpectationStore.parse(
            console, vogar.expectationFiles, vogar.modeId, vogar.variant);
        if (vogar.openBugsCommand != null) {
            expectationStore.loadBugStatuses(new CommandBugDatabase(log, vogar.openBugsCommand));
        }

        this.mode = createMode(vogar.modeId, vogar.variant);

        this.buildClasspath = Classpath.of(vogar.buildClasspath);
        if (androidSdk != null) {
            buildClasspath.addAll(androidSdk.getCompilationClasspath());
        }

        this.classFileIndex = new ClassFileIndex(log, mkdir, vogar.jarSearchDirs);
        if (vogar.suggestClasspaths) {
            classFileIndex.createIndex();
        }

        this.retrievedFiles = new RetrievedFilesFilter();
        this.reportPrinter = new XmlReportPrinter(xmlReportsDirectory, expectationStore, date);
        this.jarSuggestions = new JarSuggestions();
        this.outcomeStore = new OutcomeStore(log, mkdir, rm, resultsDir, recordResults,
                expectationStore, date);
        this.driver = new Driver(this);
        this.taskQueue = new TaskQueue(console, maxConcurrentActions);
        this.checkJni = vogar.checkJni;
        this.debugging = (vogar.debugPort != null) || vogar.debugApp;
        this.sdkVersion = vogar.sdkVersion;
        this.serialDexing = vogar.serialDexing;
        this.verboseDexStats = vogar.verboseDexStats;
    }

    private Mode createMode(ModeId modeId, Variant variant) {
        switch (modeId) {
            case JVM:
                return new JavaVm(this);
            case HOST:
                return new HostRuntime(this, modeId, variant);
            case DEVICE:
            case APP_PROCESS:
                return new DeviceRuntime(this, modeId, variant, new Supplier<String>() {
                    @Override
                    public String get() {
                        return target.getDeviceUserName();
                    }
                });
            case ACTIVITY:
                return new ActivityMode(this);
            default:
                throw new IllegalArgumentException("Unsupported mode: " + modeId);
        }
    }

    public final File localFile(Object... path) {
        return new File(localTemp + "/" + Strings.join("/", path));
    }

    public final File localDir(Object... path) {
      String joinedPath = localTemp + "/" + Strings.join("/", path);
      File f = new File(joinedPath);
      f.mkdirs();
      if (!f.exists()) {
        throw new AssertionError("Failed to mkdirs: " + joinedPath);
      }
      return f;
    }

    private File vogarJar() {
        URL jarUrl = Vogar.class.getResource(VOGAR_CLASS_RESOURCE_PATH);
        if (jarUrl == null) {
            // should we add an option for IDE users, to use a user-specified vogar.jar?
            throw new IllegalStateException("Vogar cannot find its own .jar");
        }

        /*
         * Parse a URI like jar:file:/Users/jessewilson/vogar/vogar.jar!/vogar/Vogar.class
         * to yield a .jar file like /Users/jessewilson/vogar/vogar.jar.
         */
        String url = jarUrl.toString();
        int bang = url.indexOf("!");
        if (url.startsWith(JAR_URI_PREFIX) && bang != -1) {
            return new File(url.substring(JAR_URI_PREFIX.length(), bang));
        } else if (url.startsWith(FILE_URL_PREFIX) && url.endsWith(VOGAR_CLASS_RESOURCE_PATH)) {
            // Vogar is being run from a classes directory.
            return new File(url.substring(FILE_URL_PREFIX.length(),
                    url.length() - VOGAR_CLASS_RESOURCE_PATH.length()));
        } else {
            throw new IllegalStateException("Vogar cannot find the .jar file in " + jarUrl);
        }
    }

    public final File hostJar(Object nameOrAction) {
        return localFile(nameOrAction, nameOrAction + ".jar");
    }

    /**
     * Returns a path for a Java tool such as java, javac, jar where
     * the Java home is used if present, otherwise assumes it will
     * come from the path.
     */
    public String javaPath(String tool) {
        return (javaHome == null)
            ? tool
            : new File(new File(javaHome, "bin"), tool).getPath();
    }

    public File targetDexFile(String name) {
        return new File(runnerDir, name + ".dex.jar");
    }

    public File localDexFile(String name) {
        return localFile(name, name + ".dex.jar");
    }

    /**
     * Returns a recognizable readable name for the given generated .jar file,
     * appropriate for use in naming derived files.
     *
     * @param file a product of the android build system, such as
     *     "out/core-libart_intermediates/javalib.jar".
     * @return a recognizable base name like "core-libart_intermediates".
     */
    public String basenameOfJar(File file) {
        String name = file.getName().replaceAll("(\\.jar)$", "");
        while (BANNED_NAMES.contains(name)) {
            file = file.getParentFile();
            name = file.getName();
        }
        return name;
    }

    public File vogarTemp() {
        return new File(runnerDir, "tmp");
    }

    public File dalvikCache() {
        return new File(runnerDir.getParentFile(), dalvikCache);
    }

    /**
     * Returns the directory where the VM stores its dexopt files.
     */
    public String getAndroidDataPath() {
        // The VM wants the parent directory of a directory named "dalvik-cache"
        return dalvikCache().getParentFile().getPath();
    }

    /**
     * Returns a parsed list of the --invoke-with command and its
     * arguments, or an empty list if no --invoke-with was provided.
     */
    public Iterable<String> invokeWith() {
        if (invokeWith == null) {
            return Collections.emptyList();
        }
        return Splitter.onPattern("\\s+").omitEmptyStrings().split(invokeWith);
    }
}