bazelbuild / bazel

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

unknown option: --start-lib while building protoc clang osx bazel 0.4.4 #2502

Closed pcj closed 7 years ago

pcj commented 7 years ago

Under bazel 0.4.4, I get the following error on osx while trying to build/link google/protobuf:protoc

ERROR: /private/var/tmp/_bazel_pcj/63330772b4917b139280caef8bb81867/external/com_github_google_protobuf/BUILD:334:1: Linking of rule '@com_github_google_protobuf//:protoc' failed: cc_wrapper.sh failed: error executing command external/local_config_cc/cc_wrapper.sh -o bazel-out/host/bin/external/com_github_google_protobuf/protoc ... (remaining 183 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
ld: unknown option: --start-lib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
$ clang version
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
kchodorow commented 7 years ago

This looks like a bug (we should detect if the toolchain supports --start-lib and --end-lib) but in the meantime, you should be able to workaround it by specifying --nostart_end_lib.

lberki commented 7 years ago

Assigning to Damien since he's the one most familiar with cc_configure.bzl.

damienmg commented 7 years ago

Tentatively fixed by mid year, no time for it currently, feel free to add this check to cc_configure (contribution welcome) :)

pcj commented 7 years ago

So this would be additional logic here to establish a separate boolean value than supports_gold_linker = _is_gold_supported(repository_ctx, cc)?

damienmg commented 7 years ago

I am at my limit to understand what is going on there.

I would suggest adding a test to see if the linker support -start-lib and put it where you suggested. Send the change to @mhlopko (who should be back next week).

hlopko commented 7 years ago

Hi @pcj, this is interesting. As gold is elf-only linker it shouldn't be used by your osx clang (unless you're cross compiling, please let me know if you do), and the autodetection in cc_configure.bzl should fail. Gold supports --start-lib --end-lib since 2010, so I hope we can assume that when gold is available, --start-lib --end-lib is available too. Of course if this assumption is wrong and you hitting this case, we can fix it together. Let me know.

Would you mind investigating why cc_configure.bzl thinks gold is available? Maybe the autodetection is wrong for older clang releases.

Anyway, thank you Paul for reporting and spending time on this!

hlopko commented 7 years ago

To repeat what I wrote on the dup. issue:

Can you investigate why cc_configure.bzl#_is_gold_supported returns true when it actually should return false. The logic is quite simple, it runs clang -fuse-ld=gold -o /dev/null empty.cc (empty.cc contains just int main() {}), and returns true when exit code is 0. Testing locally it returns 1 for me. Can you try to run the command and see what happens?

pcj commented 7 years ago

Sorry I haven't had time to address your question @mhlopko. I'll take a look at this again this weekend.

pcj commented 7 years ago

Yes, getting zero exit status:

$ clang -fuse-ld=gold -o /dev/null empty.cc
$ echo $?
0

Reading http://llvm.org/docs/GoldPlugin.html...

$ which ld
/usr/bin/ld
$ /usr/bin/ld -v
@(#)PROGRAM:ld  PROJECT:ld64-241.9
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7m armv7em
LTO support using: LLVM version 3.5svn
$ /usr/bin/ld -plugin
ld: unknown option: -plugin
$ echo $?
1

So this ld does not have plugin support. Trying to wrap my head around by use-ld=gold would not complain about that...

Workaround for cc_configure.bzl might be to drop gold support for darwin, or test ld directly, but I'm not sure how brittle that would be.

Workaround for me is to use --nostart_end_lib as @kchodorow suggests or get an ld that supports gold.

Will edit comment as I get more info...

hlopko commented 7 years ago

I assume your ld is not gold. -fuse-ld=gold finds gold using magic :) I'd take a look at ld.gold or gold.

You can have gold installed, but clang might not use it. That's why we detect gold through clang. That is also what bazel does, it invokes clang and lets clang delegate to the linker.

I'd like to keep gold enabled for macs, mainly so we can use --start/--end-lib :)

How about we made the command line something funnier, like:

clang -fuse-ld=gold -o /dev/null -Wl,-start-lib -Wl,-end-lib empty.cc

Could you test that for me?

pcj commented 7 years ago

Sure:

$ clang -fuse-ld=gold -o /dev/null -Wl,-start-lib -Wl,-end-lib empty.cc
warning: unknown warning option '-Wl-end-lib'; did you mean '-Wlong-long'? [-Wunknown-warning-option]
1 warning generated.
ld: unknown option: -start-lib
clang: error: linker command failed with exit code 1 (use -v to see invocation)

ld.gold does not exist on my path.

Does it help?

hlopko commented 7 years ago

Interesting, so I assume old macos clang silently ignores -fuse-ld=gold. Thanks Paul! I'm gonna upload the fix.

pcj commented 7 years ago

Thanks for working on this @mhlopko

hlopko commented 7 years ago

Thank you for your help @pcj! :)