Skip to content

Commit 48c1061

Browse files
committed
Add new bazel_first_party and bazel_third_party features
This adds 2 new features based on if the code is being compiled as part of the main repo, or as part of a third party dependency. This allows users to customize C++ toolchains (or other rules) based on these features. This can replace some uses of `--per_file_copt=external/.*@SOMETHING`. This also allows us to combine features in a way that default toolchain features can be disabled for third party code. For example you can disable layering_check for third party code, since you likely don't care if they correctly define their deps once you're pulling them in.
1 parent 5f68021 commit 48c1061

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

src/main/java/com/google/devtools/build/lib/skyframe/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,12 +2412,14 @@ java_library(
24122412
":repo_file_value",
24132413
":repository_mapping_value",
24142414
":sky_functions",
2415+
"//src/main/java/com/google/devtools/build/lib/analysis/config:feature_set",
24152416
"//src/main/java/com/google/devtools/build/lib/cmdline",
24162417
"//src/main/java/com/google/devtools/build/lib/events",
24172418
"//src/main/java/com/google/devtools/build/lib/packages",
24182419
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
24192420
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
24202421
"//src/main/java/net/starlark/java/eval",
2422+
"//third_party:guava",
24212423
"//third_party:jsr305",
24222424
],
24232425
)

src/main/java/com/google/devtools/build/lib/skyframe/RepoPackageArgsFunction.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package com.google.devtools.build.lib.skyframe;
1616

17+
import com.google.common.collect.ImmutableSet;
18+
import com.google.devtools.build.lib.analysis.config.FeatureSet;
1719
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
1820
import com.google.devtools.build.lib.cmdline.RepositoryName;
1921
import com.google.devtools.build.lib.events.Event;
@@ -32,6 +34,14 @@
3234

3335
/** A {@link SkyFunction} that returns the {@link PackageArgs} for a given repository. */
3436
public class RepoPackageArgsFunction implements SkyFunction {
37+
private static final String BAZEL_FIRST_PARTY = "bazel_first_party";
38+
private static final String BAZEL_THIRD_PARTY = "bazel_third_party";
39+
40+
private static final FeatureSet FIRST_PARTY_FEATURES =
41+
new FeatureSet(ImmutableSet.of(BAZEL_FIRST_PARTY), ImmutableSet.of());
42+
private static final FeatureSet THIRD_PARTY_FEATURES =
43+
new FeatureSet(ImmutableSet.of(BAZEL_THIRD_PARTY), ImmutableSet.of());
44+
3545
public static final RepoPackageArgsFunction INSTANCE = new RepoPackageArgsFunction();
3646

3747
/** {@link SkyValue} wrapping a PackageArgs. */
@@ -116,6 +126,8 @@ public SkyValue compute(SkyKey skyKey, Environment env)
116126
RepoFileFunction.getDisplayNameForRepo(repositoryName, mainRepoMapping.repositoryMapping());
117127

118128
PackageArgs.Builder pkgArgsBuilder = PackageArgs.builder();
129+
pkgArgsBuilder.setFeatures(
130+
repositoryName.isMain() ? FIRST_PARTY_FEATURES : THIRD_PARTY_FEATURES);
119131
LabelConverter labelConverter =
120132
new LabelConverter(
121133
PackageIdentifier.create(repositoryName, PathFragment.EMPTY_FRAGMENT),

src/test/java/com/google/devtools/build/lib/pkgcache/PackageLoadingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,8 @@ public void testPackageFeatures() throws Exception {
665665
)
666666
""");
667667
assertThat(getPackage("peach").getPackageArgs().features())
668-
.isEqualTo(FeatureSet.parse(ImmutableList.of("crosstool_default_false")));
668+
.isEqualTo(
669+
FeatureSet.parse(ImmutableList.of("bazel_first_party", "crosstool_default_false")));
669670
}
670671

671672
@Test

src/test/java/com/google/devtools/build/lib/skyframe/RepoFileFunctionTest.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,78 @@ public void featureMerger() throws Exception {
113113
""");
114114
invalidatePackages();
115115
RuleContext ruleContext = getRuleContext(getConfiguredTarget("//abc/def:what"));
116-
assertThat(ruleContext.getFeatures()).containsExactly("b", "c", "d");
116+
assertThat(ruleContext.getFeatures()).containsExactly("b", "c", "d", "bazel_first_party");
117117
assertThat(ruleContext.getDisabledFeatures()).containsExactly("a");
118118
}
119119

120+
@Test
121+
public void mainRepoHasFirstPartyFeature() throws Exception {
122+
scratch.overwriteFile("REPO.bazel", "");
123+
scratch.overwriteFile("abc/BUILD", "filegroup(name='t')");
124+
invalidatePackages();
125+
RuleContext ruleContext = getRuleContext(getConfiguredTarget("//abc:t"));
126+
assertThat(ruleContext.getFeatures()).contains("bazel_first_party");
127+
assertThat(ruleContext.getFeatures()).doesNotContain("bazel_third_party");
128+
}
129+
130+
@Test
131+
public void mainRepoHasFirstPartyFeatureWithNoRepoFile() throws Exception {
132+
scratch.overwriteFile("abc/BUILD", "filegroup(name='t')");
133+
invalidatePackages();
134+
RuleContext ruleContext = getRuleContext(getConfiguredTarget("//abc:t"));
135+
assertThat(ruleContext.getFeatures()).contains("bazel_first_party");
136+
assertThat(ruleContext.getFeatures()).doesNotContain("bazel_third_party");
137+
}
138+
139+
@Test
140+
public void externalRepoHasThirdPartyFeature() throws Exception {
141+
scratch.overwriteFile("MODULE.bazel", "bazel_dep(name='foo',version='1.0')");
142+
registry.addModule(createModuleKey("foo", "1.0"), "module(name='foo',version='1.0')");
143+
scratch.overwriteFile(
144+
moduleRoot.getRelative("foo+1.0/MODULE.bazel").getPathString(), "");
145+
scratch.overwriteFile(
146+
moduleRoot.getRelative("foo+1.0/abc/BUILD").getPathString(), "filegroup(name='t')");
147+
invalidatePackages();
148+
RuleContext ruleContext = getRuleContext(getConfiguredTarget("@@foo+//abc:t"));
149+
assertThat(ruleContext.getFeatures()).contains("bazel_third_party");
150+
assertThat(ruleContext.getFeatures()).doesNotContain("bazel_first_party");
151+
}
152+
153+
@Test
154+
public void firstPartyFeatureCanBeDisabledByRepoFile() throws Exception {
155+
scratch.overwriteFile("REPO.bazel", "repo(features=['-bazel_first_party'])");
156+
scratch.overwriteFile("abc/BUILD", "filegroup(name='t')");
157+
invalidatePackages();
158+
RuleContext ruleContext = getRuleContext(getConfiguredTarget("//abc:t"));
159+
assertThat(ruleContext.getFeatures()).doesNotContain("bazel_first_party");
160+
assertThat(ruleContext.getDisabledFeatures()).contains("bazel_first_party");
161+
}
162+
163+
@Test
164+
public void thirdPartyFeatureCanBeDisabledByRepoFile() throws Exception {
165+
scratch.overwriteFile("MODULE.bazel", "bazel_dep(name='foo',version='1.0')");
166+
registry.addModule(createModuleKey("foo", "1.0"), "module(name='foo',version='1.0')");
167+
scratch.overwriteFile(
168+
moduleRoot.getRelative("foo+1.0/REPO.bazel").getPathString(),
169+
"repo(features=['-bazel_third_party'])");
170+
scratch.overwriteFile(
171+
moduleRoot.getRelative("foo+1.0/abc/BUILD").getPathString(), "filegroup(name='t')");
172+
invalidatePackages();
173+
RuleContext ruleContext = getRuleContext(getConfiguredTarget("@@foo+//abc:t"));
174+
assertThat(ruleContext.getFeatures()).doesNotContain("bazel_third_party");
175+
assertThat(ruleContext.getDisabledFeatures()).contains("bazel_third_party");
176+
}
177+
178+
@Test
179+
public void firstPartyFeatureMergesWithUserFeatures() throws Exception {
180+
scratch.overwriteFile("REPO.bazel", "repo(features=['custom_feature'])");
181+
scratch.overwriteFile("abc/BUILD", "filegroup(name='t')");
182+
invalidatePackages();
183+
RuleContext ruleContext = getRuleContext(getConfiguredTarget("//abc:t"));
184+
assertThat(ruleContext.getFeatures()).contains("bazel_first_party");
185+
assertThat(ruleContext.getFeatures()).contains("custom_feature");
186+
}
187+
120188
@Test
121189
public void restrictedSyntax() throws Exception {
122190
scratch.overwriteFile(

src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleContextTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ public void testConfiguration() throws Exception {
11301130
public void testFeatures() throws Exception {
11311131
setRuleContext(createRuleContext("//foo:cc_with_features"));
11321132
Object result = ev.eval("ruleContext.features");
1133-
assertThat((Sequence) result).containsExactly("f1", "f2");
1133+
assertThat((Sequence) result).containsExactly("bazel_first_party", "f1", "f2");
11341134
}
11351135

11361136
@Test

0 commit comments

Comments
 (0)