d9bd15f76248169b56d802781c927f2ff414884f
[oota-llvm.git] / tools / llvm-shlib / CMakeLists.txt
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 add_definitions( -DLLVM_VERSION_INFO=\"${PACKAGE_VERSION}\" )
6
7 set(SOURCES
8   libllvm.cpp
9   )
10
11 llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
12
13 if(LLVM_LINK_LLVM_DYLIB)
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
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
36 if(NOT DEFINED LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
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
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})
54
55       
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 )
62     endforeach ()
63   endif()
64
65   if (LLVM_DYLIB_EXPORT_ALL)
66     add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
67       COMMAND echo \"LLVM*\" > ${LLVM_EXPORTED_SYMBOL_FILE} && echo \"_Z*llvm*\" >> ${LLVM_EXPORTED_SYMBOL_FILE}
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()
78
79   add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
80 else()
81   set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})
82   add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
83 endif()
84
85 add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB ${SOURCES})
86
87 list(REMOVE_DUPLICATES LIB_NAMES)
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.
90   set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
91 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
92   set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
93 endif()
94
95 target_link_libraries(LLVM PRIVATE ${LIB_NAMES})
96
97 add_dependencies(LLVM libLLVMExports)
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