OSDN Git Service

[CMake] Support multi-target runtimes build
authorPetr Hosek <phosek@chromium.org>
Tue, 11 Jul 2017 23:41:15 +0000 (23:41 +0000)
committerPetr Hosek <phosek@chromium.org>
Tue, 11 Jul 2017 23:41:15 +0000 (23:41 +0000)
This changes adds support for building runtimes for multiple
different targets using LLVM runtimes directory.

The implementation follow the model used already by the builtins
build which already supports this option. To specify the runtimes
targets to be built, use the LLVM_RUNTIME_TARGETS variable, where
the valuae is the list of targets to build runtimes for. To pass
a per target variable to the runtimes build, you can set
RUNTIMES_<target>_<variable> where <variable> will be passed to the
runtimes build for <target>.

Each runtime target (except for the default one) will be installed
into lib/<target> subdirectory. Build targets will be suffixed with
the target name.

Differential Revision: https://reviews.llvm.org/D32816

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@307731 91177308-0d34-0410-b5e6-96231b3b80d8

cmake/modules/LLVMExternalProjectUtils.cmake
runtimes/CMakeLists.txt
runtimes/Components.cmake.in

index d457389..c851eb8 100644 (file)
@@ -195,8 +195,16 @@ function(llvm_ExternalProject_Add name source_dir)
 
   # Add top-level targets
   foreach(target ${ARG_EXTRA_TARGETS})
+    string(REPLACE ":" ";" target_list ${target})
+    list(GET target_list 0 target)
+    list(LENGTH target_list target_list_len)
+    if(${target_list_len} GREATER 1)
+      list(GET target_list 1 target_name)
+    else()
+      set(target_name "${target}")
+    endif()
     llvm_ExternalProject_BuildCmd(build_runtime_cmd ${target} ${BINARY_DIR})
-    add_custom_target(${target}
+    add_custom_target(${target_name}
       COMMAND ${build_runtime_cmd}
       DEPENDS ${name}-configure
       WORKING_DIRECTORY ${BINARY_DIR}
index ded3a71..3e3eff3 100644 (file)
@@ -62,7 +62,7 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
   set(LLVM_MAIN_SRC_DIR ${LLVM_BUILD_MAIN_SRC_DIR})
 
   if(APPLE)
-   set(LLVM_ENABLE_LIBCXX ON CACHE BOOL "")
+    set(LLVM_ENABLE_LIBCXX ON CACHE BOOL "")
   endif()
 
   set(SAFE_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
@@ -96,15 +96,33 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
     # The subdirectories need to treat this as standalone builds
     set(${canon_name}_STANDALONE_BUILD On)
 
+    if(LLVM_RUNTIMES_TARGET)
+      if(NOT "${entry}" MATCHES "compiler-rt")
+        set(${canon_name}_INSTALL_PREFIX "lib/${LLVM_RUNTIMES_TARGET}/" CACHE STRING "" FORCE)
+      endif()
+    endif()
+
     # Setting a variable to let sub-projects detect which other projects
     # will be included under here.
     set(HAVE_${canon_name} On)
   endforeach()
 
+  set(SAFE_LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
+  set(SAFE_LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
+  set(SAFE_LLVM_RUNTIMES_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
+
   # We do this in two loops so that HAVE_* is set for each runtime before the
   # other runtimes are added.
   foreach(entry ${runtimes})
     get_filename_component(projName ${entry} NAME)
+
+    if(LLVM_RUNTIMES_TARGET)
+      if(NOT "${entry}" MATCHES "compiler-rt")
+        set(LLVM_BINARY_DIR "${LLVM_LIBRARY_DIR}/${LLVM_RUNTIMES_TARGET}")
+        set(LLVM_LIBRARY_OUTPUT_INTDIR "${LLVM_LIBRARY_DIR}/${LLVM_RUNTIMES_TARGET}/lib")
+        set(LLVM_RUNTIME_OUTPUT_INTDIR "${LLVM_TOOLS_BINARY_DIR}/${LLVM_RUNTIMES_TARGET}")
+      endif()
+    endif()
     
     # Between each sub-project we want to cache and clear the LIT properties
     set_property(GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
@@ -123,6 +141,14 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
     list(APPEND RUNTIMES_LIT_PARAMS ${LLVM_LIT_PARAMS})
     list(APPEND RUNTIMES_LIT_DEPENDS ${LLVM_LIT_DEPENDS})
     list(APPEND RUNTIMES_LIT_EXTRA_ARGS ${LLVM_LIT_EXTRA_ARGS})
+
+    if(LLVM_RUNTIMES_TARGET)
+      if(NOT "${entry}" MATCHES "compiler-rt")
+        set(LLVM_BINARY_DIR "${SAFE_LLVM_BINARY_DIR}")
+        set(LLVM_LIBRARY_OUTPUT_INTDIR "${SAFE_LLVM_LIBRARY_OUTPUT_INTDIR}")
+        set(LLVM_RUNTIME_OUTPUT_INTDIR "${SAFE_LLVM_RUNTIME_OUTPUT_INTDIR}")
+      endif()
+    endif()
   endforeach()
 
   if(LLVM_INCLUDE_TESTS)
@@ -147,9 +173,9 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
         message(SEND_ERROR "Missing target for runtime component ${component}!")
         continue()
       endif()
-      if(LLVM_INCLUDE_TESTS AND NOT TARGET check-${component})
-        message(SEND_ERROR "Missing check target for runtime component ${component}!")
-        continue()
+
+      if(TARGET check-${component})
+        list(APPEND SUB_CHECK_TARGETS check-${component})
       endif()
 
       if(TARGET install-${component})
@@ -157,14 +183,18 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
       endif()
     endforeach()
 
-    configure_file(
-      ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
-      ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
+    if(LLVM_RUNTIMES_TARGET)
+      configure_file(
+        ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
+        ${LLVM_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
+    else()
+      configure_file(
+        ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
+        ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
+    endif()
   endif()
 
 else() # if this is included from LLVM's CMake
-  include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
-  set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
   include(LLVMExternalProjectUtils)
 
   if(NOT LLVM_BUILD_RUNTIMES)
@@ -190,6 +220,10 @@ else() # if this is included from LLVM's CMake
       add_custom_target(builtins)
       add_custom_target(install-builtins)
       foreach(target ${LLVM_BUILTIN_TARGETS})
+        if(target STREQUAL "default")
+          set(target ${LLVM_DEFAULT_TARGET_TRIPLE})
+        endif()
+
         string(REPLACE "-" ";" builtin_target_list ${target})
         foreach(item ${builtin_target_list})
           string(TOLOWER "${item}" item_lower)
@@ -246,42 +280,137 @@ else() # if this is included from LLVM's CMake
     list(APPEND runtime_names ${projName})
   endforeach()
 
-  if(runtimes)
+  # runtime_register_target(target)
+  #   Utility function to register external runtime target.
+  function(runtime_register_target target)
+    if(target STREQUAL LLVM_DEFAULT_TARGET_TRIPLE)
+      include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
+      set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
+    else()
+      include(${LLVM_BINARY_DIR}/runtimes/${target}/Components.cmake OPTIONAL)
+      set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/${target}/Components.cmake)
+    endif()
 
     foreach(runtime_name ${runtime_names})
-      list(APPEND extra_targets
-        ${runtime_name}
-        install-${runtime_name})
+      list(APPEND ${target}_extra_targets
+        ${runtime_name}-${target}
+        install-${runtime_name}-${target})
       if(LLVM_INCLUDE_TESTS)
-        list(APPEND test_targets check-${runtime_name})
+        list(APPEND ${target}_test_targets check-${runtime_name}-${target})
       endif()
     endforeach()
 
+    foreach(name IN LISTS SUB_COMPONENTS SUB_INSTALL_TARGETS)
+      list(APPEND ${target}_extra_targets "${name}:${name}-${target}")
+    endforeach()
+
     if(LLVM_INCLUDE_TESTS)
-      list(APPEND test_targets runtimes-test-depends check-runtimes)
-      foreach(component ${SUB_COMPONENTS})
-        list(APPEND SUB_COMPONENT_CHECK_TARGETS check-${component})
+      list(APPEND ${target}_test_targets runtimes-test-depends-${target} check-runtimes-${target})
+      foreach(name IN LISTS SUB_CHECK_TARGETS)
+        list(APPEND ${target}_test_targets "${name}:${name}-${target}")
+        list(APPEND test_targets ${name}-${target})
       endforeach()
+      set(test_targets "${test_targets}" PARENT_SCOPE)
     endif()
 
-    # Create a runtimes target that uses this file as its top-level CMake file.
-    # The runtimes target is a configuration of all the runtime libraries
-    # together in a single CMake invocaiton.
-    llvm_ExternalProject_Add(runtimes
+    get_cmake_property(variableNames VARIABLES)
+    foreach(variableName ${variableNames})
+      if(variableName MATCHES "^RUNTIMES_${target}")
+        string(REPLACE "RUNTIMES_${target}_" "" new_name ${variableName})
+        list(APPEND ${target}_extra_args "-D${new_name}=${${variableName}}")
+      endif()
+    endforeach()
+
+    if(NOT target STREQUAL LLVM_DEFAULT_TARGET_TRIPLE)
+      list(APPEND ${target}_extra_args "-DLLVM_RUNTIMES_TARGET=${target}")
+    endif()
+
+    llvm_ExternalProject_Add(runtimes-${target}
                              ${CMAKE_CURRENT_SOURCE_DIR}
                              DEPENDS ${deps}
                              # Builtins were built separately above
                              CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
                                         -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
+                                        -DCMAKE_C_COMPILER_TARGET=${target}
+                                        -DCMAKE_CXX_COMPILER_TARGET=${target}
+                                        -DCMAKE_ASM_COMPILER_TARGET=${target}
+                                        -DCMAKE_C_COMPILER_WORKS=ON
+                                        -DCMAKE_CXX_COMPILER_WORKS=ON
+                                        -DCMAKE_ASM_COMPILER_WORKS=ON
+                                        -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
+                                        ${${target}_extra_args}
                              PASSTHROUGH_PREFIXES ${prefixes}
-                             EXTRA_TARGETS ${extra_targets}
-                                            ${test_targets}
-                                            ${SUB_COMPONENTS}
-                                            ${SUB_COMPONENT_CHECK_TARGETS}
-                                            ${SUB_INSTALL_TARGETS}
+                             EXTRA_TARGETS ${${target}_extra_targets}
+                                           ${${target}_test_targets}
                              USE_TOOLCHAIN
                              ${EXTRA_ARGS})
-    
+  endfunction()
+
+  if(runtimes)
+    # Create a runtimes target that uses this file as its top-level CMake file.
+    # The runtimes target is a configuration of all the runtime libraries
+    # together in a single CMake invocaiton.
+    if(NOT LLVM_RUNTIME_TARGETS)
+      include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
+      set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
+
+      foreach(runtime_name ${runtime_names})
+        list(APPEND extra_targets
+          ${runtime_name}
+          install-${runtime_name})
+        if(LLVM_INCLUDE_TESTS)
+          list(APPEND test_targets check-${runtime_name})
+        endif()
+      endforeach()
+
+      if(LLVM_INCLUDE_TESTS)
+        list(APPEND test_targets runtimes-test-depends check-runtimes)
+        foreach(component ${SUB_COMPONENTS})
+          list(APPEND SUB_CHECK_TARGETS check-${component})
+        endforeach()
+      endif()
+
+      llvm_ExternalProject_Add(runtimes
+                               ${CMAKE_CURRENT_SOURCE_DIR}
+                               DEPENDS ${deps}
+                               # Builtins were built separately above
+                               CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
+                                          -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
+                               PASSTHROUGH_PREFIXES ${prefixes}
+                               EXTRA_TARGETS ${extra_targets}
+                                              ${test_targets}
+                                              ${SUB_COMPONENTS}
+                                              ${SUB_CHECK_TARGETS}
+                                              ${SUB_INSTALL_TARGETS}
+                               USE_TOOLCHAIN
+                               ${EXTRA_ARGS})
+    else()
+      add_custom_target(runtimes)
+      add_custom_target(runtimes-configure)
+      add_custom_target(install-runtimes)
+      if(LLVM_INCLUDE_TESTS)
+        add_custom_target(check-runtimes)
+        add_custom_target(runtimes-test-depends)
+        set(test_targets "")
+      endif()
+
+      foreach(target ${LLVM_RUNTIME_TARGETS})
+        if(target STREQUAL "default")
+          set(target ${LLVM_DEFAULT_TARGET_TRIPLE})
+        endif()
+
+        runtime_register_target(${target})
+
+        add_dependencies(runtimes runtimes-${target})
+        add_dependencies(runtimes-configure runtimes-${target}-configure)
+        add_dependencies(install-runtimes install-runtimes-${target})
+        if(LLVM_INCLUDE_TESTS)
+          add_dependencies(check-runtimes check-runtimes-${target})
+          add_dependencies(runtimes-test-depends runtimes-test-depends-${target})
+        endif()
+      endforeach()
+    endif()
+
     # TODO: This is a hack needed because the libcxx headers are copied into the
     # build directory during configuration. Without that step the clang in the
     # build directory cannot find the C++ headers in certain configurations.
@@ -306,7 +435,7 @@ else() # if this is included from LLVM's CMake
           sancov
           sanstats
         )
-      foreach(target ${test_targets} ${SUB_COMPONENT_CHECK_TARGETS})
+      foreach(target ${test_targets} ${SUB_CHECK_TARGETS})
         add_dependencies(${target} ${RUNTIMES_TEST_DEPENDS})
       endforeach()
     endif()
index 6e24ac3..1d8fb7a 100644 (file)
@@ -1,2 +1,3 @@
 set(SUB_COMPONENTS @SUB_COMPONENTS@)
+set(SUB_CHECK_TARGETS @SUB_CHECK_TARGETS@)
 set(SUB_INSTALL_TARGETS @SUB_INSTALL_TARGETS@)