bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.15k stars 4.05k forks source link

cc_common.get_memory_inefficient_command_line returns invalid linker flags #20849

Open daivinhtran opened 9 months ago

daivinhtran commented 9 months ago

Description of the bug:

cc_common.get_memory_inefficient_command_line returns a set of flags to pass to the cc tool given by cc_common.get_tool_for_action.

I have cc toolchain setup that uses ld.lld as a linker. Here, I have rpaths pointing to a shared library path. As the result, link_args contains "-Xlinker" "-rpath" "-Xlinker" "$ORIGIN/../bazel/toolchains/rust/aarch64-linux-android/rust_toolchain/lib/rustlib/aarch64-linux-android/lib". However, ld.lld (returned from cc_common.get_tool_for_action) does not support the -Xlinker flag.

Ideally, cc_common.get_memory_inefficient_command_line should only provide flags that are valid to the corresponding tool.

Which category does this issue belong to?

C++/Objective-C Rules, Core

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

  1. Checkout https://github.com/bazelbuild/rules_rust/pull/2414/commits/3cfbb9a580668ec6b4e34d7495f45e93f56a5614
  2. Remove the removals of -Xlinker flag here
  3. cd examples/nix_cross_compiling
  4. bazel build //rust_binary:rust_binary_aarch64-linux-android --@rules_rust//rust/settings:std_dylib=True

The error is

  = note: ld.lld: error: unknown argument '-Xlinker'
          ld.lld: error: cannot open $ORIGIN/../bazel/toolchains/rust/aarch64-linux-android/rust_toolchain/lib/rustlib/aarch64-linux-android/lib: No such file or directory

Which operating system are you running Bazel on?

Linux

What is the output of bazel info release?

release 7.0.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

fmeum commented 9 months ago

Which version of lld is this?

The correct place for this would be in the toolchain. We could add feature detection to choose between -Wl and -Xlinker in unix_cc_toolchain_configure.bzl.

daivinhtran commented 9 months ago

@fmeum

$ /nix/store/8wdy19pya1n7wrn2664d2819m3pyamg4-llvm-binutils-16.0.6/bin/ld.lld --version
LLD 16.0.6 (compatible with GNU linkers)

I ended up fixing our own toolchains which, I think, used https://github.com/bazelbuild/bazel/blob/master/tools/cpp/unix_cc_toolchain_config.bzl as a guidance .

I think it's worth fixing https://github.com/bazelbuild/bazel/blob/master/tools/cpp/unix_cc_toolchain_config.bzl to not use -Xlinker when lld is the linker.

fmeum commented 9 months ago

Okay, this is actually working as intended (although confusingly): The C++ toolchain configures linker flags for the link action, which still uses the compiler to interface with the linker. No linker will support the -Xlinker flag as this is a compiler flag used to pass linker flags to the linker.

Can you use the C/C++ compiler as the linker in your toolchain? I think that ld.lld just shouldn't be returned from get_tool_for_action.