diff options
author | cushon <cushon@google.com> | 2017-12-19 10:23:53 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2017-12-19 10:25:51 -0800 |
commit | 56d5ba1ee89ac2dfb4eb5b2b8a076bb372258877 (patch) | |
tree | f13899ddc9cb4c1b9de4e91f6bcbb8e39ee6c59b | |
parent | 741dbc081e2479fd8b0b9c289802b8a0e4af7c06 (diff) |
Retire some pre-skylark machinery for accessing the contents of java_toolchain
and replace the only use with java_common.default_javac_opts.
PiperOrigin-RevId: 179571481
7 files changed, 89 insertions, 247 deletions
diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/BUILD b/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/BUILD index 7176deda43..ba3ae78d3b 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/BUILD +++ b/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/BUILD @@ -7,6 +7,8 @@ package( default_visibility = ["//src:__subpackages__"], ) +load(":build_defs.bzl", "default_javacopts") + # Exports the canonical javac bootclasspath artifact locations genrule( name = "javac-bootclasspath-locations", @@ -46,30 +48,10 @@ java_library( ) # Exports javacopts from the current java toolchain -java_binary( - name = "JavaBuilderConfigGenerator", - srcs = ["JavaBuilderConfigGenerator.java"], - main_class = "com.google.devtools.build.java.bazel.JavaBuilderConfigGenerator", - deps = [ - "//src/main/java/com/google/devtools/build/lib:java-toolchain-parser", - "//third_party:guava", - ], -) - -# Exports javacopts from the current java toolchain -genquery( - name = "java_toolchain_content", - expression = "kind(java_toolchain, deps(//tools/defaults:java_toolchain))", - opts = ["--output=proto"], - scope = ["//tools/defaults:java_toolchain"], -) - -genrule( - name = "javabuilder-javacopts", - srcs = [":java_toolchain_content"], - outs = ["JavaBuilderJavacOpts.java"], - cmd = "$(location :JavaBuilderConfigGenerator) $< > $@", - tools = [":JavaBuilderConfigGenerator"], +default_javacopts( + name = "gen_default_javacopts", + out = "JavaBuilderJavacOpts.java", + template = "JavaBuilderJavacOpts.java.template", ) # Provides java-level access to the javacopts in the current java toolchain @@ -77,7 +59,7 @@ java_library( name = "JavaBuilderConfig", srcs = [ "JavaBuilderConfig.java", - ":javabuilder-javacopts", + "JavaBuilderJavacOpts.java", ], deps = [ "//src/java_tools/buildjar/java/com/google/devtools/build/buildjar:javac_options", @@ -120,5 +102,9 @@ filegroup( srcs = glob([ "*.java", "*.sh", - ]) + ["BUILD"], + ]) + [ + "BUILD", + "JavaBuilderJavacOpts.java.template", + "build_defs.bzl", + ], ) diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/JavaBuilderConfigGenerator.java b/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/JavaBuilderConfigGenerator.java deleted file mode 100644 index 0353d582b7..0000000000 --- a/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/JavaBuilderConfigGenerator.java +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// 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 com.google.devtools.build.java.bazel; - -import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableMap; -import com.google.devtools.build.lib.rules.java.JavaToolchainData; -import com.google.devtools.build.lib.rules.java.JavaToolchainDataParser; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; - -/** Utility class to generate {@link JavaBuilderConfig}. */ -public class JavaBuilderConfigGenerator { - - private static void die(String message) { - System.err.println(message); - System.err.println("\nThis program is expecting the protocol buffer output of a bazel query"); - System.err.println("containing exactly one java_toolchain target. An example of such output"); - System.err.println("can be obtained by:"); - System.err.println( - "\bazel query --output=proto " - + "'kind(java_toolchain, deps(//tools/defaults:java_toolchain))'"); - System.exit(-1); - } - - public static void main(String[] args) { - try { - InputStream stream = System.in; - if (args.length > 0) { - stream = new FileInputStream(args[0]); - } - ImmutableMap<String, JavaToolchainData> data = JavaToolchainDataParser.parse(stream); - if (data.size() != 1) { - die(data.isEmpty() ? "No java_toolchain found!" : "Multiple java_toolchain found!"); - } - JavaToolchainData first = data.values().asList().get(0); - String optsString = Joiner.on("\", \"").join(first.getJavacOptions()); - System.out.println("package com.google.devtools.build.java.bazel;"); - System.out.println("public class JavaBuilderJavacOpts {"); - System.out.println( - "public static final String[] DEFAULT_JAVACOPTS = {\"" + optsString + "\"};"); - System.out.println("}"); - } catch (IOException e) { - die("Cannot load input file: " + e.getMessage()); - } - } -} diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/JavaBuilderJavacOpts.java.template b/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/JavaBuilderJavacOpts.java.template new file mode 100644 index 0000000000..8161c14c24 --- /dev/null +++ b/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/JavaBuilderJavacOpts.java.template @@ -0,0 +1,25 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// 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 com.google.devtools.build.java.bazel; + +/** + * This class is used by {@link JavaBuilderConfig} to provide Java-level access to the blessed + * JavaBuilder javacopts. + */ +public class JavaBuilderJavacOpts { + public static final String[] DEFAULT_JAVACOPTS = { + %javacopts% + }; +}
\ No newline at end of file diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/build_defs.bzl b/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/build_defs.bzl new file mode 100644 index 0000000000..2bf8c78538 --- /dev/null +++ b/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/build_defs.bzl @@ -0,0 +1,49 @@ +# Copyright 2017 The Bazel Authors. All rights reserved. +# +# 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. + +"""Rules to make the default javacopts available as a Java API.""" + +def _default_javacopts(ctx): + javacopts = java_common.default_javac_opts( + ctx, java_toolchain_attr = "_java_toolchain") + ctx.template_action( + template = ctx.file.template, + output = ctx.outputs.out, + substitutions = { + "%javacopts%": '"%s"' % '", "'.join(javacopts), + } + ) + +default_javacopts = rule( + implementation=_default_javacopts, + attrs={ + "template": attr.label( + mandatory=True, + allow_files=True, + single_file=True, + ), + "out": attr.output(mandatory=True), + "_java_toolchain": attr.label( + default = Label("//tools/jdk:current_java_toolchain"), + ), + }, +) +"""Makes the default javacopts available as a Java API. + +Args: + template: The template file to expand, replacing %javacopts% with a quoted + comma-separated list of the default javac flags. + out: The destination of the expanded file. + +""" diff --git a/src/main/java/com/google/devtools/build/lib/BUILD b/src/main/java/com/google/devtools/build/lib/BUILD index 3794ce1121..d16c7a4175 100644 --- a/src/main/java/com/google/devtools/build/lib/BUILD +++ b/src/main/java/com/google/devtools/build/lib/BUILD @@ -349,22 +349,6 @@ java_library( ) java_library( - name = "java-toolchain-parser", - srcs = [ - "rules/java/JavaToolchainData.java", - "rules/java/JavaToolchainDataParser.java", - ], - deps = [ - "//src/main/java/com/google/devtools/build/lib/concurrent", - "//src/main/java/com/google/devtools/build/lib/shell", - "//src/main/protobuf:build_java_proto", - "//third_party:guava", - "//third_party:jsr305", - "//third_party/protobuf:protobuf_java", - ], -) - -java_library( name = "transitive-info-provider", srcs = ["analysis/TransitiveInfoProvider.java"], ) @@ -743,6 +727,7 @@ java_library( "rules/java/JavaSourceInfoProvider.java", "rules/java/JavaToolchain.java", "rules/java/JavaToolchainAlias.java", + "rules/java/JavaToolchainData.java", "rules/java/JavaToolchainRule.java", "rules/java/JavaToolchainSkylarkApiProvider.java", "rules/java/JvmConfigurationLoader.java", diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainData.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainData.java index fca7a9f440..68ff205676 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainData.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainData.java @@ -24,9 +24,9 @@ import java.util.List; /** * Information about the JDK used by the <code>java_*</code> rules. * - * <p>This class contains the data of the {@code java_toolchain} rules, it is a separate object so - * it can be shared with other tools. + * <p>This class contains the data of the {@code java_toolchain} rules. */ +// TODO(cushon): inline this into JavaToolchainProvider (it used to be shared with other tools). @Immutable public class JavaToolchainData { diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainDataParser.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainDataParser.java deleted file mode 100644 index 4c1558fde4..0000000000 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainDataParser.java +++ /dev/null @@ -1,143 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// 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 com.google.devtools.build.lib.rules.java; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.devtools.build.lib.query2.proto.proto2api.Build; -import com.google.devtools.build.lib.query2.proto.proto2api.Build.QueryResult; -import com.google.devtools.build.lib.rules.java.JavaToolchainData.SupportsWorkers; -import com.google.devtools.build.lib.shell.ShellUtils; -import com.google.protobuf.TextFormat; -import com.google.protobuf.TextFormat.ParseException; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; - -/** - * A class to parse a {@link JavaToolchainData} from the result of blaze query. It is used by - * {@link com.google.devtools.build.java.bazel.BazelJavaCompiler} to get default options. - */ -public class JavaToolchainDataParser { - - /** - * Parse a {@link com.google.devtools.build.lib.query2.proto.proto2api.Build.QueryResult} as - * returned by a bazel query and look for the list of target containing a {@code java_toolchain} - * rule. These rules are then parsed into {@link JavaToolchainData}'s and returned as map with the - * name of the target as key and the {@link JavaToolchainData} as value. - */ - public static ImmutableMap<String, JavaToolchainData> parse(QueryResult queryResult) { - ImmutableMap.Builder<String, JavaToolchainData> builder = ImmutableMap.builder(); - for (Build.Target target : queryResult.getTargetList()) { - Build.Rule rule = target.getRule(); - if (target.hasRule() && rule.getRuleClass().equals("java_toolchain")) { - builder.put(rule.getName(), parseBuildRuleProto(rule)); - } - } - return builder.build(); - } - - /** - * Parse a text serialization of a - * {@link com.google.devtools.build.lib.query2.proto.proto2api.Build.QueryResult}. See - * {@link #parse(QueryResult)} for the details of the result. - * - * @throws ParseException when the provided string does not corresponds to a bazel query output. - */ - public static ImmutableMap<String, JavaToolchainData> parse(String queryResult) - throws ParseException { - QueryResult.Builder builder = QueryResult.newBuilder(); - TextFormat.merge(queryResult, builder); - return parse(builder.build()); - } - /** - * Parse a {@link com.google.devtools.build.lib.query2.proto.proto2api.Build.QueryResult} from - * an input stream. See {@link #parse(QueryResult)} for the details of the result. - */ - public static ImmutableMap<String, JavaToolchainData> parse(InputStream queryResult) - throws IOException { - return parse(QueryResult.newBuilder().mergeFrom(queryResult).build()); - } - - private static JavaToolchainData parseBuildRuleProto(Build.Rule rule) { - String source = ""; - String target = ""; - ImmutableList<String> bootclasspath = ImmutableList.of(); - ImmutableList<String> extclasspath = ImmutableList.of(); - String encoding = ""; - ImmutableList<String> xlint = ImmutableList.of(); - ImmutableList<String> misc = ImmutableList.of(); - ImmutableList<String> jvmOpts = ImmutableList.of(); - SupportsWorkers javacSupportsWorkers = SupportsWorkers.NO; - for (Build.Attribute attribute : rule.getAttributeList()) { - switch (attribute.getName()) { - case "source_version": - source = attribute.getStringValue(); - break; - case "target_version": - target = attribute.getStringValue(); - break; - case "bootclasspath": - bootclasspath = ImmutableList.copyOf(attribute.getStringListValueList()); - break; - case "extclasspath": - extclasspath = ImmutableList.copyOf(attribute.getStringListValueList()); - break; - case "encoding": - encoding = attribute.getStringValue(); - break; - case "xlint": - xlint = ImmutableList.copyOf(attribute.getStringListValueList()); - break; - case "misc": - { - List<String> options = new ArrayList<>(); - for (String option : attribute.getStringListValueList()) { - try { - ShellUtils.tokenize(options, option); - } catch (ShellUtils.TokenizationException e) { - // Tokenization failed; this likely means that the user - // did not want tokenization to happen on the argument. - // (see JavaHelper.tokenizeJavaOptions) - options.add(option); - } - } - misc = ImmutableList.copyOf(options); - break; - } - case "jvm_opts": - jvmOpts = ImmutableList.copyOf(attribute.getStringListValueList()); - break; - case "javac_supports_workers": - if (attribute.getBooleanValue()) { - javacSupportsWorkers = SupportsWorkers.YES; - } - break; - } - } - return new JavaToolchainData( - source, - target, - bootclasspath, - extclasspath, - encoding, - xlint, - misc, - jvmOpts, - javacSupportsWorkers); - } -} |