Fix -fuse-ld to be linker flag in feature detection check
authorKevin Gleason <gleasonk@google.com>
Tue, 20 Sep 2022 21:49:08 +0000 (21:49 +0000)
committerMehdi Amini <joker.eph@gmail.com>
Wed, 21 Sep 2022 04:01:57 +0000 (04:01 +0000)
commitb052eead95e0564e1230bcca7dee199de2b7f19f
treef14365420f28b849064d96ba0bf1b5d5f9d77142
parentf5fe92f6938511a8e8c6ec850b842a499c8b040f
Fix -fuse-ld to be linker flag in feature detection check

Discovered an issue working in StableHLO when attempting to build with `-Wall -Werror`:
https://github.com/openxla/stablehlo/pull/137

Currently, if `LLVM_USE_LLD` and `-DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -Wall -Werror"`
are both specified for build, the build will error with:

```
-- Performing Test CXX_SUPPORTS_CUSTOM_LINKER - Failed
CMake Error at /usr/local/google/home/gleasonk/Coding/llvm-build/lib/cmake/llvm/HandleLLVMOptions.cmake:309 (message):
  Host compiler does not support '-fuse-ld=lld'
...

$ cat <build_dir>/CMakeFiles/CMakeError.log
...
clang: error: argument unused during compilation: '-fuse-ld=lld' [-Werror,-Wunused-command-line-argument]
```

It looks like other repos have hit this same issue:
- https://github.com/golang/go/issues/41527 (mentioned in comment)
- https://github.com/iree-org/iree/pull/7450

This can be reproduced in llvm-project with the following build command:

```
# Compile command taken from https://mlir.llvm.org/getting_started/
# and modified for use case
cmake -G Ninja ../llvm \
   -DLLVM_ENABLE_PROJECTS=mlir \
   -DLLVM_BUILD_EXAMPLES=ON \
   -DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU" \
   -DCMAKE_BUILD_TYPE=Release \
   -DLLVM_ENABLE_ASSERTIONS=ON \
   -DCMAKE_CXX_COMPILER=clang++ \
   -DCMAKE_C_COMPILER=clang \
   -DLLVM_ENABLE_LLD=ON \
   -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -Wall -Werror"
```

Reviewed By: mehdi_amini

Differential Revision: https://reviews.llvm.org/D134206
llvm/cmake/modules/HandleLLVMOptions.cmake