[PATCH] D99621: [CMake][Compiler-rt] Make it possible to configure standalone compiler-rt without `LLVMConfig.cmake`.

Dan Liew via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 30 14:32:39 PDT 2021


delcypher created this revision.
delcypher added reviewers: mstorsjo, Hahnfeld, yln, kubamracek, aralisza, arphaman, phosek, beanz, vitalybuka, thakis.
Herald added subscribers: mgorny, dberris.
delcypher requested review of this revision.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.

Previously it wasn't possible to configure a standalone compiler-rt
build if the `LLVMConfig.cmake` file isn't present in a shipped
toolchain.

This patch adds a fallback behaviour for when `LLVMConfig.cmake` is not
available in the toolchain being used for configure. The fallback
behaviour mocks out the bare minimum required to make a configure
succeed when the host is Darwin. Support for other platforms could
be added in future patches.

The motivation here is to be able to generate the compiler-rt lit test
suites for an arbitrary LLVM toolchain and then run the tests against
it.

The invocation to do this looks something like.

  CC=/path/to/cc \
  CXX=/path/to/c++ \
  cmake \
    -G Ninja \
    -DLLVM_CONFIG_PATH=/path/to/llvm-config \
    -DCOMPILER_RT_INCLUDE_TESTS=ON \
    /path/to/llvm-project/compiler-rt
  
   # Note we don't compile compiler-rt in this workflow.
  bin/llvm-lit -v test/path/to/generated/test_suite

A possible alternative approach is to configure the
`cmake/modules/LLVMConfig.cmake.in` file in the LLVM source tree
and then include it. This approach was not taken because it is more
complicated.

rdar://76016632


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99621

Files:
  compiler-rt/cmake/Modules/CompilerRTMockLLVMCMakeConfig.cmake
  compiler-rt/cmake/Modules/CompilerRTUtils.cmake


Index: compiler-rt/cmake/Modules/CompilerRTUtils.cmake
===================================================================
--- compiler-rt/cmake/Modules/CompilerRTUtils.cmake
+++ compiler-rt/cmake/Modules/CompilerRTUtils.cmake
@@ -313,9 +313,15 @@
       set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR_CMAKE_STYLE}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
     endif()
 
-    list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
-    # Get some LLVM variables from LLVMConfig.
-    include("${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
+    if (EXISTS "${LLVM_CMAKE_PATH}")
+      list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
+      # Get some LLVM variables from LLVMConfig.
+      include("${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
+    else()
+      message(WARNING "LLVM CMake path (${LLVM_CMAKE_PATH}) reported by llvm-config does not exist")
+      include(CompilerRTMockLLVMCMakeConfig)
+      compiler_rt_mock_llvm_cmake_config()
+    endif()
 
     set(LLVM_LIBRARY_OUTPUT_INTDIR
       ${LLVM_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
Index: compiler-rt/cmake/Modules/CompilerRTMockLLVMCMakeConfig.cmake
===================================================================
--- /dev/null
+++ compiler-rt/cmake/Modules/CompilerRTMockLLVMCMakeConfig.cmake
@@ -0,0 +1,43 @@
+# This macro mocks enough of the changes `LLVMConfig.cmake` makes so that
+# compiler-rt can successfully configure itself when a LLVM toolchain is
+# available but the corresponding CMake build files are not.
+#
+# The motivation for this is to be able to generate the compiler-rt
+# lit tests suites and run them against an arbitrary LLVM toolchain
+# which doesn't ship the LLVM CMake build files.
+macro(compiler_rt_mock_llvm_cmake_config)
+  message(STATUS "Attempting to mock the changes made by LLVMConfig.cmake")
+  compiler_rt_mock_llvm_cmake_config_set_cmake_path()
+  compiler_rt_mock_llvm_cmake_config_set_target_triple()
+  compiler_rt_mock_llvm_cmake_config_include_cmake_files()
+endmacro()
+
+macro(compiler_rt_mock_llvm_cmake_config_set_cmake_path)
+  # Point `LLVM_CMAKE_PATH` at the source tree in the monorepo.
+  set(LLVM_CMAKE_PATH "${LLVM_MAIN_SRC_DIR}/cmake/modules")
+  if (NOT EXISTS "${LLVM_CMAKE_PATH}")
+    message(FATAL_ERROR "LLVM_CMAKE_PATH (${LLVM_CMAKE_PATH}) does not exist")
+  endif()
+  list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
+  message(STATUS "LLVM_CMAKE_PATH: \"${LLVM_CMAKE_PATH}\"")
+endmacro()
+
+macro(compiler_rt_mock_llvm_cmake_config_set_target_triple)
+  # Various bits of compiler-rt depend on this variable being defined.
+  execute_process(
+    COMMAND ${LLVM_CONFIG_PATH} --host-target
+    RESULT_VARIABLE HAD_ERROR
+    OUTPUT_VARIABLE CONFIG_OUTPUT
+    OUTPUT_STRIP_TRAILING_WHITESPACE)
+  if(NOT HAD_ERROR)
+    set(TARGET_TRIPLE "${CONFIG_OUTPUT}")
+  else()
+    message(FATAL_ERROR "FAILED to run llvm-config to determine host target")
+  endif()
+  message(STATUS "TARGET_TRIPLE: \"${TARGET_TRIPLE}\"")
+endmacro()
+
+macro(compiler_rt_mock_llvm_cmake_config_include_cmake_files)
+  # Some compiler-rt CMake code needs to call code in this file.
+  include("${LLVM_CMAKE_PATH}/AddLLVM.cmake")
+endmacro()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99621.334272.patch
Type: text/x-patch
Size: 3161 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210330/87b96a26/attachment.bin>


More information about the llvm-commits mailing list