Issue |
145406
|
Summary |
[llvm] eliminate the need for a second LLVM_BUILD_STATIC
|
Labels |
new issue
|
Assignees |
|
Reporter |
andrurogerz
|
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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs