bazel icon indicating copy to clipboard operation
bazel copied to clipboard

_find_linker_path returns wrong path

Open zakcutner opened this issue 3 years ago • 6 comments

Description of the bug:

The _find_linker_path function determines the linker path by using the output of the following command (e.g. for the lld linker):

$ cc empty.cc -o /dev/null -Wl,--start-lib -Wl,--end-lib -fuse-ld=lld -v

It then looks for a line with lld in (since this is the linker we are looking for). The problem is that on my system, this also matches -debugger-tuning=lldb:

$ cc empty.cc -o /dev/null -Wl,--start-lib -Wl,--end-lib -fuse-ld=lld -v 2>&1 | rg lld
 "/usr/local/Cellar/llvm/14.0.6_1/bin/clang-14" -cc1 -triple x86_64-apple-macosx12.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name empty.cc -mrelocation-model pic -pic-level 2 -mframe-pointer=all -ffp-contract=on -fno-rounding-math -funwind-tables=2 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu penryn -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=lldb -target-linker-version 764 -v -fcoverage-compilation-dir= -resource-dir /usr/local/Cellar/llvm/14.0.6_1/lib/clang/14.0.6 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -stdlib=libc++ -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/usr/include/c++/v1 -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/usr/local/include -internal-isystem /usr/local/Cellar/llvm/14.0.6_1/lib/clang/14.0.6/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/usr/include -fdeprecated-macro -fdebug-compilation-dir= -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/folders/bs/zp6qt5sd2_z9yh6cy2kktbp80000gp/T/foo-9bee4e.o -x c++ empty.cc
 "/usr/local/Cellar/llvm/14.0.6_1/bin/ld64.lld" -demangle -no_deduplicate -dynamic -arch x86_64 -platform_version macos 12.0.0 12.0.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -o /dev/null /var/folders/bs/zp6qt5sd2_z9yh6cy2kktbp80000gp/T/foo-9bee4e.o --start-lib --end-lib -lSystem /usr/local/Cellar/llvm/14.0.6_1/lib/clang/14.0.6/lib/darwin/libclang_rt.osx.a

This in turn causes a -fuse-ld=-debugger-tuning=lldb flag to be set, which results in the following error (since of course -debugger-tuning=lldb is not a valid linker path):

clang-14: error: invalid linker name in argument '-fuse-ld=-debugger-tuning=lldb'

I think the simplest way to fix this would be to check the flag ends with .lld, rather than check if the flag contains lld.

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

No response

Which operating system are you running Bazel on?

Mac

What is the output of bazel info release?

Starting local Bazel server and connecting to it... (15:41:06) WARNING: info command does not support starlark options. Ignoring options: [--@io_bazel_rules_go//go/config:linkmode=pie] release 5.2.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

Have you found anything relevant by searching the web?

No response

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

No response

zakcutner avatar Aug 05 '22 14:08 zakcutner

Hello @zakcutner, Could you please provide complete steps to reproduce the above issue. Thanks!

sgowroji avatar Aug 05 '22 15:08 sgowroji

Hello @sgowroji! This could be reproduced on any Bazel project that builds some C/C++ code, on a system with -debugger-tuning=lldb set (I'm not sure what causes this to be set by default on my system). Let me know if there are any more details that would be helpful.

zakcutner avatar Aug 05 '22 15:08 zakcutner

can you test this branch? https://github.com/bazelbuild/bazel/pull/15006 it should be fixed here

keith avatar Aug 05 '22 15:08 keith

Thanks @keith! I just tested your branch and it works great 😄

zakcutner avatar Aug 05 '22 17:08 zakcutner

great! hopefully it will be reviewed at some point

keith avatar Aug 05 '22 17:08 keith

So it seems I have found a workaround: installing Xcode. On a macOS without Xcode I kept getting this problem (even when compiling the sample C++ hello world bazel example), once I installed Xcode and launched it (this part is important to finish installation) the sample C++ hello world example compiles fine.

I guess bazel completely side-steps this function when Xcode is installed. Though the bug is still in there and should be resolved IMO.

dom96 avatar Sep 20 '22 14:09 dom96

@sgowroji This has been resolved by #15006.

fmeum avatar Oct 07 '22 11:10 fmeum

Thank you @fmeum, we are closing this issue with this reference https://github.com/bazelbuild/bazel/issues/16053#issuecomment-1271450181.

sgowroji avatar Oct 07 '22 11:10 sgowroji