diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt @@ -3,19 +3,7 @@ Support ) -set(CLANG_TIDY_CONFUSABLE_CHARS_GEN "clang-tidy-confusable-chars-gen" CACHE - STRING "Host clang-tidy-confusable-chars-gen executable. Saves building if cross-compiling.") - -if(NOT CLANG_TIDY_CONFUSABLE_CHARS_GEN STREQUAL "clang-tidy-confusable-chars-gen") - set(clang_tidy_confusable_chars_gen ${CLANG_TIDY_CONFUSABLE_CHARS_GEN}) - set(clang_tidy_confusable_chars_gen_target ${CLANG_TIDY_CONFUSABLE_CHARS_GEN}) -elseif(LLVM_USE_HOST_TOOLS) - build_native_tool(clang-tidy-confusable-chars-gen clang_tidy_confusable_chars_gen) - set(clang_tidy_confusable_chars_gen_target "${clang_tidy_confusable_chars_gen}") -else() - set(clang_tidy_confusable_chars_gen $) - set(clang_tidy_confusable_chars_gen_target clang-tidy-confusable-chars-gen) -endif() +setup_host_tool(clang-tidy-confusable-chars-gen CLANG_TIDY_CONFUSABLE_CHARS_GEN clang_tidy_confusable_chars_gen clang_tidy_confusable_chars_gen_target) add_subdirectory(ConfusableTable) diff --git a/clang-tools-extra/pseudo/include/CMakeLists.txt b/clang-tools-extra/pseudo/include/CMakeLists.txt --- a/clang-tools-extra/pseudo/include/CMakeLists.txt +++ b/clang-tools-extra/pseudo/include/CMakeLists.txt @@ -1,21 +1,7 @@ # The cxx.bnf grammar file set(cxx_bnf ${CMAKE_CURRENT_SOURCE_DIR}/../lib/cxx/cxx.bnf) -set(CLANG_PSEUDO_GEN "clang-pseudo-gen" CACHE - STRING "Host clang-pseudo-gen executable. Saves building if cross-compiling.") - -if(NOT CLANG_PSEUDO_GEN STREQUAL "clang-pseudo-gen") - set(pseudo_gen ${CLANG_PSEUDO_GEN}) - set(pseudo_gen_target ${CLANG_PSEUDO_GEN}) -elseif(LLVM_USE_HOST_TOOLS) - # The NATIVE executable *must* depend on the current target, otherwise the - # native one won't get rebuilt when the pseudo-gen sources change. - build_native_tool(clang-pseudo-gen pseudo_gen DEPENDS clang-pseudo-gen) - set(pseudo_gen_target "${pseudo_gen}") -else() - set(pseudo_gen $) - set(pseudo_gen_target clang-pseudo-gen) -endif() +setup_host_tool(clang-pseudo-gen CLANG_PSEUDO_GEN pseudo_gen pseudo_gen_target) # Generate inc files. set(cxx_symbols_inc ${CMAKE_CURRENT_BINARY_DIR}/CXXSymbols.inc) diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -664,6 +664,7 @@ if( WIN32 AND NOT CYGWIN ) set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools") endif() +set(LLVM_NATIVE_TOOL_DIR "" CACHE PATH "Path to a directory containing prebuilt matching native tools (such as llvm-tblgen)") set(LLVM_INTEGRATED_CRT_ALLOC "" CACHE PATH "Replace the Windows CRT allocator with any of {rpmalloc|mimalloc|snmalloc}. Only works with /MT enabled.") if(LLVM_INTEGRATED_CRT_ALLOC) diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -2397,3 +2397,34 @@ endif() endif() endfunction() + +macro(setup_host_tool tool_name setting_name exe_var_name target_var_name) + cmake_parse_arguments(ARG "" "SCOPE" "" ${ARGN}) + set(${setting_name}_DEFAULT "${tool_name}") + + if(LLVM_USE_HOST_TOOLS AND LLVM_NATIVE_TOOL_DIR) + if(EXISTS "${LLVM_NATIVE_TOOL_DIR}/${tool_name}${LLVM_HOST_EXECUTABLE_SUFFIX}") + set(${setting_name}_DEFAULT "${LLVM_NATIVE_TOOL_DIR}/${tool_name}${LLVM_HOST_EXECUTABLE_SUFFIX}") + endif() + endif() + + set(${setting_name} "${${setting_name}_DEFAULT}" CACHE + STRING "Host ${tool_name} executable. Saves building if cross-compiling.") + + if(LLVM_USE_HOST_TOOLS) + if(NOT ${setting_name} STREQUAL "${tool_name}") + set(${exe_var_name} ${${setting_name}}) + set(${target_var_name} ${${setting_name}}) + else() + build_native_tool(${tool_name} ${exe_var_name} DEPENDS ${tool_name}) + set(${target_var_name} ${${exe_var_name}}) + endif() + else() + set(${exe_var_name} $) + set(${target_var_name} ${tool_name}) + endif() + if (ARG_SCOPE STREQUAL "PARENT_SCOPE") + set(${exe_var_name} ${${exe_var_name}} PARENT_SCOPE) + set(${target_var_name} ${${target_var_name}} PARENT_SCOPE) + endif() +endmacro() diff --git a/llvm/cmake/modules/TableGen.cmake b/llvm/cmake/modules/TableGen.cmake --- a/llvm/cmake/modules/TableGen.cmake +++ b/llvm/cmake/modules/TableGen.cmake @@ -156,7 +156,13 @@ ${ADD_TABLEGEN_UNPARSED_ARGUMENTS}) set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS}) - set(${project}_TABLEGEN "${target}" CACHE + set(${project}_TABLEGEN_DEFAULT "${target}") + if (LLVM_NATIVE_TOOL_DIR) + if (EXISTS "${LLVM_NATIVE_TOOL_DIR}/${target}${LLVM_HOST_EXECUTABLE_SUFFIX}") + set(${project}_TABLEGEN_DEFAULT "${LLVM_NATIVE_TOOL_DIR}/${target}${LLVM_HOST_EXECUTABLE_SUFFIX}") + endif() + endif() + set(${project}_TABLEGEN "${${project}_TABLEGEN_DEFAULT}" CACHE STRING "Native TableGen executable. Saves building one when cross-compiling.") # Effective tblgen executable to be used: diff --git a/llvm/tools/llvm-config/CMakeLists.txt b/llvm/tools/llvm-config/CMakeLists.txt --- a/llvm/tools/llvm-config/CMakeLists.txt +++ b/llvm/tools/llvm-config/CMakeLists.txt @@ -91,10 +91,18 @@ # Add the dependency on the generation step. add_file_dependencies(${CMAKE_CURRENT_SOURCE_DIR}/llvm-config.cpp ${BUILDVARIABLES_OBJPATH}) -if(CMAKE_CROSSCOMPILING AND NOT LLVM_CONFIG_PATH) - build_native_tool(llvm-config LLVM_CONFIG_PATH) - set(LLVM_CONFIG_PATH "${LLVM_CONFIG_PATH}" CACHE STRING "") +if(CMAKE_CROSSCOMPILING) + if (LLVM_NATIVE_TOOL_DIR AND NOT LLVM_CONFIG_PATH) + if (EXISTS "${LLVM_NATIVE_TOOL_DIR}/llvm-config${LLVM_HOST_EXECUTABLE_SUFFIX}") + set(LLVM_CONFIG_PATH "${LLVM_NATIVE_TOOL_DIR}/llvm-config${LLVM_HOST_EXECUTABLE_SUFFIX}") + endif() + endif() - add_custom_target(NativeLLVMConfig DEPENDS ${LLVM_CONFIG_PATH}) - add_dependencies(llvm-config NativeLLVMConfig) + if (NOT LLVM_CONFIG_PATH) + build_native_tool(llvm-config LLVM_CONFIG_PATH) + set(LLVM_CONFIG_PATH "${LLVM_CONFIG_PATH}" CACHE STRING "") + + add_custom_target(NativeLLVMConfig DEPENDS ${LLVM_CONFIG_PATH}) + add_dependencies(llvm-config NativeLLVMConfig) + endif() endif() diff --git a/mlir/tools/mlir-linalg-ods-gen/CMakeLists.txt b/mlir/tools/mlir-linalg-ods-gen/CMakeLists.txt --- a/mlir/tools/mlir-linalg-ods-gen/CMakeLists.txt +++ b/mlir/tools/mlir-linalg-ods-gen/CMakeLists.txt @@ -14,23 +14,13 @@ MLIRParser ) -set(MLIR_LINALG_ODS_YAML_GEN mlir-linalg-ods-yaml-gen CACHE - STRING "Native mlir-linalg-ods-yaml-gen executable. Saves building one when cross-compiling.") +setup_host_tool(mlir-linalg-ods-yaml-gen MLIR_LINALG_ODS_YAML_GEN MLIR_LINALG_ODS_YAML_GEN_EXE MLIR_LINALG_ODS_YAML_GEN_TARGET SCOPE PARENT_SCOPE) -set(MLIR_LINALG_ODS_YAML_GEN_EXE ${MLIR_LINALG_ODS_YAML_GEN} PARENT_SCOPE) -set(MLIR_LINALG_ODS_YAML_GEN_TARGET mlir-linalg-ods-yaml-gen PARENT_SCOPE) +if(NOT ${MLIR_LINALG_ODS_YAML_GEN_EXE} STREQUAL "mlir-linalg-ods-yaml-gen") + add_custom_target(mlir-linalg-ods-yaml-gen-host DEPENDS ${MLIR_LINALG_ODS_YAML_GEN_EXE}) -if(LLVM_USE_HOST_TOOLS) - if (${MLIR_LINALG_ODS_YAML_GEN} STREQUAL "mlir-linalg-ods-yaml-gen") - build_native_tool(mlir-linalg-ods-yaml-gen MLIR_LINALG_ODS_YAML_GEN_EXE DEPENDS mlir-linalg-ods-yaml-gen) - set(MLIR_LINALG_ODS_YAML_GEN_EXE ${MLIR_LINALG_ODS_YAML_GEN_EXE} PARENT_SCOPE) - - add_custom_target(mlir-linalg-ods-yaml-gen-host DEPENDS ${MLIR_LINALG_ODS_YAML_GEN_EXE}) - set(MLIR_LINALG_ODS_YAML_GEN_TARGET mlir-linalg-ods-yaml-gen-host DEPENDS PARENT_SCOPE) - - if(NOT LLVM_BUILD_UTILS) - set_target_properties(mlir-linalg-ods-yaml-gen PROPERTIES EXCLUDE_FROM_ALL ON) - endif() + if(NOT LLVM_BUILD_UTILS) + set_target_properties(mlir-linalg-ods-yaml-gen PROPERTIES EXCLUDE_FROM_ALL ON) endif() endif()