blob: 7415722d3f6bf32418af34719f94167cc683885f [file] [log] [blame]
Chris Bienemanadffd012014-10-23 17:22:14 +00001# 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
Andrew Wilkins92113962015-09-01 03:14:31 +00005if(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")
11 endif()
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")
14 endif()
15 set(LLVM_DYLIB_COMPONENTS all)
16endif()
Chris Bienemanadffd012014-10-23 17:22:14 +000017
Andrew Wilkins92113962015-09-01 03:14:31 +000018# 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.
Chris Bienemanadffd012014-10-23 17:22:14 +000022if(NOT DEFINED LLVM_DYLIB_COMPONENTS)
NAKAMURA Takumic8abc4c2014-11-10 15:04:02 +000023 set(LLVM_DYLIB_COMPONENTS
Chris Bienemanadffd012014-10-23 17:22:14 +000024 ${LLVM_TARGETS_TO_BUILD}
25 Analysis
NAKAMURA Takumi42397892014-11-08 14:12:30 +000026 BitReader
Chris Bienemanadffd012014-10-23 17:22:14 +000027 BitWriter
28 CodeGen
29 Core
Keno Fischer7ddd5012015-07-13 19:41:51 +000030 DebugInfoDWARF
31 DebugInfoPDB
Chris Bienemanadffd012014-10-23 17:22:14 +000032 ExecutionEngine
Chris Bienemanadffd012014-10-23 17:22:14 +000033 IPO
34 IRReader
35 InstCombine
36 Instrumentation
37 Interpreter
38 Linker
NAKAMURA Takumi42397892014-11-08 14:12:30 +000039 MCDisassembler
Chris Bienemanadffd012014-10-23 17:22:14 +000040 MCJIT
41 ObjCARCOpts
42 Object
43 ScalarOpts
Chris Bienemanadffd012014-10-23 17:22:14 +000044 Support
45 Target
46 TransformUtils
47 Vectorize
48 native
49 )
Chris Bienemanadffd012014-10-23 17:22:14 +000050endif()
51
52add_definitions( -DLLVM_VERSION_INFO=\"${PACKAGE_VERSION}\" )
53
54set(SOURCES
55 libllvm.cpp
56 )
57
Chris Bieneman168ddf42015-04-13 21:29:46 +000058llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
59
Andrew Wilkins92113962015-09-01 03:14:31 +000060if(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).
65 #
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})
74 endif()
75 endforeach()
76 set(LIB_NAMES ${FILTERED_LIB_NAMES})
77endif()
78
Chris Bieneman168ddf42015-04-13 21:29:46 +000079if(NOT DEFINED LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
Chris Bienemanadffd012014-10-23 17:22:14 +000080
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.")
83 endif()
84
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
87
88 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm.exports)
89
Chris Bieneman3ee81962015-04-16 16:56:22 +000090 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})
Chris Bienemanadffd012014-10-23 17:22:14 +000097
Chris Bieneman3ee81962015-04-16 16:56:22 +000098
Chris Bieneman168ddf42015-04-13 21:29:46 +000099 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}
102 DEPENDS ${lib}
103 COMMENT "Generating Export list for ${lib}..."
104 VERBATIM )
Chris Bieneman3ee81962015-04-16 16:56:22 +0000105 endforeach ()
106 endif()
Chris Bienemanadffd012014-10-23 17:22:14 +0000107
Chris Bieneman3ee81962015-04-16 16:56:22 +0000108 if (LLVM_DYLIB_EXPORT_ALL)
109 add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
Chris Bienemanc82d0ca2015-04-16 21:58:22 +0000110 COMMAND echo \"LLVM*\" > ${LLVM_EXPORTED_SYMBOL_FILE} && echo \"_Z*llvm*\" >> ${LLVM_EXPORTED_SYMBOL_FILE}
Chris Bieneman3ee81962015-04-16 16:56:22 +0000111 WORKING_DIRECTORY ${LIB_DIR}
112 DEPENDS ${LLVM_DYLIB_REQUIRED_EXPORTS}
113 COMMENT "Generating combined export list...")
114 else()
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...")
120 endif()
Chris Bienemanadffd012014-10-23 17:22:14 +0000121
Chris Bieneman11fd9d62015-02-18 18:52:11 +0000122 add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
Chris Bieneman168ddf42015-04-13 21:29:46 +0000123else()
124 set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})
125 add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
Chris Bienemanadffd012014-10-23 17:22:14 +0000126endif()
127
Andrew Wilkins92113962015-09-01 03:14:31 +0000128add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB ${SOURCES})
Chris Bienemanadffd012014-10-23 17:22:14 +0000129
Chris Bieneman3ee81962015-04-16 16:56:22 +0000130list(REMOVE_DUPLICATES LIB_NAMES)
NAKAMURA Takumic8abc4c2014-11-10 15:04:02 +0000131if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") # FIXME: It should be "GNU ld for elf"
132 # GNU ld doesn't resolve symbols in the version script.
NAKAMURA Takumic8abc4c2014-11-10 15:04:02 +0000133 set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
Chris Bieneman3ee81962015-04-16 16:56:22 +0000134elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
135 set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
NAKAMURA Takumic8abc4c2014-11-10 15:04:02 +0000136endif()
137
Chris Bieneman6a1b54a2015-03-23 20:03:57 +0000138target_link_libraries(LLVM PRIVATE ${LIB_NAMES})
NAKAMURA Takumic8abc4c2014-11-10 15:04:02 +0000139
Chris Bieneman11fd9d62015-02-18 18:52:11 +0000140add_dependencies(LLVM libLLVMExports)
Chris Bienemanadffd012014-10-23 17:22:14 +0000141
142if (APPLE)
143 set_property(TARGET LLVM APPEND_STRING PROPERTY
144 LINK_FLAGS
145 " -compatibility_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR} -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")
146endif()
147