Skip to content

Commit

Permalink
Add incompatible flag to forbid loading the native Java rules.
Browse files Browse the repository at this point in the history
RELNOTES: --incompatible_load_java_rules_from_bzl was added to forbid loading the native java rules directly. See more on tracking issue #8746
PiperOrigin-RevId: 255600811
  • Loading branch information
iirina authored and copybara-github committed Jun 28, 2019
1 parent 8d76cb6 commit de9696e
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ protected JavaBinary(JavaSemantics semantics) {
@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {
JavaCommon.checkRuleLoadedThroughMacro(ruleContext);
final JavaCommon common = new JavaCommon(ruleContext, semantics);
DeployArchiveBuilder deployArchiveBuilder = new DeployArchiveBuilder(semantics, ruleContext);
Runfiles.Builder runfilesBuilder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.Target;
import com.google.devtools.build.lib.packages.TargetUtils;
import com.google.devtools.build.lib.rules.cpp.LibraryToLink;
Expand Down Expand Up @@ -351,6 +352,33 @@ public static void checkRuntimeDeps(
}
}

public static void checkRuleLoadedThroughMacro(RuleContext ruleContext)
throws RuleErrorException {
if (!ruleContext.getFragment(JavaConfiguration.class).loadJavaRulesFromBzl()) {
return;
}

if (!hasValidTag(ruleContext) || !ruleContext.getRule().wasCreatedByMacro()) {
registerMigrationRuleError(ruleContext);
}
}

private static boolean hasValidTag(RuleContext ruleContext) {
return ruleContext
.attributes()
.get("tags", Type.STRING_LIST)
.contains("__JAVA_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__");
}

private static void registerMigrationRuleError(RuleContext ruleContext)
throws RuleErrorException {
ruleContext.ruleError(
"The native Java rules are deprecated. Please load "
+ ruleContext.getRule().getRuleClass()
+ " from the rules_java repository."
+ " See http://github.com/bazelbuild/rules_java.");
}

/**
* Returns transitive Java native libraries.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public boolean alwaysGenerateOutputMapping() {
private final ImmutableList<Label> pluginList;
private final boolean requireJavaToolchainHeaderCompilerDirect;
private final boolean disallowResourceJars;
private final boolean loadJavaRulesFromBzl;
private final boolean disallowLegacyJavaToolchainFlags;

// TODO(dmarting): remove once we have a proper solution for #2539
Expand Down Expand Up @@ -217,6 +218,7 @@ public boolean alwaysGenerateOutputMapping() {
this.jplPropagateCcLinkParamsStore = javaOptions.jplPropagateCcLinkParamsStore;
this.isJlplStrictDepsEnforced = javaOptions.isJlplStrictDepsEnforced;
this.disallowResourceJars = javaOptions.disallowResourceJars;
this.loadJavaRulesFromBzl = javaOptions.loadJavaRulesFromBzl;
this.addTestSupportToCompileTimeDeps = javaOptions.addTestSupportToCompileTimeDeps;

ImmutableList.Builder<Label> translationsBuilder = ImmutableList.builder();
Expand Down Expand Up @@ -501,4 +503,8 @@ public boolean requireJavaToolchainHeaderCompilerDirect() {
public boolean disallowResourceJars() {
return disallowResourceJars;
}

public boolean loadJavaRulesFromBzl() {
return loadJavaRulesFromBzl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected JavaImport(JavaSemantics semantics) {
@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {
JavaCommon.checkRuleLoadedThroughMacro(ruleContext);
ImmutableList<Artifact> srcJars = ImmutableList.of();
ImmutableList<Artifact> jars = collectJars(ruleContext);
Artifact srcJar = ruleContext.getPrerequisiteArtifact("srcjar", Mode.TARGET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected JavaLibrary(JavaSemantics semantics) {
@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {
JavaCommon.checkRuleLoadedThroughMacro(ruleContext);
JavaCommon common = new JavaCommon(ruleContext, semantics);
return init(
ruleContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,20 @@ public ImportDepsCheckingLevelConverter() {
"Disables the resource_jars attribute; use java_import and deps or runtime_deps instead.")
public boolean disallowResourceJars;

@Option(
name = "incompatible_load_java_rules_from_bzl",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
metadataTags = {
OptionMetadataTag.INCOMPATIBLE_CHANGE,
OptionMetadataTag.TRIGGERED_BY_ALL_INCOMPATIBLE_CHANGES
},
help =
"If enabled, direct usage of the native Java rules is disabled. Please use "
+ "the Starlark rules instead https://github.com/bazelbuild/rules_java")
public boolean loadJavaRulesFromBzl;

Label defaultJavaBase() {
return Label.parseAbsoluteUnchecked(DEFAULT_JAVABASE);
}
Expand Down Expand Up @@ -683,6 +697,7 @@ public FragmentOptions getHost() {
host.requireJavaToolchainHeaderCompilerDirect = requireJavaToolchainHeaderCompilerDirect;

host.disallowResourceJars = disallowResourceJars;
host.loadJavaRulesFromBzl = loadJavaRulesFromBzl;

// Save host options for further use.
host.hostJavaBase = hostJavaBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class JavaPackageConfiguration implements RuleConfiguredTargetFactory {
@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {
JavaCommon.checkRuleLoadedThroughMacro(ruleContext);
ImmutableList<PackageSpecificationProvider> packages =
ImmutableList.copyOf(
ruleContext.getPrerequisites(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.devtools.build.lib.analysis.PackageSpecificationProvider;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.config.ConfigAwareRuleClassBuilder;
import com.google.devtools.build.lib.analysis.config.HostTransition;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.syntax.Type;
Expand All @@ -33,7 +34,10 @@ public class JavaPackageConfigurationRule implements RuleDefinition {

@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment) {
return builder
return ConfigAwareRuleClassBuilder.of(builder)
.requiresHostConfigurationFragments(JavaConfiguration.class)
.originalBuilder()
.requiresConfigurationFragments(JavaConfiguration.class)
/* <!-- #BLAZE_RULE(java_package_configuration).ATTRIBUTE(packages) -->
The set of <code><a href="${link package_group}">package_group</a></code>s
the configuration should be applied to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected JavaPlugin(JavaSemantics semantics) {
@Override
public final ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {
JavaCommon.checkRuleLoadedThroughMacro(ruleContext);
return new JavaLibrary(semantics)
.init(
ruleContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class JavaRuntime implements RuleConfiguredTargetFactory {
@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {
JavaCommon.checkRuleLoadedThroughMacro(ruleContext);
NestedSetBuilder<Artifact> filesBuilder = NestedSetBuilder.stableOrder();
filesBuilder.addTransitive(PrerequisiteArtifacts.nestedSet(ruleContext, "srcs", Mode.TARGET));
PathFragment javaHome = defaultJavaHome(ruleContext.getLabel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.TemplateVariableInfo;
import com.google.devtools.build.lib.analysis.config.ConfigAwareRuleClassBuilder;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.util.FileTypeSet;

/** Rule definition for {@code java_runtime} */
public final class JavaRuntimeRule implements RuleDefinition {
@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
return builder
return ConfigAwareRuleClassBuilder.of(builder)
.requiresHostConfigurationFragments(JavaConfiguration.class)
.originalBuilder()
.requiresConfigurationFragments(JavaConfiguration.class)
.advertiseProvider(TemplateVariableInfo.class)
/* <!-- #BLAZE_RULE(java_runtime).ATTRIBUTE(srcs) -->
All files in the runtime.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected JavaToolchain(JavaSemantics semantics) {
@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {
JavaCommon.checkRuleLoadedThroughMacro(ruleContext);
ImmutableList<String> javacopts = getJavacOpts(ruleContext);
NestedSet<Artifact> bootclasspath =
PrerequisiteArtifacts.nestedSet(ruleContext, "bootclasspath", Mode.HOST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.rules.java.JavaCommon;
import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider;
import com.google.devtools.build.lib.rules.java.JavaConfiguration;
import com.google.devtools.build.lib.rules.java.JavaInfo;
Expand All @@ -50,6 +51,7 @@ public class JavaLiteProtoLibrary implements RuleConfiguredTargetFactory {
@Override
public ConfiguredTarget create(final RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {
JavaCommon.checkRuleLoadedThroughMacro(ruleContext);

Iterable<JavaProtoLibraryAspectProvider> javaProtoLibraryAspectProviders =
ruleContext.getPrerequisites("deps", Mode.TARGET, JavaProtoLibraryAspectProvider.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.devtools.build.lib.analysis.Runfiles;
import com.google.devtools.build.lib.analysis.RunfilesProvider;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.rules.java.JavaCommon;
import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider;
import com.google.devtools.build.lib.rules.java.JavaConfiguration;
import com.google.devtools.build.lib.rules.java.JavaInfo;
Expand All @@ -46,6 +47,8 @@ public class JavaProtoLibrary implements RuleConfiguredTargetFactory {
public ConfiguredTarget create(final RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {

JavaCommon.checkRuleLoadedThroughMacro(ruleContext);

if (ruleContext.getFragment(JavaConfiguration.class).isDisallowStrictDepsForJpl()
&& ruleContext.attributes().has("strict_deps")
&& ruleContext.attributes().isAttributeValueExplicitlySpecified("strict_deps")) {
Expand Down

0 comments on commit de9696e

Please sign in to comment.