Skip to content

Commit

Permalink
Add --incompatible_disable_legacy_crosstool_fields
Browse files Browse the repository at this point in the history
Incompatible flag issue: #6861
Tracking issue: #5883

RELNOTES: Added --incompatible_disable_legacy_crosstool_fields. See the
migration notes at #6861.
PiperOrigin-RevId: 227104932
  • Loading branch information
hlopko authored and Copybara-Service committed Dec 28, 2018
1 parent 331796f commit c1b4912
Show file tree
Hide file tree
Showing 14 changed files with 564 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

package com.google.devtools.build.lib.rules.cpp;

import static com.google.devtools.build.lib.rules.cpp.CppConfiguration.getLegacyCrosstoolFieldErrorMessage;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.config.CompilationMode;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.NativeInfo;
Expand Down Expand Up @@ -181,8 +184,10 @@ public CcToolchainConfigInfo(
this.proto = proto;
}

public static CcToolchainConfigInfo fromToolchain(CToolchain toolchain) throws EvalException {

public static CcToolchainConfigInfo fromToolchain(RuleContext ruleContext, CToolchain toolchain)
throws EvalException {
boolean disableLegacyCrosstoolFields =
ruleContext.getFragment(CppConfiguration.class).disableLegacyCrosstoolFields();
ImmutableList.Builder<ActionConfig> actionConfigBuilder = ImmutableList.builder();
for (CToolchain.ActionConfig actionConfig : toolchain.getActionConfigList()) {
actionConfigBuilder.add(new ActionConfig(actionConfig));
Expand Down Expand Up @@ -214,6 +219,103 @@ public static CcToolchainConfigInfo fromToolchain(CToolchain toolchain) throws E
ImmutableList.Builder<String> dynamicLinkerFlags = ImmutableList.builder();
ImmutableList.Builder<String> mostlyStaticLibrariesLinkerFlags = ImmutableList.builder();

if (disableLegacyCrosstoolFields) {
if (toolchain.getCompilationModeFlagsCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("compilation_mode_flags"));
}
if (toolchain.getLinkingModeFlagsCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("linking_mode_flags"));
}
if (toolchain.getArFlagCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("ar_flag"));
}
if (toolchain.getArThinArchivesFlagCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("ar_thin_archives_flag"));
}
if (toolchain.getCompilerFlagCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("compiler_flag"));
}
if (toolchain.getCxxFlagCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("cxx_flag"));
}
if (toolchain.getDebianExtraRequiresCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("debian_extra_requires"));
}
if (toolchain.hasDefaultPythonTop()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("default_python_top"));
}
if (toolchain.hasDefaultPythonVersion()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("default_python_version"));
}
if (toolchain.getDynamicLibraryLinkerFlagCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("dynamic_library_linker_flag"));
}
if (toolchain.getGccPluginCompilerFlagCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("gcc_plugin_compiler_flag"));
}
if (toolchain.getGccPluginHeaderDirectoryCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("gcc_plugin_header_directory"));
}
if (toolchain.getLdEmbedFlagCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("ld_embed_flag"));
}
if (toolchain.getLinkerFlagCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("linker_flag"));
}
if (toolchain.getMaoPluginHeaderDirectoryCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("mao_plugin_header_directory"));
}
if (toolchain.hasNeedsPic()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("needsPic"));
}
if (toolchain.getObjcopyEmbedFlagCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("objcopy_embed_flag"));
}
if (toolchain.hasPythonPreloadSwigdeps()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("python_preload_swigdeps"));
}
if (toolchain.hasStaticRuntimesFilegroup()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("static_runtimes_filegroup"));
}
if (toolchain.hasDynamicRuntimesFilegroup()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("dynamic_runtimes_filegroup"));
}
if (toolchain.hasSupportsDsym()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("supports_dsym"));
}
if (toolchain.hasSupportsEmbeddedRuntimes()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("supports_embedded_runtimes"));
}
if (toolchain.hasSupportsFission()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("supports_fission"));
}
if (toolchain.hasSupportsGoldLinker()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("supports_gold_linker"));
}
if (toolchain.hasSupportsIncrementalLinker()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("supports_incremental_linker"));
}
if (toolchain.hasSupportsInterfaceSharedObjects()) {
ruleContext.ruleError(
getLegacyCrosstoolFieldErrorMessage("supports_interface_shared_objects"));
}
if (toolchain.hasSupportsNormalizingAr()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("supports_normalizing_ar"));
}
if (toolchain.hasSupportsStartEndLib()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("supports_start_end_lib"));
}
if (toolchain.hasSupportsThinArchives()) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("supports_thin_archives"));
}
if (toolchain.getTestOnlyLinkerFlagCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("test_only_linker_flag"));
}
if (toolchain.getUnfilteredCxxFlagCount() != 0) {
ruleContext.ruleError(getLegacyCrosstoolFieldErrorMessage("unfiltered_cxx_flag"));
}
}

for (CompilationModeFlags flag : toolchain.getCompilationModeFlagsList()) {
switch (flag.getMode()) {
case OPT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,6 @@ private static CppToolchainInfo getCppToolchainInfo(
ruleContext.getLabel(),
configInfo,
cppConfiguration.disableLegacyCrosstoolFields(),
cppConfiguration.disableCompilationModeFlags(),
cppConfiguration.disableLinkingModeFlags(),
cppConfiguration.disableGenruleCcToolchainDependency());
} catch (EvalException e) {
throw ruleContext.throwWithRuleError(e.getMessage());
Expand All @@ -748,13 +746,12 @@ private static CppToolchainInfo getCppToolchainInfo(
toolchain =
CppToolchainInfo.addLegacyFeatures(
toolchain, CppToolchainInfo.getToolsDirectory(attributes.getCcToolchainLabel()));
CcToolchainConfigInfo ccToolchainConfigInfo = CcToolchainConfigInfo.fromToolchain(toolchain);
CcToolchainConfigInfo ccToolchainConfigInfo =
CcToolchainConfigInfo.fromToolchain(ruleContext, toolchain);
return CppToolchainInfo.create(
attributes.getCcToolchainLabel(),
ccToolchainConfigInfo,
cppConfiguration.disableLegacyCrosstoolFields(),
cppConfiguration.disableCompilationModeFlags(),
cppConfiguration.disableLinkingModeFlags(),
cppConfiguration.disableGenruleCcToolchainDependency());
} catch (EvalException e) {
throw ruleContext.throwWithRuleError(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,12 @@ public boolean disableLegacyCrosstoolFields() {
return cppOptions.disableLegacyCrosstoolFields;
}

public boolean disableCompilationModeFlags() {
return cppOptions.disableCompilationModeFlags;
}

public boolean disableLinkingModeFlags() {
return cppOptions.disableLinkingModeFlags;
public static String getLegacyCrosstoolFieldErrorMessage(String field) {
Preconditions.checkNotNull(field);
return field
+ " is disabled by --incompatible_disable_legacy_crosstool_fields, please "
+ "migrate your CROSSTOOL (see https://github.com/bazelbuild/bazel/issues/6861 for "
+ "migration instructions).";
}

public boolean enableLinkoptsInUserLinkFlags() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,31 +685,18 @@ public Label getFdoPrefetchHintsLabel() {
public boolean useLLVMCoverageMapFormat;

@Option(
name = "experimental_disable_linking_mode_flags",
name = "incompatible_disable_legacy_crosstool_fields",
oldName = "experimental_disable_legacy_crosstool_fields",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.TOOLCHAIN,
effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
metadataTags = {OptionMetadataTag.EXPERIMENTAL},
help = "If true, Bazel will not read crosstool flags from linking_mode_flags field.")
public boolean disableLinkingModeFlags;

@Option(
name = "experimental_disable_compilation_mode_flags",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.TOOLCHAIN,
effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
metadataTags = {OptionMetadataTag.EXPERIMENTAL},
help = "If true, Bazel will not read crosstool flags from compilation_mode_flags field.")
public boolean disableCompilationModeFlags;

@Option(
name = "experimental_disable_legacy_crosstool_fields",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.TOOLCHAIN,
effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
metadataTags = {OptionMetadataTag.EXPERIMENTAL},
metadataTags = {
OptionMetadataTag.INCOMPATIBLE_CHANGE,
OptionMetadataTag.TRIGGERED_BY_ALL_INCOMPATIBLE_CHANGES
},
help =
"If true, Bazel will not read crosstool flags from legacy crosstool fields (see #5187).")
"If true, Bazel will not read crosstool flags from legacy crosstool fields "
+ "(see https://github.com/bazelbuild/bazel/issues/6861 for migration instructions).")
public boolean disableLegacyCrosstoolFields;

@Option(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ public static CppToolchainInfo create(
Label toolchainLabel,
CcToolchainConfigInfo ccToolchainConfigInfo,
boolean disableLegacyCrosstoolFields,
boolean disableCompilationModeFlags,
boolean disableLinkingModeFlags,
boolean disableGenruleCcToolchainDependency)
throws EvalException {
ImmutableMap<String, PathFragment> toolPaths =
Expand All @@ -128,7 +126,7 @@ public static CppToolchainInfo create(
ImmutableListMultimap.builder();

boolean haveDynamicMode = false;
if (!disableLinkingModeFlags) {
if (!disableLegacyCrosstoolFields) {
// If a toolchain supports dynamic libraries at all, there must be at least one
// of the following:
// - a "DYNAMIC" section in linking_mode_flags (even if no flags are needed)
Expand All @@ -153,7 +151,7 @@ public static CppToolchainInfo create(
ImmutableListMultimap.Builder<CompilationMode, String> linkOptionsFromCompilationModeBuilder =
ImmutableListMultimap.builder();

if (!disableCompilationModeFlags) {
if (!disableLegacyCrosstoolFields) {
cFlagsBuilder.putAll(
importCompilationMode(CrosstoolConfig.CompilationMode.OPT),
ccToolchainConfigInfo.getOptCompilationModeCompilerFlags());
Expand Down Expand Up @@ -212,19 +210,27 @@ public static CppToolchainInfo create(
disableLegacyCrosstoolFields
? ImmutableList.of()
: ccToolchainConfigInfo.getTestOnlyLinkerFlags(),
ccToolchainConfigInfo.getLdEmbedFlags(),
ccToolchainConfigInfo.getObjcopyEmbedFlags(),
disableLegacyCrosstoolFields
? ImmutableList.of()
: ccToolchainConfigInfo.getLdEmbedFlags(),
disableLegacyCrosstoolFields
? ImmutableList.of()
: ccToolchainConfigInfo.getObjcopyEmbedFlags(),
toolchainLabel,
toolchainLabel.getRelativeWithRemapping(
!ccToolchainConfigInfo.getStaticRuntimesFilegroup().isEmpty()
? ccToolchainConfigInfo.getStaticRuntimesFilegroup()
: "static-runtime-libs-" + ccToolchainConfigInfo.getTargetCpu(),
ImmutableMap.of()),
toolchainLabel.getRelativeWithRemapping(
!ccToolchainConfigInfo.getDynamicRuntimesFilegroup().isEmpty()
? ccToolchainConfigInfo.getDynamicRuntimesFilegroup()
: "dynamic-runtime-libs-" + ccToolchainConfigInfo.getTargetCpu(),
ImmutableMap.of()),
disableLegacyCrosstoolFields
? null
: toolchainLabel.getRelativeWithRemapping(
!ccToolchainConfigInfo.getStaticRuntimesFilegroup().isEmpty()
? ccToolchainConfigInfo.getStaticRuntimesFilegroup()
: "static-runtime-libs-" + ccToolchainConfigInfo.getTargetCpu(),
ImmutableMap.of()),
disableLegacyCrosstoolFields
? null
: toolchainLabel.getRelativeWithRemapping(
!ccToolchainConfigInfo.getDynamicRuntimesFilegroup().isEmpty()
? ccToolchainConfigInfo.getDynamicRuntimesFilegroup()
: "dynamic-runtime-libs-" + ccToolchainConfigInfo.getTargetCpu(),
ImmutableMap.of()),
"_solib_" + ccToolchainConfigInfo.getTargetCpu(),
ccToolchainConfigInfo.getAbiVersion(),
ccToolchainConfigInfo.getTargetSystemName(),
Expand All @@ -240,13 +246,17 @@ public static CppToolchainInfo create(
disableLegacyCrosstoolFields
? ImmutableList.of()
: ccToolchainConfigInfo.getUnfilteredCxxFlags(),
ccToolchainConfigInfo.supportsFission(),
ccToolchainConfigInfo.supportsStartEndLib(),
ccToolchainConfigInfo.supportsEmbeddedRuntimes(),
haveDynamicMode || !ccToolchainConfigInfo.getDynamicLibraryLinkerFlags().isEmpty(),
ccToolchainConfigInfo.supportsInterfaceSharedLibraries(),
ccToolchainConfigInfo.supportsGoldLinker(),
ccToolchainConfigInfo.needsPic());
disableLegacyCrosstoolFields ? false : ccToolchainConfigInfo.supportsFission(),
disableLegacyCrosstoolFields ? false : ccToolchainConfigInfo.supportsStartEndLib(),
disableLegacyCrosstoolFields ? false : ccToolchainConfigInfo.supportsEmbeddedRuntimes(),
disableLegacyCrosstoolFields
? false
: haveDynamicMode || !ccToolchainConfigInfo.getDynamicLibraryLinkerFlags().isEmpty(),
disableLegacyCrosstoolFields
? false
: ccToolchainConfigInfo.supportsInterfaceSharedLibraries(),
disableLegacyCrosstoolFields ? false : ccToolchainConfigInfo.supportsGoldLinker(),
disableLegacyCrosstoolFields ? false : ccToolchainConfigInfo.needsPic());
} catch (LabelSyntaxException e) {
// All of the above label.getRelativeWithRemapping() calls are valid labels, and the
// crosstool_top was already checked earlier in the process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,12 @@ public void testFilesToBuild() throws Exception {
public void testFilesToBuildWithoutDSO() throws Exception {
CrosstoolConfig.CrosstoolRelease.Builder release = CrosstoolConfig.CrosstoolRelease.newBuilder()
.mergeFrom(CrosstoolConfigurationHelper.simpleCompleteToolchainProto());
release.getToolchainBuilder(0)
release
.getToolchainBuilder(0)
.setTargetCpu("k8")
.setCompiler("compiler")
.clearLinkingModeFlags();
// To remove "supports_dynamic_linker" feature
.clearFeature();

scratch.file("crosstool/BUILD",
"cc_toolchain_suite(",
Expand Down
Loading

0 comments on commit c1b4912

Please sign in to comment.