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