diff --git a/compiler-rt/cmake/Modules/CompilerRTUtils.cmake b/compiler-rt/cmake/Modules/CompilerRTUtils.cmake --- a/compiler-rt/cmake/Modules/CompilerRTUtils.cmake +++ b/compiler-rt/cmake/Modules/CompilerRTUtils.cmake @@ -219,6 +219,18 @@ "Reconfigure with -DLLVM_CONFIG_PATH=path/to/llvm-config.") endif() endif() + + # Compute path to LLVM sources assuming the monorepo layout. + # We don't set `LLVM_MAIN_SRC_DIR` directly to avoid overriding a user provided + # CMake cache value. + get_filename_component(LLVM_MAIN_SRC_DIR_DEFAULT "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" ABSOLUTE) + if (NOT EXISTS "${LLVM_MAIN_SRC_DIR_DEFAULT}") + # TODO(dliew): Remove this legacy fallback path. + message(WARNING + "LLVM source tree not found at \"${LLVM_MAIN_SRC_DIR_DEFAULT}\". " + "You are not using the monorepo layout. This configuration is DEPRECATED.") + endif() + if (LLVM_CONFIG_PATH) execute_process( COMMAND ${LLVM_CONFIG_PATH} "--obj-root" "--bindir" "--libdir" "--src-root" "--includedir" @@ -236,10 +248,20 @@ set(LLVM_BINARY_DIR ${BINARY_DIR} CACHE PATH "Path to LLVM build tree") set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib") - set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree") set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin") set(LLVM_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Paths to LLVM headers") + if (NOT EXISTS "${LLVM_MAIN_SRC_DIR_DEFAULT}") + # TODO(dliew): Remove this legacy fallback path. + message(WARNING + "Consulting llvm-config for the LLVM source path " + "as a fallback. This behavior will be removed in the future.") + # We don't set `LLVM_MAIN_SRC_DIR` directly to avoid overriding a user + # provided CMake cache value. + set(LLVM_MAIN_SRC_DIR_DEFAULT "${MAIN_SRC_DIR}") + message(STATUS "Using LLVM source path (${LLVM_MAIN_SRC_DIR_DEFAULT}) from llvm-config") + endif() + # Detect if we have the LLVMXRay and TestingSupport library installed and # available from llvm-config. execute_process( @@ -305,6 +327,20 @@ set(LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX}) endif() + + # Finally set the cache variable now that `llvm-config` has also had a chance + # to set `LLVM_MAIN_SRC_DIR_DEFAULT`. + set(LLVM_MAIN_SRC_DIR "${LLVM_MAIN_SRC_DIR_DEFAULT}" CACHE PATH "Path to LLVM source tree") + message(STATUS "LLVM_MAIN_SRC_DIR: \"${LLVM_MAIN_SRC_DIR}\"") + if (NOT EXISTS "${LLVM_MAIN_SRC_DIR}") + # TODO(dliew): Make this a hard error + message(WARNING "LLVM_MAIN_SRC_DIR (${LLVM_MAIN_SRC_DIR}) does not exist. " + "You can override the inferred path by adding " + "`-DLLVM_MAIN_SRC_DIR=` to your CMake invocation " + "where `` is the path to the `llvm` directory in " + "the `llvm-project` repo. " + "This will be treated as error in the future.") + endif() endmacro() macro(construct_compiler_rt_default_triple)