Skip to content

Commit b0511ae

Browse files
committed
Use ${pwd} prefix for INCLUDE path values
1 parent a1243a3 commit b0511ae

File tree

3 files changed

+114
-2
lines changed

3 files changed

+114
-2
lines changed

cargo/private/cargo_build_script.bzl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,25 @@ def _prefix_pwd_to_flag(args, flag_variations):
212212

213213
return res
214214

215+
def _prefix_pwd_to_paths(args):
216+
"""Prefix execroot-relative paths with ${pwd}.
217+
218+
Handles plain path arguments without any associated flags.
219+
220+
Args:
221+
args (list): List of path arguments.
222+
223+
Returns:
224+
list: The modified argument list with relative paths prefixed with ${pwd}.
225+
"""
226+
res = []
227+
for path in args:
228+
if not paths.is_absolute(path):
229+
res.append("${{pwd}}/{}".format(path))
230+
else:
231+
res.append(path)
232+
return res
233+
215234
def _pwd_flags_sysroot(args):
216235
"""Prefix execroot-relative paths in --sysroot= arguments with ${pwd}."""
217236
return _prefix_pwd_to_flag(args, ["--sysroot="])
@@ -236,6 +255,10 @@ def _pwd_flags_resource_dir(args):
236255
"""Prefix execroot-relative paths in -resource-dir arguments with ${pwd}."""
237256
return _prefix_pwd_to_flag(args, ["-resource-dir=", "-resource-dir"])
238257

258+
def _pwd_paths(args):
259+
"""Prefix execroot-relative paths with ${pwd}."""
260+
return _prefix_pwd_to_paths(args)
261+
239262
def _pwd_flags(args):
240263
return _pwd_flags_fsanitize_ignorelist(_pwd_flags_isystem(_pwd_flags_L(_pwd_flags_B(_pwd_flags_resource_dir(_pwd_flags_sysroot(args))))))
241264

@@ -446,7 +469,11 @@ def _cargo_build_script_impl(ctx):
446469
cc_c_args, cc_cxx_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
447470
include = cc_env.get("INCLUDE")
448471
if include:
449-
env["INCLUDE"] = include
472+
if toolchain.exec_triple.str.find("windows") > 0:
473+
path_separator = ";"
474+
else:
475+
path_separator = ":"
476+
env["INCLUDE"] = path_separator.join(_pwd_paths(include.split(path_separator)))
450477

451478
toolchain_tools.append(cc_toolchain.all_files)
452479

test/cargo_build_script/cc_args_and_env/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ load(
44
"bindir_relative_test",
55
"fsanitize_ignorelist_absolute_test",
66
"fsanitize_ignorelist_relative_test",
7+
"include_absolute_test",
8+
"include_mixed_test",
9+
"include_relative_test",
710
"isystem_absolute_test",
811
"isystem_relative_test",
912
"legacy_cc_toolchain_test",
@@ -43,3 +46,9 @@ libpath_relative_test(name = "libpath_relative_test")
4346
resource_dir_absolute_test(name = "resource_dir_absolute_test")
4447

4548
resource_dir_relative_test(name = "resource_dir_relative_test")
49+
50+
include_relative_test(name = "include_relative_test")
51+
52+
include_absolute_test(name = "include_absolute_test")
53+
54+
include_mixed_test(name = "include_mixed_test")

test/cargo_build_script/cc_args_and_env/cc_args_and_env_test.bzl

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ To verify the processed cargo cc_args, we use cc_args_and_env_analysis_test().
99

1010
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
1111
load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES", "ACTION_NAME_GROUPS")
12-
load("@rules_cc//cc:cc_toolchain_config_lib.bzl", "action_config", "feature", "flag_group", "flag_set", "tool", "tool_path")
12+
load("@rules_cc//cc:cc_toolchain_config_lib.bzl", "action_config", "env_entry", "env_set", "feature", "flag_group", "flag_set", "tool", "tool_path")
1313
load("@rules_cc//cc:defs.bzl", "cc_toolchain")
1414
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
1515
load("//cargo:defs.bzl", "cargo_build_script")
@@ -35,6 +35,17 @@ def _test_cc_config_impl(ctx):
3535
]),
3636
),
3737
],
38+
env_sets = [
39+
env_set(
40+
actions = ACTION_NAME_GROUPS.all_cc_compile_actions,
41+
env_entries = [
42+
env_entry(
43+
key = "INCLUDE",
44+
value = ctx.attr.extra_include_paths,
45+
),
46+
],
47+
),
48+
] if ctx.attr.extra_include_paths else [],
3849
),
3950
feature(
4051
name = "default_cpp_flags",
@@ -155,6 +166,7 @@ test_cc_config = rule(
155166
"extra_ar_flags": attr.string_list(),
156167
"extra_cc_compile_flags": attr.string_list(),
157168
"extra_cxx_compile_flags": attr.string_list(),
169+
"extra_include_paths": attr.string(default = ""),
158170
"legacy_cc_toolchain": attr.bool(default = False),
159171
},
160172
provides = [CcToolchainConfigInfo],
@@ -240,6 +252,15 @@ def _cc_args_and_env_analysis_test_impl(ctx):
240252
"set up by most Bazel C/C++ toolchains is extremely error-prone.",
241253
)
242254

255+
if ctx.attr.expected_include:
256+
actual_include = cargo_action.env.get("INCLUDE")
257+
asserts.equals(
258+
env,
259+
ctx.attr.expected_include,
260+
actual_include,
261+
"error: expected INCLUDE '{}' but got '{}'".format(ctx.attr.expected_include, actual_include),
262+
)
263+
243264
return analysistest.end(env)
244265

245266
cc_args_and_env_analysis_test = analysistest.make(
@@ -248,6 +269,7 @@ cc_args_and_env_analysis_test = analysistest.make(
248269
attrs = {
249270
"expected_cflags": attr.string_list(default = ["-Wall"]),
250271
"expected_cxxflags": attr.string_list(default = ["-fno-rtti"]),
272+
"expected_include": attr.string(default = ""),
251273
"legacy_cc_toolchain": attr.bool(default = False),
252274
},
253275
)
@@ -258,6 +280,7 @@ def cargo_build_script_with_extra_cc_compile_flags(
258280
extra_cc_compile_flags = ["-Wall"],
259281
extra_cxx_compile_flags = ["-fno-rtti"],
260282
extra_ar_flags = ["-x"],
283+
extra_include_paths = "",
261284
legacy_cc_toolchain = False):
262285
"""Produces a test cargo_build_script target that's set up to use a custom cc_toolchain with the extra_cc_compile_flags.
263286
@@ -272,6 +295,7 @@ def cargo_build_script_with_extra_cc_compile_flags(
272295
extra_cc_compile_flags: Extra C/C++ args for the cc_toolchain.
273296
extra_cxx_compile_flags: Extra C++-specific args for the cc_toolchain.
274297
extra_ar_flags: Extra archiver args for the cc_toolchain.
298+
extra_include_paths: INCLUDE environment variable value for the cc_toolchain.
275299
legacy_cc_toolchain: Enables legacy tool_path configuration of the cc
276300
cc toolchain.
277301
"""
@@ -281,6 +305,7 @@ def cargo_build_script_with_extra_cc_compile_flags(
281305
extra_cc_compile_flags = extra_cc_compile_flags,
282306
extra_cxx_compile_flags = extra_cxx_compile_flags,
283307
extra_ar_flags = extra_ar_flags,
308+
extra_include_paths = extra_include_paths,
284309
legacy_cc_toolchain = legacy_cc_toolchain,
285310
)
286311
cc_toolchain(
@@ -466,3 +491,54 @@ def resource_dir_absolute_test(name):
466491
target_under_test = "%s/cargo_build_script" % name,
467492
expected_cflags = ["-resource-dir", "/test/absolute/resources", "-resource-dir=/test/absolute/resources2", "-resource-dir=", "some_unrelated_arg"],
468493
)
494+
495+
def include_relative_test(name):
496+
cargo_build_script_with_extra_cc_compile_flags(
497+
name = "%s/cargo_build_script" % name,
498+
extra_include_paths = select({
499+
"@platforms//os:windows": "test/relative/include;another/relative/path",
500+
"//conditions:default": "test/relative/include:another/relative/path",
501+
}),
502+
)
503+
cc_args_and_env_analysis_test(
504+
name = name,
505+
target_under_test = "%s/cargo_build_script" % name,
506+
expected_include = select({
507+
"@platforms//os:windows": "${pwd}/test/relative/include;${pwd}/another/relative/path",
508+
"//conditions:default": "${pwd}/test/relative/include:${pwd}/another/relative/path",
509+
}),
510+
)
511+
512+
def include_absolute_test(name):
513+
cargo_build_script_with_extra_cc_compile_flags(
514+
name = "%s/cargo_build_script" % name,
515+
extra_include_paths = select({
516+
"@platforms//os:windows": "/test/absolute/include;/another/absolute/path",
517+
"//conditions:default": "/test/absolute/include:/another/absolute/path",
518+
}),
519+
)
520+
cc_args_and_env_analysis_test(
521+
name = name,
522+
target_under_test = "%s/cargo_build_script" % name,
523+
expected_include = select({
524+
"@platforms//os:windows": "/test/absolute/include;/another/absolute/path",
525+
"//conditions:default": "/test/absolute/include:/another/absolute/path",
526+
}),
527+
)
528+
529+
def include_mixed_test(name):
530+
cargo_build_script_with_extra_cc_compile_flags(
531+
name = "%s/cargo_build_script" % name,
532+
extra_include_paths = select({
533+
"@platforms//os:windows": "/test/absolute/include;test/relative/path",
534+
"//conditions:default": "/test/absolute/include:test/relative/path",
535+
}),
536+
)
537+
cc_args_and_env_analysis_test(
538+
name = name,
539+
target_under_test = "%s/cargo_build_script" % name,
540+
expected_include = select({
541+
"@platforms//os:windows": "/test/absolute/include;${pwd}/test/relative/path",
542+
"//conditions:default": "/test/absolute/include:${pwd}/test/relative/path",
543+
}),
544+
)

0 commit comments

Comments
 (0)