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 # libLLVM.so should not have any dependencies on any other LLVM
21 # shared libraries. When using the "all" pseudo-component,
22 # LLVM_AVAILABLE_LIBS is added to the dependencies, which may
23 # contain shared libraries (e.g. libLTO).
25 # Also exclude libLLVMTableGen for the following reasons:
26 # - it is only used by internal *-tblgen utilities;
27 # - it pollutes the global options space.
28 foreach(lib ${LIB_NAMES})
29 get_target_property(t ${lib} TYPE)
30 if("${lib}" STREQUAL "LLVMTableGen")
31 elseif("x${t}" STREQUAL "xSTATIC_LIBRARY")
32 list(APPEND FILTERED_LIB_NAMES ${lib})
35 set(LIB_NAMES ${FILTERED_LIB_NAMES})
37 if(LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
38 set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})
39 add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
42 if (LLVM_LINK_LLVM_DYLIB)
43 set(INSTALL_WITH_TOOLCHAIN INSTALL_WITH_TOOLCHAIN)
45 add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB SONAME ${INSTALL_WITH_TOOLCHAIN} ${SOURCES})
47 list(REMOVE_DUPLICATES LIB_NAMES)
48 if(("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") OR (MINGW) OR (HAIKU)
49 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
50 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "GNU")
51 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "OpenBSD")
52 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
53 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "DragonFly")
54 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")) # FIXME: It should be "GNU ld for elf"
56 ${CMAKE_CURRENT_SOURCE_DIR}/simple_version_script.map.in
57 ${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map)
59 # GNU ld doesn't resolve symbols in the version script.
60 set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
61 if (NOT LLVM_LINKER_IS_SOLARISLD AND NOT MINGW)
62 # Solaris ld does not accept global: *; so there is no way to version *all* global symbols
63 set(LIB_NAMES -Wl,--version-script,${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map ${LIB_NAMES})
65 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
66 set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
69 target_link_libraries(LLVM PRIVATE ${LIB_NAMES})
72 set_property(TARGET LLVM APPEND_STRING PROPERTY
74 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")
77 if(TARGET libLLVMExports)
78 add_dependencies(LLVM libLLVMExports)
82 if(LLVM_BUILD_LLVM_C_DYLIB AND NOT MSVC)
84 message(FATAL_ERROR "Generating libLLVM-c is only supported on Darwin")
87 if(NOT LLVM_BUILD_LLVM_DYLIB)
88 message(FATAL_ERROR "Generating libLLVM-c requires LLVM_BUILD_LLVM_C_DYLIB on Darwin")
91 # To get the export list for a single llvm library:
92 # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports
94 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm-c.exports)
96 set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
97 set(LIB_NAME ${LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}LLVM)
98 set(LIB_PATH ${LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})
99 set(LIB_EXPORTS_PATH ${LIB_NAME}.exports)
100 list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH})
102 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
103 COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LLVM_EXPORTED_SYMBOL_FILE}
104 WORKING_DIRECTORY ${LIB_DIR}
106 COMMENT "Generating Export list for LLVM..."
109 add_custom_target(libLLVMCExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
111 add_llvm_library(LLVM-C SHARED ${SOURCES} INSTALL_WITH_TOOLCHAIN)
113 target_link_libraries(LLVM-C PUBLIC LLVM)
114 add_dependencies(LLVM-C libLLVMCExports)
116 set_property(TARGET LLVM-C APPEND_STRING PROPERTY
118 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH} -Wl,-reexport_library ${LIB_PATH}")
122 # Build the LLVM-C.dll library that exports the C API.
124 set(LLVM_LINK_COMPONENTS
125 ${LLVM_DYLIB_COMPONENTS}
128 llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
129 list(REMOVE_DUPLICATES LIB_NAMES)
131 # The python script needs to know whether symbols are prefixed with underscores or not.
132 if(LLVM_HOST_TRIPLE STREQUAL "i686-pc-win32")
133 set(GEN_UNDERSCORE "--underscore")
135 set(GEN_UNDERSCORE "")
138 # Set this name here, not used in multi conf loop,
139 # but add script will pick the right one.
140 set(LIBSFILE ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.args)
142 # Get the full name to the libs so the python script understands them.
143 foreach(lib ${LIB_NAMES})
144 list(APPEND FULL_LIB_NAMES ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/${lib}.lib)
147 # Need to seperate lib names with newlines.
148 string(REPLACE ";" "\n" FILE_CONTENT "${FULL_LIB_NAMES}")
150 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
151 foreach(BUILD_MODE ${CMAKE_CONFIGURATION_TYPES})
152 # Replace the special string with a per config directory.
153 string(REPLACE ${CMAKE_CFG_INTDIR} ${BUILD_MODE} PER_CONF_CONTENT "${FILE_CONTENT}")
155 # Write out the full lib names into file to be read by the python script.
156 # One libsfile per build, the add_custom_command should expand
157 # ${CMAKE_CFG_INTDIR} correctly and select the right one.
158 file(WRITE ${CMAKE_BINARY_DIR}/${BUILD_MODE}/libllvm-c.args "${PER_CONF_CONTENT}")
161 # Write out the full lib names into file to be read by the python script.
162 file(WRITE ${LIBSFILE} "${FILE_CONTENT}")
165 # Generate the exports file dynamically.
166 set(GEN_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/gen-msvc-exports.py)
168 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.exports)
170 if(CMAKE_CROSSCOMPILING)
171 build_native_tool(llvm-nm llvm_nm)
172 set(llvm_nm_target "${llvm_nm}")
174 set(llvm_nm $<TARGET_FILE:llvm-nm>)
175 set(llvm_nm_target llvm-nm)
178 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
179 COMMAND ${PYTHON_EXECUTABLE} ${GEN_SCRIPT} --libsfile ${LIBSFILE} ${GEN_UNDERSCORE} --nm "${llvm_nm}" -o ${LLVM_EXPORTED_SYMBOL_FILE}
180 DEPENDS ${LIB_NAMES} ${llvm_nm_target}
181 COMMENT "Generating export list for LLVM-C"
184 # Finally link the target.
185 add_llvm_library(LLVM-C SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES} DEPENDS intrinsics_gen)