Building Swift failing because of invalid linker option on Ubuntu 20.04

My usual build invocation suddenly started recently failing because it's selecting gold as the linker and passing it an invalid option --color-diagnostics.

Here is the error:

FAILED: lib/swift/linux/x86_64/libswiftRemoteMirror.so 
: && /home/augusto/developer/swift-project-2/build/Ninja-ReleaseAssert/llvm-linux-x86_64/./bin/clang++ -fPIC -Wno-unknown-warning-option -Werror=unguarded-availability-new -fno-stack-protector -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-class-memaccess -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wstring-conversion -fdiagnostics-color -ffunction-sections -fdata-sections -Werror=switch -Wdocumentation -Wimplicit-fallthrough -Wunreachable-code -Woverloaded-virtual -DOBJC_OLD_DISPATCH_PROTOTYPES=0 -O3  -Wl,--color-diagnostics   -target x86_64-unknown-linux-gnu -fuse-ld=gold -shared -Wl,-soname,libswiftRemoteMirror.so -o lib/swift/linux/x86_64/libswiftRemoteMirror.so stdlib/public/LLVMSupport/CMakeFiles/swiftLLVMSupport-linux-x86_64.dir/ErrorHandling.cpp.o stdlib/public/LLVMSupport/CMakeFiles/swiftLLVMSupport-linux-x86_64.dir/Hashing.cpp.o stdlib/public/LLVMSupport/CMakeFiles/swiftLLVMSupport-linux-x86_64.dir/MemAlloc.cpp.o stdlib/public/LLVMSupport/CMakeFiles/swiftLLVMSupport-linux-x86_64.dir/SmallPtrSet.cpp.o stdlib/public/LLVMSupport/CMakeFiles/swiftLLVMSupport-linux-x86_64.dir/SmallVector.cpp.o stdlib/public/LLVMSupport/CMakeFiles/swiftLLVMSupport-linux-x86_64.dir/StringRef.cpp.o stdlib/public/SwiftRemoteMirror/CMakeFiles/swiftRemoteMirror-linux-x86_64.dir/SwiftRemoteMirror.cpp.o -L/home/augusto/developer/swift-project-2/build/Ninja-ReleaseAssert/llvm-linux-x86_64/./lib   -L/home/augusto/developer/swift-project-2/build/Ninja-ReleaseAssert/swift-linux-x86_64/./lib/swift/linux/x86_64   -L/home/augusto/developer/swift-project-2/build/Ninja-ReleaseAssert/swift-linux-x86_64/./bin/../lib/swift/linux/x86_64   -L/home/augusto/developer/swift-project-2/build/Ninja-ReleaseAssert/swift-linux-x86_64/./bin/../lib/swift/linux   -L//usr/lib/swift -Wl,-rpath,"\$ORIGIN"  lib/swift/linux/x86_64/libswiftReflection.a  -lpthread  -ldl && :
/usr/bin/ld.gold: --color-diagnostics: unknown option
/usr/bin/ld.gold: use the --help option for usage information
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)

Notice that we're selecting gold as the linker: -fuse-ld=gold.

I'm unsure if the problem is that we're selecting gold as the linker, or if that's correct and we shouldn't be passing --color-diagnostics.

Could this be a bug on the make file that applies only to Ubuntu 20.04?

I just built that library on Ubuntu 20.04 with the Dec. 5 source snapshot and the gold linker a couple days ago, no problem. I tried re-running the command from my build.ninja in verbose mode and no --color-diagnostics flag was passed to the linker. Maybe this is a recent change to clang in the trunk branch, try searching LLVM to see if that flag was recently enabled for the linker too.

Hi Buttaface. I tried compiling using commits from 4th of December, it still fails with the same error. It might be something in my system that's causing it to choose gold over lld for some reason?

No, as I said, I'm using gold too, that is the default that Swift uses on linux.

Is it possible you set LDFLAGS with that lld flag, assuming lld will always be used? You could try doing what the compiler says, add the -v flag and see if clang is passing that flag to gold. It isn't for me: if it isn't for you, this flag is coming from somewhere else.

1 Like

I thought that could be the problem as well, but right now my build invocation is simply utils/build-script -R and I still get the same error. I also re-cloned the project from scratch just to make sure.

How can I tell if that's happening? I ran build-script with --verbose and the output I got is here.

Thank you for helping me out by the way!

I think I found it: LLVM has long checked for that flag and added it if the default linker in the system path supported it, but Swift always links with gold by default and that flag will incorrectly be applied to it too.

No problem if the default linker is gold: did you recently change /usr/bin/ld to use lld? That would cause this problem.

2 Likes

Wow! I did change the default linker a while ago, but since I haven't compiled in a long time it slipped my mind! I'll change it back to the default linker.Thanks for all the help! I could try opening a PR to fix this issue.

Just to confirm that was actually the problem. I changed the linker to the default one and it compiled successfully.

1 Like
Terms of Service

Privacy Policy

Cookie Policy