Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit e84dfb6

Browse files
IanChildsfacebook-github-bot
authored andcommitted
Add executable for running D8Command
Reviewed By: mykola-semko fbshipit-source-id: fe264065a850d1af9b084a658eba2ee65719a4b9
1 parent 9af214b commit e84dfb6

4 files changed

Lines changed: 134 additions & 0 deletions

File tree

src/com/facebook/buck/android/dex/BUCK

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@ java_library(
66
],
77
deps = [
88
"//third-party/java/d8:d8",
9+
"//third-party/java/guava:guava",
10+
],
11+
)
12+
13+
java_binary(
14+
name = "run_d8_binary",
15+
main_class = "com.facebook.buck.android.dex.D8ExecutableMain",
16+
deps = [
17+
":dex",
918
],
1019
)
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.facebook.buck.android.dex;
18+
19+
import com.android.tools.r8.CompilationFailedException;
20+
import com.google.common.collect.ImmutableSet;
21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.util.Collection;
26+
import java.util.Optional;
27+
import java.util.logging.Logger; // NOPMD
28+
29+
/**
30+
* Main entry point for executing {@link com.android.tools.r8.D8Command} calls.
31+
*
32+
* <p>Expected usage: {@code this_binary <output_dex_file> <files_to_dex_path> <android_jar_path>
33+
* options}.
34+
*/
35+
public class D8ExecutableMain {
36+
37+
private static final Logger LOG = Logger.getLogger(D8ExecutableMain.class.getName());
38+
39+
public static void main(String[] args) throws IOException {
40+
if (args.length < 3) {
41+
LOG.severe(
42+
"Must specify an output dex file, a file containing the files to dex, and an android jar path, plus any other optional parameters");
43+
System.exit(1);
44+
}
45+
46+
Path outputDexFile = Paths.get(args[0]);
47+
Path filesToDexPath = Paths.get(args[1]);
48+
ImmutableSet<Path> filesToDex =
49+
Files.readAllLines(filesToDexPath).stream()
50+
.map(Paths::get)
51+
.collect(ImmutableSet.toImmutableSet());
52+
Path androidJarPath = Paths.get(args[2]);
53+
54+
Optional<Path> primaryDexClassNamesPath = Optional.empty();
55+
Optional<Path> referencedResourcesPath = Optional.empty();
56+
ImmutableSet.Builder<D8Options> d8OptionsBuilder = ImmutableSet.builder();
57+
Collection<Path> classpathFiles = null;
58+
Optional<String> bucketId = Optional.empty();
59+
Optional<Integer> minSdkVersion = Optional.empty();
60+
61+
for (int argsConsumed = 3; argsConsumed < args.length; argsConsumed++) {
62+
String arg = args[argsConsumed];
63+
64+
switch (arg) {
65+
case "--intermediate":
66+
d8OptionsBuilder.add(D8Options.INTERMEDIATE);
67+
break;
68+
case "--no-optimize":
69+
d8OptionsBuilder.add(D8Options.NO_OPTIMIZE);
70+
break;
71+
case "--force-jumbo":
72+
d8OptionsBuilder.add(D8Options.FORCE_JUMBO);
73+
break;
74+
case "--no-desugar":
75+
d8OptionsBuilder.add(D8Options.NO_DESUGAR);
76+
break;
77+
case "--primary-dex-class-names-path":
78+
primaryDexClassNamesPath = Optional.of(Paths.get(args[++argsConsumed]));
79+
break;
80+
case "--referenced-resources-path":
81+
referencedResourcesPath = Optional.of(Paths.get(args[++argsConsumed]));
82+
break;
83+
case "--classpathFiles":
84+
Path classpathFilesPath = Paths.get(args[++argsConsumed]);
85+
classpathFiles =
86+
Files.readAllLines(classpathFilesPath).stream()
87+
.map(Paths::get)
88+
.collect(ImmutableSet.toImmutableSet());
89+
break;
90+
case "--bucket-id":
91+
bucketId = Optional.of(args[++argsConsumed]);
92+
break;
93+
case "--min-sdk-version":
94+
minSdkVersion = Optional.of(Integer.parseInt(args[++argsConsumed]));
95+
break;
96+
default:
97+
throw new RuntimeException("Unknown arg: " + arg);
98+
}
99+
}
100+
101+
try {
102+
Collection<String> referencedResources =
103+
D8Utils.runD8Command(
104+
new D8Utils.D8DiagnosticsHandler(),
105+
outputDexFile,
106+
filesToDex,
107+
d8OptionsBuilder.build(),
108+
primaryDexClassNamesPath,
109+
androidJarPath,
110+
classpathFiles,
111+
bucketId,
112+
minSdkVersion);
113+
114+
if (referencedResourcesPath.isPresent()) {
115+
Files.write(referencedResourcesPath.get(), referencedResources);
116+
}
117+
} catch (CompilationFailedException e) {
118+
throw new IOException(e);
119+
}
120+
121+
System.exit(0);
122+
}
123+
}

third-party/java/d8/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ prebuilt_jar(
99
"//third-party/java/asm:asm",
1010
"//third-party/java/commons-compress:commons-compress",
1111
"//third-party/java/fastutil:fastutil",
12+
"//third-party/java/gson:gson",
1213
"//third-party/java/guava:guava",
1314
"//third-party/java/jopt-simple:jopt-simple",
1415
"//third-party/java/json-simple:json-simple",

third-party/java/gson/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ prebuilt_jar(
1616
"//starlark/...",
1717
"//test/com/facebook/buck/worker:worker", # Legacy caller
1818
"//third-party/java/aosp:aosp", # Sad transitive deps are sad
19+
"//third-party/java/d8:d8", # Legacy caller
1920
"//third-party/java/grpc:grpc-core", # Legacy caller which should probably use Jackson
2021
"//tools/ideabuck:", # Not inside buck
2122
],

0 commit comments

Comments
 (0)