[compiler-rt] Reapply [compiler-rt] Check for and use -lunwind when linking with -nodefaultlibs (PR #66584)

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 29 00:03:11 PDT 2023


petrhosek wrote:

> > I'm surprised that CMake is trying to use targets defined in the same build in `try_compile`. What would happen if this a `LIBRARY` target like `unwind_shared` or `unwind_static`? That target hasn't been built yet (it's built by the build we're configuring). Is this a CMake issue or is there a well defined behavior for this case?
> 
> I tested replacing this with a check for `unwind_shared`, and that proceeded just fine (and it tested linking right away even if the target `unwind_shared` wasn't found).
> 
> Thus, if the `unwind` target wouldn't be of the type `UTILITY`, this might work - but would that cause other cmake confusion at some point? If the target/library entity `unwind` both means a preexisting library (link by passing `-lunwind`) and a library that gets built during the build (where it instead adds dependencies on it, builds that before using it, and links against the newly generated file instead of necessarily using literally `-lunwind`).

Thanks, I tried it locally as well to better understand the issue.

With the following:
```
cmake_minimum_required(VERSION 3.13.4)
project(Test LANGUAGES CXX)
add_library(unwind_static STATIC unwind.cpp)
add_custom_target(unwind DEPENDS unwind_static)
include(CheckLibraryExists)
check_library_exists(unwind _Unwind_RaiseException "" COMPILER_RT_HAS_LIBUNWIND)
```
I get the same error:
```
CMake Error at cmake/linux-x64/share/cmake-3.25/Modules/CheckLibraryExists.cmake:71 (try_compile):
  Only libraries may be used as try_compile or try_run IMPORTED
  LINK_LIBRARIES.  Got unwind of type UTILITY.
Call Stack (most recent call first):
  CMakeLists.txt:9 (check_library_exists)
```
However, with the following:
```
cmake_minimum_required(VERSION 3.13.4)
project(Test LANGUAGES CXX)
add_library(unwind STATIC unwind.cpp)
include(CheckLibraryExists)
check_library_exists(unwind _Unwind_RaiseException "" COMPILER_RT_HAS_LIBUNWIND)
```
CMake fails to find `libunwind`:
```
/usr/bin/ld: cannot find -lunwind: No such file or directory
```

To me this really looks like a bug in CMake since this behavior is inconsistent. Specifically, I don't think CMake should consider the previously defined targets when performing checks. Perhaps we should file an issue against CMake and include a link in the comment?

https://github.com/llvm/llvm-project/pull/66584


More information about the llvm-commits mailing list