1*0a6a1f1dSLionel Sambuc# This tool creates a shared library from the LLVM libraries. Generating this
2*0a6a1f1dSLionel Sambuc# library is enabled by setting LLVM_BUILD_LLVM_DYLIB=yes on the CMake
3*0a6a1f1dSLionel Sambuc# commandline. By default the shared library only exports the LLVM C API.
4*0a6a1f1dSLionel Sambuc
5*0a6a1f1dSLionel Sambuc
6*0a6a1f1dSLionel Sambuc# You can configure which libraries from LLVM you want to include in the shared
7*0a6a1f1dSLionel Sambuc# library by setting LLVM_DYLIB_COMPONENTS to a semi-colon delimited list of
8*0a6a1f1dSLionel Sambuc# LLVM components. All compoenent names handled by llvm-config are valid.
9*0a6a1f1dSLionel Sambuc
10*0a6a1f1dSLionel Sambucif(NOT DEFINED LLVM_DYLIB_COMPONENTS)
11*0a6a1f1dSLionel Sambuc  set(LLVM_DYLIB_COMPONENTS
12*0a6a1f1dSLionel Sambuc    ${LLVM_TARGETS_TO_BUILD}
13*0a6a1f1dSLionel Sambuc    Analysis
14*0a6a1f1dSLionel Sambuc    BitReader
15*0a6a1f1dSLionel Sambuc    BitWriter
16*0a6a1f1dSLionel Sambuc    CodeGen
17*0a6a1f1dSLionel Sambuc    Core
18*0a6a1f1dSLionel Sambuc    ExecutionEngine
19*0a6a1f1dSLionel Sambuc    IPA
20*0a6a1f1dSLionel Sambuc    IPO
21*0a6a1f1dSLionel Sambuc    IRReader
22*0a6a1f1dSLionel Sambuc    InstCombine
23*0a6a1f1dSLionel Sambuc    Instrumentation
24*0a6a1f1dSLionel Sambuc    Interpreter
25*0a6a1f1dSLionel Sambuc    Linker
26*0a6a1f1dSLionel Sambuc    MCDisassembler
27*0a6a1f1dSLionel Sambuc    MCJIT
28*0a6a1f1dSLionel Sambuc    ObjCARCOpts
29*0a6a1f1dSLionel Sambuc    Object
30*0a6a1f1dSLionel Sambuc    ScalarOpts
31*0a6a1f1dSLionel Sambuc    Support
32*0a6a1f1dSLionel Sambuc    Target
33*0a6a1f1dSLionel Sambuc    TransformUtils
34*0a6a1f1dSLionel Sambuc    Vectorize
35*0a6a1f1dSLionel Sambuc    native
36*0a6a1f1dSLionel Sambuc    )
37*0a6a1f1dSLionel Sambucendif()
38*0a6a1f1dSLionel Sambuc
39*0a6a1f1dSLionel Sambucadd_definitions( -DLLVM_VERSION_INFO=\"${PACKAGE_VERSION}\" )
40*0a6a1f1dSLionel Sambuc
41*0a6a1f1dSLionel Sambucset(SOURCES
42*0a6a1f1dSLionel Sambuc  libllvm.cpp
43*0a6a1f1dSLionel Sambuc  )
44*0a6a1f1dSLionel Sambuc
45*0a6a1f1dSLionel Sambucif(NOT DEFINED LLVM_EXPORTED_SYMBOL_FILE)
46*0a6a1f1dSLionel Sambuc
47*0a6a1f1dSLionel Sambuc  if( WIN32 AND NOT CYGWIN )
48*0a6a1f1dSLionel Sambuc    message(FATAL_ERROR "Auto-generation not implemented for Win32 without GNU utils. Please specify LLVM_EXPORTED_SYMBOL_FILE.")
49*0a6a1f1dSLionel Sambuc  endif()
50*0a6a1f1dSLionel Sambuc
51*0a6a1f1dSLionel Sambuc  # To get the export list for a single llvm library:
52*0a6a1f1dSLionel Sambuc  # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports
53*0a6a1f1dSLionel Sambuc
54*0a6a1f1dSLionel Sambuc  set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm.exports)
55*0a6a1f1dSLionel Sambuc
56*0a6a1f1dSLionel Sambuc  llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
57*0a6a1f1dSLionel Sambuc
58*0a6a1f1dSLionel Sambuc  foreach (lib ${LIB_NAMES})
59*0a6a1f1dSLionel Sambuc
60*0a6a1f1dSLionel Sambuc    set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
61*0a6a1f1dSLionel Sambuc    set(LIB_NAME ${LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${lib})
62*0a6a1f1dSLionel Sambuc    set(LIB_PATH ${LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
63*0a6a1f1dSLionel Sambuc    set(LIB_EXPORTS_PATH ${LIB_NAME}.exports)
64*0a6a1f1dSLionel Sambuc
65*0a6a1f1dSLionel Sambuc    list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH})
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc    add_custom_command(OUTPUT ${LIB_EXPORTS_PATH}
68*0a6a1f1dSLionel Sambuc      COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_EXPORTS_PATH}
69*0a6a1f1dSLionel Sambuc      WORKING_DIRECTORY ${LIB_DIR}
70*0a6a1f1dSLionel Sambuc      DEPENDS ${lib}
71*0a6a1f1dSLionel Sambuc      COMMENT "Generating Export list for ${lib}..."
72*0a6a1f1dSLionel Sambuc      VERBATIM )
73*0a6a1f1dSLionel Sambuc  endforeach ()
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc  add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
76*0a6a1f1dSLionel Sambuc    COMMAND cat ${LLVM_DYLIB_REQUIRED_EXPORTS} > ${LLVM_EXPORTED_SYMBOL_FILE}
77*0a6a1f1dSLionel Sambuc    WORKING_DIRECTORY ${LIB_DIR}
78*0a6a1f1dSLionel Sambuc    DEPENDS ${LLVM_DYLIB_REQUIRED_EXPORTS}
79*0a6a1f1dSLionel Sambuc    COMMENT "Generating combined export list...")
80*0a6a1f1dSLionel Sambuc
81*0a6a1f1dSLionel Sambucendif()
82*0a6a1f1dSLionel Sambuc
83*0a6a1f1dSLionel Sambucadd_llvm_library(LLVM SHARED ${SOURCES})
84*0a6a1f1dSLionel Sambuc
85*0a6a1f1dSLionel Sambucif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") # FIXME: It should be "GNU ld for elf"
86*0a6a1f1dSLionel Sambuc  # GNU ld doesn't resolve symbols in the version script.
87*0a6a1f1dSLionel Sambuc  list(REMOVE_DUPLICATES LIB_NAMES)
88*0a6a1f1dSLionel Sambuc  set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
89*0a6a1f1dSLionel Sambucendif()
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuctarget_link_libraries(LLVM ${cmake_2_8_12_PRIVATE} ${LIB_NAMES})
92*0a6a1f1dSLionel Sambuc
93*0a6a1f1dSLionel Sambucadd_dependencies(LLVM ${LLVM_EXPORTED_SYMBOL_FILE})
94*0a6a1f1dSLionel Sambuc
95*0a6a1f1dSLionel Sambucif (APPLE)
96*0a6a1f1dSLionel Sambuc  set_property(TARGET LLVM APPEND_STRING PROPERTY
97*0a6a1f1dSLionel Sambuc              LINK_FLAGS
98*0a6a1f1dSLionel Sambuc              " -compatibility_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR} -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")
99*0a6a1f1dSLionel Sambucendif()
100*0a6a1f1dSLionel Sambuc
101