Description
The LLVM_ABI
and related macros defined in llvm/Support/Compiler.h
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
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
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.