Skip to content

Commit d5153fb

Browse files
authored
Rewrite relative paths for bin dirs (-B) in build scripts (#3446)
With a hermetic gcc toolchain binary dirs (`-B`) may be necessary to invoke the C pre-processor. I ran into this issue when using the [mongo gcc toolchain](https://github.com/mongodb/mongo/blob/master/bazel/toolchains/cc/mongo_linux/mongo_toolchain.BUILD.tmpl) where the packaging of the toolchain and relative bindir paths cause failures when compiling crates that use the cc toolchain. In these cases compilation with cc-rs would fail with an error `cannot execute 'cc1': posix_spawnp: No such file or directory`
1 parent 7e9252f commit d5153fb

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

cargo/private/cargo_build_script.bzl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,27 @@ def _pwd_flags_isystem(args):
193193

194194
return res
195195

196+
def _pwd_flags_bindir(args):
197+
"""Prefix execroot-relative paths of known arguments with ${pwd}.
198+
199+
Args:
200+
args (list): List of tool arguments.
201+
202+
Returns:
203+
list: The modified argument list.
204+
"""
205+
res = []
206+
fix_next_arg = False
207+
for arg in args:
208+
if fix_next_arg and not paths.is_absolute(arg):
209+
res.append("${{pwd}}/{}".format(arg))
210+
else:
211+
res.append(arg)
212+
213+
fix_next_arg = arg == "-B"
214+
215+
return res
216+
196217
def _pwd_flags_fsanitize_ignorelist(args):
197218
"""Prefix execroot-relative paths of known arguments with ${pwd}.
198219
@@ -212,7 +233,7 @@ def _pwd_flags_fsanitize_ignorelist(args):
212233
return res
213234

214235
def _pwd_flags(args):
215-
return _pwd_flags_fsanitize_ignorelist(_pwd_flags_isystem(_pwd_flags_sysroot(args)))
236+
return _pwd_flags_fsanitize_ignorelist(_pwd_flags_isystem(_pwd_flags_bindir(_pwd_flags_sysroot(args))))
216237

217238
def _feature_enabled(ctx, feature_name, default = False):
218239
"""Check if a feature is enabled.

test/cargo_build_script/cc_args_and_env/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
load(
22
"cc_args_and_env_test.bzl",
3+
"bindir_absolute_test",
4+
"bindir_relative_test",
35
"fsanitize_ignorelist_absolute_test",
46
"fsanitize_ignorelist_relative_test",
57
"isystem_absolute_test",
@@ -19,6 +21,10 @@ isystem_relative_test(name = "isystem_relative_test")
1921

2022
isystem_absolute_test(name = "isystem_absolute_test")
2123

24+
bindir_relative_test(name = "bindir_relative_test")
25+
26+
bindir_absolute_test(name = "bindir_absolute_test")
27+
2228
fsanitize_ignorelist_absolute_test(name = "fsanitize_ignorelist_absolute_test")
2329

2430
fsanitize_ignorelist_relative_test(name = "fsanitize_ignorelist_relative_test")

test/cargo_build_script/cc_args_and_env/cc_args_and_env_test.bzl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,28 @@ def isystem_absolute_test(name):
207207
expected_cflags = ["-isystem", "/test/absolute/path"],
208208
)
209209

210+
def bindir_relative_test(name):
211+
cargo_build_script_with_extra_cc_compile_flags(
212+
name = "%s/cargo_build_script" % name,
213+
extra_cc_compile_flags = ["-B", "test/relative/path"],
214+
)
215+
cc_args_and_env_analysis_test(
216+
name = name,
217+
target_under_test = "%s/cargo_build_script" % name,
218+
expected_cflags = ["-B", "${pwd}/test/relative/path"],
219+
)
220+
221+
def bindir_absolute_test(name):
222+
cargo_build_script_with_extra_cc_compile_flags(
223+
name = "%s/cargo_build_script" % name,
224+
extra_cc_compile_flags = ["-B", "/test/absolute/path"],
225+
)
226+
cc_args_and_env_analysis_test(
227+
name = name,
228+
target_under_test = "%s/cargo_build_script" % name,
229+
expected_cflags = ["-B", "/test/absolute/path"],
230+
)
231+
210232
def fsanitize_ignorelist_relative_test(name):
211233
cargo_build_script_with_extra_cc_compile_flags(
212234
name = "%s/cargo_build_script" % name,

0 commit comments

Comments
 (0)