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})
53 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
54 set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
57 target_link_libraries(LLVM PRIVATE ${LIB_NAMES})
60 set_property(TARGET LLVM APPEND_STRING PROPERTY
62 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")
65 if(TARGET libLLVMExports)
66 add_dependencies(LLVM libLLVMExports)
70 if(LLVM_BUILD_LLVM_C_DYLIB AND NOT MSVC)
72 message(FATAL_ERROR "Generating libLLVM-c is only supported on Darwin")
75 if(NOT LLVM_BUILD_LLVM_DYLIB)
76 message(FATAL_ERROR "Generating libLLVM-c requires LLVM_BUILD_LLVM_C_DYLIB on Darwin")
79 # To get the export list for a single llvm library:
80 # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports
82 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm-c.exports)
84 set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
85 set(LIB_NAME ${LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}LLVM)
86 set(LIB_PATH ${LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})
87 set(LIB_EXPORTS_PATH ${LIB_NAME}.exports)
88 list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH})
90 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
91 COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LLVM_EXPORTED_SYMBOL_FILE}
92 WORKING_DIRECTORY ${LIB_DIR}
94 COMMENT "Generating Export list for LLVM..."
97 add_custom_target(libLLVMCExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
99 add_llvm_library(LLVM-C SHARED ${SOURCES} INSTALL_WITH_TOOLCHAIN)
101 target_link_libraries(LLVM-C PUBLIC LLVM)
102 add_dependencies(LLVM-C libLLVMCExports)
104 set_property(TARGET LLVM-C APPEND_STRING PROPERTY
106 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH} -Wl,-reexport_library ${LIB_PATH}")
110 # Build the LLVM-C.dll library that exports the C API.
112 set(LLVM_LINK_COMPONENTS
113 ${LLVM_DYLIB_COMPONENTS}
116 llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
117 list(REMOVE_DUPLICATES LIB_NAMES)
119 # The python script needs to know whether symbols are prefixed with underscores or not.
120 if(LLVM_HOST_TRIPLE STREQUAL "i686-pc-win32")
121 set(GEN_UNDERSCORE "--underscore")
123 set(GEN_UNDERSCORE "")
126 # Set this name here, not used in multi conf loop,
127 # but add script will pick the right one.
128 set(LIBSFILE ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.args)
130 # Get the full name to the libs so the python script understands them.
131 foreach(lib ${LIB_NAMES})
132 list(APPEND FULL_LIB_NAMES ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/${lib}.lib)
135 # Need to seperate lib names with newlines.
136 string(REPLACE ";" "\n" FILE_CONTENT "${FULL_LIB_NAMES}")
138 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
139 foreach(BUILD_MODE ${CMAKE_CONFIGURATION_TYPES})
140 # Replace the special string with a per config directory.
141 string(REPLACE ${CMAKE_CFG_INTDIR} ${BUILD_MODE} PER_CONF_CONTENT "${FILE_CONTENT}")
143 # Write out the full lib names into file to be read by the python script.
144 # One libsfile per build, the add_custom_command should expand
145 # ${CMAKE_CFG_INTDIR} correctly and select the right one.
146 file(WRITE ${CMAKE_BINARY_DIR}/${BUILD_MODE}/libllvm-c.args "${PER_CONF_CONTENT}")
149 # Write out the full lib names into file to be read by the python script.
150 file(WRITE ${LIBSFILE} "${FILE_CONTENT}")
153 # Generate the exports file dynamically.
154 set(GEN_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/gen-msvc-exports.py)
156 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.exports)
158 if(CMAKE_CROSSCOMPILING)
159 build_native_tool(llvm-nm llvm_nm)
160 set(llvm_nm_target "${llvm_nm}")
162 set(llvm_nm $<TARGET_FILE:llvm-nm>)
163 set(llvm_nm_target llvm-nm)
166 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
167 COMMAND ${PYTHON_EXECUTABLE} ${GEN_SCRIPT} --libsfile ${LIBSFILE} ${GEN_UNDERSCORE} --nm "${llvm_nm}" -o ${LLVM_EXPORTED_SYMBOL_FILE}
168 DEPENDS ${LIB_NAMES} ${llvm_nm_target}
169 COMMENT "Generating export list for LLVM-C"
172 # Finally link the target.
173 add_llvm_library(LLVM-C SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES} DEPENDS intrinsics_gen)