<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/84724>84724</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Wrong result of llvm_config macro with USE_SHARED and default llvm build
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
karlwessel
</td>
</tr>
</table>
<pre>
The `llvm_config` makro with the `USE_SHARED` flag returns the shared library instead of the static libraries when used with LLVM compiled with static component libraries (`LLVM_BUILD_LLVM_DYLIB` = off). This of course causes linking projects using llvm to fail:
```
[2/2] Linking CXX executable simple-tool
FAILED: simple-tool
: && /usr/bin/c++ -Wl,--gc-sections CMakeFiles/simple-tool.dir/tool.cpp.o -o simple-tool -L/root/llvmtest/llvm-project/build/./lib -Wl,-rpath,"\$ORIGIN/../lib:/root/llvmtest/llvm-project/build/./lib" -lLLVM && :
/usr/bin/ld: cannot find -lLLVM: No such file or directory
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
```
The wrong result is caused by inconsistency between `LLVM_BUILD_LLVM_DYLIB` (default off) and `LLVM_DYLIB_COMPONENTS` (default=all).
https://github.com/llvm/llvm-project/blob/35b784379e707423f3b4f5a2cfedabcfa6f5b47d/llvm/CMakeLists.txt#L819-L823
https://github.com/llvm/llvm-project/blob/35b784379e707423f3b4f5a2cfedabcfa6f5b47d/llvm/CMakeLists.txt#L804
`LLVM_DYLIB_COMPONENTS` is used by the `llvm_config` makro to filter the passed components, assuming that all components listed there are available in the shared library, even when no shared library was created.
https://github.com/llvm/llvm-project/blob/35b784379e707423f3b4f5a2cfedabcfa6f5b47d/llvm/cmake/modules/LLVM-Config.cmake#L88-L92
Until now this probably hasn't caused many problems because the documentation still recomends to use `llvm_map_components_to_libnames`, which works fine for static libraries.
https://github.com/llvm/llvm-project/blob/d8d2dea7fc6f452ac6a24948fe3ff99920f81c99/llvm/docs/CMake.rst?plain=1#L1059-L1066
People using `llvm_config` probably use it because they have an llvm that uses shared libraries and therefore the macro works fine for them. But when the documentation is updated to recommend to use `llvm_config` macro (e.g. by https://github.com/llvm/llvm-project/pull/82769/) more people could get problems.
Possible solutions probably would be to either:
- always set `LLVM_DYLIB_COMPONENTS` to empty when `LLVM_BUILD_LLVM_DYLIB` is `false` or
- check `LLVM_BUILD_LLVM_DYLIB` in the `llvm_makro`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVk1v27oS_TX0ZmBBpmRZWniR2NF7Ady06Mdr38qgyJHFhiIFkorrf39Byk6ctghQXODiAokhaT40OnPmDJlz8qAR12R5S5bbGRt9Z-z6kVl1ROdQzRojTuvPHQIpUqWe-j03upUHUqTQs0dr4Ch9B35y-PLpbv_pvzcf77bB3ip2AIt-tNpFD9cxiwKUbCyzJ5DaeWQCTDtZPfOSn60SHRw71DA6FNM7drv_vQNu-kGqy6NzSHhoNGp_FUxoSYo0xOxvv9zvtvt4uf3_7v421EayLZi2JbRK4HMnXSiCm9E6BM5Ghw6U1I9SH2Cw5jty72B04TZgAN5Ay6Qi2Q1JtyS9IUV6_ptul7eU0JqS5RZ25zSbb98AfyAfPWsUgpP9oHDujVFTTH1zv7vbkuzm2gTnfNkNEFoQWgCh9egsoXUjNaE1J_SW0FuA-VdF6GY-P_C5Q-6l0Q4279gj1lKhI7S-ypoIGTLESz4MiYG5efXW-Y7Q2hrjCa3D93p0l8v5GY5QwSiVILROgkk2lxLswHxH6IZQSpYbQvP3H-__c_8QHM-eAbY_z08ohbmKJLhg8Qz_a1CUCIBxprXx0EotznHh6YMBN_IOWqkQjAUhLXJv7GnKxI1SyD0NrmitseFCiTOLUcAC8If0kXijm2K01N9Z8IsFg_NmGDCW4MaGm75nWkS6oEh-T5f4G2bsaI0OI-NG5UG6iYoCmjAr3GgnnUfNT9CgPyJqeJPhtBTYspBpIjqEOi4R0W2_ef_uw_uHu4fPn15HkGzLlIrDMRXXeT-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