<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/145406>145406</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[llvm] eliminate the need for a second LLVM_BUILD_STATIC
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
andrurogerz
</td>
</tr>
</table>
<pre>
The `LLVM_ABI` and related macros defined in [`llvm/Support/Compiler.h`](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/Compiler.h#L156) are used to annotate LLVM's public symbols for export. They resolve to different annotations depending on the build target:
- When building LLVM as a Windows DLL, they are defined to either `__declspec(dllexport)` when building the DLL or `__declspec(dllimport)` when building clients.
- When building LLVM as a shared library on for ELF or dylib for Mach-O, they are defined to default visibility annotations `__attribute__(visibility(("default")))` to ensure the symbols are exported when default visibility is set to hidden.
- When building LLVM as a static library, they are noops.
However, even when building LLVM as a DLL or shared library, a subset of libraries like tablegen always link against individual static libraries of LLVM components, such as the Support library. In order to make the `LLVM_ABI` macros apply correctly in this case (e.g. become noops), there is an override preprocessor define `LLVM_BUILD_STATIC`. This definition is enabled dynamically whenever an LLVM target defined with `llvm_add_library` or related CMake macros passes the [`DISABLE_LLVM_LINK_LLVM_DYLIB`](https://github.com/llvm/llvm-project/blob/main/llvm/cmake/modules/AddLLVM.cmake#L500) argument.
For correctness, the LLVM libraries should be built for either static or dynamic linking with one static set of `LLVM_ABI` definitions-- the definitions should not be controllable via a dynamic definition like `LLVM_BUILD_STATIC`. This means we need one set of libraries and llvm-config.h for targets that use `DISABLE_LLVM_LINK_LLVM_DYLIB` and another for those that link against the DLL or dynamic library.
Ideally, the `LLVM_ABI` definitions in Compiler.h would be simplified to the following:
```
#if defined(LLVM_BUILD_STATIC)
#define LLVM_ABI
#define LLVM_TEMPLATE_ABI
#define LLVM_EXPORT_TEMPLATE
#define LLVM_ABI_EXPORT
#elif defined(_WIN32) && !defined(__MINGW32__)
#if defined(LLVM_EXPORTS)
#define LLVM_ABI __declspec(dllexport)
#define LLVM_TEMPLATE_ABI
#define LLVM_EXPORT_TEMPLATE __declspec(dllexport)
#else
#define LLVM_ABI __declspec(dllimport)
#define LLVM_TEMPLATE_ABI __declspec(dllimport)
#define LLVM_EXPORT_TEMPLATE
#endif
```
`LLVM_BUILD_STATIC` would be defined (or not) in [`llvm-config.h`](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Config/llvm-config.h.cmake) based on CMake configuration. Other related definitions in `llvm-config.h` like `LLVM_BUILD_LLVM_DYLIB` and `LLVM_BUILD_SHARED_LIBS` could potentially be eliminated.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0V9tu4zYQ_Rr6ZRBDpi-xH_zgxHHXqLK72KRN-2RQ4thilyIFkrLrfn0xlBTHibPbRVtAwG54mcs5M2do4b3aGcQ5G9-w8bIn6lBYNxdGutrZHbq_epmVx_ljgcAmSZr-er9Z3KzZJAFhJDjUIqCEUuTOepC4VQYlKANkb5JovS8ZXz3UVWVdYHx1a8tKaXT9gk0SNl4yPi1CqDwbLhhfMb7aqVDUWT-3dK-9Tv9cVc7-gTnZyLTNGF-VQpnTGWVyXUs8LVz0yYfpYDxhfAbCIdQeJQQLwhgbRECg9Bi_9lDVmVY5-GOZWe1hax3gn2SuD48FHsGht3qPdFmq7RYdmtCZUdYQEhUaqcwOrIFQIGS10hKCcDsMlGyyuIKnAk2zQQfJOQgPAp6UkfbgYZmmjN_S9WOMt4M3WEAVCnREyWYjMde-wpzxqdS6iZPxGXF0OPNAcSzTFOzFi6p892KuFZrg-9-M2hfCoQStMifckfIm2O7SFfmTR62yuHAv8uLq03t5SdyKWgfYK68ypVU4nsEawxYhOJXVATcbxqenk4xP48dbI4xzyqb5JkmEzfjaYQSi45YCaDBD2aR9IQblwWMgC4WSEs13kKBw8w6Js1SNtVXEkSWLD_aAe3S0j3s0rzA_m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