Skip to content

Commit 17d5fe6

Browse files
authored
Merge pull request #4 from salesforce/gunnar/javac
Introduce rich fork for BazelJavaBuilder
2 parents 2dc05b3 + c5058fd commit 17d5fe6

29 files changed

+3421
-72
lines changed

BUILD

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ filegroup(
1919

2020
java_binary(
2121
name = "JdtJavaBuilder",
22-
main_class = "com.salesforce.bazel.jdt.toolchain.builder.JdtJavaBuilder",
22+
#main_class = "com.salesforce.bazel.jdt.toolchain.builder.JdtJavaBuilder",
23+
main_class = "com.google.devtools.build.buildjar.BazelEcjJavaBuilder",
2324
runtime_deps = ["//compiler:jdt_java_builder_lib"],
2425
)
2526

compiler/BUILD

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ java_library(
1010
"//compiler/src/main/protobuf:deps_java_proto",
1111
"//compiler/src/main/protobuf:java_compilation_java_proto",
1212
"//compiler/src/main/protobuf:worker_protocol_java_proto",
13-
"@com_google_protobuf//:protobuf_java",
1413
"@rules_jdt_guava",
14+
"@com_google_protobuf//:protobuf_java",
15+
"@com_google_protobuf//:protobuf_java_util",
1516
],
1617
visibility = ["//visibility:public"],
1718
)
@@ -33,8 +34,17 @@ java_library(
3334
name = "buildjar",
3435
srcs = glob(["src/main/buildjar/**/*.java"]),
3536
deps = [
37+
":work_request_handlers",
38+
":ecj",
39+
"//compiler/src/main/protobuf:deps_java_proto",
40+
"//compiler/src/main/protobuf:java_compilation_java_proto",
41+
"//compiler/src/main/protobuf:worker_protocol_java_proto",
3642
"//compiler/third_party/auto_value:processor",
43+
"@rules_jdt_jsr305",
44+
"@rules_jdt_caffeine",
3745
"@rules_jdt_guava",
46+
"@rules_jdt_jacoco_core",
47+
"@com_google_protobuf//:protobuf_java",
3848
],
3949
)
4050

@@ -46,5 +56,5 @@ java_library(
4656
resources = glob(["src/main/ecj/**"], exclude = ["**/*.java"]),
4757
resource_strip_prefix = "compiler/src/main/ecj/",
4858
javacopts = ["-nowarn", "-XepDisableAllChecks"],
49-
deps = ["//compiler/third_party/ecj:javax17api"],
59+
#deps = ["//compiler/third_party/ecj:javax17api"],
5060
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2014 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+
/** Exception to be thrown on command line parsing errors */
18+
public class InvalidCommandLineException extends Exception {
19+
20+
public InvalidCommandLineException(String message) {
21+
super(message);
22+
}
23+
24+
public InvalidCommandLineException(String message, Throwable cause) {
25+
super(message, cause);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.nio.file.Path;
18+
import java.util.Optional;
19+
20+
import com.google.auto.value.AutoValue;
21+
22+
/**
23+
* Holds information about the Bazel rule that created a certain jar.
24+
*
25+
* <p>Rules that use Aspects (http://bazel.build/rules/aspects) to compile jars will result in
26+
* 'aspect()' being populated.
27+
*/
28+
@AutoValue
29+
public abstract class JarOwner {
30+
31+
public abstract Path jar();
32+
33+
public abstract Optional<String> label();
34+
35+
public abstract Optional<String> aspect();
36+
37+
public static JarOwner create(Path jar) {
38+
return new AutoValue_JarOwner(jar, Optional.empty(), Optional.empty());
39+
}
40+
41+
public static JarOwner create(Path jar, String label, Optional<String> aspect) {
42+
return new AutoValue_JarOwner(jar, Optional.of(label), aspect);
43+
}
44+
45+
public JarOwner withLabel(Optional<String> label) {
46+
return new AutoValue_JarOwner(jar(), label, aspect());
47+
}
48+
}

0 commit comments

Comments
 (0)