Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use library paths instead of
-Lnative/-lstatic
when linking native …
…dependencies (#841) * update static library naming mixed rust+cc deps Fixes static library naming issues that cause the build of a rust_binary that depends on a cc_library that itself depends on a rust_library to fail. Consider a rust_binary that depends on a cc_library that itself depends on a rust_library: ``` . ├── bin.rs ├── BUILD └── lib.rs ``` ```rust // bin.rs fn main() { println!("hi"); } ``` ```rust // lib.rs: empty ``` ```bazel load("@rules_rust//rust:defs.bzl", "rust_benchmark", "rust_binary", "rust_library") rust_library( name = "rust_lib", srcs = ["lib.rs"], ) cc_library( name = "cc_lib", srcs = [], deps = [":rust_lib"], ) rust_binary( name = "bin", srcs = ["bin.rs"], deps = [":cc_lib"], ) ``` Running `bazel build //project:bin` runs into linker errors like: ``` /usr/bin/ld.gold: error: cannot find -lrust_lib /usr/bin/ld.gold: error: cannot find -lrustc_std_workspace_alloc-1ff59d4f23b10626 ``` This is because the linker expects the static library corresponding to `-lname` to be named `libname.a`. Also Bazel CC link actions get confused by static library names that have multiple dots, e.g., end in "libcrate.rlib.a", so updated the scripts to produce them as "libcrate-rlib.a" instead. * don't pass panic_abort if panic_unwind is present Fixes an issue that causes the build of a rust_binary that depends on a cc_library that depends on a rust_library to fail. Consider a rust_binary that depends on a cc_library that itself depends on a rust_library: ``` . ├── bin.rs ├── BUILD └── lib.rs ``` ```rust // bin.rs fn main() { println!("hi"); } ``` ```rust // lib.rs: empty ``` ```bazel load("@rules_rust//rust:defs.bzl", "rust_benchmark", "rust_binary", "rust_library") rust_library( name = "rust_lib", srcs = ["lib.rs"], ) cc_library( name = "cc_lib", srcs = [], deps = [":rust_lib"], ) rust_binary( name = "bin", srcs = ["bin.rs"], deps = [":cc_lib"], ) ``` Running `bazel build //project:bin` runs into linker errors like: /usr/bin/ld.gold: error: bazel-out/k8-fastbuild/bin/external/rust_linux_x86_64/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-5f5ff14665a8d5c5-rlib.a(panic_unwind-5f5ff14665a8d5c5.panic_unwind.p9ngf95o-cgu.0.rcgu.o): multiple definition of '__rust_panic_cleanup' /usr/bin/ld.gold: bazel-out/k8-fastbuild/bin/external/rust_linux_x86_64/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_abort-c6166278e71d9daf-rlib.a(panic_abort-c6166278e71d9daf.panic_abort.4p8k2zko-cgu.0.rcgu.o): previous definition here That's because we're passing both panic_unwind and panic_abort to the linker. The standard library by default depends on panic_unwind. This updates the rules to prefer panic_unwind. It would be nice to have an option for the user to pick the panic runtime similarly to how it could be done upstream: https://rustc-dev-guide.rust-lang.org/panic-implementation.html#step-2-the-panic-runtime * rename test/unit/stdlib_ordering to /test/unit/stdlib I'll add a additional test case there. * add tests * restrict test assert to only rlibs and libs This should address the Windows buildbot failure. * move panic_{unwind,abort} filtering to _make_libstd_and_allocator_ccinfo * remove `-rlib` postfix from the names of static library symlinks * move _make_dota to utils * remove postfix argument docstring * when linking, emit the path to a native lib instead of -lnativelibname * fix native_deps_test for mac and windows * don't emit `-Lnative=package` for native libraries * Bring back `-Lnative` for dynamic libs They are needed to compile the examples. * also handle dylibs via `-Clink-arg` * Put back -ldylib for dylibs Trying out to build them with -Clink-arg produces test binaries that fail to find the dynamic library at runtime, e.g; https://buildkite.com/bazel/rules-rust-rustlang/builds/3850#d19409bc-a30a-4944-9f81-e5da5e9d06a6 /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/sandbox/linux-sandbox/1458/execroot/rules_rust/bazel-out/k8-fastbuild/bin/external/examples/ffi/rust_calling_c/matrix_dylib_test.launcher.runfiles/rules_rust/external/examples/ffi/rust_calling_c/matrix_dylib_test: error while loading shared libraries: bazel-out/k8-fastbuild/bin/_solib_k8/_U@examples_S_Sffi_Srust_Ucalling_Uc_Sc_Cnative_Umatrix_Uso___Uexternal_Sexamples_Sffi_Srust_Ucalling_Uc_Sc/libnative_matrix_so.so: cannot open shared object file: No such file or directory Added a TODO to investigate. Co-authored-by: UebelAndre <github@uebelandre.com> Co-authored-by: Marcel Hlopko <hlopko@google.com>
- Loading branch information