1
+ // Copyright 2016 The Bazel Authors. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ package com .google .devtools .build .buildjar ;
16
+
17
+ import java .io .IOException ;
18
+ import java .io .OutputStreamWriter ;
19
+ import java .io .PrintWriter ;
20
+ import java .io .Writer ;
21
+ import java .nio .charset .Charset ;
22
+ import java .time .Duration ;
23
+ import java .util .Arrays ;
24
+ import java .util .List ;
25
+ import java .util .function .Supplier ;
26
+
27
+ import com .google .common .annotations .VisibleForTesting ;
28
+ import com .google .common .collect .ImmutableList ;
29
+ import com .google .devtools .build .buildjar .javac .BlazeJavacResult ;
30
+ import com .google .devtools .build .buildjar .javac .BlazeJavacResult .Status ;
31
+ import com .google .devtools .build .buildjar .javac .FormattedDiagnostic ;
32
+ import com .google .devtools .build .buildjar .javac .JavacOptions ;
33
+ import com .google .devtools .build .buildjar .javac .plugins .BlazeJavaCompilerPlugin ;
34
+ import com .google .devtools .build .buildjar .javac .plugins .dependency .DependencyModule ;
35
+ import com .google .devtools .build .lib .worker .ProtoWorkerMessageProcessor ;
36
+ import com .google .devtools .build .lib .worker .WorkRequestHandler ;
37
+ import com .google .devtools .build .lib .worker .WorkRequestHandler .WorkRequestCallback ;
38
+ import com .google .devtools .build .lib .worker .WorkRequestHandler .WorkRequestHandlerBuilder ;
39
+
40
+ /** The JavaBuilder main called by bazel. (renamed from BazelJavaBuilder) */
41
+ public class BazelEcjJavaBuilder {
42
+
43
+ private static final String CMDNAME = "BazelEcjJavaBuilder" ;
44
+
45
+ /** The main method of the BazelJavaBuilder. */
46
+ public static void main (String [] args ) {
47
+ BazelEcjJavaBuilder builder = new BazelEcjJavaBuilder ();
48
+ if (args .length == 1 && args [0 ].equals ("--persistent_worker" )) {
49
+ WorkRequestHandler workerHandler =
50
+ new WorkRequestHandlerBuilder (
51
+ new WorkRequestCallback ((request , pw ) -> builder .parseAndBuild (request .getArgumentsList (), pw )),
52
+ System .err ,
53
+ new ProtoWorkerMessageProcessor (System .in , System .out ))
54
+ .setCpuUsageBeforeGc (Duration .ofSeconds (10 ))
55
+ .build ();
56
+ try {
57
+ workerHandler .processRequests ();
58
+ } catch (IOException e ) {
59
+ System .err .println (e .getMessage ());
60
+ } finally {
61
+ // Prevent hanging threads from keeping the worker alive.
62
+ System .exit (1 );
63
+ }
64
+ } else {
65
+ PrintWriter pw =
66
+ new PrintWriter (new OutputStreamWriter (System .err , Charset .defaultCharset ()));
67
+ int returnCode ;
68
+ try {
69
+ returnCode = builder .parseAndBuild (Arrays .asList (args ), pw );
70
+ } finally {
71
+ pw .flush ();
72
+ }
73
+ System .exit (returnCode );
74
+ }
75
+ }
76
+
77
+ public int parseAndBuild (List <String > args , PrintWriter pw ) {
78
+ try {
79
+ JavaLibraryBuildRequest build = parse (args );
80
+ try (SimpleJavaLibraryBuilder builder = getBuilder (build ).get ()) {
81
+ return build (builder , build , pw );
82
+ }
83
+ } catch (InvalidCommandLineException e ) {
84
+ pw .println (CMDNAME + " threw exception: " + e .getMessage ());
85
+ return 1 ;
86
+ } catch (Exception e ) {
87
+ e .printStackTrace ();
88
+ return 1 ;
89
+ }
90
+ }
91
+
92
+ private Supplier <SimpleJavaLibraryBuilder > getBuilder (JavaLibraryBuildRequest build ) {
93
+ if (build .getJavacOpts ().contains ("-Xecj_use_direct_deps_only" ))
94
+ return StrictDepsClasspathJavaLibraryBuilder ::new ;
95
+ return build .getDependencyModule ().reduceClasspath ()
96
+ ? ReducedClasspathJavaLibraryBuilder ::new
97
+ : SimpleJavaLibraryBuilder ::new ;
98
+ }
99
+
100
+ /**
101
+ * Uses {@code builder} to build the target passed in {@code buildRequest}. All errors and
102
+ * diagnostics should be written to {@code err}.
103
+ *
104
+ * @return An error code, 0 is success, any other value is an error.
105
+ */
106
+ protected int build (
107
+ SimpleJavaLibraryBuilder builder , JavaLibraryBuildRequest buildRequest , Writer err )
108
+ throws Exception {
109
+ BlazeJavacResult result = builder .run (buildRequest );
110
+ if (result .status () == Status .REQUIRES_FALLBACK ) {
111
+ return 0 ;
112
+ }
113
+ for (FormattedDiagnostic d : result .diagnostics ()) {
114
+ err .write (d .getFormatted () + "\n " );
115
+ }
116
+ err .write (result .output ());
117
+ return result .isOk () ? 0 : 1 ;
118
+ }
119
+
120
+ /**
121
+ * Parses the list of arguments into a {@link JavaLibraryBuildRequest}. The returned {@link
122
+ * JavaLibraryBuildRequest} object can be then used to configure the compilation itself.
123
+ *
124
+ * @throws IOException if the argument list contains a file (with the @ prefix) and reading that
125
+ * file failed
126
+ * @throws InvalidCommandLineException on any command line error
127
+ */
128
+ @ VisibleForTesting
129
+ public JavaLibraryBuildRequest parse (List <String > args )
130
+ throws IOException , InvalidCommandLineException {
131
+ OptionsParser optionsParser =
132
+ new OptionsParser (args , JavacOptions .createWithWarningsAsErrorsDefault (ImmutableList .of ()));
133
+ ImmutableList <BlazeJavaCompilerPlugin > plugins = ImmutableList .of ();//new ErrorPronePlugin());
134
+ return new JavaLibraryBuildRequest (optionsParser , plugins , new DependencyModule .Builder ());
135
+ }
136
+ }
0 commit comments