Skip to content

Commit c9a5bfb

Browse files
committed
Use includes instead of system_includes for includes attr
Previously even though the attribute was named `includes` it was passed through the `system_includes` field of the compilation context. This resulted in toolchains passing these include paths with `-isystem`, which is unexpected if you use this for first party code. Many non bazel-first projects have header directory structures that require custom include paths be propagated throughout the graph, the alternative to `includes` is to use `strip_include_prefix`. The downside of `strip_include_prefix` is that you add 1 include path per `cc_library`, even if the libraries are in the same package. With `includes` these are deduplicated. In the case of LLVM using `includes` reduced the number of search paths on the order of hundreds. If users want to use `-isystem` for third party code that uses `includes`, they can pass `--features=external_include_paths --host_features=external_include_paths` If there are first party libraries users want to use `-isystem` with, they can use `features = ["system_include_paths"]` Fixes bazelbuild#20267 RELNOTES[INC]: Use `-I` instead of `-isystem` for `cc_library` / `cc_binary` `includes` attr. To use `-isystem` for only external repositories, you can pass `--features=external_include_paths --host_features=external_include_paths`. To use `-isystem` for a single `cc_library` / `cc_binary` `includes`, you can set `features = ["system_include_paths"],` on the target
1 parent 106903d commit c9a5bfb

File tree

12 files changed

+74
-30
lines changed

12 files changed

+74
-30
lines changed

compile.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ if [[ $PLATFORM == "darwin" ]] && \
6161
fi
6262

6363
if [[ $PLATFORM == "windows" ]]; then
64-
EXTRA_BAZEL_ARGS="${EXTRA_BAZEL_ARGS-} --cxxopt=/std:c++17 --host_cxxopt=/std:c++17"
64+
EXTRA_BAZEL_ARGS="${EXTRA_BAZEL_ARGS-} --cxxopt=/std:c++17 --host_cxxopt=/std:c++17 --copt=/external:W0 --host_copt=/external:W0"
6565
fi
6666

6767
source scripts/bootstrap/bootstrap.sh

scripts/bootstrap/bootstrap.sh

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ _BAZEL_ARGS="--spawn_strategy=standalone \
4444
--enable_bzlmod \
4545
--check_direct_dependencies=error \
4646
--lockfile_mode=update \
47+
--features=external_include_paths --host_features=external_include_paths \
4748
--override_repository=$(cat derived/maven/MAVEN_CANONICAL_REPO_NAME)=derived/maven \
4849
--java_runtime_version=${JAVA_VERSION} \
4950
--java_language_version=${JAVA_VERSION} \

src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,10 @@ public void validateInclusions(
11331133

11341134
private Iterable<PathFragment> getValidationIgnoredDirs() {
11351135
List<PathFragment> cxxSystemIncludeDirs = getBuiltInIncludeDirectories();
1136-
return Iterables.concat(cxxSystemIncludeDirs, ccCompilationContext.getSystemIncludeDirs());
1136+
return Iterables.concat(
1137+
cxxSystemIncludeDirs,
1138+
ccCompilationContext.getSystemIncludeDirs(),
1139+
ccCompilationContext.getExternalIncludeDirs());
11371140
}
11381141

11391142
@VisibleForTesting

src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
510510
cxx_flags = cc_helper.get_copts(ctx, feature_configuration, additional_make_variable_substitutions, attr = "cxxopts"),
511511
defines = cc_helper.defines(ctx, additional_make_variable_substitutions),
512512
local_defines = cc_helper.local_defines(ctx, additional_make_variable_substitutions) + cc_helper.get_local_defines_for_runfiles_lookup(ctx, ctx.attr.deps),
513-
system_includes = cc_helper.system_include_dirs(ctx, additional_make_variable_substitutions),
513+
includes = cc_helper.include_dirs(ctx, additional_make_variable_substitutions),
514514
private_hdrs = cc_helper.get_private_hdrs(ctx),
515515
public_hdrs = cc_helper.get_public_hdrs(ctx),
516516
copts_filter = cc_helper.copts_filter(ctx, additional_make_variable_substitutions),

src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl

+3-2
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ def _package_source_root(repository, package, sibling_repository_layout):
10621062
repository = repository[1:]
10631063
return paths.get_relative(paths.get_relative("external", repository), package)
10641064

1065-
def _system_include_dirs(ctx, additional_make_variable_substitutions):
1065+
def _include_dirs(ctx, additional_make_variable_substitutions):
10661066
result = []
10671067
sibling_repository_layout = ctx.configuration.is_sibling_repository_layout()
10681068
package = ctx.label.package
@@ -1264,7 +1264,8 @@ cc_helper = struct(
12641264
get_private_hdrs = _get_private_hdrs,
12651265
get_public_hdrs = _get_public_hdrs,
12661266
report_invalid_options = _report_invalid_options,
1267-
system_include_dirs = _system_include_dirs,
1267+
include_dirs = _include_dirs,
1268+
system_include_dirs = _include_dirs, # TODO: Remove uses of old name
12681269
get_coverage_environment = _get_coverage_environment,
12691270
create_cc_instrumented_files_info = _create_cc_instrumented_files_info,
12701271
linkopts = _linkopts,

src/main/starlark/builtins_bzl/common/cc/cc_library.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _cc_library_impl(ctx):
6363
cxx_flags = cc_helper.get_copts(ctx, feature_configuration, additional_make_variable_substitutions, attr = "cxxopts"),
6464
defines = cc_helper.defines(ctx, additional_make_variable_substitutions),
6565
local_defines = cc_helper.local_defines(ctx, additional_make_variable_substitutions) + cc_helper.get_local_defines_for_runfiles_lookup(ctx, ctx.attr.deps + ctx.attr.implementation_deps),
66-
system_includes = cc_helper.system_include_dirs(ctx, additional_make_variable_substitutions),
66+
includes = cc_helper.include_dirs(ctx, additional_make_variable_substitutions),
6767
copts_filter = cc_helper.copts_filter(ctx, additional_make_variable_substitutions),
6868
purpose = "cc_library-compile",
6969
srcs = cc_helper.get_srcs(ctx),

src/main/starlark/builtins_bzl/common/cc/compile/cc_compilation_helper.bzl

+4-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ def _init_cc_compilation_context(
227227
external_include_dirs = []
228228
declared_include_srcs = []
229229

230-
if not external:
230+
if not external and feature_configuration.is_requested("system_include_paths"):
231+
system_include_dirs_for_context = system_include_dirs + include_dirs
232+
include_dirs_for_context = []
233+
elif not external:
231234
system_include_dirs_for_context = list(system_include_dirs)
232235
include_dirs_for_context = list(include_dirs)
233236
else:

src/main/starlark/builtins_bzl/common/objc/compilation_support.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _build_common_variables(
105105
has_module_map = has_module_map,
106106
attr_linkopts = attr_linkopts,
107107
direct_cc_compilation_contexts = direct_cc_compilation_contexts,
108-
includes = cc_helper.system_include_dirs(ctx, {}) if hasattr(ctx.attr, "includes") else [],
108+
includes = cc_helper.include_dirs(ctx, {}) if hasattr(ctx.attr, "includes") else [],
109109
)
110110

111111
return struct(

src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java

+43-3
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ public void testIsolatedIncludes() throws Exception {
573573
ConfiguredTarget foo = getConfiguredTarget("//bang:bang");
574574

575575
String includesRoot = "bang/bang_includes";
576-
assertThat(foo.get(CcInfo.PROVIDER).getCcCompilationContext().getSystemIncludeDirs())
576+
assertThat(foo.get(CcInfo.PROVIDER).getCcCompilationContext().getIncludeDirs())
577577
.containsAtLeast(
578578
PathFragment.create(includesRoot),
579579
targetConfig.getGenfilesFragment(RepositoryName.MAIN).getRelative(includesRoot));
@@ -596,12 +596,12 @@ public void testDisabledGenfilesDontShowUpInSystemIncludePaths() throws Exceptio
596596
ConfiguredTarget foo = getConfiguredTarget("//bang:bang");
597597
PathFragment genfilesDir =
598598
targetConfig.getGenfilesFragment(RepositoryName.MAIN).getRelative(includesRoot);
599-
assertThat(foo.get(CcInfo.PROVIDER).getCcCompilationContext().getSystemIncludeDirs())
599+
assertThat(foo.get(CcInfo.PROVIDER).getCcCompilationContext().getIncludeDirs())
600600
.contains(genfilesDir);
601601

602602
useConfiguration("--incompatible_merge_genfiles_directory");
603603
foo = getConfiguredTarget("//bang:bang");
604-
assertThat(foo.get(CcInfo.PROVIDER).getCcCompilationContext().getSystemIncludeDirs())
604+
assertThat(foo.get(CcInfo.PROVIDER).getCcCompilationContext().getIncludeDirs())
605605
.doesNotContain(genfilesDir);
606606
}
607607

@@ -626,6 +626,7 @@ public void testUseIsystemForIncludes() throws Exception {
626626
name = "bang",
627627
srcs = ["bang.cc"],
628628
includes = ["bang_includes"],
629+
features = ["system_include_paths"],
629630
)
630631
""");
631632

@@ -644,6 +645,45 @@ public void testUseIsystemForIncludes() throws Exception {
644645
.containsExactlyElementsIn(expected);
645646
}
646647

648+
@Test
649+
public void testUseIForIncludes() throws Exception {
650+
// Tests the effect of --use_isystem_for_includes.
651+
useConfiguration("--incompatible_merge_genfiles_directory=false");
652+
scratch.file(
653+
"no_includes/BUILD",
654+
"""
655+
cc_library(
656+
name = "no_includes",
657+
srcs = ["no_includes.cc"],
658+
)
659+
""");
660+
ConfiguredTarget noIncludes = getConfiguredTarget("//no_includes:no_includes");
661+
662+
scratch.file(
663+
"bang/BUILD",
664+
"""
665+
cc_library(
666+
name = "bang",
667+
srcs = ["bang.cc"],
668+
includes = ["bang_includes"],
669+
)
670+
""");
671+
672+
ConfiguredTarget foo = getConfiguredTarget("//bang:bang");
673+
674+
String includesRoot = "bang/bang_includes";
675+
List<PathFragment> expected =
676+
new ImmutableList.Builder<PathFragment>()
677+
.addAll(
678+
noIncludes.get(CcInfo.PROVIDER).getCcCompilationContext().getIncludeDirs())
679+
.add(PathFragment.create(includesRoot))
680+
.add(targetConfig.getGenfilesFragment(RepositoryName.MAIN).getRelative(includesRoot))
681+
.add(targetConfig.getBinFragment(RepositoryName.MAIN).getRelative(includesRoot))
682+
.build();
683+
assertThat(foo.get(CcInfo.PROVIDER).getCcCompilationContext().getIncludeDirs())
684+
.containsExactlyElementsIn(expected);
685+
}
686+
647687
@Test
648688
public void testCcTestDisallowsAlwaysLink() throws Exception {
649689
scratch.file(

src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java

+12-16
Original file line numberDiff line numberDiff line change
@@ -1196,18 +1196,12 @@ public void testIncludePathOrder() throws Exception {
11961196
assertContainsSublist(
11971197
action.getCompilerOptions(),
11981198
ImmutableList.of(
1199-
"-isystem",
1200-
"foo/foo",
1201-
"-isystem",
1202-
genfilesDir + "/foo/foo",
1203-
"-isystem",
1204-
binDir + "/foo/foo",
1205-
"-isystem",
1206-
"foo/bar",
1207-
"-isystem",
1208-
genfilesDir + "/foo/bar",
1209-
"-isystem",
1210-
binDir + "/foo/bar"));
1199+
"-Ifoo/foo",
1200+
"-I" + genfilesDir + "/foo/foo",
1201+
"-I" + binDir + "/foo/foo",
1202+
"-Ifoo/bar",
1203+
"-I" + genfilesDir + "/foo/bar",
1204+
"-I" + binDir + "/foo/bar"));
12111205
}
12121206

12131207
@Test
@@ -1911,22 +1905,24 @@ public void testImplementationDepsCompilationContextIsNotPropagated() throws Exc
19111905
assertThat(artifactsToStrings(libCompilationContext.getDeclaredIncludeSrcs()))
19121906
.doesNotContain("src foo/implementation_dep.h");
19131907

1914-
assertThat(pathfragmentsToStrings(libCompilationContext.getSystemIncludeDirs()))
1908+
assertThat(pathfragmentsToStrings(libCompilationContext.getIncludeDirs()))
19151909
.contains("foo/public_dep");
1916-
assertThat(pathfragmentsToStrings(libCompilationContext.getSystemIncludeDirs()))
1910+
assertThat(pathfragmentsToStrings(libCompilationContext.getIncludeDirs()))
19171911
.contains("foo/interface_dep");
1912+
assertThat(pathfragmentsToStrings(libCompilationContext.getIncludeDirs()))
1913+
.doesNotContain("foo/implementation_dep");
19181914
assertThat(pathfragmentsToStrings(libCompilationContext.getSystemIncludeDirs()))
19191915
.doesNotContain("foo/implementation_dep");
19201916

19211917
CcCompilationContext publicDepCompilationContext =
19221918
getCppCompileAction("//foo:public_dep").getCcCompilationContext();
19231919
assertThat(artifactsToStrings(publicDepCompilationContext.getDeclaredIncludeSrcs()))
19241920
.contains("src foo/interface_dep.h");
1925-
assertThat(pathfragmentsToStrings(publicDepCompilationContext.getSystemIncludeDirs()))
1921+
assertThat(pathfragmentsToStrings(publicDepCompilationContext.getIncludeDirs()))
19261922
.contains("foo/interface_dep");
19271923
assertThat(artifactsToStrings(publicDepCompilationContext.getDeclaredIncludeSrcs()))
19281924
.contains("src foo/implementation_dep.h");
1929-
assertThat(pathfragmentsToStrings(publicDepCompilationContext.getSystemIncludeDirs()))
1925+
assertThat(pathfragmentsToStrings(publicDepCompilationContext.getIncludeDirs()))
19301926
.contains("foo/implementation_dep");
19311927
}
19321928

src/test/java/com/google/devtools/build/lib/rules/cpp/StarlarkCcCommonTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1398,11 +1398,11 @@ def _impl(ctx):
13981398

13991399
List<String> mergedSystemIncludes =
14001400
((Depset) myInfo.getValue("merged_system_includes")).getSet(String.class).toList();
1401-
assertThat(mergedSystemIncludes).containsAtLeast("foo/bar", "a/dep1/baz", "a/dep2/qux");
1401+
assertThat(mergedSystemIncludes).contains("foo/bar");
14021402

14031403
List<String> mergedIncludes =
14041404
((Depset) myInfo.getValue("merged_includes")).getSet(String.class).toList();
1405-
assertThat(mergedIncludes).contains("baz/qux");
1405+
assertThat(mergedIncludes).containsAtLeast("baz/qux", "a/dep1/baz", "a/dep2/qux");
14061406

14071407
List<String> mergedQuoteIncludes =
14081408
((Depset) myInfo.getValue("merged_quote_includes")).getSet(String.class).toList();

src/test/java/com/google/devtools/build/lib/rules/objc/ObjcLibraryTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ public void testIncludesDirsOfTransitiveCcDepsGetPassedToCompileAction() throws
13971397
Iterables.concat(
13981398
Iterables.transform(
13991399
rootedIncludePaths("package/foo/bar"),
1400-
element -> ImmutableList.of("-isystem", element)))));
1400+
element -> ImmutableList.of("-I" + element)))));
14011401
}
14021402

14031403
@Test

0 commit comments

Comments
 (0)