1717package com .facebook .buck .android ;
1818
1919import com .android .tools .r8 .CompilationFailedException ;
20- import com .android .tools .r8 .CompilationMode ;
21- import com .android .tools .r8 .D8Command ;
2220import com .android .tools .r8 .Diagnostic ;
23- import com .android .tools .r8 .DiagnosticsHandler ;
24- import com .android .tools .r8 .OutputMode ;
2521import com .android .tools .r8 .utils .AbortException ;
26- import com .android .tools .r8 .utils .InternalOptions ;
2722import com .facebook .buck .android .dex .D8Options ;
23+ import com .facebook .buck .android .dex .D8Utils ;
2824import com .facebook .buck .android .toolchain .AndroidPlatformTarget ;
2925import com .facebook .buck .core .build .execution .context .IsolatedExecutionContext ;
3026import com .facebook .buck .event .ConsoleEvent ;
3632import com .google .common .collect .ImmutableList ;
3733import com .google .common .collect .ImmutableSet ;
3834import com .google .common .collect .Sets ;
39- import java .io .File ;
4035import java .io .IOException ;
41- import java .nio .file .Files ;
4236import java .nio .file .Path ;
43- import java .nio .file .StandardCopyOption ;
44- import java .util .ArrayList ;
4537import java .util .Collection ;
4638import java .util .EnumSet ;
47- import java .util .HashSet ;
48- import java .util .List ;
4939import java .util .Optional ;
5040import java .util .Set ;
51- import java .util .stream .Stream ;
41+ import java .util .stream .Collectors ;
5242import javax .annotation .Nullable ;
5343
5444/** Runs d8. */
@@ -137,65 +127,24 @@ public StepExecutionResult executeIsolatedStep(IsolatedExecutionContext context)
137127 }
138128
139129 private int executeInProcess (IsolatedExecutionContext context ) throws IOException {
140- D8DiagnosticsHandler diagnosticsHandler = new D8DiagnosticsHandler ();
130+ D8Utils . D8DiagnosticsHandler diagnosticsHandler = new D8Utils . D8DiagnosticsHandler ();
141131 try {
142- Set <Path > inputs = new HashSet <>();
143- for (Path rawFile : filesToDex ) {
144- Path toDex = filesystem .resolve (rawFile );
145- if (Files .isRegularFile (toDex )) {
146- inputs .add (toDex );
147- } else {
148- try (Stream <Path > paths = Files .walk (toDex )) {
149- paths .filter (path -> path .toFile ().isFile ()).forEach (inputs ::add );
150- }
151- }
152- }
153-
154- // D8 only outputs to dex if the output path is a directory. So we output to a temporary dir
155- // and move it over to the final location
156- boolean outputToDex = outputDexFile .getFileName ().toString ().endsWith (".dex" );
157- Path output = outputToDex ? Files .createTempDirectory ("buck-d8" ) : outputDexFile ;
158-
159- D8Command .Builder builder =
160- D8Command .builder (diagnosticsHandler )
161- .addProgramFiles (inputs )
162- .setIntermediate (options .contains (D8Options .INTERMEDIATE ))
163- .addLibraryFiles (androidPlatformTarget .getAndroidJar ())
164- .setMode (
165- options .contains (D8Options .NO_OPTIMIZE )
166- ? CompilationMode .DEBUG
167- : CompilationMode .RELEASE )
168- .setOutput (output , OutputMode .DexIndexed )
169- .setDisableDesugaring (options .contains (D8Options .NO_DESUGAR ))
170- .setInternalOptionsModifier (
171- (InternalOptions opt ) -> {
172- opt .testing .forceJumboStringProcessing =
173- options .contains (D8Options .FORCE_JUMBO );
174- });
175-
176- bucketId .ifPresent (builder ::setBucketId );
177- minSdkVersion .ifPresent (builder ::setMinApiLevel );
178- primaryDexClassNamesPath .ifPresent (builder ::addMainDexListFiles );
132+ resourcesReferencedInCode =
133+ D8Utils .runD8Command (
134+ diagnosticsHandler ,
135+ outputDexFile ,
136+ filesToDex .stream ().map (filesystem ::resolve ).collect (Collectors .toList ()),
137+ options ,
138+ primaryDexClassNamesPath ,
139+ androidPlatformTarget .getAndroidJar (),
140+ classpathFiles == null
141+ ? null
142+ : classpathFiles .stream ()
143+ .map (filesystem ::getPathForRelativeExistingPath )
144+ .collect (Collectors .toList ()),
145+ bucketId ,
146+ minSdkVersion );
179147
180- if (classpathFiles != null && !classpathFiles .isEmpty ()) {
181- // classpathFiles is needed only for D8 java 8 desugar
182- ImmutableSet .Builder <Path > absolutePaths = ImmutableSet .builder ();
183- for (Path classpathFile : classpathFiles ) {
184- absolutePaths .add (filesystem .getPathForRelativeExistingPath (classpathFile ));
185- }
186- builder .addClasspathFiles (absolutePaths .build ());
187- }
188- D8Command d8Command = builder .build ();
189- com .android .tools .r8 .D8 .run (d8Command );
190-
191- if (outputToDex ) {
192- File [] outputs = output .toFile ().listFiles ();
193- if (outputs != null && (outputs .length > 0 )) {
194- Files .move (outputs [0 ].toPath (), outputDexFile , StandardCopyOption .REPLACE_EXISTING );
195- }
196- }
197-
198- resourcesReferencedInCode = d8Command .getDexItemFactory ().computeReferencedResources ();
199148 return SUCCESS_EXIT_CODE ;
200149 } catch (CompilationFailedException e ) {
201150 if (isOverloadedDexException (e )) {
@@ -222,7 +171,7 @@ private boolean isOverloadedDexException(CompilationFailedException e) {
222171 }
223172
224173 private void postCompilationFailureToConsole (
225- IsolatedExecutionContext context , D8DiagnosticsHandler diagnosticsHandler ) {
174+ IsolatedExecutionContext context , D8Utils . D8DiagnosticsHandler diagnosticsHandler ) {
226175 context .postEvent (
227176 ConsoleEvent .severe (
228177 String .join (
@@ -292,19 +241,6 @@ Collection<String> getResourcesReferencedInCode() {
292241 return resourcesReferencedInCode ;
293242 }
294243
295- private static class D8DiagnosticsHandler implements DiagnosticsHandler {
296-
297- private final List <Diagnostic > diagnostics = new ArrayList <>();
298-
299- @ Override
300- public void warning (Diagnostic warning ) {
301- diagnostics .add (warning );
302- }
303-
304- @ Override
305- public void info (Diagnostic info ) {}
306- }
307-
308244 public Path getOutputDexFile () {
309245 return outputDexFile ;
310246 }
0 commit comments