aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar hlopko <hlopko@google.com>2018-07-17 09:37:23 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-07-17 09:39:02 -0700
commiteb4c54015ccdd4acb323e3ff722b01f20a5336c6 (patch)
tree5f03db22fb5f7bb2fc70da9cdf2ae273f9864f2c
parent7c8607eb87a85700016e4ba0f1c2fb6070b83e4d (diff)
Add flags for disabling legacy crosstool fields
This cl adds following flags: * --experimental_disable_legacy_cc_compilation_api * --experimental_disable_legacy_cc_linking_api * --experimental_disable_linking_mode_flags * --experimental_disable_compilation_mode_flags * --experimental_disable_legacy_crosstool_fields RELNOTES: None PiperOrigin-RevId: 204924599
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java5
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProvider.java33
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java44
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java46
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CppToolchainInfo.java112
-rw-r--r--src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcToolchainProviderApi.java48
-rw-r--r--src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java233
7 files changed, 430 insertions, 91 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java
index 8aea1525a2..28ab7dedc7 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java
@@ -665,7 +665,10 @@ public class CcToolchain implements RuleConfiguredTargetFactory {
return CppToolchainInfo.create(
cppConfiguration.getCrosstoolTopPathFragment(),
cppConfiguration.getCcToolchainRuleLabel(),
- crosstoolInfo);
+ crosstoolInfo,
+ cppConfiguration.disableLegacyCrosstoolFields(),
+ cppConfiguration.disableCompilationModeFlags(),
+ cppConfiguration.disableLinkingModeFlags());
} catch (InvalidConfigurationException e) {
throw ruleContext.throwWithRuleError(e.getMessage());
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProvider.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProvider.java
index b20c61a632..c702eae16b 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProvider.java
@@ -704,7 +704,8 @@ public final class CcToolchainProvider extends ToolchainInfo implements CcToolch
@Override
// TODO(b/24373706): Remove this method once new C++ toolchain API is available
public ImmutableList<String> getUnfilteredCompilerOptionsWithSysroot(
- Iterable<String> featuresNotUsedAnymore) {
+ Iterable<String> featuresNotUsedAnymore) throws EvalException {
+ cppConfiguration.checkForLegacyCompilationApiAvailability();
return toolchainInfo.getUnfilteredCompilerOptions(sysroot);
}
@@ -723,10 +724,12 @@ public final class CcToolchainProvider extends ToolchainInfo implements CcToolch
}
@Override
- public ImmutableList<String> getLinkOptionsWithSysroot() {
- return cppConfiguration == null
- ? ImmutableList.of()
- : cppConfiguration.getLinkOptionsDoNotUse(sysroot);
+ public ImmutableList<String> getLinkOptionsWithSysroot() throws EvalException {
+ if (cppConfiguration == null) {
+ return ImmutableList.of();
+ }
+ cppConfiguration.checkForLegacyLinkingApiAvailability();
+ return cppConfiguration.getLinkOptionsDoNotUse(sysroot);
}
public ImmutableList<String> getLinkOptions() {
@@ -883,7 +886,8 @@ public final class CcToolchainProvider extends ToolchainInfo implements CcToolch
* not use in new code. Will be removed soon as part of the new Skylark API to the C++ toolchain.
*/
@Override
- public ImmutableList<String> getCompilerOptions() {
+ public ImmutableList<String> getCompilerOptions() throws EvalException {
+ cppConfiguration.checkForLegacyCompilationApiAvailability();
return getLegacyCompileOptionsWithCopts();
}
@@ -896,7 +900,8 @@ public final class CcToolchainProvider extends ToolchainInfo implements CcToolch
* CcToolchainProvider#getLegacyCompileOptionsWithCopts()}.
*/
@Override
- public ImmutableList<String> getCOptions() {
+ public ImmutableList<String> getCOptions() throws EvalException {
+ cppConfiguration.checkForLegacyCompilationApiAvailability();
return cppConfiguration.getCOptions();
}
@@ -909,7 +914,8 @@ public final class CcToolchainProvider extends ToolchainInfo implements CcToolch
*/
@Override
@Deprecated
- public ImmutableList<String> getCxxOptionsWithCopts() {
+ public ImmutableList<String> getCxxOptionsWithCopts() throws EvalException {
+ cppConfiguration.checkForLegacyCompilationApiAvailability();
return ImmutableList.<String>builder()
.addAll(getLegacyCxxOptions())
.addAll(cppConfiguration.getCxxopts())
@@ -935,6 +941,7 @@ public final class CcToolchainProvider extends ToolchainInfo implements CcToolch
@Override
@Deprecated
public ImmutableList<String> getFullyStaticLinkOptions(Boolean sharedLib) throws EvalException {
+ cppConfiguration.checkForLegacyLinkingApiAvailability();
if (!sharedLib) {
throw new EvalException(
Location.BUILTIN, "fully_static_link_options is deprecated, new uses are not allowed.");
@@ -946,14 +953,15 @@ public final class CcToolchainProvider extends ToolchainInfo implements CcToolch
* WARNING: This method is only added to allow incremental migration of existing users. Please do
* not use in new code. Will be removed soon as part of the new Skylark API to the C++ toolchain.
*
- * Returns the immutable list of linker options for mostly statically linked outputs. Does not
+ * <p>Returns the immutable list of linker options for mostly statically linked outputs. Does not
* include command-line options passed via --linkopt or --linkopts.
*
* @param sharedLib true if the output is a shared lib, false if it's an executable
*/
@Override
@Deprecated
- public ImmutableList<String> getMostlyStaticLinkOptions(Boolean sharedLib) {
+ public ImmutableList<String> getMostlyStaticLinkOptions(Boolean sharedLib) throws EvalException {
+ cppConfiguration.checkForLegacyLinkingApiAvailability();
return CppHelper.getMostlyStaticLinkOptions(
cppConfiguration, this, sharedLib, /* shouldStaticallyLinkCppRuntimes= */ true);
}
@@ -962,14 +970,15 @@ public final class CcToolchainProvider extends ToolchainInfo implements CcToolch
* WARNING: This method is only added to allow incremental migration of existing users. Please do
* not use in new code. Will be removed soon as part of the new Skylark API to the C++ toolchain.
*
- * Returns the immutable list of linker options for artifacts that are not fully or mostly
+ * <p>Returns the immutable list of linker options for artifacts that are not fully or mostly
* statically linked. Does not include command-line options passed via --linkopt or --linkopts.
*
* @param sharedLib true if the output is a shared lib, false if it's an executable
*/
@Override
@Deprecated
- public ImmutableList<String> getDynamicLinkOptions(Boolean sharedLib) {
+ public ImmutableList<String> getDynamicLinkOptions(Boolean sharedLib) throws EvalException {
+ cppConfiguration.checkForLegacyLinkingApiAvailability();
return CppHelper.getDynamicLinkOptions(cppConfiguration, this, sharedLib);
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java
index 375f735c72..4bbfeafc16 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java
@@ -92,6 +92,18 @@ public final class CppConfiguration extends BuildConfiguration.Fragment
/** String constant for CC_FLAGS make variable name */
public static final String CC_FLAGS_MAKE_VARIABLE_NAME = "CC_FLAGS";
+ public boolean disableLegacyCrosstoolFields() {
+ return cppOptions.disableLegacyCrosstoolFields;
+ }
+
+ public boolean disableCompilationModeFlags() {
+ return cppOptions.disableCompilationModeFlags;
+ }
+
+ public boolean disableLinkingModeFlags() {
+ return cppOptions.disableLinkingModeFlags;
+ }
+
/**
* An enumeration of all the tools that comprise a toolchain.
*/
@@ -223,7 +235,12 @@ public final class CppConfiguration extends BuildConfiguration.Fragment
params.crosstoolTop.getPackageIdentifier().getPathUnderExecRoot();
CppToolchainInfo cppToolchainInfo =
CppToolchainInfo.create(
- crosstoolTopPathFragment, params.ccToolchainLabel, params.crosstoolInfo);
+ crosstoolTopPathFragment,
+ params.ccToolchainLabel,
+ params.crosstoolInfo,
+ cppOptions.disableLegacyCrosstoolFields,
+ cppOptions.disableCompilationModeFlags,
+ cppOptions.disableLinkingModeFlags);
CompilationMode compilationMode = params.commonOptions.compilationMode;
@@ -555,6 +572,7 @@ public final class CppConfiguration extends BuildConfiguration.Fragment
public ImmutableList<String> getCompilerOptions(Iterable<String> featuresNotUsedAnymore)
throws EvalException {
checkForToolchainSkylarkApiAvailability();
+ checkForLegacyCompilationApiAvailability();
return compilerFlags;
}
@@ -567,6 +585,7 @@ public final class CppConfiguration extends BuildConfiguration.Fragment
@Deprecated
public ImmutableList<String> getCOptionsForSkylark() throws EvalException {
checkForToolchainSkylarkApiAvailability();
+ checkForLegacyCompilationApiAvailability();
return getCOptions();
}
@@ -586,6 +605,7 @@ public final class CppConfiguration extends BuildConfiguration.Fragment
public ImmutableList<String> getCxxOptions(Iterable<String> featuresNotUsedAnymore)
throws EvalException {
checkForToolchainSkylarkApiAvailability();
+ checkForLegacyCompilationApiAvailability();
return cxxFlags;
}
@@ -603,6 +623,7 @@ public final class CppConfiguration extends BuildConfiguration.Fragment
public ImmutableList<String> getUnfilteredCompilerOptionsWithLegacySysroot(
Iterable<String> featuresNotUsedAnymore) throws EvalException {
checkForToolchainSkylarkApiAvailability();
+ checkForLegacyCompilationApiAvailability();
return getUnfilteredCompilerOptionsDoNotUse(nonConfiguredSysroot);
}
@@ -637,6 +658,7 @@ public final class CppConfiguration extends BuildConfiguration.Fragment
@Override
public ImmutableList<String> getLinkOptionsWithLegacySysroot() throws EvalException {
checkForToolchainSkylarkApiAvailability();
+ checkForLegacyLinkingApiAvailability();
return getLinkOptionsDoNotUse(nonConfiguredSysroot);
}
@@ -694,6 +716,7 @@ public final class CppConfiguration extends BuildConfiguration.Fragment
public ImmutableList<String> getFullyStaticLinkOptions(
Iterable<String> featuresNotUsedAnymore, Boolean sharedLib) throws EvalException {
checkForToolchainSkylarkApiAvailability();
+ checkForLegacyLinkingApiAvailability();
if (!sharedLib) {
throw new EvalException(
Location.BUILTIN, "fully_static_link_options is deprecated, new uses are not allowed.");
@@ -716,6 +739,7 @@ public final class CppConfiguration extends BuildConfiguration.Fragment
public ImmutableList<String> getMostlyStaticLinkOptions(
Iterable<String> featuresNotUsedAnymore, Boolean sharedLib) throws EvalException {
checkForToolchainSkylarkApiAvailability();
+ checkForLegacyLinkingApiAvailability();
if (sharedLib) {
return getSharedLibraryLinkOptions(
cppToolchainInfo.supportsEmbeddedRuntimes()
@@ -741,6 +765,7 @@ public final class CppConfiguration extends BuildConfiguration.Fragment
public ImmutableList<String> getDynamicLinkOptions(
Iterable<String> featuresNotUsedAnymore, Boolean sharedLib) throws EvalException {
checkForToolchainSkylarkApiAvailability();
+ checkForLegacyLinkingApiAvailability();
if (sharedLib) {
return getSharedLibraryLinkOptions(dynamicLinkFlags);
} else {
@@ -1173,6 +1198,23 @@ public final class CppConfiguration extends BuildConfiguration.Fragment
}
}
+ public void checkForLegacyCompilationApiAvailability() throws EvalException {
+ if (cppOptions.disableLegacyCompilationApi) {
+ throw new EvalException(
+ null,
+ "Skylark APIs accessing compilation flags has been removed. "
+ + "Use the new API on cc_common.");
+ }
+ }
+
+ public void checkForLegacyLinkingApiAvailability() throws EvalException {
+ if (cppOptions.disableLegacyLinkingApi) {
+ throw new EvalException(
+ null,
+ "Skylark APIs accessing linking flags has been removed. Use the new API on cc_common.");
+ }
+ }
+
public static PathFragment computeDefaultSysroot(String builtInSysroot) {
if (builtInSysroot.isEmpty()) {
return null;
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java
index 2ecb33e6d6..bcb5a7d81f 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java
@@ -804,6 +804,52 @@ public class CppOptions extends FragmentOptions {
public boolean enableCcSkylarkApi;
@Option(
+ name = "experimental_disable_legacy_cc_linking_api",
+ defaultValue = "false",
+ documentationCategory = OptionDocumentationCategory.TOOLCHAIN,
+ effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
+ metadataTags = {OptionMetadataTag.EXPERIMENTAL},
+ help = "If true, C++ Skylark API exposing linking flags will be disabled.")
+ public boolean disableLegacyLinkingApi;
+
+ @Option(
+ name = "experimental_disable_legacy_cc_compilation_api",
+ defaultValue = "false",
+ documentationCategory = OptionDocumentationCategory.TOOLCHAIN,
+ effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
+ metadataTags = {OptionMetadataTag.EXPERIMENTAL},
+ help = "If true, C++ Skylark API exposing compilation flags will be disabled.")
+ public boolean disableLegacyCompilationApi;
+
+ @Option(
+ name = "experimental_disable_linking_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 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},
+ help =
+ "If true, Bazel will not read crosstool flags from legacy crosstool fields (see #5187.")
+ public boolean disableLegacyCrosstoolFields;
+
+ @Option(
name = "experimental_enable_cc_dynlibs_for_runtime",
defaultValue = "true",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppToolchainInfo.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppToolchainInfo.java
index 1396779f83..de7514e72b 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppToolchainInfo.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppToolchainInfo.java
@@ -111,7 +111,12 @@ public final class CppToolchainInfo {
/** Creates a CppToolchainInfo from CROSSTOOL info encapsulated in {@link CrosstoolInfo}. */
public static CppToolchainInfo create(
- PathFragment crosstoolTopPathFragment, Label toolchainLabel, CrosstoolInfo crosstoolInfo)
+ PathFragment crosstoolTopPathFragment,
+ Label toolchainLabel,
+ CrosstoolInfo crosstoolInfo,
+ boolean disableLegacyCrosstoolFields,
+ boolean disableCompilationModeFlags,
+ boolean disableLinkingModeFlags)
throws InvalidConfigurationException {
ImmutableMap<String, PathFragment> toolPaths =
computeToolPaths(crosstoolInfo, crosstoolTopPathFragment);
@@ -121,21 +126,24 @@ public final class CppToolchainInfo {
ImmutableListMultimap.Builder<LinkingMode, String> linkOptionsFromLinkingModeBuilder =
ImmutableListMultimap.builder();
- // 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)
- // - a non-empty list in one of the dynamicLibraryLinkerFlag fields
- // If none of the above contain data, then the toolchain can't do dynamic linking.
- boolean haveDynamicMode = crosstoolInfo.hasDynamicLinkingModeFlags();
- linkOptionsFromLinkingModeBuilder.putAll(
- LinkingMode.DYNAMIC, crosstoolInfo.getDynamicLinkingModeFlags());
- linkOptionsFromLinkingModeBuilder.putAll(
- LinkingMode.LEGACY_FULLY_STATIC, crosstoolInfo.getFullyStaticLinkingModeFlags());
- linkOptionsFromLinkingModeBuilder.putAll(
- LinkingMode.STATIC, crosstoolInfo.getMostlyStaticLinkingModeFlags());
- linkOptionsFromLinkingModeBuilder.putAll(
- LinkingMode.LEGACY_MOSTLY_STATIC_LIBRARIES,
- crosstoolInfo.getMostlyStaticLibrariesLinkingModeFlags());
+ boolean haveDynamicMode = false;
+ if (!disableLinkingModeFlags) {
+ // 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)
+ // - a non-empty list in one of the dynamicLibraryLinkerFlag fields
+ // If none of the above contain data, then the toolchain can't do dynamic linking.
+ haveDynamicMode = crosstoolInfo.hasDynamicLinkingModeFlags();
+ linkOptionsFromLinkingModeBuilder.putAll(
+ LinkingMode.DYNAMIC, crosstoolInfo.getDynamicLinkingModeFlags());
+ linkOptionsFromLinkingModeBuilder.putAll(
+ LinkingMode.LEGACY_FULLY_STATIC, crosstoolInfo.getFullyStaticLinkingModeFlags());
+ linkOptionsFromLinkingModeBuilder.putAll(
+ LinkingMode.STATIC, crosstoolInfo.getMostlyStaticLinkingModeFlags());
+ linkOptionsFromLinkingModeBuilder.putAll(
+ LinkingMode.LEGACY_MOSTLY_STATIC_LIBRARIES,
+ crosstoolInfo.getMostlyStaticLibrariesLinkingModeFlags());
+ }
ImmutableListMultimap.Builder<CompilationMode, String> cFlagsBuilder =
ImmutableListMultimap.builder();
@@ -144,33 +152,35 @@ public final class CppToolchainInfo {
ImmutableListMultimap.Builder<CompilationMode, String> linkOptionsFromCompilationModeBuilder =
ImmutableListMultimap.builder();
- cFlagsBuilder.putAll(
- importCompilationMode(CrosstoolConfig.CompilationMode.OPT),
- crosstoolInfo.getOptCompilationModeCompilerFlags());
- cxxFlagsBuilder.putAll(
- importCompilationMode(CrosstoolConfig.CompilationMode.OPT),
- crosstoolInfo.getOptCompilationModeCxxFlags());
- linkOptionsFromCompilationModeBuilder.putAll(
- importCompilationMode(CrosstoolConfig.CompilationMode.OPT),
- crosstoolInfo.getOptCompilationModeLinkerFlags());
- cFlagsBuilder.putAll(
- importCompilationMode(CrosstoolConfig.CompilationMode.DBG),
- crosstoolInfo.getDbgCompilationModeCompilerFlags());
- cxxFlagsBuilder.putAll(
- importCompilationMode(CrosstoolConfig.CompilationMode.DBG),
- crosstoolInfo.getDbgCompilationModeCxxFlags());
- linkOptionsFromCompilationModeBuilder.putAll(
- importCompilationMode(CrosstoolConfig.CompilationMode.DBG),
- crosstoolInfo.getDbgCompilationModeLinkerFlags());
- cFlagsBuilder.putAll(
- importCompilationMode(CrosstoolConfig.CompilationMode.FASTBUILD),
- crosstoolInfo.getFastbuildCompilationModeCompilerFlags());
- cxxFlagsBuilder.putAll(
- importCompilationMode(CrosstoolConfig.CompilationMode.FASTBUILD),
- crosstoolInfo.getFastbuildCompilationModeCxxFlags());
- linkOptionsFromCompilationModeBuilder.putAll(
- importCompilationMode(CrosstoolConfig.CompilationMode.FASTBUILD),
- crosstoolInfo.getFastbuildCompilationModeLinkerFlags());
+ if (!disableCompilationModeFlags) {
+ cFlagsBuilder.putAll(
+ importCompilationMode(CrosstoolConfig.CompilationMode.OPT),
+ crosstoolInfo.getOptCompilationModeCompilerFlags());
+ cxxFlagsBuilder.putAll(
+ importCompilationMode(CrosstoolConfig.CompilationMode.OPT),
+ crosstoolInfo.getOptCompilationModeCxxFlags());
+ linkOptionsFromCompilationModeBuilder.putAll(
+ importCompilationMode(CrosstoolConfig.CompilationMode.OPT),
+ crosstoolInfo.getOptCompilationModeLinkerFlags());
+ cFlagsBuilder.putAll(
+ importCompilationMode(CrosstoolConfig.CompilationMode.DBG),
+ crosstoolInfo.getDbgCompilationModeCompilerFlags());
+ cxxFlagsBuilder.putAll(
+ importCompilationMode(CrosstoolConfig.CompilationMode.DBG),
+ crosstoolInfo.getDbgCompilationModeCxxFlags());
+ linkOptionsFromCompilationModeBuilder.putAll(
+ importCompilationMode(CrosstoolConfig.CompilationMode.DBG),
+ crosstoolInfo.getDbgCompilationModeLinkerFlags());
+ cFlagsBuilder.putAll(
+ importCompilationMode(CrosstoolConfig.CompilationMode.FASTBUILD),
+ crosstoolInfo.getFastbuildCompilationModeCompilerFlags());
+ cxxFlagsBuilder.putAll(
+ importCompilationMode(CrosstoolConfig.CompilationMode.FASTBUILD),
+ crosstoolInfo.getFastbuildCompilationModeCxxFlags());
+ linkOptionsFromCompilationModeBuilder.putAll(
+ importCompilationMode(CrosstoolConfig.CompilationMode.FASTBUILD),
+ crosstoolInfo.getFastbuildCompilationModeLinkerFlags());
+ }
try {
return new CppToolchainInfo(
@@ -191,11 +201,15 @@ public final class CppToolchainInfo {
defaultSysroot,
crosstoolInfo.getTargetLibc(),
crosstoolInfo.getHostSystemName(),
- crosstoolInfo.getDynamicLibraryLinkerFlags(),
- crosstoolInfo.getLinkerFlags(),
+ disableLegacyCrosstoolFields
+ ? ImmutableList.of()
+ : crosstoolInfo.getDynamicLibraryLinkerFlags(),
+ disableLegacyCrosstoolFields ? ImmutableList.of() : crosstoolInfo.getLinkerFlags(),
linkOptionsFromLinkingModeBuilder.build(),
linkOptionsFromCompilationModeBuilder.build(),
- crosstoolInfo.getTestOnlyLinkerFlags(),
+ disableLegacyCrosstoolFields
+ ? ImmutableList.of()
+ : crosstoolInfo.getTestOnlyLinkerFlags(),
crosstoolInfo.getLdEmbedFlags(),
crosstoolInfo.getObjcopyEmbedFlags(),
toolchainLabel,
@@ -213,11 +227,11 @@ public final class CppToolchainInfo {
crosstoolInfo.getAbiVersion(),
crosstoolInfo.getTargetSystemName(),
computeAdditionalMakeVariables(crosstoolInfo),
- crosstoolInfo.getCompilerFlags(),
- crosstoolInfo.getCxxFlags(),
+ disableLegacyCrosstoolFields ? ImmutableList.of() : crosstoolInfo.getCompilerFlags(),
+ disableLegacyCrosstoolFields ? ImmutableList.of() : crosstoolInfo.getCxxFlags(),
cFlagsBuilder.build(),
cxxFlagsBuilder.build(),
- crosstoolInfo.getUnfilteredCxxFlags(),
+ disableLegacyCrosstoolFields ? ImmutableList.of() : crosstoolInfo.getUnfilteredCxxFlags(),
crosstoolInfo.supportsFission(),
crosstoolInfo.supportsStartEndLib(),
crosstoolInfo.supportsEmbeddedRuntimes(),
diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcToolchainProviderApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcToolchainProviderApi.java
index 078f5b6e3d..6d2e01494a 100644
--- a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcToolchainProviderApi.java
+++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcToolchainProviderApi.java
@@ -57,29 +57,26 @@ public interface CcToolchainProviderApi extends ToolchainInfoApi {
name = "unfiltered_compiler_options",
doc =
"<b>Deprecated</b>. Returns the default list of options which cannot be filtered by "
- + "BUILD rules. These should be appended to the command line after filtering.",
+ + "BUILD rules. These should be appended to the command line after filtering.",
parameters = {
@Param(
name = "features",
doc = "Unused.",
positional = true,
named = false,
- type = SkylarkList.class
- )
- }
- )
+ type = SkylarkList.class)
+ })
// TODO(b/24373706): Remove this method once new C++ toolchain API is available
public ImmutableList<String> getUnfilteredCompilerOptionsWithSysroot(
- Iterable<String> featuresNotUsedAnymore);
+ Iterable<String> featuresNotUsedAnymore) throws EvalException;
@SkylarkCallable(
- name = "link_options_do_not_use",
- structField = true,
- doc =
- "Returns the set of command-line linker options, including any flags "
- + "inferred from the command-line options."
- )
- public ImmutableList<String> getLinkOptionsWithSysroot();
+ name = "link_options_do_not_use",
+ structField = true,
+ doc =
+ "Returns the set of command-line linker options, including any flags "
+ + "inferred from the command-line options.")
+ public ImmutableList<String> getLinkOptionsWithSysroot() throws EvalException;
@SkylarkCallable(
name = "target_gnu_system_name",
@@ -95,9 +92,8 @@ public interface CcToolchainProviderApi extends ToolchainInfoApi {
"Returns the default options to use for compiling C, C++, and assembler. "
+ "This is just the options that should be used for all three languages. "
+ "There may be additional C-specific or C++-specific options that should be used, "
- + "in addition to the ones returned by this method"
- )
- public ImmutableList<String> getCompilerOptions();
+ + "in addition to the ones returned by this method")
+ public ImmutableList<String> getCompilerOptions() throws EvalException;
@SkylarkCallable(
name = "c_options",
@@ -105,7 +101,7 @@ public interface CcToolchainProviderApi extends ToolchainInfoApi {
"Returns the list of additional C-specific options to use for compiling C. "
+ "These should be go on the command line after the common options returned by "
+ "<code>compiler_options</code>")
- public ImmutableList<String> getCOptions();
+ public ImmutableList<String> getCOptions() throws EvalException;
@SkylarkCallable(
name = "cxx_options",
@@ -114,7 +110,7 @@ public interface CcToolchainProviderApi extends ToolchainInfoApi {
+ "These should be go on the command line after the common options returned by "
+ "<code>compiler_options</code>")
@Deprecated
- public ImmutableList<String> getCxxOptionsWithCopts();
+ public ImmutableList<String> getCxxOptionsWithCopts() throws EvalException;
@SkylarkCallable(
name = "fully_static_link_options",
@@ -147,12 +143,10 @@ public interface CcToolchainProviderApi extends ToolchainInfoApi {
doc = "If true, returns the link options for a shared library.",
positional = true,
named = false,
- type = Boolean.class
- )
- }
- )
+ type = Boolean.class)
+ })
@Deprecated
- public ImmutableList<String> getMostlyStaticLinkOptions(Boolean sharedLib);
+ public ImmutableList<String> getMostlyStaticLinkOptions(Boolean sharedLib) throws EvalException;
@SkylarkCallable(
name = "dynamic_link_options",
@@ -166,12 +160,10 @@ public interface CcToolchainProviderApi extends ToolchainInfoApi {
doc = "If true, returns the link options for a shared library.",
positional = true,
named = false,
- type = Boolean.class
- )
- }
- )
+ type = Boolean.class)
+ })
@Deprecated
- public ImmutableList<String> getDynamicLinkOptions(Boolean sharedLib);
+ public ImmutableList<String> getDynamicLinkOptions(Boolean sharedLib) throws EvalException;
@SkylarkCallable(
name = "ld_executable",
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java
index ce099c9628..9264059f08 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java
@@ -15,12 +15,15 @@
package com.google.devtools.build.lib.rules.cpp;
import static com.google.common.truth.Truth.assertThat;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
+import com.google.devtools.build.lib.analysis.RuleContext;
+import com.google.devtools.build.lib.analysis.config.CompilationMode;
import com.google.devtools.build.lib.analysis.util.AnalysisMock;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
@@ -182,4 +185,234 @@ public class CcToolchainProviderTest extends BuildViewTestCase {
List<String> unfilteredCompilerOptions = (List<String>) ct.get("unfiltered_compiler_options");
assertThat(unfilteredCompilerOptions).contains("--sysroot=/usr/local/custom-sysroot");
}
+
+ @Test
+ public void testDisablingMostlyStaticLinkOptions() throws Exception {
+ testDisablingLinkingApiMethod("provider.mostly_static_link_options(False)");
+ }
+
+ @Test
+ public void testDisablingFullyStaticLinkOptions() throws Exception {
+ testDisablingLinkingApiMethod("provider.fully_static_link_options(True)");
+ }
+
+ @Test
+ public void testDisablingDynamicLinkOptions() throws Exception {
+ testDisablingLinkingApiMethod("provider.dynamic_link_options(False)");
+ }
+
+ @Test
+ public void testDisablingLinkOptions() throws Exception {
+ testDisablingLinkingApiMethod("provider.link_options_do_not_use");
+ }
+
+ @Test
+ public void testDisablingMostlyStaticLinkOptionsFromConfiguration() throws Exception {
+ testDisablingLinkingApiMethod("ctx.fragments.cpp.mostly_static_link_options([], False)");
+ }
+
+ @Test
+ public void testDisablingFullyStaticLinkOptionsFromConfiguration() throws Exception {
+ testDisablingLinkingApiMethod("ctx.fragments.cpp.fully_static_link_options([], True)");
+ }
+
+ @Test
+ public void testDisablingDynamicLinkOptionsFromConfiguration() throws Exception {
+ testDisablingLinkingApiMethod("ctx.fragments.cpp.dynamic_link_options([], False)");
+ }
+
+ @Test
+ public void testDisablingLinkOptionsFromConfiguration() throws Exception {
+ testDisablingLinkingApiMethod("ctx.fragments.cpp.link_options");
+ }
+
+ private void testDisablingLinkingApiMethod(String method) throws Exception {
+ useConfiguration("--experimental_disable_legacy_cc_linking_api");
+ scratch.file(
+ "test/rule.bzl",
+ "def _impl(ctx):",
+ " provider = ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]",
+ " return struct(",
+ " link_options = " + method + ",",
+ " )",
+ "",
+ "my_rule = rule(",
+ " _impl,",
+ " fragments = [ 'cpp' ],",
+ " attrs = {'_cc_toolchain': attr.label(default=Label('//test:toolchain')) }",
+ ")");
+ scratch.file(
+ "test/BUILD",
+ "load(':rule.bzl', 'my_rule')",
+ "cc_toolchain_alias(name = 'toolchain')",
+ "my_rule(name = 'target')");
+ AssertionError e =
+ assertThrows(AssertionError.class, () -> getConfiguredTarget("//test:target"));
+ assertThat(e)
+ .hasMessageThat()
+ .contains(
+ "Skylark APIs accessing linking flags has been removed. "
+ + "Use the new API on cc_common.");
+ }
+
+ @Test
+ public void testDisablingCompilerOptions() throws Exception {
+ testDisablingCompilationApiMethod("provider.compiler_options()");
+ }
+
+ @Test
+ public void testDisablingCxxOptions() throws Exception {
+ testDisablingCompilationApiMethod("provider.cxx_options()");
+ }
+
+ @Test
+ public void testDisablingCOptions() throws Exception {
+ testDisablingCompilationApiMethod("provider.c_options()");
+ }
+
+ @Test
+ public void testDisablingUnfilteredOptions() throws Exception {
+ testDisablingCompilationApiMethod("provider.unfiltered_compiler_options([])");
+ }
+
+ @Test
+ public void testDisablingCompilerOptionsFromConfiguration() throws Exception {
+ testDisablingCompilationApiMethod("ctx.fragments.cpp.compiler_options([])");
+ }
+
+ @Test
+ public void testDisablingCxxOptionsFromConfiguration() throws Exception {
+ testDisablingCompilationApiMethod("ctx.fragments.cpp.cxx_options([])");
+ }
+
+ @Test
+ public void testDisablingCOptionsFromConfiguration() throws Exception {
+ testDisablingCompilationApiMethod("ctx.fragments.cpp.c_options");
+ }
+
+ @Test
+ public void testDisablingUnfilteredOptionsFromConfiguration() throws Exception {
+ testDisablingCompilationApiMethod("ctx.fragments.cpp.unfiltered_compiler_options([])");
+ }
+
+ private void testDisablingCompilationApiMethod(String method) throws Exception {
+ useConfiguration("--experimental_disable_legacy_cc_compilation_api");
+ scratch.file(
+ "test/rule.bzl",
+ "def _impl(ctx):",
+ " provider = ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]",
+ " return struct(",
+ " compile_options = " + method + ",",
+ " )",
+ "",
+ "my_rule = rule(",
+ " _impl,",
+ " fragments = [ 'cpp' ],",
+ " attrs = {'_cc_toolchain': attr.label(default=Label('//test:toolchain')) }",
+ ")");
+ scratch.file(
+ "test/BUILD",
+ "load(':rule.bzl', 'my_rule')",
+ "cc_toolchain_alias(name = 'toolchain')",
+ "my_rule(name = 'target')");
+ AssertionError e =
+ assertThrows(AssertionError.class, () -> getConfiguredTarget("//test:target"));
+ assertThat(e)
+ .hasMessageThat()
+ .contains(
+ "Skylark APIs accessing compilation flags has been removed. "
+ + "Use the new API on cc_common.");
+ }
+
+ @Test
+ public void testDisablingCompilationModeFlags() throws Exception {
+ AnalysisMock.get()
+ .ccSupport()
+ .setupCrosstool(
+ mockToolsConfig,
+ "compilation_mode_flags { mode: OPT compiler_flag: '-foo_from_compilation_mode' }",
+ "compilation_mode_flags { mode: OPT cxx_flag: '-bar_from_compilation_mode' }",
+ "compilation_mode_flags { mode: OPT linker_flag: '-baz_from_compilation_mode' }");
+ scratch.file("a/BUILD", "cc_library(name='a', srcs=['a.cc'])");
+
+ useConfiguration("-c", "opt");
+ CcToolchainProvider ccToolchainProvider = getCcToolchainProvider();
+ assertThat(ccToolchainProvider.getCompilerOptions()).contains("-foo_from_compilation_mode");
+ assertThat(ccToolchainProvider.getLegacyCxxOptions()).contains("-bar_from_compilation_mode");
+ assertThat(ccToolchainProvider.getLegacyMostlyStaticLinkFlags(CompilationMode.OPT))
+ .contains("-baz_from_compilation_mode");
+
+ useConfiguration("-c", "opt", "--experimental_disable_compilation_mode_flags");
+ ccToolchainProvider = getCcToolchainProvider();
+ assertThat(ccToolchainProvider.getCompilerOptions())
+ .doesNotContain("-foo_from_compilation_mode");
+ assertThat(ccToolchainProvider.getLegacyCxxOptions())
+ .doesNotContain("-bar_from_compilation_mode");
+ assertThat(ccToolchainProvider.getLegacyMostlyStaticLinkFlags(CompilationMode.OPT))
+ .doesNotContain("-baz_from_compilation_mode");
+ }
+
+ private CcToolchainProvider getCcToolchainProvider() throws Exception {
+ ConfiguredTarget target = getConfiguredTarget("//a");
+ RuleContext ruleContext = getRuleContext(target);
+ return CppHelper.getToolchainUsingDefaultCcToolchainAttribute(ruleContext);
+ }
+
+ @Test
+ public void testDisablingLinkingModeFlags() throws Exception {
+ AnalysisMock.get()
+ .ccSupport()
+ .setupCrosstool(
+ mockToolsConfig,
+ "linking_mode_flags { mode: MOSTLY_STATIC linker_flag: '-foo_from_linking_mode' }");
+ scratch.file("a/BUILD", "cc_library(name='a', srcs=['a.cc'])");
+
+ useConfiguration();
+ CcToolchainProvider ccToolchainProvider = getCcToolchainProvider();
+ assertThat(ccToolchainProvider.getLegacyMostlyStaticLinkFlags(CompilationMode.OPT))
+ .contains("-foo_from_linking_mode");
+
+ useConfiguration("--experimental_disable_linking_mode_flags");
+ ccToolchainProvider = getCcToolchainProvider();
+ assertThat(ccToolchainProvider.getLegacyMostlyStaticLinkFlags(CompilationMode.OPT))
+ .doesNotContain("-foo_from_linking_mode");
+ }
+
+ @Test
+ public void testDisablingLegacyCrosstoolFields() throws Exception {
+ AnalysisMock.get()
+ .ccSupport()
+ .setupCrosstool(
+ mockToolsConfig,
+ "compiler_flag: '-foo_compiler_flag'",
+ "cxx_flag: '-foo_cxx_flag'",
+ "unfiltered_cxx_flag: '-foo_unfiltered_cxx_flag'",
+ "linker_flag: '-foo_linker_flag'",
+ "dynamic_library_linker_flag: '-foo_dynamic_library_linker_flag'",
+ "test_only_linker_flag: '-foo_test_only_linker_flag'");
+ scratch.file("a/BUILD", "cc_library(name='a', srcs=['a.cc'])");
+
+ useConfiguration();
+ CcToolchainProvider ccToolchainProvider = getCcToolchainProvider();
+ assertThat(ccToolchainProvider.getLegacyCompileOptions()).contains("-foo_compiler_flag");
+ assertThat(ccToolchainProvider.getLegacyCxxOptions()).contains("-foo_cxx_flag");
+ assertThat(ccToolchainProvider.getUnfilteredCompilerOptions())
+ .contains("-foo_unfiltered_cxx_flag");
+ assertThat(ccToolchainProvider.getLegacyLinkOptions()).contains("-foo_linker_flag");
+ assertThat(ccToolchainProvider.getSharedLibraryLinkOptions(/* flags= */ ImmutableList.of()))
+ .contains("-foo_dynamic_library_linker_flag");
+ assertThat(ccToolchainProvider.getTestOnlyLinkOptions()).contains("-foo_test_only_linker_flag");
+
+ useConfiguration("--experimental_disable_legacy_crosstool_fields");
+ ccToolchainProvider = getCcToolchainProvider();
+ assertThat(ccToolchainProvider.getLegacyCompileOptions()).doesNotContain("-foo_compiler_flag");
+ assertThat(ccToolchainProvider.getLegacyCxxOptions()).doesNotContain("-foo_cxx_flag");
+ assertThat(ccToolchainProvider.getUnfilteredCompilerOptions())
+ .doesNotContain("-foo_unfiltered_cxx_flag");
+ assertThat(ccToolchainProvider.getLegacyLinkOptions()).doesNotContain("-foo_linker_flag");
+ assertThat(ccToolchainProvider.getSharedLibraryLinkOptions(/* flags= */ ImmutableList.of()))
+ .doesNotContain("-foo_dynamic_library_linker_flag");
+ assertThat(ccToolchainProvider.getTestOnlyLinkOptions())
+ .doesNotContain("-foo_test_only_linker_flag");
+ }
}