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.
5 if(LLVM_LINK_LLVM_DYLIB)
6 if(DEFINED LLVM_DYLIB_COMPONENTS)
7 # To avoid inscrutable link errors, just disallow setting
8 # LLVM_DYLIB_COMPONENTS when we're intending to link tools
9 # and shared libraries with the dylib.
10 message(FATAL_ERROR "LLVM_DYLIB_COMPONENTS must not be set when LLVM_LINK_LLVM_DYLIB is ON")
12 if(NOT LLVM_DYLIB_EXPORT_ALL)
13 message(FATAL_ERROR "LLVM_DYLIB_EXPORT_ALL must be ON when LLVM_LINK_LLVM_DYLIB is ON")
15 set(LLVM_DYLIB_COMPONENTS all)
18 # If LLVM_LINK_LLVM_DYLIB is not OFF, you can configure which libraries from
19 # LLVM you want to include in the shared library by setting
20 # LLVM_DYLIB_COMPONENTS to a semi-colon delimited list of LLVM components.
21 # All component names handled by llvm-config are valid.
22 if(NOT DEFINED LLVM_DYLIB_COMPONENTS)
23 set(LLVM_DYLIB_COMPONENTS
24 ${LLVM_TARGETS_TO_BUILD}
52 add_definitions( -DLLVM_VERSION_INFO=\"${PACKAGE_VERSION}\" )
58 llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
60 if(LLVM_LINK_LLVM_DYLIB)
61 # libLLVM.so should not have any dependencies on any other LLVM
62 # shared libraries. When using the "all" pseudo-component,
63 # LLVM_AVAILABLE_LIBS is added to the dependencies, which may
64 # contain shared libraries (e.g. libLTO).
66 # Also exclude libLLVMTableGen for the following reasons:
67 # - it is only used by internal *-tblgen utilities;
68 # - it pollutes the global options space.
69 foreach(lib ${LIB_NAMES})
70 get_target_property(t ${lib} TYPE)
71 if("${lib}" STREQUAL "LLVMTableGen")
72 elseif("x${t}" STREQUAL "xSTATIC_LIBRARY")
73 list(APPEND FILTERED_LIB_NAMES ${lib})
76 set(LIB_NAMES ${FILTERED_LIB_NAMES})
79 if(NOT DEFINED LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
81 if( WIN32 AND NOT CYGWIN )
82 message(FATAL_ERROR "Auto-generation not implemented for Win32 without GNU utils. Please specify LLVM_EXPORTED_SYMBOL_FILE.")
85 # To get the export list for a single llvm library:
86 # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports
88 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm.exports)
90 if (NOT LLVM_DYLIB_EXPORT_ALL)
91 foreach (lib ${LIB_NAMES})
92 set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
93 set(LIB_NAME ${LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${lib})
94 set(LIB_PATH ${LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
95 set(LIB_EXPORTS_PATH ${LIB_NAME}.exports)
96 list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH})
99 add_custom_command(OUTPUT ${LIB_EXPORTS_PATH}
100 COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_EXPORTS_PATH}
101 WORKING_DIRECTORY ${LIB_DIR}
103 COMMENT "Generating Export list for ${lib}..."
108 if (LLVM_DYLIB_EXPORT_ALL)
109 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
110 COMMAND echo \"LLVM*\" > ${LLVM_EXPORTED_SYMBOL_FILE} && echo \"_Z*llvm*\" >> ${LLVM_EXPORTED_SYMBOL_FILE}
111 WORKING_DIRECTORY ${LIB_DIR}
112 DEPENDS ${LLVM_DYLIB_REQUIRED_EXPORTS}
113 COMMENT "Generating combined export list...")
115 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
116 COMMAND cat ${LLVM_DYLIB_REQUIRED_EXPORTS} > ${LLVM_EXPORTED_SYMBOL_FILE}
117 WORKING_DIRECTORY ${LIB_DIR}
118 DEPENDS ${LLVM_DYLIB_REQUIRED_EXPORTS}
119 COMMENT "Generating combined export list...")
122 add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
124 set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})
125 add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
128 add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB ${SOURCES})
130 list(REMOVE_DUPLICATES LIB_NAMES)
131 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") # FIXME: It should be "GNU ld for elf"
132 # GNU ld doesn't resolve symbols in the version script.
133 set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
134 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
135 set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
138 target_link_libraries(LLVM PRIVATE ${LIB_NAMES})
140 add_dependencies(LLVM libLLVMExports)
143 set_property(TARGET LLVM APPEND_STRING PROPERTY
145 " -compatibility_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR} -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")