diff tools/llvm-shlib/CMakeLists.txt @ 147:c2174574ed3a

LLVM 10
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 16:55:33 +0900
parents 803732b1fca8
children
line wrap: on
line diff
--- a/tools/llvm-shlib/CMakeLists.txt	Sat Feb 17 09:57:20 2018 +0900
+++ b/tools/llvm-shlib/CMakeLists.txt	Wed Aug 14 16:55:33 2019 +0900
@@ -6,79 +6,91 @@
   libllvm.cpp
   )
 
-llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
-
 if(LLVM_LINK_LLVM_DYLIB AND LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
   message(WARNING "Using LLVM_LINK_LLVM_DYLIB with LLVM_DYLIB_EXPORTED_SYMBOL_FILE may not work. Use at your own risk.")
 endif()
 
-# libLLVM.so should not have any dependencies on any other LLVM
-# shared libraries. When using the "all" pseudo-component,
-# LLVM_AVAILABLE_LIBS is added to the dependencies, which may
-# contain shared libraries (e.g. libLTO).
-#
-# Also exclude libLLVMTableGen for the following reasons:
-#  - it is only used by internal *-tblgen utilities;
-#  - it pollutes the global options space.
-foreach(lib ${LIB_NAMES})
-  get_target_property(t ${lib} TYPE)
-  if("${lib}" STREQUAL "LLVMTableGen")
-  elseif("x${t}" STREQUAL "xSTATIC_LIBRARY")
-    list(APPEND FILTERED_LIB_NAMES ${lib})
+if(LLVM_BUILD_LLVM_DYLIB)
+  if(MSVC)
+    message(FATAL_ERROR "Generating libLLVM is not supported on MSVC")
+  endif()
+
+  llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
+
+  # libLLVM.so should not have any dependencies on any other LLVM
+  # shared libraries. When using the "all" pseudo-component,
+  # LLVM_AVAILABLE_LIBS is added to the dependencies, which may
+  # contain shared libraries (e.g. libLTO).
+  #
+  # Also exclude libLLVMTableGen for the following reasons:
+  #  - it is only used by internal *-tblgen utilities;
+  #  - it pollutes the global options space.
+  foreach(lib ${LIB_NAMES})
+    get_target_property(t ${lib} TYPE)
+    if("${lib}" STREQUAL "LLVMTableGen")
+    elseif("x${t}" STREQUAL "xSTATIC_LIBRARY")
+      list(APPEND FILTERED_LIB_NAMES ${lib})
+    endif()
+  endforeach()
+  set(LIB_NAMES ${FILTERED_LIB_NAMES})
+
+  if(LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
+    set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})
+    add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
+  endif()
+
+  if (LLVM_LINK_LLVM_DYLIB)
+    set(INSTALL_WITH_TOOLCHAIN INSTALL_WITH_TOOLCHAIN)
   endif()
-endforeach()
-set(LIB_NAMES ${FILTERED_LIB_NAMES})
+  add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB SONAME ${INSTALL_WITH_TOOLCHAIN} ${SOURCES})
+
+  list(REMOVE_DUPLICATES LIB_NAMES)
+  if(("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") OR (MINGW) OR (HAIKU)
+     OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
+     OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "GNU")
+     OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "OpenBSD")
+     OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
+     OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "DragonFly")
+     OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")) # FIXME: It should be "GNU ld for elf"
+    configure_file(
+    ${CMAKE_CURRENT_SOURCE_DIR}/simple_version_script.map.in
+    ${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map)
 
-if(LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
-  set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})
-  add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
+    # GNU ld doesn't resolve symbols in the version script.
+    set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
+    if (NOT LLVM_LINKER_IS_SOLARISLD AND NOT MINGW)
+      # Solaris ld does not accept global: *; so there is no way to version *all* global symbols
+      set(LIB_NAMES -Wl,--version-script,${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map ${LIB_NAMES})
+    endif()
+  elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
+    set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
+  endif()
+
+  target_link_libraries(LLVM PRIVATE ${LIB_NAMES})
+
+  if (APPLE)
+    set_property(TARGET LLVM APPEND_STRING PROPERTY
+                LINK_FLAGS
+                " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")
+  endif()
+
+  if(TARGET libLLVMExports)
+    add_dependencies(LLVM libLLVMExports)
+  endif()
 endif()
 
-add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB SONAME ${SOURCES})
-
-list(REMOVE_DUPLICATES LIB_NAMES)
-if(("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") OR (MINGW) OR (HAIKU)
-   OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
-   OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "DragonFly")
-   OR ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")) # FIXME: It should be "GNU ld for elf"
-  configure_file(
-  ${CMAKE_CURRENT_SOURCE_DIR}/simple_version_script.map.in
-  ${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map)
-
-  # GNU ld doesn't resolve symbols in the version script.
-  set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
-  if (NOT LLVM_LINKER_IS_SOLARISLD)
-    # Solaris ld does not accept global: *; so there is no way to version *all* global symbols
-    set(LIB_NAMES -Wl,--version-script,${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map ${LIB_NAMES})
-  endif()
-elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
-  set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
-endif()
-
-target_link_libraries(LLVM PRIVATE ${LIB_NAMES})
-
-if (LLVM_DYLIB_SYMBOL_VERSIONING)
-  set_property(TARGET LLVM APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--default-symver")
-endif()
-
-if (APPLE)
-  set_property(TARGET LLVM APPEND_STRING PROPERTY
-              LINK_FLAGS
-              " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")
-endif()
-
-if(TARGET libLLVMExports)
-  add_dependencies(LLVM libLLVMExports)
-endif()
-
-if(LLVM_BUILD_LLVM_C_DYLIB)
-  # To get the export list for a single llvm library:
-  # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports
-
+if(LLVM_BUILD_LLVM_C_DYLIB AND NOT MSVC)
   if(NOT APPLE)
     message(FATAL_ERROR "Generating libLLVM-c is only supported on Darwin")
   endif()
 
+  if(NOT LLVM_BUILD_LLVM_DYLIB)
+    message(FATAL_ERROR "Generating libLLVM-c requires LLVM_BUILD_LLVM_C_DYLIB on Darwin")
+  endif()
+
+  # To get the export list for a single llvm library:
+  # nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports
+
   set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm-c.exports)
 
   set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
@@ -96,7 +108,7 @@
 
   add_custom_target(libLLVMCExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
 
-  add_llvm_library(LLVM-C SHARED ${SOURCES})
+  add_llvm_library(LLVM-C SHARED ${SOURCES} INSTALL_WITH_TOOLCHAIN)
   
   target_link_libraries(LLVM-C PUBLIC LLVM)
   add_dependencies(LLVM-C libLLVMCExports)
@@ -106,3 +118,70 @@
               " -compatibility_version 1 -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH} -Wl,-reexport_library ${LIB_PATH}")
 endif()
 
+if(MSVC)
+  # Build the LLVM-C.dll library that exports the C API.
+
+  set(LLVM_LINK_COMPONENTS
+    ${LLVM_DYLIB_COMPONENTS}
+    )
+
+  llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
+  list(REMOVE_DUPLICATES LIB_NAMES)
+
+  # The python script needs to know whether symbols are prefixed with underscores or not.
+  if(LLVM_HOST_TRIPLE STREQUAL "i686-pc-win32")
+    set(GEN_UNDERSCORE "--underscore")
+  else()
+    set(GEN_UNDERSCORE "")
+  endif()
+
+  # Set this name here, not used in multi conf loop,
+  # but add script will pick the right one.
+  set(LIBSFILE ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.args)
+
+  # Get the full name to the libs so the python script understands them.
+  foreach(lib ${LIB_NAMES})
+    list(APPEND FULL_LIB_NAMES ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/${lib}.lib)
+  endforeach()
+
+  # Need to seperate lib names with newlines.
+  string(REPLACE ";" "\n" FILE_CONTENT "${FULL_LIB_NAMES}")
+
+  if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
+    foreach(BUILD_MODE ${CMAKE_CONFIGURATION_TYPES})
+      # Replace the special string with a per config directory.
+      string(REPLACE ${CMAKE_CFG_INTDIR} ${BUILD_MODE} PER_CONF_CONTENT "${FILE_CONTENT}")
+
+      # Write out the full lib names into file to be read by the python script.
+      # One libsfile per build, the add_custom_command should expand
+      # ${CMAKE_CFG_INTDIR} correctly and select the right one.
+      file(WRITE ${CMAKE_BINARY_DIR}/${BUILD_MODE}/libllvm-c.args "${PER_CONF_CONTENT}")
+    endforeach()
+  else()
+    # Write out the full lib names into file to be read by the python script.
+    file(WRITE ${LIBSFILE} "${FILE_CONTENT}")
+  endif()
+
+  # Generate the exports file dynamically.
+  set(GEN_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/gen-msvc-exports.py)
+
+  set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libllvm-c.exports)
+
+  if(CMAKE_CROSSCOMPILING)
+    build_native_tool(llvm-nm llvm_nm)
+    set(llvm_nm_target "${llvm_nm}")
+  else()
+    set(llvm_nm $<TARGET_FILE:llvm-nm>)
+    set(llvm_nm_target llvm-nm)
+  endif()
+
+  add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
+    COMMAND ${PYTHON_EXECUTABLE} ${GEN_SCRIPT} --libsfile ${LIBSFILE} ${GEN_UNDERSCORE} --nm "${llvm_nm}" -o ${LLVM_EXPORTED_SYMBOL_FILE}
+    DEPENDS ${LIB_NAMES} ${llvm_nm_target}
+    COMMENT "Generating export list for LLVM-C"
+    VERBATIM )
+
+  # Finally link the target.
+  add_llvm_library(LLVM-C SHARED ${SOURCES} DEPENDS intrinsics_gen)
+
+endif()