[clang-tools-extra] d3da906 - [CMake] Allow setting the location of host tools with LLVM_NATIVE_TOOL_DIR

Martin Storsjö via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 18 14:04:18 PST 2023


Author: Martin Storsjö
Date: 2023-01-18T23:56:15+02:00
New Revision: d3da9067d143f3d4ce59b6d9ab4606a8ef1dc937

URL: https://github.com/llvm/llvm-project/commit/d3da9067d143f3d4ce59b6d9ab4606a8ef1dc937
DIFF: https://github.com/llvm/llvm-project/commit/d3da9067d143f3d4ce59b6d9ab4606a8ef1dc937.diff

LOG: [CMake] Allow setting the location of host tools with LLVM_NATIVE_TOOL_DIR

This avoids having to specify the location of all individual tools.
In current builds, one may want to specify LLVM_TABLEGEN, CLANG_TABLEGEN,
LLDB_TABLEGEN, LLVM_CONFIG_PATH, CLANG_PSEUDO_GEN and
CLANG_TIDY_CONFUSABLE_CHARS_GEN; specifying just the base directory
containing all of them is much more convenient.

Factorize the code for setting up use of a tool that is used during
the build (which either is newly built in the same build, or
built in a separate nested cmake build - when cross compiling or
when e.g. optimized tablegen is requested - or used from an existing
prebuilt binary).

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/misc/CMakeLists.txt
    clang-tools-extra/pseudo/include/CMakeLists.txt
    llvm/CMakeLists.txt
    llvm/cmake/modules/AddLLVM.cmake
    llvm/cmake/modules/TableGen.cmake
    llvm/tools/llvm-config/CMakeLists.txt
    mlir/tools/mlir-linalg-ods-gen/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
index 1387f551d6b12..a72362906e0b8 100644
--- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
@@ -3,19 +3,7 @@ set(LLVM_LINK_COMPONENTS
   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 $<TARGET_FILE: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
index ce2f97db78ad9..2334cfa12e337 100644
--- 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 $<TARGET_FILE:clang-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
index fb486bdfcb2fc..28ed35f8b8025 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -665,6 +665,7 @@ set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
 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
index 368f0d1c2a32c..38b0f18fcb4f0 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -2397,3 +2397,31 @@ function(find_first_existing_vc_file path out_var)
     endif()
   endif()
 endfunction()
+
+function(setup_host_tool tool_name setting_name exe_var_name target_var_name)
+  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_name ${${setting_name}})
+      set(target_name ${${setting_name}})
+    else()
+      build_native_tool(${tool_name} exe_name DEPENDS ${tool_name})
+      set(target_name ${exe_name})
+    endif()
+  else()
+    set(exe_name $<TARGET_FILE:${tool_name}>)
+    set(target_name ${tool_name})
+  endif()
+  set(${exe_var_name} "${exe_name}" CACHE STRING "")
+  set(${target_var_name} "${target_name}" CACHE STRING "")
+endfunction()

diff  --git a/llvm/cmake/modules/TableGen.cmake b/llvm/cmake/modules/TableGen.cmake
index c165728cf18e4..f7fe197ad9e66 100644
--- a/llvm/cmake/modules/TableGen.cmake
+++ b/llvm/cmake/modules/TableGen.cmake
@@ -156,7 +156,13 @@ macro(add_tablegen target project)
     ${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
index ed3aa31fcc790..61474d5d2b74a 100644
--- a/llvm/tools/llvm-config/CMakeLists.txt
+++ b/llvm/tools/llvm-config/CMakeLists.txt
@@ -91,10 +91,18 @@ endif()
 # 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
index 88ab69f476234..787a0bb35d7b1 100644
--- a/mlir/tools/mlir-linalg-ods-gen/CMakeLists.txt
+++ b/mlir/tools/mlir-linalg-ods-gen/CMakeLists.txt
@@ -14,23 +14,13 @@ target_link_libraries(mlir-linalg-ods-yaml-gen PRIVATE
   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)
 
-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()
 


        


More information about the cfe-commits mailing list