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 | |
Andrew Wilkins | 9211396 | 2015-09-01 03:14:31 +0000 | [diff] [blame] | 11 | if(LLVM_LINK_LLVM_DYLIB) |
Chris Bieneman | 9c5e41f | 2015-10-27 16:02:04 +0000 | [diff] [blame] | 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.") |
Andrew Wilkins | bb6d95f | 2015-09-05 08:27:33 +0000 | [diff] [blame] | 14 | endif() |
| 15 | |
Andrew Wilkins | 9211396 | 2015-09-01 03:14:31 +0000 | [diff] [blame] | 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}) |
| 32 | endif() |
| 33 | |
Chris Bieneman | 9c5e41f | 2015-10-27 16:02:04 +0000 | [diff] [blame] | 34 | if(LLVM_DYLIB_EXPORTED_SYMBOL_FILE) |
Chris Bieneman | 168ddf4 | 2015-04-13 21:29:46 +0000 | [diff] [blame] | 35 | set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE}) |
| 36 | add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE}) |
Chris Bieneman | adffd01 | 2014-10-23 17:22:14 +0000 | [diff] [blame] | 37 | endif() |
| 38 | |
Chris Bieneman | 5e96fe9 | 2015-11-04 23:11:12 +0000 | [diff] [blame] | 39 | add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB SONAME ${SOURCES}) |
Chris Bieneman | adffd01 | 2014-10-23 17:22:14 +0000 | [diff] [blame] | 40 | |
Chris Bieneman | 3ee8196 | 2015-04-16 16:56:22 +0000 | [diff] [blame] | 41 | list(REMOVE_DUPLICATES LIB_NAMES) |
NAKAMURA Takumi | c8abc4c | 2014-11-10 15:04:02 +0000 | [diff] [blame] | 42 | if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") # FIXME: It should be "GNU ld for elf" |
| 43 | # GNU ld doesn't resolve symbols in the version script. |
NAKAMURA Takumi | c8abc4c | 2014-11-10 15:04:02 +0000 | [diff] [blame] | 44 | set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive) |
Chris Bieneman | 3ee8196 | 2015-04-16 16:56:22 +0000 | [diff] [blame] | 45 | elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") |
| 46 | set(LIB_NAMES -Wl,-all_load ${LIB_NAMES}) |
NAKAMURA Takumi | c8abc4c | 2014-11-10 15:04:02 +0000 | [diff] [blame] | 47 | endif() |
| 48 | |
Chris Bieneman | 6a1b54a | 2015-03-23 20:03:57 +0000 | [diff] [blame] | 49 | target_link_libraries(LLVM PRIVATE ${LIB_NAMES}) |
NAKAMURA Takumi | c8abc4c | 2014-11-10 15:04:02 +0000 | [diff] [blame] | 50 | |
Chris Bieneman | adffd01 | 2014-10-23 17:22:14 +0000 | [diff] [blame] | 51 | if (APPLE) |
| 52 | set_property(TARGET LLVM APPEND_STRING PROPERTY |
| 53 | LINK_FLAGS |
Chris Bieneman | cfa3109 | 2015-10-14 07:50:21 +0000 | [diff] [blame] | 54 | " -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] | 55 | endif() |
| 56 | |
Chris Bieneman | 9c5e41f | 2015-10-27 16:02:04 +0000 | [diff] [blame] | 57 | if(TARGET libLLVMExports) |
| 58 | add_dependencies(LLVM libLLVMExports) |
| 59 | endif() |
| 60 | |
| 61 | if(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}") |
| 94 | endif() |
| 95 | |