// Copyright 2014 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.ImmutableListMultimap; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.FilesToRunProvider; import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode; import com.google.devtools.build.lib.analysis.platform.ToolchainInfo; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.packages.RuleErrorConsumer; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; import java.util.List; import javax.annotation.Nullable; /** Information about the JDK used by the java_* rules. */ @Immutable @AutoCodec public class JavaToolchainProvider extends ToolchainInfo { /** Returns the Java Toolchain associated with the rule being analyzed or {@code null}. */ public static JavaToolchainProvider from(RuleContext ruleContext) { return from(ruleContext, ":java_toolchain"); } public static JavaToolchainProvider from( RuleContext ruleContext, String attributeName) { TransitiveInfoCollection prerequisite = ruleContext.getPrerequisite(attributeName, Mode.TARGET); return from(prerequisite, ruleContext); } public static JavaToolchainProvider from(TransitiveInfoCollection collection) { return from(collection, null); } private static JavaToolchainProvider from( TransitiveInfoCollection collection, @Nullable RuleErrorConsumer errorConsumer) { ToolchainInfo toolchainInfo = collection.get(ToolchainInfo.PROVIDER); if (toolchainInfo instanceof JavaToolchainProvider) { return (JavaToolchainProvider) toolchainInfo; } if (errorConsumer != null) { errorConsumer.ruleError("The selected Java toolchain is not a JavaToolchainProvider"); } return null; } public static JavaToolchainProvider create( Label label, ImmutableList javacOptions, ImmutableList jvmOptions, boolean javacSupportsWorkers, NestedSet bootclasspath, NestedSet extclasspath, List defaultJavacFlags, Artifact javac, NestedSet tools, FilesToRunProvider javaBuilder, @Nullable FilesToRunProvider headerCompiler, boolean forciblyDisableHeaderCompilation, Artifact singleJar, @Nullable Artifact oneVersion, @Nullable Artifact oneVersionWhitelist, Artifact genClass, @Nullable Artifact resourceJarBuilder, @Nullable Artifact timezoneData, FilesToRunProvider ijar, ImmutableListMultimap compatibleJavacOptions, ImmutableList packageConfiguration, JavaSemantics javaSemantics) { return new JavaToolchainProvider( label, bootclasspath, extclasspath, javac, tools, javaBuilder, headerCompiler, forciblyDisableHeaderCompilation, singleJar, oneVersion, oneVersionWhitelist, genClass, resourceJarBuilder, timezoneData, ijar, compatibleJavacOptions, // merges the defaultJavacFlags from // {@link JavaConfiguration} with the flags from the {@code java_toolchain} rule. ImmutableList.builder().addAll(javacOptions).addAll(defaultJavacFlags).build(), jvmOptions, javacSupportsWorkers, packageConfiguration, javaSemantics); } private final Label label; private final NestedSet bootclasspath; private final NestedSet extclasspath; private final Artifact javac; private final NestedSet tools; private final FilesToRunProvider javaBuilder; @Nullable private final FilesToRunProvider headerCompiler; private final boolean forciblyDisableHeaderCompilation; private final Artifact singleJar; @Nullable private final Artifact oneVersion; @Nullable private final Artifact oneVersionWhitelist; private final Artifact genClass; @Nullable private final Artifact resourceJarBuilder; @Nullable private final Artifact timezoneData; private final FilesToRunProvider ijar; private final ImmutableListMultimap compatibleJavacOptions; private final ImmutableList javacOptions; private final ImmutableList jvmOptions; private final boolean javacSupportsWorkers; private final ImmutableList packageConfiguration; private final JavaSemantics javaSemantics; @VisibleForSerialization JavaToolchainProvider( Label label, NestedSet bootclasspath, NestedSet extclasspath, Artifact javac, NestedSet tools, FilesToRunProvider javaBuilder, @Nullable FilesToRunProvider headerCompiler, boolean forciblyDisableHeaderCompilation, Artifact singleJar, @Nullable Artifact oneVersion, @Nullable Artifact oneVersionWhitelist, Artifact genClass, @Nullable Artifact resourceJarBuilder, @Nullable Artifact timezoneData, FilesToRunProvider ijar, ImmutableListMultimap compatibleJavacOptions, ImmutableList javacOptions, ImmutableList jvmOptions, boolean javacSupportsWorkers, ImmutableList packageConfiguration, JavaSemantics javaSemantics) { super(ImmutableMap.of(), Location.BUILTIN); this.label = label; this.bootclasspath = bootclasspath; this.extclasspath = extclasspath; this.javac = javac; this.tools = tools; this.javaBuilder = javaBuilder; this.headerCompiler = headerCompiler; this.forciblyDisableHeaderCompilation = forciblyDisableHeaderCompilation; this.singleJar = singleJar; this.oneVersion = oneVersion; this.oneVersionWhitelist = oneVersionWhitelist; this.genClass = genClass; this.resourceJarBuilder = resourceJarBuilder; this.timezoneData = timezoneData; this.ijar = ijar; this.compatibleJavacOptions = compatibleJavacOptions; this.javacOptions = javacOptions; this.jvmOptions = jvmOptions; this.javacSupportsWorkers = javacSupportsWorkers; this.packageConfiguration = packageConfiguration; this.javaSemantics = javaSemantics; } /** Returns the label for this {@code java_toolchain}. */ public Label getToolchainLabel() { return label; } /** @return the target Java bootclasspath */ public NestedSet getBootclasspath() { return bootclasspath; } /** @return the target Java extclasspath */ public NestedSet getExtclasspath() { return extclasspath; } /** Returns the {@link Artifact} of the javac jar */ public Artifact getJavac() { return javac; } /** Returns the {@link Artifact}s of compilation tools. */ public NestedSet getTools() { return tools; } /** Returns the {@link FilesToRunProvider} of JavaBuilder */ public FilesToRunProvider getJavaBuilder() { return javaBuilder; } /** @return the {@link FilesToRunProvider} of the Header Compiler deploy jar */ @Nullable public FilesToRunProvider getHeaderCompiler() { return headerCompiler; } /** * Returns {@code true} if header compilation should be forcibly disabled, overriding * --java_header_compilation. */ public boolean getForciblyDisableHeaderCompilation() { return forciblyDisableHeaderCompilation; } /** Returns the {@link Artifact} of the SingleJar deploy jar */ public Artifact getSingleJar() { return singleJar; } /** * Return the {@link Artifact} of the binary that enforces one-version compliance of java * binaries. */ @Nullable public Artifact getOneVersionBinary() { return oneVersion; } /** Return the {@link Artifact} of the whitelist used by the one-version compliance checker. */ @Nullable public Artifact getOneVersionWhitelist() { return oneVersionWhitelist; } /** Returns the {@link Artifact} of the GenClass deploy jar */ public Artifact getGenClass() { return genClass; } @Nullable public Artifact getResourceJarBuilder() { return resourceJarBuilder; } /** * Returns the {@link Artifact} of the latest timezone data resource jar that can be loaded by * Java 8 binaries. */ @Nullable public Artifact getTimezoneData() { return timezoneData; } /** Returns the ijar executable */ public FilesToRunProvider getIjar() { return ijar; } ImmutableListMultimap getCompatibleJavacOptions() { return compatibleJavacOptions; } /** @return the map of target environment-specific javacopts. */ public ImmutableList getCompatibleJavacOptions(String key) { return getCompatibleJavacOptions().get(key); } /** @return the list of default options for the java compiler */ public ImmutableList getJavacOptions() { return javacOptions; } /** * @return the list of default options for the JVM running the java compiler and associated tools. */ public ImmutableList getJvmOptions() { return jvmOptions; } /** @return whether JavaBuilders supports running as a persistent worker or not */ public boolean getJavacSupportsWorkers() { return javacSupportsWorkers; } /** Returns the global {@code java_plugin_configuration} data. */ public ImmutableList packageConfiguration() { return packageConfiguration; } public JavaSemantics getJavaSemantics() { return javaSemantics; } }