xref: /aosp_15_r20/external/llvm/tools/llvm-shlib/CMakeLists.txt (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker# This tool creates a shared library from the LLVM libraries. Generating this
2*9880d681SAndroid Build Coastguard Worker# library is enabled by setting LLVM_BUILD_LLVM_DYLIB=yes on the CMake
3*9880d681SAndroid Build Coastguard Worker# commandline. By default the shared library only exports the LLVM C API.
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Workerset(SOURCES
6*9880d681SAndroid Build Coastguard Worker  libllvm.cpp
7*9880d681SAndroid Build Coastguard Worker  )
8*9880d681SAndroid Build Coastguard Worker
9*9880d681SAndroid Build Coastguard Workerllvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard Workerif(LLVM_LINK_LLVM_DYLIB)
12*9880d681SAndroid Build Coastguard Worker  if(LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
13*9880d681SAndroid Build Coastguard Worker    message(WARNING "Using LLVM_LINK_LLVM_DYLIB with LLVM_DYLIB_EXPORTED_SYMBOL_FILE may not work. Use at your own risk.")
14*9880d681SAndroid Build Coastguard Worker  endif()
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker  # libLLVM.so should not have any dependencies on any other LLVM
17*9880d681SAndroid Build Coastguard Worker  # shared libraries. When using the "all" pseudo-component,
18*9880d681SAndroid Build Coastguard Worker  # LLVM_AVAILABLE_LIBS is added to the dependencies, which may
19*9880d681SAndroid Build Coastguard Worker  # contain shared libraries (e.g. libLTO).
20*9880d681SAndroid Build Coastguard Worker  #
21*9880d681SAndroid Build Coastguard Worker  # Also exclude libLLVMTableGen for the following reasons:
22*9880d681SAndroid Build Coastguard Worker  #  - it is only used by internal *-tblgen utilities;
23*9880d681SAndroid Build Coastguard Worker  #  - it pollutes the global options space.
24*9880d681SAndroid Build Coastguard Worker  foreach(lib ${LIB_NAMES})
25*9880d681SAndroid Build Coastguard Worker    get_target_property(t ${lib} TYPE)
26*9880d681SAndroid Build Coastguard Worker    if("${lib}" STREQUAL "LLVMTableGen")
27*9880d681SAndroid Build Coastguard Worker    elseif("x${t}" STREQUAL "xSTATIC_LIBRARY")
28*9880d681SAndroid Build Coastguard Worker      list(APPEND FILTERED_LIB_NAMES ${lib})
29*9880d681SAndroid Build Coastguard Worker    endif()
30*9880d681SAndroid Build Coastguard Worker  endforeach()
31*9880d681SAndroid Build Coastguard Worker  set(LIB_NAMES ${FILTERED_LIB_NAMES})
32*9880d681SAndroid Build Coastguard Workerendif()
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Workerif(LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
35*9880d681SAndroid Build Coastguard Worker  set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})
36*9880d681SAndroid Build Coastguard Worker  add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
37*9880d681SAndroid Build Coastguard Workerendif()
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Workeradd_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB SONAME ${SOURCES})
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Workerlist(REMOVE_DUPLICATES LIB_NAMES)
42*9880d681SAndroid Build Coastguard Workerif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") # FIXME: It should be "GNU ld for elf"
43*9880d681SAndroid Build Coastguard Worker  # GNU ld doesn't resolve symbols in the version script.
44*9880d681SAndroid Build Coastguard Worker  set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
45*9880d681SAndroid Build Coastguard Workerelseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
46*9880d681SAndroid Build Coastguard Worker  set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
47*9880d681SAndroid Build Coastguard Workerendif()
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Workertarget_link_libraries(LLVM PRIVATE ${LIB_NAMES})
50*9880d681SAndroid Build Coastguard Worker
51*9880d681SAndroid Build Coastguard Workerif (APPLE)
52*9880d681SAndroid Build Coastguard Worker  set_property(TARGET LLVM APPEND_STRING PROPERTY
53*9880d681SAndroid Build Coastguard Worker              LINK_FLAGS
54*9880d681SAndroid Build Coastguard Worker              " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")
55*9880d681SAndroid Build Coastguard Workerendif()
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard Workerif(TARGET libLLVMExports)
58*9880d681SAndroid Build Coastguard Worker  add_dependencies(LLVM libLLVMExports)
59*9880d681SAndroid Build Coastguard Workerendif()
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Workerif(LLVM_BUILD_LLVM_C_DYLIB)
62*9880d681SAndroid Build Coastguard Worker  # To get the export list for a single llvm library:
63*9880d681SAndroid Build Coastguard Worker  # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker  if(NOT APPLE)
66*9880d681SAndroid Build Coastguard Worker    message(FATAL_ERROR "Generating libLLVM-c is only supported on Darwin")
67*9880d681SAndroid Build Coastguard Worker  endif()
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker  set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm-c.exports)
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker  set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
72*9880d681SAndroid Build Coastguard Worker  set(LIB_NAME ${LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}LLVM)
73*9880d681SAndroid Build Coastguard Worker  set(LIB_PATH ${LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})
74*9880d681SAndroid Build Coastguard Worker  set(LIB_EXPORTS_PATH ${LIB_NAME}.exports)
75*9880d681SAndroid Build Coastguard Worker  list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH})
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker  add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
78*9880d681SAndroid Build Coastguard Worker    COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LLVM_EXPORTED_SYMBOL_FILE}
79*9880d681SAndroid Build Coastguard Worker    WORKING_DIRECTORY ${LIB_DIR}
80*9880d681SAndroid Build Coastguard Worker    DEPENDS LLVM
81*9880d681SAndroid Build Coastguard Worker    COMMENT "Generating Export list for LLVM..."
82*9880d681SAndroid Build Coastguard Worker    VERBATIM )
83*9880d681SAndroid Build Coastguard Worker
84*9880d681SAndroid Build Coastguard Worker  add_custom_target(libLLVMCExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Worker  add_llvm_library(LLVM-C SHARED ${SOURCES})
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker  target_link_libraries(LLVM-C PUBLIC LLVM)
89*9880d681SAndroid Build Coastguard Worker  add_dependencies(LLVM-C libLLVMCExports)
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker  set_property(TARGET LLVM-C APPEND_STRING PROPERTY
92*9880d681SAndroid Build Coastguard Worker              LINK_FLAGS
93*9880d681SAndroid Build Coastguard Worker              " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH} -Wl,-reexport_library ${LIB_PATH}")
94*9880d681SAndroid Build Coastguard Workerendif()
95*9880d681SAndroid Build Coastguard Worker
96