[CMake] Support runtimes targets without specifying triple
authorPetr Hosek <phosek@google.com>
Wed, 30 Jun 2021 06:34:18 +0000 (23:34 -0700)
committerPetr Hosek <phosek@google.com>
Wed, 5 Apr 2023 20:12:13 +0000 (20:12 +0000)
Currently, for BUILTIN_TARGETS and RUNTIME_TARGETS you can either use
the special "default" value, or a target triple.

For the "default" value, we don't set any target triple and passthrough
a subset of CMake variables into the subbuild. This is typically used
on Darwin where we build universal binaries and a single target triple
therefore isn't sufficient.

For the target triple value, you can set arbitrary CMake variables
through RUNTIMES_<target>_<variable>, but we always set target triple
to <target>. This gives more flexibility, because we can precisely
control what variables are set in the subbuild, but is unsuitable for
platforms like Darwin.

To address this, we would like to introduce a third option which is
similar to the second option, except we won't set target triple in
the subbuild (you can always do so yourself by setting the appropriate
CMake variable, e.g. RUNTIMES_<name>_CMAKE_C_COMPILER_TARGET=<triple>).

This change is a first step in that direction, by eliminating the support
of target triples from builtin_register_target and runtime_register_target
helper functions.

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

llvm/runtimes/CMakeLists.txt

index cc5f530..b9e1f32 100644 (file)
@@ -96,36 +96,33 @@ function(builtin_default_target compiler_rt_path)
                            ${EXTRA_ARGS})
 endfunction()
 
-function(builtin_register_target compiler_rt_path target)
-  cmake_parse_arguments(ARG "" "" "DEPENDS" ${ARGN})
-
-  check_apple_target(${target} builtin)
+function(builtin_register_target compiler_rt_path name)
+  cmake_parse_arguments(ARG "" "" "DEPENDS;CMAKE_ARGS;EXTRA_ARGS" ${ARGN})
 
-  get_cmake_property(variableNames VARIABLES)
-  foreach(variableName ${variableNames})
-    string(FIND "${variableName}" "BUILTINS_${target}" out)
+  set(${name}_extra_args ${ARG_CMAKE_ARGS})
+  get_cmake_property(variable_names VARIABLES)
+  foreach(variable_name ${variable_names})
+    string(FIND "${variable_name}" "BUILTINS_${name}" out)
     if("${out}" EQUAL 0)
-      string(REPLACE "BUILTINS_${target}_" "" new_name ${variableName})
-      string(REPLACE ";" "|" new_value "${${variableName}}")
-      list(APPEND ${target}_extra_args "-D${new_name}=${new_value}")
+      string(REPLACE "BUILTINS_${name}_" "" new_name ${variable_name})
+      string(REPLACE ";" "|" new_value "${${variable_name}}")
+      list(APPEND ${name}_extra_args "-D${new_name}=${new_value}")
     endif()
   endforeach()
 
-  llvm_ExternalProject_Add(builtins-${target}
+  llvm_ExternalProject_Add(builtins-${name}
                            ${compiler_rt_path}/lib/builtins
                            DEPENDS ${ARG_DEPENDS}
                            CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
                                       -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
-                                      -DLLVM_DEFAULT_TARGET_TRIPLE=${target}
                                       -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON
                                       -DCMAKE_C_COMPILER_WORKS=ON
                                       -DCMAKE_ASM_COMPILER_WORKS=ON
                                       -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
                                       ${COMMON_CMAKE_ARGS}
-                                      ${${target}_extra_args}
+                                      ${${name}_extra_args}
                            USE_TOOLCHAIN
-                           TARGET_TRIPLE ${target}
-                           ${EXTRA_ARGS})
+                           ${EXTRA_ARGS} ${ARG_EXTRA_ARGS})
 endfunction()
 
 # If compiler-rt is present we need to build the builtin libraries first. This
@@ -148,8 +145,12 @@ if(compiler_rt_path)
     endif()
 
     foreach(target ${LLVM_BUILTIN_TARGETS})
+      check_apple_target(${target} builtin)
+
       builtin_register_target(${compiler_rt_path} ${target}
-        DEPENDS clang-resource-headers)
+        DEPENDS clang-resource-headers
+        CMAKE_ARGS -DLLVM_DEFAULT_TARGET_TRIPLE=${target}
+        EXTRA_ARGS TARGET_TRIPLE ${target})
 
       add_dependencies(builtins builtins-${target})
       add_dependencies(install-builtins install-builtins-${target})
@@ -254,20 +255,13 @@ function(runtime_default_target)
                            ${EXTRA_ARGS})
 endfunction()
 
-# runtime_register_target(target)
+# runtime_register_target(name)
 #   Utility function to register external runtime target.
-function(runtime_register_target name target)
-  cmake_parse_arguments(ARG "" "" "DEPENDS;CMAKE_ARGS" ${ARGN})
+function(runtime_register_target name)
+  cmake_parse_arguments(ARG "" "BASE_NAME" "DEPENDS;CMAKE_ARGS;EXTRA_ARGS" ${ARGN})
   include(${LLVM_BINARY_DIR}/runtimes/${name}/Components.cmake OPTIONAL)
   set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/${name}/Components.cmake)
 
-  check_apple_target(${target} runtime)
-
-  set(${name}_deps ${ARG_DEPENDS})
-  if(NOT name STREQUAL target)
-    list(APPEND ${name}_deps runtimes-${target})
-  endif()
-
   foreach(runtime_name ${runtime_names})
     set(${runtime_name}-${name} ${runtime_name})
     set(install-${runtime_name}-${name} install-${runtime_name})
@@ -279,15 +273,15 @@ function(runtime_register_target name target)
     endif()
   endforeach()
 
-  foreach(target_name IN LISTS SUB_COMPONENTS)
-    set(${target_name}-${name} ${target_name})
-    list(APPEND ${name}_extra_targets ${target_name}-${name})
+  foreach(component IN LISTS SUB_COMPONENTS)
+    set(${component}-${name} ${component})
+    list(APPEND ${name}_extra_targets ${component}-${name})
   endforeach()
 
-  foreach(target_name IN LISTS SUB_INSTALL_TARGETS)
-    set(${target_name}-${name} ${target_name})
-    set(${target_name}-${name}-stripped ${target_name}-stripped)
-    list(APPEND ${name}_extra_targets ${target_name}-${name} ${target_name}-${name}-stripped)
+  foreach(target IN LISTS SUB_INSTALL_TARGETS)
+    set(${target}-${name} ${target})
+    set(${target}-${name}-stripped ${target}-stripped)
+    list(APPEND ${name}_extra_targets ${target}-${name} ${target}-${name}-stripped)
   endforeach()
 
   foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
@@ -313,53 +307,39 @@ function(runtime_register_target name target)
       endif()
     endforeach()
 
-    foreach(target_name IN LISTS SUB_CHECK_TARGETS component_check_targets)
-      set(${target_name}-${name} ${target_name})
-      list(APPEND ${name}_test_targets ${target_name}-${name})
-      list(APPEND test_targets ${target_name}-${name})
+    foreach(target IN LISTS SUB_CHECK_TARGETS component_check_targets)
+      set(${target}-${name} ${target})
+      list(APPEND ${name}_test_targets ${target}-${name})
+      list(APPEND test_targets ${target}-${name})
     endforeach()
     set(test_targets "${test_targets}" PARENT_SCOPE)
   endif()
 
   set(${name}_extra_args ${ARG_CMAKE_ARGS})
-  get_cmake_property(variableNames VARIABLES)
-  foreach(variableName ${variableNames})
-    string(FIND "${variableName}" "RUNTIMES_${target}_" out)
-    if("${out}" EQUAL 0)
-      string(REPLACE "RUNTIMES_${target}_" "" new_name ${variableName})
-      string(REPLACE ";" "|" new_value "${${variableName}}")
-      list(APPEND ${name}_extra_args "-D${new_name}=${new_value}")
-    endif()
-  endforeach()
-  if(NOT "${name}" STREQUAL "${target}")
-    foreach(variableName ${variableNames})
-      string(FIND "${variableName}" "RUNTIMES_${name}_" out)
+  string(REPLACE ";" "|" LVM_ENABLE_RUNTIMES_PASSTHROUGH "${LLVM_ENABLE_RUNTIMES}")
+  list(APPEND ${name}_extra_args -DLLVM_ENABLE_RUNTIMES=${LLVM_ENABLE_RUNTIMES_PASSTHROUGH})
+  list(APPEND ${name}_extra_args -DLLVM_USE_LINKER=${LLVM_USE_LINKER})
+
+  get_cmake_property(variable_names VARIABLES)
+  foreach(extra_name IN ITEMS ${ARG_BASE_NAME} ${name})
+    foreach(variable_name ${variable_names})
+      string(FIND "${variable_name}" "RUNTIMES_${extra_name}_" out)
       if("${out}" EQUAL 0)
-        string(REPLACE "RUNTIMES_${name}_" "" new_name ${variableName})
-        string(REPLACE ";" "|" new_value "${${variableName}}")
+        string(REPLACE "RUNTIMES_${extra_name}_" "" new_name ${variable_name})
+        string(REPLACE ";" "|" new_value "${${variable_name}}")
         list(APPEND ${name}_extra_args "-D${new_name}=${new_value}")
       endif()
     endforeach()
-  endif()
-
-  if(NOT RUNTIMES_${name}_LLVM_ENABLE_RUNTIMES AND NOT RUNTIMES_${target}_LLVM_ENABLE_RUNTIMES)
-    string(REPLACE ";" "|" LLVM_ENABLE_RUNTIMES_PASSTHROUGH "${LLVM_ENABLE_RUNTIMES}")
-    list(APPEND ${name}_extra_args -DLLVM_ENABLE_RUNTIMES=${LLVM_ENABLE_RUNTIMES_PASSTHROUGH})
-  endif()
-
-  if(NOT RUNTIMES_${name}_LLVM_USE_LINKER AND NOT RUNTIMES_${target}_LLVM_USE_LINKER)
-    list(APPEND ${name}_extra_args -DLLVM_USE_LINKER=${LLVM_USE_LINKER})
-  endif()
+  endforeach()
 
   set_enable_per_target_runtime_dir()
 
   llvm_ExternalProject_Add(runtimes-${name}
                            ${CMAKE_CURRENT_SOURCE_DIR}/../../runtimes
-                           DEPENDS ${${name}_deps}
+                           DEPENDS ${ARG_DEPENDS}
                            # Builtins were built separately above
-                           CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
+                           CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=OFF
                                       -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
-                                      -DLLVM_DEFAULT_TARGET_TRIPLE=${target}
                                       -DLLVM_ENABLE_PROJECTS_USED=${LLVM_ENABLE_PROJECTS_USED}
                                       -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=${LLVM_ENABLE_PER_TARGET_RUNTIME_DIR}
                                       -DCMAKE_C_COMPILER_WORKS=ON
@@ -372,8 +352,7 @@ function(runtime_register_target name target)
                            EXTRA_TARGETS ${${name}_extra_targets}
                                          ${${name}_test_targets}
                            USE_TOOLCHAIN
-                           TARGET_TRIPLE ${target}
-                           ${EXTRA_ARGS})
+                           ${EXTRA_ARGS} ${ARG_EXTRA_ARGS})
 endfunction()
 
 if(runtimes)
@@ -452,9 +431,13 @@ if(runtimes)
           set(builtins_dep_name ${builtins_dep})
         endif()
       endif()
-      runtime_register_target(${name} ${name}
+
+      check_apple_target(${name} runtime)
+
+      runtime_register_target(${name}
         DEPENDS ${builtins_dep_name} ${libc_tools}
-        CMAKE_ARGS ${libc_cmake_args})
+        CMAKE_ARGS -DLLVM_DEFAULT_TARGET_TRIPLE=${name} ${libc_cmake_args}
+        EXTRA_ARGS TARGET_TRIPLE ${name})
 
       add_dependencies(runtimes runtimes-${name})
       add_dependencies(runtimes-configure runtimes-${name}-configure)
@@ -478,10 +461,14 @@ if(runtimes)
 
     foreach(multilib ${LLVM_RUNTIME_MULTILIBS})
       foreach(name ${LLVM_RUNTIME_MULTILIB_${multilib}_TARGETS})
-        runtime_register_target(${name}+${multilib} ${name}
+        runtime_register_target(${name}+${multilib}
           DEPENDS runtimes-${name}
-          CMAKE_ARGS -DLLVM_RUNTIMES_PREFIX=${name}/
-                     -DLLVM_RUNTIMES_LIBDIR_SUBDIR=${multilib})
+          CMAKE_ARGS -DLLVM_DEFAULT_TARGET_TRIPLE=${name}
+                     -DLLVM_RUNTIMES_PREFIX=${name}/
+                     -DLLVM_RUNTIMES_LIBDIR_SUBDIR=${multilib}
+          BASE_NAME ${name}
+          EXTRA_ARGS TARGET_TRIPLE ${name})
+
         add_dependencies(runtimes runtimes-${name}+${multilib})
         add_dependencies(runtimes-configure runtimes-${name}+${multilib}-configure)
         add_dependencies(install-runtimes install-runtimes-${name}+${multilib})