83
|
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
|
|
5 set(SOURCES
|
|
6 libllvm.cpp
|
|
7 )
|
|
8
|
120
|
9 if(LLVM_LINK_LLVM_DYLIB AND LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
|
|
10 message(WARNING "Using LLVM_LINK_LLVM_DYLIB with LLVM_DYLIB_EXPORTED_SYMBOL_FILE may not work. Use at your own risk.")
|
|
11 endif()
|
83
|
12
|
147
|
13 if(LLVM_BUILD_LLVM_DYLIB)
|
|
14 if(MSVC)
|
|
15 message(FATAL_ERROR "Generating libLLVM is not supported on MSVC")
|
|
16 endif()
|
|
17
|
|
18 llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
|
|
19
|
|
20 # libLLVM.so should not have any dependencies on any other LLVM
|
|
21 # shared libraries. When using the "all" pseudo-component,
|
|
22 # LLVM_AVAILABLE_LIBS is added to the dependencies, which may
|
|
23 # contain shared libraries (e.g. libLTO).
|
|
24 #
|
|
25 # Also exclude libLLVMTableGen for the following reasons:
|
|
26 # - it is only used by internal *-tblgen utilities;
|
|
27 # - it pollutes the global options space.
|
|
28 foreach(lib ${LIB_NAMES})
|
|
29 get_target_property(t ${lib} TYPE)
|
|
30 if("${lib}" STREQUAL "LLVMTableGen")
|
|
31 elseif("x${t}" STREQUAL "xSTATIC_LIBRARY")
|
|
32 list(APPEND FILTERED_LIB_NAMES ${lib})
|
|
33 endif()
|
|
34 endforeach()
|
|
35 set(LIB_NAMES ${FILTERED_LIB_NAMES})
|
|
36
|
|
37 if(LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
|
|
38 set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})
|
|
39 add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
|
|
40 endif()
|
|
41
|
|
42 if (LLVM_LINK_LLVM_DYLIB)
|
|
43 set(INSTALL_WITH_TOOLCHAIN INSTALL_WITH_TOOLCHAIN)
|
120
|
44 endif()
|
147
|
45 add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB SONAME ${INSTALL_WITH_TOOLCHAIN} ${SOURCES})
|
|
46
|
|
47 list(REMOVE_DUPLICATES LIB_NAMES)
|
|
48 if(("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") OR (MINGW) OR (HAIKU)
|
|
49 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
|
|
50 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "GNU")
|
|
51 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "OpenBSD")
|
|
52 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
|
|
53 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "DragonFly")
|
|
54 OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")) # FIXME: It should be "GNU ld for elf"
|
|
55 configure_file(
|
|
56 ${CMAKE_CURRENT_SOURCE_DIR}/simple_version_script.map.in
|
|
57 ${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map)
|
95
|
58
|
147
|
59 # GNU ld doesn't resolve symbols in the version script.
|
|
60 set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
|
|
61 if (NOT LLVM_LINKER_IS_SOLARISLD AND NOT MINGW)
|
|
62 # Solaris ld does not accept global: *; so there is no way to version *all* global symbols
|
|
63 set(LIB_NAMES -Wl,--version-script,${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map ${LIB_NAMES})
|
|
64 endif()
|
|
65 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
|
|
66 set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
|
|
67 endif()
|
|
68
|
|
69 target_link_libraries(LLVM PRIVATE ${LIB_NAMES})
|
|
70
|
|
71 if (APPLE)
|
|
72 set_property(TARGET LLVM APPEND_STRING PROPERTY
|
|
73 LINK_FLAGS
|
|
74 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")
|
|
75 endif()
|
|
76
|
|
77 if(TARGET libLLVMExports)
|
|
78 add_dependencies(LLVM libLLVMExports)
|
|
79 endif()
|
83
|
80 endif()
|
|
81
|
147
|
82 if(LLVM_BUILD_LLVM_C_DYLIB AND NOT MSVC)
|
100
|
83 if(NOT APPLE)
|
|
84 message(FATAL_ERROR "Generating libLLVM-c is only supported on Darwin")
|
|
85 endif()
|
|
86
|
147
|
87 if(NOT LLVM_BUILD_LLVM_DYLIB)
|
|
88 message(FATAL_ERROR "Generating libLLVM-c requires LLVM_BUILD_LLVM_C_DYLIB on Darwin")
|
|
89 endif()
|
|
90
|
|
91 # To get the export list for a single llvm library:
|
|
92 # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports
|
|
93
|
100
|
94 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm-c.exports)
|
|
95
|
|
96 set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
|
|
97 set(LIB_NAME ${LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}LLVM)
|
|
98 set(LIB_PATH ${LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})
|
|
99 set(LIB_EXPORTS_PATH ${LIB_NAME}.exports)
|
|
100 list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH})
|
|
101
|
|
102 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
|
|
103 COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LLVM_EXPORTED_SYMBOL_FILE}
|
|
104 WORKING_DIRECTORY ${LIB_DIR}
|
|
105 DEPENDS LLVM
|
|
106 COMMENT "Generating Export list for LLVM..."
|
|
107 VERBATIM )
|
|
108
|
|
109 add_custom_target(libLLVMCExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
|
|
110
|
147
|
111 add_llvm_library(LLVM-C SHARED ${SOURCES} INSTALL_WITH_TOOLCHAIN)
|
100
|
112
|
|
113 target_link_libraries(LLVM-C PUBLIC LLVM)
|
|
114 add_dependencies(LLVM-C libLLVMCExports)
|
|
115
|
|
116 set_property(TARGET LLVM-C APPEND_STRING PROPERTY
|
83
|
117 LINK_FLAGS
|
100
|
118 " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH} -Wl,-reexport_library ${LIB_PATH}")
|
83
|
119 endif()
|
|
120
|
147
|
121 if(MSVC)
|
|
122 # Build the LLVM-C.dll library that exports the C API.
|
|
123
|
|
124 set(LLVM_LINK_COMPONENTS
|
|
125 ${LLVM_DYLIB_COMPONENTS}
|
|
126 )
|
|
127
|
|
128 llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
|
|
129 list(REMOVE_DUPLICATES LIB_NAMES)
|
|
130
|
|
131 # The python script needs to know whether symbols are prefixed with underscores or not.
|
|
132 if(LLVM_HOST_TRIPLE STREQUAL "i686-pc-win32")
|
|
133 set(GEN_UNDERSCORE "--underscore")
|
|
134 else()
|
|
135 set(GEN_UNDERSCORE "")
|
|
136 endif()
|
|
137
|
|
138 # Set this name here, not used in multi conf loop,
|
|
139 # but add script will pick the right one.
|
|
140 set(LIBSFILE ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.args)
|
|
141
|
|
142 # Get the full name to the libs so the python script understands them.
|
|
143 foreach(lib ${LIB_NAMES})
|
|
144 list(APPEND FULL_LIB_NAMES ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/${lib}.lib)
|
|
145 endforeach()
|
|
146
|
|
147 # Need to seperate lib names with newlines.
|
|
148 string(REPLACE ";" "\n" FILE_CONTENT "${FULL_LIB_NAMES}")
|
|
149
|
|
150 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
|
|
151 foreach(BUILD_MODE ${CMAKE_CONFIGURATION_TYPES})
|
|
152 # Replace the special string with a per config directory.
|
|
153 string(REPLACE ${CMAKE_CFG_INTDIR} ${BUILD_MODE} PER_CONF_CONTENT "${FILE_CONTENT}")
|
|
154
|
|
155 # Write out the full lib names into file to be read by the python script.
|
|
156 # One libsfile per build, the add_custom_command should expand
|
|
157 # ${CMAKE_CFG_INTDIR} correctly and select the right one.
|
|
158 file(WRITE ${CMAKE_BINARY_DIR}/${BUILD_MODE}/libllvm-c.args "${PER_CONF_CONTENT}")
|
|
159 endforeach()
|
|
160 else()
|
|
161 # Write out the full lib names into file to be read by the python script.
|
|
162 file(WRITE ${LIBSFILE} "${FILE_CONTENT}")
|
|
163 endif()
|
|
164
|
|
165 # Generate the exports file dynamically.
|
|
166 set(GEN_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/gen-msvc-exports.py)
|
|
167
|
|
168 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.exports)
|
|
169
|
|
170 if(CMAKE_CROSSCOMPILING)
|
|
171 build_native_tool(llvm-nm llvm_nm)
|
|
172 set(llvm_nm_target "${llvm_nm}")
|
|
173 else()
|
|
174 set(llvm_nm $<TARGET_FILE:llvm-nm>)
|
|
175 set(llvm_nm_target llvm-nm)
|
|
176 endif()
|
|
177
|
|
178 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
|
|
179 COMMAND ${PYTHON_EXECUTABLE} ${GEN_SCRIPT} --libsfile ${LIBSFILE} ${GEN_UNDERSCORE} --nm "${llvm_nm}" -o ${LLVM_EXPORTED_SYMBOL_FILE}
|
|
180 DEPENDS ${LIB_NAMES} ${llvm_nm_target}
|
|
181 COMMENT "Generating export list for LLVM-C"
|
|
182 VERBATIM )
|
|
183
|
|
184 # Finally link the target.
|
|
185 add_llvm_library(LLVM-C SHARED ${SOURCES} DEPENDS intrinsics_gen)
|
|
186
|
|
187 endif()
|