brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.5 KiB · 53003d9 Raw
213 lines · plain
1# This tool creates a shared library from the LLVM libraries. Generating this2# library is enabled by setting LLVM_BUILD_LLVM_DYLIB=yes on the CMake3# commandline. By default the shared library only exports the LLVM C API.4 5set(SOURCES6  libllvm.cpp7  )8 9if(LLVM_LINK_LLVM_DYLIB AND LLVM_DYLIB_EXPORTED_SYMBOL_FILE)10  message(WARNING "Using LLVM_LINK_LLVM_DYLIB with LLVM_DYLIB_EXPORTED_SYMBOL_FILE may not work. Use at your own risk.")11endif()12 13if(LLVM_BUILD_LLVM_DYLIB)14  if(MSVC AND NOT LLVM_BUILD_LLVM_DYLIB_VIS)15    message(FATAL_ERROR "Generating libLLVM is not supported on MSVC")16  endif()17  if(ZOS)18    message(FATAL_ERROR "Generating libLLVM is not supported on z/OS")19  endif()20 21  llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})22 23  # Exclude libLLVMTableGen for the following reasons:24  #  - it is only used by internal *-tblgen utilities;25  #  - it pollutes the global options space.26  list(REMOVE_ITEM LIB_NAMES "LLVMTableGen")27 28  if(LLVM_DYLIB_EXPORTED_SYMBOL_FILE)29    set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})30    add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})31  endif()32 33  if (LLVM_LINK_LLVM_DYLIB)34    set(INSTALL_WITH_TOOLCHAIN INSTALL_WITH_TOOLCHAIN)35  endif()36  if (WIN32 OR CYGWIN)37    add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB SONAME ${INSTALL_WITH_TOOLCHAIN} ${SOURCES})38  else()39    add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB OUTPUT_NAME LLVM ${INSTALL_WITH_TOOLCHAIN} ${SOURCES})40    # Add symlink for backwards compatibility with old library name41    llvm_install_library_symlink(LLVM-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX} $<TARGET_FILE_NAME:LLVM> SHARED FULL_DEST COMPONENT LLVM)42  endif()43 44  if (MINGW OR CYGWIN)45    # The LLVM DLL is supposed to export all symbols (except for ones46    # that are explicitly hidden). Normally, this is what happens anyway, but47    # if there are symbols that are marked explicitly as dllexport, we'd only48    # export them and nothing else. Therefore, add --export-all-symbols to49    # make sure we export all symbols despite potential dllexports.50    target_link_options(LLVM PRIVATE LINKER:--export-all-symbols)51  endif()52 53  list(REMOVE_DUPLICATES LIB_NAMES)54  if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")55    set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})56  else()57    if("${CMAKE_CFG_INTDIR}" STREQUAL ".")58      configure_file(59        ${CMAKE_CURRENT_SOURCE_DIR}/simple_version_script.map.in60        ${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map)61    else()62      foreach(BUILD_MODE ${CMAKE_CONFIGURATION_TYPES})63        # Replace the special string with a per config directory.64        string(REPLACE ${CMAKE_CFG_INTDIR} ${BUILD_MODE} PER_CONF_LIBRARY_DIR ${LLVM_LIBRARY_DIR})65        configure_file(66          ${CMAKE_CURRENT_SOURCE_DIR}/simple_version_script.map.in67          ${PER_CONF_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map)68      endforeach()69    endif()70    if(MSVC)71      target_link_directories(LLVM PRIVATE ${LLVM_LIBRARY_DIR})72      foreach(library ${LIB_NAMES})73        target_link_options(LLVM PRIVATE /WHOLEARCHIVE:${library}.lib)74      endforeach()75    else()76      # GNU ld doesn't resolve symbols in the version script.77      set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)78      if (NOT LLVM_LINKER_IS_SOLARISLD AND NOT MINGW AND NOT CYGWIN)79        # Solaris ld does not accept global: *; so there is no way to version *all* global symbols80        set(LIB_NAMES -Wl,--version-script,${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map ${LIB_NAMES})81      endif()82      if (LLVM_LINKER_SUPPORTS_B_SYMBOLIC_FUNCTIONS)83        # Optimize function calls for default visibility definitions to avoid PLT and84        # reduce dynamic relocations.85        # Note: for -fno-pic default, the address of a function may be different from86        # inside and outside libLLVM.so.87        target_link_options(LLVM PRIVATE LINKER:-Bsymbolic-functions)88      endif()89    endif()90  endif()91 92  target_link_libraries(LLVM PRIVATE ${LIB_NAMES})93 94  if(LLVM_ENABLE_THREADS AND NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)95    target_link_libraries(LLVM PUBLIC atomic)96  endif()97 98  if (APPLE)99    set_property(TARGET LLVM APPEND_STRING PROPERTY100                LINK_FLAGS101                " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")102  endif()103 104  if(TARGET libLLVMExports)105    add_dependencies(LLVM libLLVMExports)106  endif()107endif()108 109if(LLVM_BUILD_LLVM_C_DYLIB AND NOT MSVC)110  if(NOT APPLE)111    message(FATAL_ERROR "Generating libLLVM-c is only supported on Darwin")112  endif()113 114  if(NOT LLVM_BUILD_LLVM_DYLIB)115    message(FATAL_ERROR "Generating libLLVM-c requires LLVM_BUILD_LLVM_C_DYLIB on Darwin")116  endif()117 118  # To get the export list for a single llvm library:119  # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports120 121  set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_BINARY_DIR}/libllvm-c.exports)122 123  set(LIB_DIR ${LLVM_LIBRARY_DIR})124  set(LIB_NAME ${LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}LLVM)125  set(LIB_PATH ${LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})126  set(LIB_EXPORTS_PATH ${LIB_NAME}.exports)127  list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH})128 129  add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}130    COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LLVM_EXPORTED_SYMBOL_FILE}131    WORKING_DIRECTORY ${LIB_DIR}132    DEPENDS LLVM133    COMMENT "Generating Export list for LLVM..."134    VERBATIM )135 136  add_custom_target(libLLVMCExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})137 138  add_llvm_library(LLVM-C SHARED ${SOURCES} INSTALL_WITH_TOOLCHAIN)139 140  target_link_libraries(LLVM-C PUBLIC LLVM)141  add_dependencies(LLVM-C libLLVMCExports)142 143  set_property(TARGET LLVM-C APPEND_STRING PROPERTY144              LINK_FLAGS145              " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH} -Wl,-reexport_library ${LIB_PATH}")146endif()147 148if(LLVM_BUILD_LLVM_C_DYLIB AND MSVC)149  # Build the LLVM-C.dll library that exports the C API.150 151  set(LLVM_LINK_COMPONENTS152    ${LLVM_DYLIB_COMPONENTS}153    )154 155  llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})156  list(REMOVE_DUPLICATES LIB_NAMES)157 158  # The python script needs to know whether symbols are prefixed with underscores or not.159  if(LLVM_HOST_TRIPLE MATCHES "i?86-.*win.*")160    set(GEN_UNDERSCORE "--underscore")161  else()162    set(GEN_UNDERSCORE "")163  endif()164 165  # Set this name here, not used in multi conf loop,166  # but add script will pick the right one.167  set(LIBSFILE ${LLVM_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.args)168 169  # Get the full name to the libs so the python script understands them.170  foreach(lib ${LIB_NAMES})171    list(APPEND FULL_LIB_NAMES ${LLVM_LIBRARY_DIR}/${lib}.lib)172  endforeach()173 174  # Need to separate lib names with newlines.175  string(REPLACE ";" "\n" FILE_CONTENT "${FULL_LIB_NAMES}")176 177  if("${CMAKE_CFG_INTDIR}" STREQUAL ".")178    # Write out the full lib names into file to be read by the python script.179    file(WRITE ${LIBSFILE} "${FILE_CONTENT}")180  else()181    foreach(BUILD_MODE ${CMAKE_CONFIGURATION_TYPES})182      # Replace the special string with a per config directory.183      string(REPLACE ${CMAKE_CFG_INTDIR} ${BUILD_MODE} PER_CONF_CONTENT "${FILE_CONTENT}")184 185      # Write out the full lib names into file to be read by the python script.186      # One libsfile per build, the add_custom_command should expand187      # ${CMAKE_CFG_INTDIR} correctly and select the right one.188      file(WRITE ${LLVM_BINARY_DIR}/${BUILD_MODE}/libllvm-c.args "${PER_CONF_CONTENT}")189    endforeach()190  endif()191 192  # Generate the exports file dynamically.193  set(GEN_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/gen-msvc-exports.py)194 195  set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.exports)196  get_host_tool_path(llvm-nm LLVM_NM llvm_nm_exe llvm_nm_target)197 198  add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}199    COMMAND "${Python3_EXECUTABLE}" ${GEN_SCRIPT} --libsfile ${LIBSFILE} ${GEN_UNDERSCORE} --nm "${llvm_nm_exe}" -o ${LLVM_EXPORTED_SYMBOL_FILE}200    DEPENDS ${LIB_NAMES} ${llvm_nm_target}201    COMMENT "Generating export list for LLVM-C"202    VERBATIM )203 204  # Finally link the target.205  add_llvm_library(LLVM-C SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES} DEPENDS intrinsics_gen)206 207  if (LLVM_INTEGRATED_CRT_ALLOC AND MSVC)208    # Make sure we search LLVMSupport first, before the CRT libs209    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_CXX_LINKER_WRAPPER_FLAG}-INCLUDE:malloc")210  endif()211  212endif()213