[runtimes] Generalize how we reorder projects
authorLouis Dionne <ldionne.2@gmail.com>
Fri, 6 May 2022 19:43:18 +0000 (15:43 -0400)
committerLouis Dionne <ldionne.2@gmail.com>
Mon, 16 May 2022 12:55:32 +0000 (08:55 -0400)
This way, we could use it for LLVM_ENABLE_PROJECTS too if desired.

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

cmake/Modules/SortSubset.cmake [new file with mode: 0644]
runtimes/CMakeLists.txt

diff --git a/cmake/Modules/SortSubset.cmake b/cmake/Modules/SortSubset.cmake
new file mode 100644 (file)
index 0000000..af65c27
--- /dev/null
@@ -0,0 +1,25 @@
+# Sort a subset of a list according to the ordering in the full list.
+#
+# Given a list and a subset of that list, this function sorts the subset
+# according to the order in the full list, and returns that in the given
+# output variable.
+#
+# full_list:
+#   The list containing the desired order of elements in the sub-list.
+#
+# sub_list:
+#   A subset of the elements in `full_list`. Those elements will be sorted
+#   according to the order in `full_list`.
+#
+# out_var:
+#   A variable to store the resulting sorted sub-list in.
+function(sort_subset full_list sub_list out_var)
+  set(result "${full_list}")
+  foreach(project IN LISTS full_list)
+    if (NOT project IN_LIST sub_list)
+      list(REMOVE_ITEM result ${project})
+    endif()
+  endforeach()
+
+  set(${out_var} "${result}" PARENT_SCOPE)
+endfunction()
index 99d4ac3..c8766b9 100644 (file)
@@ -2,34 +2,27 @@
 cmake_minimum_required(VERSION 3.13.4)
 project(Runtimes C CXX ASM)
 
-set(LLVM_ALL_RUNTIMES "compiler-rt;libc;libcxx;libcxxabi;libunwind;openmp")
+# Add path for custom and the LLVM build's modules to the CMake module path.
+set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
+list(INSERT CMAKE_MODULE_PATH 0
+  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
+  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
+  "${LLVM_COMMON_CMAKE_UTILS}"
+  "${LLVM_COMMON_CMAKE_UTILS}/Modules"
+  "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake"
+  "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake/modules"
+)
+
+# We order libraries to mirror roughly how they are layered, except that compiler-rt can depend
+# on libc++, so we put it after.
+set(LLVM_ALL_RUNTIMES "libc;libunwind;libcxxabi;libcxx;compiler-rt;openmp")
 set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
   "Semicolon-separated list of runtimes to build (${LLVM_ALL_RUNTIMES}), or \"all\".")
 if(LLVM_ENABLE_RUNTIMES STREQUAL "all" )
   set(LLVM_ENABLE_RUNTIMES ${LLVM_ALL_RUNTIMES})
 endif()
-
-# Some of the runtimes will conditionally use the compiler-rt sanitizers.
-# To make this work smoothly, we ensure that compiler-rt is added first in
-# the list of sub-projects. This allows other sub-projects to have checks
-# like `if(TARGET asan)` to enable building with asan.
-if (compiler-rt IN_LIST LLVM_ENABLE_RUNTIMES)
-  list(REMOVE_ITEM LLVM_ENABLE_RUNTIMES compiler-rt)
-  if(NOT DEFINED LLVM_BUILD_COMPILER_RT OR LLVM_BUILD_COMPILER_RT)
-    list(PREPEND LLVM_ENABLE_RUNTIMES compiler-rt)
-  endif()
-endif()
-
-# If both libc++ and libc++abi are being built, always configure libc++abi before libc++.
-# This allows libc++ to depend on targets set up by libc++abi when it needs to.
-if (libcxx IN_LIST LLVM_ENABLE_RUNTIMES AND libcxxabi IN_LIST LLVM_ENABLE_RUNTIMES)
-  list(FIND LLVM_ENABLE_RUNTIMES libcxx _libcxx_index)
-  list(FIND LLVM_ENABLE_RUNTIMES libcxxabi _libcxxabi_index)
-  if (_libcxx_index LESS _libcxxabi_index)
-    list(TRANSFORM LLVM_ENABLE_RUNTIMES REPLACE "^libcxx$" "libcxxabi" AT ${_libcxx_index})
-    list(TRANSFORM LLVM_ENABLE_RUNTIMES REPLACE "^libcxxabi$" "libcxx" AT ${_libcxxabi_index})
-  endif()
-endif()
+include(SortSubset)
+sort_subset("${LLVM_ALL_RUNTIMES}" "${LLVM_ENABLE_RUNTIMES}" LLVM_ENABLE_RUNTIMES)
 
 foreach(proj ${LLVM_ENABLE_RUNTIMES})
   set(proj_dir "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
@@ -50,18 +43,6 @@ endfunction()
 find_package(LLVM PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
 find_package(Clang PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
 
-set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
-
-# Add path for custom and the LLVM build's modules to the CMake module path.
-list(INSERT CMAKE_MODULE_PATH 0
-  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
-  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
-  "${LLVM_COMMON_CMAKE_UTILS}"
-  "${LLVM_COMMON_CMAKE_UTILS}/Modules"
-  "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake"
-  "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake/modules"
-)
-
 set(LLVM_THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../third-party")
 
 # If building standalone by pointing CMake at this runtimes directory,