Skip to content

Commit 34ea934

Browse files
authored
Prefix execroot-relative file arguments with pwd (#4067)
Prefix execroot-relative bare positional file args with pwd in cargo build scripts. This is already done for `-L`, `-isystem`, `--sysroot`, etc but not for bare positional arguments like `bazel-out/bin/compiler-rt/libclang_rt.builtins.static.a`. This problem arised when compiling `aws-lc-sys` with [hermetic-llvm](https://github.com/hermeticbuild/hermetic-llvm), see hermeticbuild/hermetic-llvm#405 A minimal working example to reproduce is created in https://github.com/martin4861/mwe-bootstrapped-rust-clang_rt.builtins.static: ``` clang: error: no such file or directory: 'bazel-out/darwin_arm64-fastbuild-ST-bdec89fd5d65/bin/external/llvm++llvm_source+compiler-rt/clang_rt.builtins.static_/libclang_rt.builtins.static.a' ```
1 parent 301d425 commit 34ea934

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

cargo/private/cargo_build_script.bzl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,39 @@ def _pwd_flags_resource_dir(args):
207207
"""Prefix execroot-relative paths in -resource-dir arguments with ${pwd}."""
208208
return _prefix_pwd_to_flag(args, ["-resource-dir=", "-resource-dir"])
209209

210+
_DIRECT_LIB_EXTENSIONS = (".a", ".o", ".so", ".dylib")
211+
212+
def _pwd_flags_direct_libs(args):
213+
"""Prefix execroot-relative object/library file arguments with ${pwd}.
214+
215+
Handles bare object and library file paths passed directly to the linker
216+
without any associated flag (e.g. a positional path to
217+
libclang_rt.builtins.a, or a positional .o/.so/.dylib). These are emitted
218+
by some cc toolchains (e.g. hermetic LLVM passing the compiler-rt builtins
219+
archive as a positional input) and would otherwise stay execroot-relative
220+
and fail to resolve from the build script's working directory.
221+
222+
Args:
223+
args (list): List of tool arguments.
224+
225+
Returns:
226+
list: The modified argument list with relative object/library file
227+
paths prefixed with ${pwd}.
228+
"""
229+
res = []
230+
for arg in args:
231+
if not arg.startswith("-") and not paths.is_absolute(arg) and arg.endswith(_DIRECT_LIB_EXTENSIONS):
232+
res.append("${{pwd}}/{}".format(arg))
233+
else:
234+
res.append(arg)
235+
return res
236+
210237
def _pwd_paths(args):
211238
"""Prefix execroot-relative paths with ${pwd}."""
212239
return _prefix_pwd_to_paths(args)
213240

214241
def _pwd_flags(args):
215-
return _pwd_flags_fsanitize_ignorelist(_pwd_flags_isystem(_pwd_flags_L(_pwd_flags_B(_pwd_flags_resource_dir(_pwd_flags_sysroot(args))))))
242+
return _pwd_flags_direct_libs(_pwd_flags_fsanitize_ignorelist(_pwd_flags_isystem(_pwd_flags_L(_pwd_flags_B(_pwd_flags_resource_dir(_pwd_flags_sysroot(args)))))))
216243

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

cargo/tests/cargo_build_script/cc_args_and_env/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ load(
22
"cc_args_and_env_test.bzl",
33
"bindir_absolute_test",
44
"bindir_relative_test",
5+
"direct_libs_absolute_test",
6+
"direct_libs_relative_test",
57
"fsanitize_ignorelist_absolute_test",
68
"fsanitize_ignorelist_relative_test",
79
"include_absolute_test",
@@ -58,3 +60,7 @@ include_relative_test(name = "include_relative_test")
5860
include_absolute_test(name = "include_absolute_test")
5961

6062
include_mixed_test(name = "include_mixed_test")
63+
64+
direct_libs_relative_test(name = "direct_libs_relative_test")
65+
66+
direct_libs_absolute_test(name = "direct_libs_absolute_test")

cargo/tests/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
@@ -565,3 +565,25 @@ def include_mixed_test(name):
565565
"//conditions:default": "/test/absolute/include:${pwd}/test/relative/path",
566566
}),
567567
)
568+
569+
def direct_libs_relative_test(name):
570+
cargo_build_script_with_extra_cc_compile_flags(
571+
name = "%s/cargo_build_script" % name,
572+
extra_cc_compile_flags = ["bazel-out/bin/compiler-rt/libclang_rt.builtins.static.a", "test/relative/obj.o", "test/relative/libfoo.so", "test/relative/libbar.dylib", "some_unrelated_arg"],
573+
)
574+
cc_args_and_env_analysis_test(
575+
name = name,
576+
target_under_test = "%s/cargo_build_script" % name,
577+
expected_cflags = ["${pwd}/bazel-out/bin/compiler-rt/libclang_rt.builtins.static.a", "${pwd}/test/relative/obj.o", "${pwd}/test/relative/libfoo.so", "${pwd}/test/relative/libbar.dylib", "some_unrelated_arg"],
578+
)
579+
580+
def direct_libs_absolute_test(name):
581+
cargo_build_script_with_extra_cc_compile_flags(
582+
name = "%s/cargo_build_script" % name,
583+
extra_cc_compile_flags = ["/test/absolute/libclang_rt.builtins.static.a", "/test/absolute/obj.o", "/test/absolute/libfoo.so", "/test/absolute/libbar.dylib", "some_unrelated_arg"],
584+
)
585+
cc_args_and_env_analysis_test(
586+
name = name,
587+
target_under_test = "%s/cargo_build_script" % name,
588+
expected_cflags = ["/test/absolute/libclang_rt.builtins.static.a", "/test/absolute/obj.o", "/test/absolute/libfoo.so", "/test/absolute/libbar.dylib", "some_unrelated_arg"],
589+
)

0 commit comments

Comments
 (0)