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 9llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS}) 10 11if(LLVM_LINK_LLVM_DYLIB) 12 if(LLVM_DYLIB_EXPORTED_SYMBOL_FILE) 13 message(WARNING "Using LLVM_LINK_LLVM_DYLIB with LLVM_DYLIB_EXPORTED_SYMBOL_FILE may not work. Use at your own risk.") 14 endif() 15 16 # libLLVM.so should not have any dependencies on any other LLVM 17 # shared libraries. When using the "all" pseudo-component, 18 # LLVM_AVAILABLE_LIBS is added to the dependencies, which may 19 # contain shared libraries (e.g. libLTO). 20 # 21 # Also exclude libLLVMTableGen for the following reasons: 22 # - it is only used by internal *-tblgen utilities; 23 # - it pollutes the global options space. 24 foreach(lib ${LIB_NAMES}) 25 get_target_property(t ${lib} TYPE) 26 if("${lib}" STREQUAL "LLVMTableGen") 27 elseif("x${t}" STREQUAL "xSTATIC_LIBRARY") 28 list(APPEND FILTERED_LIB_NAMES ${lib}) 29 endif() 30 endforeach() 31 set(LIB_NAMES ${FILTERED_LIB_NAMES}) 32endif() 33 34if(LLVM_DYLIB_EXPORTED_SYMBOL_FILE) 35 set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE}) 36 add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE}) 37endif() 38 39add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB SONAME ${SOURCES}) 40 41list(REMOVE_DUPLICATES LIB_NAMES) 42if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") # FIXME: It should be "GNU ld for elf" 43 # GNU ld doesn't resolve symbols in the version script. 44 set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive) 45elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") 46 set(LIB_NAMES -Wl,-all_load ${LIB_NAMES}) 47endif() 48 49target_link_libraries(LLVM PRIVATE ${LIB_NAMES}) 50 51if (APPLE) 52 set_property(TARGET LLVM APPEND_STRING PROPERTY 53 LINK_FLAGS 54 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}") 55endif() 56 57if(TARGET libLLVMExports) 58 add_dependencies(LLVM libLLVMExports) 59endif() 60 61if(LLVM_BUILD_LLVM_C_DYLIB) 62 # To get the export list for a single llvm library: 63 # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports 64 65 if(NOT APPLE) 66 message(FATAL_ERROR "Generating libLLVM-c is only supported on Darwin") 67 endif() 68 69 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm-c.exports) 70 71 set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX}) 72 set(LIB_NAME ${LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}LLVM) 73 set(LIB_PATH ${LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}) 74 set(LIB_EXPORTS_PATH ${LIB_NAME}.exports) 75 list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH}) 76 77 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE} 78 COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LLVM_EXPORTED_SYMBOL_FILE} 79 WORKING_DIRECTORY ${LIB_DIR} 80 DEPENDS LLVM 81 COMMENT "Generating Export list for LLVM..." 82 VERBATIM ) 83 84 add_custom_target(libLLVMCExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE}) 85 86 add_llvm_library(LLVM-C SHARED ${SOURCES}) 87 88 target_link_libraries(LLVM-C PUBLIC LLVM) 89 add_dependencies(LLVM-C libLLVMCExports) 90 91 set_property(TARGET LLVM-C APPEND_STRING PROPERTY 92 LINK_FLAGS 93 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH} -Wl,-reexport_library ${LIB_PATH}") 94endif() 95 96