[CMake] libLLVM's compatibility version should be 1.
[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   # To get the export list for a single llvm library:
38   # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports
39
40   if (NOT LLVM_DYLIB_EXPORT_ALL)
41     if( WIN32 AND NOT CYGWIN )
42       message(FATAL_ERROR "Auto-generation not implemented for Win32 without GNU utils. Please specify LLVM_EXPORTED_SYMBOL_FILE.")
43     endif()
44
45     set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm.exports)
46
47     foreach (lib ${LIB_NAMES})
48       set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
49       set(LIB_NAME ${LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${lib})
50       set(LIB_PATH ${LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
51       set(LIB_EXPORTS_PATH ${LIB_NAME}.exports)
52       list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH})
53
54       
55       add_custom_command(OUTPUT ${LIB_EXPORTS_PATH}
56         COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_EXPORTS_PATH}
57         WORKING_DIRECTORY ${LIB_DIR}
58         DEPENDS ${lib}
59         COMMENT "Generating Export list for ${lib}..."
60         VERBATIM )
61     endforeach ()
62
63     add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
64       COMMAND cat ${LLVM_DYLIB_REQUIRED_EXPORTS} > ${LLVM_EXPORTED_SYMBOL_FILE}
65       WORKING_DIRECTORY ${LIB_DIR}
66       DEPENDS ${LLVM_DYLIB_REQUIRED_EXPORTS}
67       COMMENT "Generating combined export list...")
68     add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
69   endif()
70 else()
71   set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})
72   add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
73 endif()
74
75 add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB ${SOURCES})
76
77 list(REMOVE_DUPLICATES LIB_NAMES)
78 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") # FIXME: It should be "GNU ld for elf"
79   # GNU ld doesn't resolve symbols in the version script.
80   set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
81 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
82   set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
83 endif()
84
85 target_link_libraries(LLVM PRIVATE ${LIB_NAMES})
86
87 if(TARGET libLLVMExports)
88   add_dependencies(LLVM libLLVMExports)
89 endif()
90
91 if (APPLE)
92   set_property(TARGET LLVM APPEND_STRING PROPERTY
93               LINK_FLAGS
94               " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")
95 endif()
96