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 | add_definitions( -DLLVM_VERSION_INFO=\"${PACKAGE_VERSION}\" ) |
| 6 | |
| 7 | set(SOURCES |
| 8 | libllvm.cpp |
| 9 | ) |
| 10 | |
Chris Bieneman | 168ddf4 | 2015-04-13 21:29:46 +0000 | [diff] [blame] | 11 | llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS}) |
| 12 | |
Andrew Wilkins | 9211396 | 2015-09-01 03:14:31 +0000 | [diff] [blame] | 13 | if(LLVM_LINK_LLVM_DYLIB) |
Andrew Wilkins | bb6d95f | 2015-09-05 08:27:33 +0000 | [diff] [blame^] | 14 | if(NOT LLVM_DYLIB_EXPORT_ALL) |
| 15 | message(FATAL_ERROR "LLVM_DYLIB_EXPORT_ALL must be ON when LLVM_LINK_LLVM_DYLIB is ON") |
| 16 | endif() |
| 17 | |
Andrew Wilkins | 9211396 | 2015-09-01 03:14:31 +0000 | [diff] [blame] | 18 | # libLLVM.so should not have any dependencies on any other LLVM |
| 19 | # shared libraries. When using the "all" pseudo-component, |
| 20 | # LLVM_AVAILABLE_LIBS is added to the dependencies, which may |
| 21 | # contain shared libraries (e.g. libLTO). |
| 22 | # |
| 23 | # Also exclude libLLVMTableGen for the following reasons: |
| 24 | # - it is only used by internal *-tblgen utilities; |
| 25 | # - it pollutes the global options space. |
| 26 | foreach(lib ${LIB_NAMES}) |
| 27 | get_target_property(t ${lib} TYPE) |
| 28 | if("${lib}" STREQUAL "LLVMTableGen") |
| 29 | elseif("x${t}" STREQUAL "xSTATIC_LIBRARY") |
| 30 | list(APPEND FILTERED_LIB_NAMES ${lib}) |
| 31 | endif() |
| 32 | endforeach() |
| 33 | set(LIB_NAMES ${FILTERED_LIB_NAMES}) |
| 34 | endif() |
| 35 | |
Chris Bieneman | 168ddf4 | 2015-04-13 21:29:46 +0000 | [diff] [blame] | 36 | if(NOT DEFINED LLVM_DYLIB_EXPORTED_SYMBOL_FILE) |
Chris Bieneman | adffd01 | 2014-10-23 17:22:14 +0000 | [diff] [blame] | 37 | |
| 38 | if( WIN32 AND NOT CYGWIN ) |
| 39 | message(FATAL_ERROR "Auto-generation not implemented for Win32 without GNU utils. Please specify LLVM_EXPORTED_SYMBOL_FILE.") |
| 40 | endif() |
| 41 | |
| 42 | # To get the export list for a single llvm library: |
| 43 | # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports |
| 44 | |
| 45 | set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm.exports) |
| 46 | |
Chris Bieneman | 3ee8196 | 2015-04-16 16:56:22 +0000 | [diff] [blame] | 47 | if (NOT LLVM_DYLIB_EXPORT_ALL) |
| 48 | foreach (lib ${LIB_NAMES}) |
| 49 | set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX}) |
| 50 | set(LIB_NAME ${LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${lib}) |
| 51 | set(LIB_PATH ${LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}) |
| 52 | set(LIB_EXPORTS_PATH ${LIB_NAME}.exports) |
| 53 | list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH}) |
Chris Bieneman | adffd01 | 2014-10-23 17:22:14 +0000 | [diff] [blame] | 54 | |
Chris Bieneman | 3ee8196 | 2015-04-16 16:56:22 +0000 | [diff] [blame] | 55 | |
Chris Bieneman | 168ddf4 | 2015-04-13 21:29:46 +0000 | [diff] [blame] | 56 | add_custom_command(OUTPUT ${LIB_EXPORTS_PATH} |
| 57 | COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_EXPORTS_PATH} |
| 58 | WORKING_DIRECTORY ${LIB_DIR} |
| 59 | DEPENDS ${lib} |
| 60 | COMMENT "Generating Export list for ${lib}..." |
| 61 | VERBATIM ) |
Chris Bieneman | 3ee8196 | 2015-04-16 16:56:22 +0000 | [diff] [blame] | 62 | endforeach () |
| 63 | endif() |
Chris Bieneman | adffd01 | 2014-10-23 17:22:14 +0000 | [diff] [blame] | 64 | |
Chris Bieneman | 3ee8196 | 2015-04-16 16:56:22 +0000 | [diff] [blame] | 65 | if (LLVM_DYLIB_EXPORT_ALL) |
| 66 | add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE} |
Chris Bieneman | c82d0ca | 2015-04-16 21:58:22 +0000 | [diff] [blame] | 67 | COMMAND echo \"LLVM*\" > ${LLVM_EXPORTED_SYMBOL_FILE} && echo \"_Z*llvm*\" >> ${LLVM_EXPORTED_SYMBOL_FILE} |
Chris Bieneman | 3ee8196 | 2015-04-16 16:56:22 +0000 | [diff] [blame] | 68 | WORKING_DIRECTORY ${LIB_DIR} |
| 69 | DEPENDS ${LLVM_DYLIB_REQUIRED_EXPORTS} |
| 70 | COMMENT "Generating combined export list...") |
| 71 | else() |
| 72 | add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE} |
| 73 | COMMAND cat ${LLVM_DYLIB_REQUIRED_EXPORTS} > ${LLVM_EXPORTED_SYMBOL_FILE} |
| 74 | WORKING_DIRECTORY ${LIB_DIR} |
| 75 | DEPENDS ${LLVM_DYLIB_REQUIRED_EXPORTS} |
| 76 | COMMENT "Generating combined export list...") |
| 77 | endif() |
Chris Bieneman | adffd01 | 2014-10-23 17:22:14 +0000 | [diff] [blame] | 78 | |
Chris Bieneman | 11fd9d6 | 2015-02-18 18:52:11 +0000 | [diff] [blame] | 79 | add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE}) |
Chris Bieneman | 168ddf4 | 2015-04-13 21:29:46 +0000 | [diff] [blame] | 80 | else() |
| 81 | set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE}) |
| 82 | add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE}) |
Chris Bieneman | adffd01 | 2014-10-23 17:22:14 +0000 | [diff] [blame] | 83 | endif() |
| 84 | |
Andrew Wilkins | 9211396 | 2015-09-01 03:14:31 +0000 | [diff] [blame] | 85 | add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB ${SOURCES}) |
Chris Bieneman | adffd01 | 2014-10-23 17:22:14 +0000 | [diff] [blame] | 86 | |
Chris Bieneman | 3ee8196 | 2015-04-16 16:56:22 +0000 | [diff] [blame] | 87 | list(REMOVE_DUPLICATES LIB_NAMES) |
NAKAMURA Takumi | c8abc4c | 2014-11-10 15:04:02 +0000 | [diff] [blame] | 88 | if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") # FIXME: It should be "GNU ld for elf" |
| 89 | # GNU ld doesn't resolve symbols in the version script. |
NAKAMURA Takumi | c8abc4c | 2014-11-10 15:04:02 +0000 | [diff] [blame] | 90 | set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive) |
Chris Bieneman | 3ee8196 | 2015-04-16 16:56:22 +0000 | [diff] [blame] | 91 | elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") |
| 92 | set(LIB_NAMES -Wl,-all_load ${LIB_NAMES}) |
NAKAMURA Takumi | c8abc4c | 2014-11-10 15:04:02 +0000 | [diff] [blame] | 93 | endif() |
| 94 | |
Chris Bieneman | 6a1b54a | 2015-03-23 20:03:57 +0000 | [diff] [blame] | 95 | target_link_libraries(LLVM PRIVATE ${LIB_NAMES}) |
NAKAMURA Takumi | c8abc4c | 2014-11-10 15:04:02 +0000 | [diff] [blame] | 96 | |
Chris Bieneman | 11fd9d6 | 2015-02-18 18:52:11 +0000 | [diff] [blame] | 97 | add_dependencies(LLVM libLLVMExports) |
Chris Bieneman | adffd01 | 2014-10-23 17:22:14 +0000 | [diff] [blame] | 98 | |
| 99 | if (APPLE) |
| 100 | set_property(TARGET LLVM APPEND_STRING PROPERTY |
| 101 | LINK_FLAGS |
| 102 | " -compatibility_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR} -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}") |
| 103 | endif() |
| 104 | |