Skip to content

Commit

Permalink
Starlarkify CcToolchainProviderHelper.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 525419738
Change-Id: Ib887b0859edadf3b207f698d902e810cd11fbea7
  • Loading branch information
buildbreaker2021 authored and copybara-github committed Apr 19, 2023
1 parent c6c33b9 commit 0dc0c7e
Show file tree
Hide file tree
Showing 13 changed files with 749 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ public CcCompilationContext createCcCompilationContext(
Sequence<?> directPublicHdrs,
Sequence<?> directPrivateHdrs,
Object purposeNoneable,
Object moduleMap,
StarlarkThread thread)
throws EvalException {
isCalledFromStarlarkCcCommon(thread);
Expand Down Expand Up @@ -844,6 +845,9 @@ public CcCompilationContext createCcCompilationContext(
&& purposeNoneable != Starlark.NONE) {
ccCompilationContext.setPurpose((String) purposeNoneable);
}
if (moduleMap != null && moduleMap != Starlark.UNBOUND && moduleMap != Starlark.NONE) {
ccCompilationContext.setCppModuleMap((CppModuleMap) moduleMap);
}

return ccCompilationContext.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@

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

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.docgen.annot.DocCategory;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.CommandLineExpansionException;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.analysis.starlark.StarlarkActionFactory;
import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleContext;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.collect.nestedset.Depset.TypeException;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.packages.Attribute.ComputedDefault;
import com.google.devtools.build.lib.packages.AttributeMap;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
Expand All @@ -29,12 +37,18 @@
import com.google.devtools.build.lib.rules.cpp.CcLinkingContext.Linkstamp;
import com.google.devtools.build.lib.starlarkbuildapi.NativeComputedDefaultApi;
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
import com.google.devtools.build.lib.util.Pair;
import com.google.devtools.build.lib.vfs.PathFragment;
import javax.annotation.Nullable;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.ParamType;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.NoneType;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkValue;
import net.starlark.java.syntax.Location;

Expand All @@ -44,6 +58,269 @@ public class CcStarlarkInternal implements StarlarkValue {

public static final String NAME = "cc_internal";

@Nullable
private PathFragment getPathfragmentOrNone(Object o) {
String pathString = CcModule.convertFromNoneable(o, null);
if (pathString == null) {
return null;
}
return PathFragment.create(pathString);
}

private ImmutableMap<String, PathFragment> castDict(Dict<?, ?> d) throws EvalException {
return Dict.cast(d, String.class, String.class, "tool_paths").entrySet().stream()
.map(p -> Pair.of(p.getKey(), PathFragment.create(p.getValue())))
.collect(toImmutableMap(Pair::getFirst, Pair::getSecond));
}

@StarlarkMethod(
name = "construct_toolchain_provider",
documented = false,
parameters = {
@Param(name = "ctx", positional = false, named = true),
@Param(name = "cpp_config", positional = false, named = true),
@Param(name = "toolchain_features", positional = false, named = true),
@Param(name = "tools_directory", positional = false, named = true),
@Param(name = "attributes", positional = false, named = true),
@Param(
name = "static_runtime_link_inputs",
positional = false,
named = true,
allowedTypes = {
@ParamType(type = Depset.class),
@ParamType(type = NoneType.class),
}),
@Param(
name = "dynamic_runtime_link_symlinks",
positional = false,
named = true,
allowedTypes = {
@ParamType(type = Depset.class),
@ParamType(type = NoneType.class),
}),
@Param(name = "runtime_solib_dir", positional = false, named = true),
@Param(name = "cc_compilation_context", positional = false, named = true),
@Param(name = "builtin_include_files", positional = false, named = true),
@Param(name = "target_builtin_include_files", positional = false, named = true),
@Param(name = "builtin_include_directories", positional = false, named = true),
@Param(name = "sysroot", positional = false, named = true),
@Param(name = "target_sysroot", positional = false, named = true),
@Param(name = "fdo_context", positional = false, named = true),
@Param(name = "is_tool_configuration", positional = false, named = true),
@Param(name = "tool_paths", positional = false, named = true),
@Param(name = "toolchain_config_info", positional = false, named = true),
@Param(name = "default_sysroot", positional = false, named = true),
@Param(name = "runtime_sysroot", positional = false, named = true),
@Param(name = "solib_directory", positional = false, named = true),
@Param(name = "additional_make_variables", positional = false, named = true),
@Param(name = "legacy_cc_flags_make_variable", positional = false, named = true),
@Param(name = "objcopy", positional = false, named = true),
@Param(name = "compiler", positional = false, named = true),
@Param(name = "preprocessor", positional = false, named = true),
@Param(name = "nm", positional = false, named = true),
@Param(name = "objdump", positional = false, named = true),
@Param(name = "ar", positional = false, named = true),
@Param(name = "strip", positional = false, named = true),
@Param(name = "ld", positional = false, named = true),
@Param(name = "gcov", positional = false, named = true),
})
public CcToolchainProvider getCcToolchainProvider(
StarlarkRuleContext ruleContext,
Object cppConfigurationObject,
CcToolchainFeatures toolchainFeatures,
String toolsDirectoryStr,
CcToolchainAttributesProvider attributes,
Object staticRuntimeLinkInputsObject,
Object dynamicRuntimeLinkInputsObject,
String dynamicRuntimeSolibDirStr,
CcCompilationContext ccCompilationContext,
Sequence<?> builtinIncludeFiles,
Sequence<?> targetBuiltinIncludeFiles,
Sequence<?> builtInIncludeDirectoriesStr,
Object sysrootObject,
Object targetSysrootObject,
FdoContext fdoContext,
boolean isToolConfiguration,
Dict<?, ?> toolPathsDict,
CcToolchainConfigInfo toolchainConfigInfo,
Object defaultSysrootObject,
Object runtimeSysrootObject,
String solibDirectory,
Dict<?, ?> additionalMakeVariablesDict,
String legacyCcFlagsMakeVariable,
String objcopyExecutable,
String compilerExecutable,
String preprocessorExecutable,
String nmExecutable,
String objdumpExecutable,
String arExecutable,
String stripExecutable,
String ldExecutable,
String gcovExecutable)
throws EvalException {
CppConfiguration cppConfiguration = CcModule.convertFromNoneable(cppConfigurationObject, null);
PathFragment toolsDirectory = PathFragment.create(toolsDirectoryStr);
NestedSet<Artifact> staticRuntimeLinkInputsSet = null;
NestedSet<Artifact> dynamicRuntimeLinkInputsSet = null;
try {
if (staticRuntimeLinkInputsObject != Starlark.NONE) {
staticRuntimeLinkInputsSet =
((Depset) staticRuntimeLinkInputsObject).getSet(Artifact.class);
}
if (dynamicRuntimeLinkInputsObject != Starlark.NONE) {
dynamicRuntimeLinkInputsSet =
((Depset) dynamicRuntimeLinkInputsObject).getSet(Artifact.class);
}
} catch (TypeException e) {
throw new EvalException(e);
}
PathFragment dynamicRuntimeSolibDir = PathFragment.create(dynamicRuntimeSolibDirStr);
ImmutableList<PathFragment> builtInIncludeDirectories =
Sequence.cast(builtInIncludeDirectoriesStr, String.class, "builtin_include_directories")
.stream()
.map(PathFragment::create)
.collect(toImmutableList());
PathFragment sysroot = getPathfragmentOrNone(sysrootObject);
PathFragment targetSysroot = getPathfragmentOrNone(targetSysrootObject);
Dict<String, String> additionalMakeVariables =
Dict.cast(additionalMakeVariablesDict, String.class, String.class, "tool_paths");
PathFragment defaultSysroot = getPathfragmentOrNone(defaultSysrootObject);
PathFragment runtimeSysroot = getPathfragmentOrNone(runtimeSysrootObject);

return new CcToolchainProvider(
/* cppConfiguration= */ cppConfiguration,
/* toolchainFeatures= */ toolchainFeatures,
/* crosstoolTopPathFragment= */ toolsDirectory,
/* allFiles= */ attributes.getAllFiles(),
/* allFilesIncludingLibc= */ attributes.getFullInputsForCrosstool(),
/* compilerFiles= */ attributes.getCompilerFiles(),
/* compilerFilesWithoutIncludes= */ attributes.getCompilerFilesWithoutIncludes(),
/* stripFiles= */ attributes.getStripFiles(),
/* objcopyFiles= */ attributes.getObjcopyFiles(),
/* asFiles= */ attributes.getAsFiles(),
/* arFiles= */ attributes.getArFiles(),
/* linkerFiles= */ attributes.getFullInputsForLink(),
/* interfaceSoBuilder= */ attributes.getIfsoBuilder(),
/* dwpFiles= */ attributes.getDwpFiles(),
/* coverageFiles= */ attributes.getCoverage(),
/* libcLink= */ attributes.getLibc(),
/* targetLibcLink= */ attributes.getTargetLibc(),
/* staticRuntimeLinkInputs= */ staticRuntimeLinkInputsSet,
/* dynamicRuntimeLinkInputs= */ dynamicRuntimeLinkInputsSet,
/* dynamicRuntimeSolibDir= */ dynamicRuntimeSolibDir,
/* ccCompilationContext= */ ccCompilationContext,
/* supportsParamFiles= */ attributes.isSupportsParamFiles(),
/* supportsHeaderParsing= */ attributes.isSupportsHeaderParsing(),
/* additionalBuildVariablesComputer= */ attributes.getAdditionalBuildVariablesComputer(),
/* buildVariables= */ CcToolchainProviderHelper.getBuildVariables(
ruleContext.getRuleContext().getConfiguration().getOptions(),
cppConfiguration,
sysroot,
attributes.getAdditionalBuildVariablesComputer()),
/* builtinIncludeFiles= */ Sequence.cast(
builtinIncludeFiles, Artifact.class, "builtin_include_files")
.getImmutableList(),
/* targetBuiltinIncludeFiles= */ Sequence.cast(
targetBuiltinIncludeFiles, Artifact.class, "target_builtin_include_files")
.getImmutableList(),
/* linkDynamicLibraryTool= */ attributes.getLinkDynamicLibraryTool(),
/* builtInIncludeDirectories= */ builtInIncludeDirectories,
/* sysroot= */ sysroot,
/* targetSysroot= */ targetSysroot,
/* fdoContext= */ fdoContext,
/* isToolConfiguration= */ isToolConfiguration,
/* licensesProvider= */ attributes.getLicensesProvider(),
/* toolPaths= */ castDict(toolPathsDict),
/* toolchainIdentifier= */ toolchainConfigInfo.getToolchainIdentifier(),
/* compiler= */ toolchainConfigInfo.getCompiler(),
/* abiGlibcVersion= */ toolchainConfigInfo.getAbiLibcVersion(),
/* targetCpu= */ toolchainConfigInfo.getTargetCpu(),
/* targetOS= */ toolchainConfigInfo.getCcTargetOs(),
/* defaultSysroot= */ defaultSysroot,
/* runtimeSysroot= */ runtimeSysroot,
/* targetLibc= */ toolchainConfigInfo.getTargetLibc(),
/* ccToolchainLabel= */ ruleContext.getRuleContext().getLabel(),
/* solibDirectory= */ solibDirectory,
/* abi= */ toolchainConfigInfo.getAbiVersion(),
/* targetSystemName= */ toolchainConfigInfo.getTargetSystemName(),
/* additionalMakeVariables= */ ImmutableMap.copyOf(additionalMakeVariables),
/* legacyCcFlagsMakeVariable= */ legacyCcFlagsMakeVariable,
/* allowlistForLayeringCheck= */ attributes.getAllowlistForLayeringCheck(),
/* allowListForLooseHeaderCheck= */ attributes.getAllowlistForLooseHeaderCheck(),
/* objcopyExecutable= */ objcopyExecutable,
/* compilerExecutable= */ compilerExecutable,
/* preprocessorExecutable= */ preprocessorExecutable,
/* nmExecutable= */ nmExecutable,
/* objdumpExecutable= */ objdumpExecutable,
/* arExecutable= */ arExecutable,
/* stripExecutable= */ stripExecutable,
/* ldExecutable= */ ldExecutable,
/* gcovExecutable= */ gcovExecutable);
}

@StarlarkMethod(
name = "solib_symlink_action",
documented = false,
parameters = {
@Param(name = "ctx", positional = false, named = true),
@Param(name = "artifact", positional = false, named = true),
@Param(name = "solib_directory", positional = false, named = true),
@Param(name = "runtime_solib_dir_base", positional = false, named = true),
})
public Artifact solibSymlinkAction(
StarlarkRuleContext ruleContext,
Artifact artifact,
String solibDirectory,
String runtimeSolibDirBase) {
return SolibSymlinkAction.getCppRuntimeSymlink(
ruleContext.getRuleContext(), artifact, solibDirectory, runtimeSolibDirBase);
}

@StarlarkMethod(
name = "fdo_context",
documented = false,
parameters = {
@Param(name = "ctx", positional = false, named = true),
@Param(name = "attributes", positional = false, named = true),
@Param(name = "configuration", positional = false, named = true),
@Param(name = "cpp_config", positional = false, named = true),
@Param(name = "tool_paths", positional = false, named = true),
},
allowReturnNones = true)
@Nullable
public FdoContext fdoContext(
StarlarkRuleContext ruleContext,
CcToolchainAttributesProvider attributes,
BuildConfigurationValue configuration,
CppConfiguration cppConfiguration,
Dict<?, ?> toolPathsDict)
throws EvalException, InterruptedException {
try {
return FdoHelper.getFdoContext(
ruleContext.getRuleContext(),
attributes,
configuration,
cppConfiguration,
castDict(toolPathsDict));
} catch (RuleErrorException e) {
throw new EvalException(e);
}
}

@StarlarkMethod(
name = "cc_toolchain_features",
documented = false,
parameters = {
@Param(name = "toolchain_config_info", positional = false, named = true),
@Param(name = "tools_directory", positional = false, named = true),
})
public CcToolchainFeatures ccToolchainFeatures(
CcToolchainConfigInfo ccToolchainConfigInfo, String toolsDirectoryPathString)
throws EvalException {
return new CcToolchainFeatures(
ccToolchainConfigInfo, PathFragment.create(toolsDirectoryPathString));
}

@StarlarkMethod(
name = "is_package_headers_checking_mode_set",
documented = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
package com.google.devtools.build.lib.rules.cpp;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
Expand All @@ -29,6 +30,8 @@
import java.io.Serializable;
import java.util.HashMap;
import javax.annotation.Nullable;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkFunction;
import net.starlark.java.syntax.Location;

/**
Expand Down Expand Up @@ -86,10 +89,22 @@ public ConfiguredTarget create(RuleContext ruleContext)
.build();
}

StarlarkFunction getCcToolchainProvider =
(StarlarkFunction) ruleContext.getStarlarkDefinedBuiltin("get_cc_toolchain_provider");
ruleContext.initStarlarkRuleContext();
Object starlarkCcToolchainProvider =
ruleContext.callStarlarkOrThrowRuleError(
getCcToolchainProvider,
ImmutableList.of(
/* ctx */ ruleContext.getStarlarkRuleContext(), /* attributes */ attributes),
ImmutableMap.of());

// This is a platforms-backed build, we will not analyze cc_toolchain_suite at all, and we are
// sure current cc_toolchain is the one selected. We can create CcToolchainProvider here.
CcToolchainProvider ccToolchainProvider =
CcToolchainProviderHelper.getCcToolchainProvider(ruleContext, attributes);
starlarkCcToolchainProvider != Starlark.NONE
? (CcToolchainProvider) starlarkCcToolchainProvider
: null;

if (ccToolchainProvider == null) {
// Skyframe restart
Expand Down
Loading

0 comments on commit 0dc0c7e

Please sign in to comment.