Skip to content

Commit b8ad10b

Browse files
committed
Introduce shorten_virtual_includes cc toolchain feature
Fixes bazelbuild#18683 Related: protocolbuffers/protobuf#20085 This change is a rework of bazelbuild#26005 to enable short virtual includes based on a cc feature, therefore we can limit the change to only MSVC compiler on Windows. RELNOTES: If a cc toolchain feature named `shorten_virtual_includes` is enabled, virtual include header files are linked under `bin/_virtual_includes/<hash of target path>` instead of `bin/<target package path>/_virtual_includes/<target name>`. This shortens the virtual include paths which is critical for mitigating long path issue with MSVC on Windows. Closes bazelbuild#26528. PiperOrigin-RevId: 781975309 Change-Id: Ia573a5f25707ad2462aa3a4459fc66db1779df36
1 parent ebc7f9f commit b8ad10b

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@ public static ToolchainTypeRequirement ccToolchainTypeRequirement(RuleDefinition
387387
/** A string constant for a feature that, if enabled, disables .d file handling. */
388388
public static final String NO_DOTD_FILE = "no_dotd_file";
389389

390+
/**
391+
* A string constant for a feature that, if enabled, shortens the virtual include paths via
392+
* hashing.
393+
*/
394+
public static final String SHORTEN_VIRTUAL_INCLUDES = "shorten_virtual_includes";
395+
390396
/*
391397
* A string constant for the fdo_instrument feature.
392398
*/

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ def _compute_public_headers(
5959
label,
6060
binfiles_dir,
6161
non_module_map_headers,
62-
is_sibling_repository_layout):
62+
is_sibling_repository_layout,
63+
shorten_virtual_includes):
6364
if include_prefix:
6465
if not paths.is_normalized(include_prefix, False):
6566
fail("include prefix should not contain uplevel references: " + include_prefix)
@@ -111,7 +112,11 @@ def _compute_public_headers(
111112

112113
module_map_headers = []
113114
virtual_to_original_headers_list = []
114-
virtual_include_dir = paths.join(paths.join(package_source_root(label.workspace_name, label.package, is_sibling_repository_layout), _VIRTUAL_INCLUDES_DIR), label.name)
115+
source_package_path = package_source_root(label.workspace_name, label.package, is_sibling_repository_layout)
116+
if shorten_virtual_includes:
117+
virtual_include_dir = paths.join(_VIRTUAL_INCLUDES_DIR, "%x" % hash(paths.join(source_package_path, label.name)))
118+
else:
119+
virtual_include_dir = paths.join(source_package_path, _VIRTUAL_INCLUDES_DIR, label.name)
115120
for original_header in public_headers_artifacts:
116121
repo_relative_path = _repo_relative_path(original_header)
117122
if not repo_relative_path.startswith(strip_prefix):
@@ -228,6 +233,7 @@ def _init_cc_compilation_context(
228233
bin_include_dir = _include_dir(binfiles_dir, repo_path, sibling_repo_layout)
229234
quote_include_dirs_for_context = [repo_path, gen_include_dir, bin_include_dir] + quote_include_dirs
230235
external = repo_name != "" and _enabled(feature_configuration, "external_include_paths")
236+
shorten_virtual_includes = _enabled(feature_configuration, "shorten_virtual_includes")
231237
external_include_dirs = []
232238
declared_include_srcs = []
233239

@@ -256,6 +262,7 @@ def _init_cc_compilation_context(
256262
binfiles_dir,
257263
non_module_map_headers,
258264
sibling_repo_layout,
265+
shorten_virtual_includes,
259266
)
260267
if public_headers.virtual_include_path:
261268
if external:
@@ -293,6 +300,7 @@ def _init_cc_compilation_context(
293300
binfiles_dir,
294301
non_module_map_headers,
295302
sibling_repo_layout,
303+
shorten_virtual_includes,
296304
)
297305

298306
separate_module = None

src/test/java/com/google/devtools/build/lib/analysis/mock/cc_toolchain_config.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ _FEATURE_NAMES = struct(
122122
cpp_compile_with_requirements = "cpp_compile_with_requirements",
123123
no_copts_tokenization = "no_copts_tokenization",
124124
generate_linkmap = "generate_linkmap",
125+
shorten_virtual_includes = "shorten_virtual_includes",
125126
)
126127

127128
_cpp_modules_feature = feature(
@@ -142,6 +143,11 @@ _supports_dynamic_linker_feature = feature(
142143
enabled = True,
143144
)
144145

146+
_shorten_virtual_includes_feature = feature(
147+
name = _FEATURE_NAMES.shorten_virtual_includes,
148+
enabled = True,
149+
)
150+
145151
_supports_interface_shared_libraries_feature = feature(
146152
name = _FEATURE_NAMES.supports_interface_shared_libraries,
147153
enabled = True,
@@ -1451,6 +1457,7 @@ _feature_name_to_feature = {
14511457
_FEATURE_NAMES.cpp_compile_with_requirements: _cpp_compile_with_requirements,
14521458
_FEATURE_NAMES.generate_pdb_file: _generate_pdb_file_feature,
14531459
_FEATURE_NAMES.generate_linkmap: _generate_linkmap_feature,
1460+
_FEATURE_NAMES.shorten_virtual_includes: _shorten_virtual_includes_feature,
14541461
"header_modules_feature_configuration": _header_modules_feature_configuration,
14551462
"env_var_feature_configuration": _env_var_feature_configuration,
14561463
"host_and_nonhost_configuration": _host_and_nonhost_configuration,

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,37 @@ public void testIncludeManglingSmoke() throws Exception {
10791079
.getRelative("third_party/a/_virtual_includes/a"));
10801080
}
10811081

1082+
@Test
1083+
public void testIncludeManglingSmokeWithShortendVirtualIncludes() throws Exception {
1084+
getAnalysisMock()
1085+
.ccSupport()
1086+
.setupCcToolchainConfig(
1087+
mockToolsConfig,
1088+
CcToolchainConfig.builder().withFeatures(CppRuleClasses.SHORTEN_VIRTUAL_INCLUDES));
1089+
scratch.file(
1090+
"third_party/a/BUILD",
1091+
"""
1092+
licenses(["notice"])
1093+
1094+
cc_library(
1095+
name = "a",
1096+
hdrs = ["v1/b/c.h"],
1097+
include_prefix = "lib",
1098+
strip_include_prefix = "v1",
1099+
)
1100+
""");
1101+
1102+
ConfiguredTarget lib = getConfiguredTarget("//third_party/a");
1103+
CcCompilationContext ccCompilationContext = lib.get(CcInfo.PROVIDER).getCcCompilationContext();
1104+
assertThat(ActionsTestUtil.prettyArtifactNames(ccCompilationContext.getDeclaredIncludeSrcs()))
1105+
.containsExactly("_virtual_includes/207132b2/lib/b/c.h", "third_party/a/v1/b/c.h");
1106+
assertThat(ccCompilationContext.getIncludeDirs())
1107+
.containsExactly(
1108+
getTargetConfiguration()
1109+
.getBinFragment(RepositoryName.MAIN)
1110+
.getRelative("_virtual_includes/207132b2"));
1111+
}
1112+
10821113
@Test
10831114
public void testUpLevelReferencesInIncludeMangling() throws Exception {
10841115
scratch.file(

0 commit comments

Comments
 (0)