1 # This tool creates a shared library from the LLVM libraries. Generating this
2 # library is enabled by setting LLVM_BUILD_LLVM_DYLIB=yes on the CMake
3 # commandline. By default the shared library only exports the LLVM C API.
9 if(LLVM_LINK_LLVM_DYLIB AND LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
10 message(WARNING "Using LLVM_LINK_LLVM_DYLIB with LLVM_DYLIB_EXPORTED_SYMBOL_FILE may not work. Use at your own risk.")
13 if(LLVM_BUILD_LLVM_DYLIB)
15 message(FATAL_ERROR "Generating libLLVM is not supported on MSVC")
18 llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
20 # Exclude libLLVMTableGen for the following reasons:
21 # - it is only used by internal *-tblgen utilities;
22 # - it pollutes the global options space.
23 list(REMOVE_ITEM LIB_NAMES "LLVMTableGen")
25 if(LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
26 set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})
27 add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
30 if (LLVM_LINK_LLVM_DYLIB)
31 set(INSTALL_WITH_TOOLCHAIN INSTALL_WITH_TOOLCHAIN)
33 add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB SONAME ${INSTALL_WITH_TOOLCHAIN} ${SOURCES})
35 list(REMOVE_DUPLICATES LIB_NAMES)
36 if(("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") OR (MINGW) OR (HAIKU)
37 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
38 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "GNU")
39 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "OpenBSD")
40 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
41 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "DragonFly")
42 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")) # FIXME: It should be "GNU ld for elf"
44 ${CMAKE_CURRENT_SOURCE_DIR}/simple_version_script.map.in
45 ${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map)
47 # GNU ld doesn't resolve symbols in the version script.
48 set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
49 if (NOT LLVM_LINKER_IS_SOLARISLD AND NOT MINGW)
50 # Solaris ld does not accept global: *; so there is no way to version *all* global symbols
51 set(LIB_NAMES -Wl,--version-script,${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map ${LIB_NAMES})
54 # Optimize function calls for default visibility definitions to avoid PLT and
55 # reduce dynamic relocations.
56 # Note: for -fno-pic default, the address of a function may be different from
57 # inside and outside libLLVM.so.
58 target_link_options(LLVM PRIVATE LINKER:-Bsymbolic-functions)
60 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
61 set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
64 target_link_libraries(LLVM PRIVATE ${LIB_NAMES})
67 set_property(TARGET LLVM APPEND_STRING PROPERTY
69 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")
72 if(TARGET libLLVMExports)
73 add_dependencies(LLVM libLLVMExports)
77 if(LLVM_BUILD_LLVM_C_DYLIB AND NOT MSVC)
79 message(FATAL_ERROR "Generating libLLVM-c is only supported on Darwin")
82 if(NOT LLVM_BUILD_LLVM_DYLIB)
83 message(FATAL_ERROR "Generating libLLVM-c requires LLVM_BUILD_LLVM_C_DYLIB on Darwin")
86 # To get the export list for a single llvm library:
87 # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports
89 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm-c.exports)
91 set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
92 set(LIB_NAME ${LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}LLVM)
93 set(LIB_PATH ${LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})
94 set(LIB_EXPORTS_PATH ${LIB_NAME}.exports)
95 list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH})
97 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
98 COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LLVM_EXPORTED_SYMBOL_FILE}
99 WORKING_DIRECTORY ${LIB_DIR}
101 COMMENT "Generating Export list for LLVM..."
104 add_custom_target(libLLVMCExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
106 add_llvm_library(LLVM-C SHARED ${SOURCES} INSTALL_WITH_TOOLCHAIN)
108 target_link_libraries(LLVM-C PUBLIC LLVM)
109 add_dependencies(LLVM-C libLLVMCExports)
111 set_property(TARGET LLVM-C APPEND_STRING PROPERTY
113 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH} -Wl,-reexport_library ${LIB_PATH}")
116 if(LLVM_BUILD_LLVM_C_DYLIB AND MSVC)
117 # Build the LLVM-C.dll library that exports the C API.
119 set(LLVM_LINK_COMPONENTS
120 ${LLVM_DYLIB_COMPONENTS}
123 llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
124 list(REMOVE_DUPLICATES LIB_NAMES)
126 # The python script needs to know whether symbols are prefixed with underscores or not.
127 if(LLVM_HOST_TRIPLE STREQUAL "i686-pc-win32")
128 set(GEN_UNDERSCORE "--underscore")
130 set(GEN_UNDERSCORE "")
133 # Set this name here, not used in multi conf loop,
134 # but add script will pick the right one.
135 set(LIBSFILE ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.args)
137 # Get the full name to the libs so the python script understands them.
138 foreach(lib ${LIB_NAMES})
139 list(APPEND FULL_LIB_NAMES ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/${lib}.lib)
142 # Need to separate lib names with newlines.
143 string(REPLACE ";" "\n" FILE_CONTENT "${FULL_LIB_NAMES}")
145 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
146 foreach(BUILD_MODE ${CMAKE_CONFIGURATION_TYPES})
147 # Replace the special string with a per config directory.
148 string(REPLACE ${CMAKE_CFG_INTDIR} ${BUILD_MODE} PER_CONF_CONTENT "${FILE_CONTENT}")
150 # Write out the full lib names into file to be read by the python script.
151 # One libsfile per build, the add_custom_command should expand
152 # ${CMAKE_CFG_INTDIR} correctly and select the right one.
153 file(WRITE ${CMAKE_BINARY_DIR}/${BUILD_MODE}/libllvm-c.args "${PER_CONF_CONTENT}")
156 # Write out the full lib names into file to be read by the python script.
157 file(WRITE ${LIBSFILE} "${FILE_CONTENT}")
160 # Generate the exports file dynamically.
161 set(GEN_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/gen-msvc-exports.py)
163 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.exports)
165 if(CMAKE_CROSSCOMPILING)
166 build_native_tool(llvm-nm llvm_nm)
167 set(llvm_nm_target "${llvm_nm}")
169 set(llvm_nm $<TARGET_FILE:llvm-nm>)
170 set(llvm_nm_target llvm-nm)
173 set(llvm_nm ${LLVM_NM})
174 set(llvm_nm_target "")
177 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
178 COMMAND "${Python3_EXECUTABLE}" ${GEN_SCRIPT} --libsfile ${LIBSFILE} ${GEN_UNDERSCORE} --nm "${llvm_nm}" -o ${LLVM_EXPORTED_SYMBOL_FILE}
179 DEPENDS ${LIB_NAMES} ${llvm_nm_target}
180 COMMENT "Generating export list for LLVM-C"
183 # Finally link the target.
184 add_llvm_library(LLVM-C SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES} DEPENDS intrinsics_gen)
186 if (LLVM_INTEGRATED_CRT_ALLOC AND MSVC)
187 # Make sure we search LLVMSupport first, before the CRT libs
188 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -INCLUDE:malloc")