diff options
author | 2018-06-28 05:38:50 -0700 | |
---|---|---|
committer | 2018-06-28 05:39:55 -0700 | |
commit | c71157f9a826db25e2588a0147de76e97ff30eb2 (patch) | |
tree | deeea287e7e47c4db04b7e8f99f5e133900ea181 /src/main/java/com/google/devtools/build/lib/rules | |
parent | 74a9f015270083d51e0e32e8390a33c3aecb9a2c (diff) |
Expose Skylark API to the C++ toolchain
This is an implementation of the design at
https://docs.google.com/document/d/1g91BWJITcYw_X-VxsDC0VgUn5E9g0kRBGoBSpoO41gA/edit>.
More thorough documentation will be sent in a separate cl. The api was approved
at
https://docs.google.com/document/d/1M8JA7kzZnWpLZ3WEX9rp6k2u_nlwE8smsHYgVTSSJ9k/edit?ts=5b292400#.
Work towards #4571 (only the docs are missing).
RELNOTES: None.
PiperOrigin-RevId: 202464331
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java | 167 |
1 files changed, 166 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java index d6b450debd..0e7eca0a51 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java @@ -14,15 +14,180 @@ package com.google.devtools.build.lib.rules.cpp; +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.platform.ToolchainInfo; +import com.google.devtools.build.lib.collect.nestedset.NestedSet; +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.Provider; +import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; import com.google.devtools.build.lib.skylarkbuildapi.cpp.CcModuleApi; +import com.google.devtools.build.lib.syntax.EvalException; +import com.google.devtools.build.lib.syntax.EvalUtils; +import com.google.devtools.build.lib.syntax.SkylarkDict; +import com.google.devtools.build.lib.syntax.SkylarkList; +import com.google.devtools.build.lib.syntax.SkylarkNestedSet; +import javax.annotation.Nullable; /** A module that contains Skylark utilities for C++ support. */ -public class CcModule implements CcModuleApi { +public class CcModule + implements CcModuleApi<CcToolchainProvider, FeatureConfiguration, CcToolchainVariables> { @Override public Provider getCcToolchainProvider() { return ToolchainInfo.PROVIDER; } + + @Override + public FeatureConfiguration configureFeatures( + CcToolchainProvider toolchain, + SkylarkList<String> requestedFeatures, + SkylarkList<String> unsupportedFeatures) + throws EvalException { + return CcCommon.configureFeaturesOrThrowEvalException( + ImmutableSet.copyOf(requestedFeatures), + ImmutableSet.copyOf(unsupportedFeatures), + toolchain); + } + + @Override + public String getToolForAction(FeatureConfiguration featureConfiguration, String actionName) { + return featureConfiguration + .getToolForAction(actionName) + .getToolPathFragment() + .getSafePathString(); + } + + @Override + public boolean isEnabled(FeatureConfiguration featureConfiguration, String featureName) { + return featureConfiguration.isEnabled(featureName); + } + + @Override + public SkylarkList<String> getCommandLine( + FeatureConfiguration featureConfiguration, + String actionName, + CcToolchainVariables variables) { + return SkylarkList.createImmutable(featureConfiguration.getCommandLine(actionName, variables)); + } + + @Override + public SkylarkDict<String, String> getEnvironmentVariable( + FeatureConfiguration featureConfiguration, + String actionName, + CcToolchainVariables variables) { + return SkylarkDict.copyOf( + null, featureConfiguration.getEnvironmentVariables(actionName, variables)); + } + + @Override + public CcToolchainVariables getCompileBuildVariables( + CcToolchainProvider ccToolchainProvider, + FeatureConfiguration featureConfiguration, + Object sourceFile, + Object outputFile, + Object userCompileFlags, + Object includeDirs, + Object quoteIncludeDirs, + Object systemIncludeDirs, + Object defines, + boolean usePic, + boolean addLegacyCxxOptions) + throws EvalException { + return CompileBuildVariables.setupVariablesOrThrowEvalException( + featureConfiguration, + ccToolchainProvider, + convertFromNoneable(sourceFile, /* defaultValue= */ null), + convertFromNoneable(outputFile, /* defaultValue= */ null), + /* gcnoFile= */ null, + /* dwoFile= */ null, + /* ltoIndexingFile= */ null, + /* includes= */ ImmutableList.of(), + asStringNestedSet(userCompileFlags), + /* cppModuleMap= */ null, + usePic, + /* fakeOutputFile= */ null, + /* fdoStamp= */ null, + /* dotdFileExecPath= */ null, + /* variablesExtensions= */ ImmutableList.of(), + /* additionalBuildVariables= */ ImmutableMap.of(), + /* directModuleMaps= */ ImmutableList.of(), + asStringNestedSet(includeDirs), + asStringNestedSet(quoteIncludeDirs), + asStringNestedSet(systemIncludeDirs), + asStringNestedSet(defines), + addLegacyCxxOptions); + } + + @Override + public CcToolchainVariables getLinkBuildVariables( + CcToolchainProvider ccToolchainProvider, + FeatureConfiguration featureConfiguration, + Object librarySearchDirectories, + Object runtimeLibrarySearchDirectories, + Object userLinkFlags, + Object outputFile, + Object paramFile, + Object defFile, + boolean isUsingLinkerNotArchiver, + boolean isCreatingSharedLibrary, + boolean mustKeepDebug, + boolean useTestOnlyFlags, + boolean isStaticLinkingMode) + throws EvalException { + return LinkBuildVariables.setupVariables( + isUsingLinkerNotArchiver, + /* binDirectoryPath= */ null, + convertFromNoneable(outputFile, /* defaultValue= */ null), + isCreatingSharedLibrary, + convertFromNoneable(paramFile, /* defaultValue= */ null), + /* thinltoParamFile= */ null, + /* thinltoMergedObjectFile= */ null, + mustKeepDebug, + /* symbolCounts= */ null, + ccToolchainProvider, + featureConfiguration, + useTestOnlyFlags, + /* isLtoIndexing= */ false, + asStringNestedSet(userLinkFlags), + /* interfaceLibraryBuilder= */ null, + /* interfaceLibraryOutput= */ null, + /* ltoOutputRootPrefix= */ null, + convertFromNoneable(defFile, /* defaultValue= */ null), + /* fdoSupport= */ null, + asStringNestedSet(runtimeLibrarySearchDirectories), + /* librariesToLink= */ null, + asStringNestedSet(librarySearchDirectories), + /* isLegacyFullyStaticLinkingMode= */ false, + isStaticLinkingMode); + } + + @Override + public CcToolchainVariables getVariables() { + return CcToolchainVariables.EMPTY; + } + + /** + * Converts an object that can be the NoneType to the actual object if it is not or returns the + * default value if none. + */ + @SuppressWarnings("unchecked") + protected static <T> T convertFromNoneable(Object obj, @Nullable T defaultValue) { + if (EvalUtils.isNullOrNone(obj)) { + return defaultValue; + } + return (T) obj; + } + + /** Converts an object that can be the either SkylarkNestedSet or None into NestedSet. */ + protected NestedSet<String> asStringNestedSet(Object o) throws EvalException { + SkylarkNestedSet skylarkNestedSet = convertFromNoneable(o, /* defaultValue= */ null); + if (skylarkNestedSet != null) { + return skylarkNestedSet.getSet(String.class); + } else { + return NestedSetBuilder.emptySet(Order.STABLE_ORDER); + } + } } |