diff options
author | 2017-05-26 11:37:28 +0200 | |
---|---|---|
committer | 2017-05-26 13:25:58 +0200 | |
commit | a47780541536764cf56d09f78a988d6155689c7f (patch) | |
tree | 3ec1afd6b557ac27a6bd827fcdcee51865fef69f /src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java | |
parent | 1fd27bc4d97533031401c54aa05eb75b13c6874b (diff) |
Use FeatureConfiguration to compute value of CC_FLAGS make variable
This cl introduces new action_config type for Crosstool named 'generic'. This
can be used to set the value of CC_FLAGS make variable using much more
expressive mechanism (action_configs + features) than previous make_variable
Crosstool messages. This has been requested by the C++ LPT.
However, as FeatureConfiguration needs RuleContext, CC_FLAGS cannot be
computed using configuration only anymore. Also, FeatureConfiguration is C++
rules specific class, and Bazel build-base cannot depend on it. Therefore we
cannot use FeatureConfiguration for ExtraActions, for example. Because it cannot
be made perfect, this cl is not updating all the possible places that expand
make variables but limits the scope to:
* genrule (the only widely used rule that often expands make variables)
* *_test (CC_FLAGS is used by Tensorflow in the 'args' attribute)
* cc_rules (people sometimes set 'copts' to something like:
"-DCC_STRING=\\\"$(CC)\\\" -DCC_FLAGS_STRING=\"$(CC_FLAGS)\""
The long term plan is to use Skylark C++ API to get C++ command lines, so
CC_FLAGS together with this inconsistent solution will be removed.
RELNOTES: CC_FLAGS can be defined using 'generic' action_config in CROSSTOOL
PiperOrigin-RevId: 157202883
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java index f5de614815..505d8e33b0 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java @@ -13,7 +13,10 @@ // limitations under the License. package com.google.devtools.build.lib.rules.cpp; +import com.google.common.base.Joiner; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Maps; @@ -21,6 +24,7 @@ import com.google.devtools.build.lib.actions.ActionAnalysisMetadata; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.AnalysisEnvironment; import com.google.devtools.build.lib.analysis.FileProvider; +import com.google.devtools.build.lib.analysis.MakeVariableSupplier; import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode; import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; @@ -32,6 +36,7 @@ import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.rules.apple.Platform; import com.google.devtools.build.lib.rules.cpp.CcLibraryHelper.SourceCategory; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; +import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables; import com.google.devtools.build.lib.rules.cpp.CppConfiguration.DynamicMode; import com.google.devtools.build.lib.rules.cpp.CppConfiguration.HeadersCheckingMode; import com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector; @@ -50,6 +55,7 @@ import java.util.Map; import java.util.Set; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import javax.annotation.Nullable; /** * Common parts of the implementation of cc rules. @@ -89,6 +95,7 @@ public final class CcCommon { CppRuleClasses.PIC, CppRuleClasses.PER_OBJECT_DEBUG_INFO, CppRuleClasses.PREPROCESSOR_DEFINES); + public static final String CC_TOOLCHAIN_DEFAULT_ATTRIBUTE_NAME = ":cc_toolchain"; /** C++ configuration */ private final CppConfiguration cppConfiguration; @@ -304,6 +311,62 @@ public final class CcCommon { return getHeaders(ruleContext); } + /** + * Supply CC_FLAGS Make variable value computed from FeatureConfiguration. Appends them to + * original CC_FLAGS, so FeatureConfiguration can override legacy values. + */ + public static class CcFlagsSupplier implements MakeVariableSupplier { + + private final RuleContext ruleContext; + + public CcFlagsSupplier(RuleContext ruleContext) { + this.ruleContext = Preconditions.checkNotNull(ruleContext); + } + + @Override + @Nullable + public String getMakeVariable(String variableName) { + if (!variableName.equals(CppConfiguration.CC_FLAGS_MAKE_VARIABLE_NAME)) { + return null; + } + + CcToolchainProvider toolchain = + CppHelper.getToolchainUsingDefaultCcToolchainAttribute(ruleContext); + FeatureConfiguration featureConfiguration = + CcCommon.configureFeatures(ruleContext, toolchain); + if (!featureConfiguration.actionIsConfigured( + CppCompileAction.CC_FLAGS_MAKE_VARIABLE_ACTION_NAME)) { + return null; + } + + Variables buildVariables = new Variables.Builder() + .addAllStringVariables(toolchain.getBuildVariables()) + .build(); + String toolchainCcFlags = + Joiner.on(" ") + .join( + featureConfiguration.getCommandLine( + CppCompileAction.CC_FLAGS_MAKE_VARIABLE_ACTION_NAME, buildVariables)); + + ImmutableMap<String, String> currentMakeVariables = + ruleContext.getMakeVariables(ImmutableList.of(CC_TOOLCHAIN_DEFAULT_ATTRIBUTE_NAME)); + Preconditions.checkArgument( + currentMakeVariables.containsKey(CppConfiguration.CC_FLAGS_MAKE_VARIABLE_NAME)); + + return FluentIterable.of( + currentMakeVariables.get(CppConfiguration.CC_FLAGS_MAKE_VARIABLE_NAME)) + .append(toolchainCcFlags) + .join(Joiner.on(" ")); + } + + @Override + public ImmutableMap<String, String> getAllMakeVariables() { + return ImmutableMap.of( + CppConfiguration.CC_FLAGS_MAKE_VARIABLE_NAME, + getMakeVariable(CppConfiguration.CC_FLAGS_MAKE_VARIABLE_NAME)); + } + } + private static ImmutableList<String> getPackageCopts(RuleContext ruleContext) { List<String> unexpanded = ruleContext.getRule().getPackage().getDefaultCopts(); return ImmutableList.copyOf(CppHelper.expandMakeVariables(ruleContext, "copts", unexpanded)); |