Skip to content

Commit

Permalink
Add CcToolchainInfo.all_files field
Browse files Browse the repository at this point in the history
Previously, we accessed that information from TransitiveInfoCollection, but when we migrate to platforms/toolchains we only have access to the toolchain provider. This cl adds a field on the `CcToolchainInfo` so we can migrate C++ rules to platforms/toolchains.

#6516

RELNOTES: None.
PiperOrigin-RevId: 236351968
  • Loading branch information
hlopko authored and copybara-github committed Mar 1, 2019
1 parent 04952fc commit f59038d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.devtools.build.lib.rules.cpp.Link.LinkingMode;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skylarkbuildapi.cpp.CcToolchainProviderApi;
import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
import com.google.devtools.build.lib.util.Pair;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain;
Expand Down Expand Up @@ -362,6 +363,11 @@ public ImmutableList<String> getBuiltInIncludeDirectoriesAsStrings() {
.collect(ImmutableList.toImmutableList());
}

@Override
public SkylarkNestedSet getAllFilesForStarlark() {
return SkylarkNestedSet.of(Artifact.class, getAllFiles());
}

public ImmutableList<PathFragment> getBuiltInIncludeDirectories() {
return builtInIncludeDirectories;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.devtools.build.lib.skylarkinterface.Param;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
import com.google.devtools.build.lib.syntax.StarlarkSemantics.FlagIdentifier;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -60,6 +61,14 @@ public interface CcToolchainProviderApi<FeatureConfigurationT extends FeatureCon
structField = true)
public ImmutableList<String> getBuiltInIncludeDirectoriesAsStrings();

@SkylarkCallable(
name = "all_files",
doc =
"Returns all toolchain files (so they can be passed to actions using this "
+ "toolchain as inputs).",
structField = true)
public SkylarkNestedSet getAllFilesForStarlark();

@SkylarkCallable(
name = "sysroot",
structField = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,36 @@ public void setSkylarkSemanticsOptions() throws Exception {
invalidatePackages();
}

@Test
public void testAllFiles() throws Exception {
scratch.file(
"a/BUILD",
"load(':rule.bzl', 'crule')",
"cc_toolchain_alias(name='alias')",
"crule(name='r')");

scratch.file(
"a/rule.bzl",
"def _impl(ctx):",
" toolchain = ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]",
" return struct(all_files = toolchain.all_files)",
"crule = rule(",
" _impl,",
" attrs = { ",
" '_cc_toolchain': attr.label(default=Label('//a:alias'))",
" },",
");");

ConfiguredTarget r = getConfiguredTarget("//a:r");
@SuppressWarnings("unchecked")
SkylarkNestedSet allFiles = (SkylarkNestedSet) r.get("all_files");
RuleContext ruleContext = getRuleContext(r);
CcToolchainProvider toolchain =
CppHelper.getToolchain(
ruleContext, ruleContext.getPrerequisite("$cc_toolchain", Mode.TARGET));
assertThat(allFiles.getSet(Artifact.class)).isEqualTo(toolchain.getAllFiles());
}

@Test
public void testGetToolForAction() throws Exception {
scratch.file(
Expand Down

0 comments on commit f59038d

Please sign in to comment.