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. 4 5set(SOURCES 6 libllvm.cpp 7 ) 8 9if(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.") 11endif() 12 13if(LLVM_BUILD_LLVM_DYLIB) 14 if(MSVC) 15 message(FATAL_ERROR "Generating libLLVM is not supported on MSVC") 16 endif() 17 18 llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS}) 19 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") 24 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}) 28 endif() 29 30 if (LLVM_LINK_LLVM_DYLIB) 31 set(INSTALL_WITH_TOOLCHAIN INSTALL_WITH_TOOLCHAIN) 32 endif() 33 add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB SONAME ${INSTALL_WITH_TOOLCHAIN} ${SOURCES}) 34 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" 43 configure_file( 44 ${CMAKE_CURRENT_SOURCE_DIR}/simple_version_script.map.in 45 ${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map) 46 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}) 52 endif() 53 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") 54 set(LIB_NAMES -Wl,-all_load ${LIB_NAMES}) 55 endif() 56 57 target_link_libraries(LLVM PRIVATE ${LIB_NAMES}) 58 59 if (APPLE) 60 set_property(TARGET LLVM APPEND_STRING PROPERTY 61 LINK_FLAGS 62 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}") 63 endif() 64 65 if(TARGET libLLVMExports) 66 add_dependencies(LLVM libLLVMExports) 67 endif() 68endif() 69 70if(LLVM_BUILD_LLVM_C_DYLIB AND NOT MSVC) 71 if(NOT APPLE) 72 message(FATAL_ERROR "Generating libLLVM-c is only supported on Darwin") 73 endif() 74 75 if(NOT LLVM_BUILD_LLVM_DYLIB) 76 message(FATAL_ERROR "Generating libLLVM-c requires LLVM_BUILD_LLVM_C_DYLIB on Darwin") 77 endif() 78 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 81 82 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm-c.exports) 83 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}) 89 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} 93 DEPENDS LLVM 94 COMMENT "Generating Export list for LLVM..." 95 VERBATIM ) 96 97 add_custom_target(libLLVMCExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE}) 98 99 add_llvm_library(LLVM-C SHARED ${SOURCES} INSTALL_WITH_TOOLCHAIN) 100 101 target_link_libraries(LLVM-C PUBLIC LLVM) 102 add_dependencies(LLVM-C libLLVMCExports) 103 104 set_property(TARGET LLVM-C APPEND_STRING PROPERTY 105 LINK_FLAGS 106 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH} -Wl,-reexport_library ${LIB_PATH}") 107endif() 108 109if(LLVM_BUILD_LLVM_C_DYLIB AND MSVC) 110 # Build the LLVM-C.dll library that exports the C API. 111 112 set(LLVM_LINK_COMPONENTS 113 ${LLVM_DYLIB_COMPONENTS} 114 ) 115 116 llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS}) 117 list(REMOVE_DUPLICATES LIB_NAMES) 118 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") 122 else() 123 set(GEN_UNDERSCORE "") 124 endif() 125 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) 129 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) 133 endforeach() 134 135 # Need to separate lib names with newlines. 136 string(REPLACE ";" "\n" FILE_CONTENT "${FULL_LIB_NAMES}") 137 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}") 142 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}") 147 endforeach() 148 else() 149 # Write out the full lib names into file to be read by the python script. 150 file(WRITE ${LIBSFILE} "${FILE_CONTENT}") 151 endif() 152 153 # Generate the exports file dynamically. 154 set(GEN_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/gen-msvc-exports.py) 155 156 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.exports) 157 if(NOT LLVM_NM) 158 if(CMAKE_CROSSCOMPILING) 159 build_native_tool(llvm-nm llvm_nm) 160 set(llvm_nm_target "${llvm_nm}") 161 else() 162 set(llvm_nm $<TARGET_FILE:llvm-nm>) 163 set(llvm_nm_target llvm-nm) 164 endif() 165 else() 166 set(llvm_nm ${LLVM_NM}) 167 set(llvm_nm_target "") 168 endif() 169 170 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE} 171 COMMAND "${Python3_EXECUTABLE}" ${GEN_SCRIPT} --libsfile ${LIBSFILE} ${GEN_UNDERSCORE} --nm "${llvm_nm}" -o ${LLVM_EXPORTED_SYMBOL_FILE} 172 DEPENDS ${LIB_NAMES} ${llvm_nm_target} 173 COMMENT "Generating export list for LLVM-C" 174 VERBATIM ) 175 176 # Finally link the target. 177 add_llvm_library(LLVM-C SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES} DEPENDS intrinsics_gen) 178 179 if (LLVM_INTEGRATED_CRT_ALLOC AND MSVC) 180 # Make sure we search LLVMSupport first, before the CRT libs 181 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -INCLUDE:malloc") 182 endif() 183 184endif() 185