https://github.com/wenju-he created 
https://github.com/llvm/llvm-project/pull/186726

When LLVM_TARGETS_TO_BUILD contains host target, runtime build sets 
CMAKE_C_COMPILER to clang-cl on Windows. We should switch to clang and 
llvm-ar/llvm-ranlib for libclc because:
- libclc struggles to pass specific flags to clang-cl MSVC-like interface.
- compile flag handling will be consistent across all host systems.
- libclc build is cross-compilation for offloading targets.

>From 6be7c8cc3793c459d2a2f41e0797333b723db096 Mon Sep 17 00:00:00 2001
From: Wenju He <[email protected]>
Date: Mon, 16 Mar 2026 03:47:46 +0100
Subject: [PATCH] [libclc][CMake] Use clang/llvm-ar on Windows

When LLVM_TARGETS_TO_BUILD contains host target, runtime build sets
CMAKE_C_COMPILER to clang-cl on Windows. We should switch to clang and
llvm-ar/llvm-ranlib for libclc because:
- libclc struggles to pass specific flags to clang-cl MSVC-like interface.
- compile flag handling will be consistent across all host systems.
- libclc build is cross-compilation for offloading targets.
---
 .../cmake/modules/CMakeCLCInformation.cmake   | 24 ++++++++++++++++---
 .../modules/CMakeDetermineCLCCompiler.cmake   | 17 ++++++++++++-
 llvm/runtimes/CMakeLists.txt                  |  2 +-
 3 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/libclc/cmake/modules/CMakeCLCInformation.cmake 
b/libclc/cmake/modules/CMakeCLCInformation.cmake
index f92592221f034..3fb67d91dd1e6 100644
--- a/libclc/cmake/modules/CMakeCLCInformation.cmake
+++ b/libclc/cmake/modules/CMakeCLCInformation.cmake
@@ -11,14 +11,32 @@ if(NOT CMAKE_CLC_COMPILE_OBJECT)
     "<CMAKE_CLC_COMPILER> -x cl <DEFINES> <INCLUDES> <FLAGS> -c -o <OBJECT> 
<SOURCE>")
 endif()
 
+# Finds a required LLVM tool by searching the CLC compiler directory first.
+function(find_llvm_tool name out_var)
+  cmake_path(GET CMAKE_CLC_COMPILER PARENT_PATH llvm_bin_dir)
+  find_program(${out_var}
+    NAMES ${name}
+    HINTS "${llvm_bin_dir}"
+    DOC "libclc: path to the ${name} tool"
+  )
+  if(NOT ${out_var})
+    message(FATAL_ERROR "${name} not found for libclc build.")
+  endif()
+endfunction()
+
+find_llvm_tool(llvm-ar CLC_AR)
+find_llvm_tool(llvm-ranlib CLC_RANLIB)
+
 if(NOT DEFINED CMAKE_CLC_ARCHIVE_CREATE)
-  set(CMAKE_CLC_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>")
+  set(CMAKE_CLC_ARCHIVE_CREATE "${CLC_AR} qc <TARGET> <OBJECTS>")
 endif()
+
 if(NOT DEFINED CMAKE_CLC_ARCHIVE_APPEND)
-  set(CMAKE_CLC_ARCHIVE_APPEND "<CMAKE_AR> q <TARGET> <LINK_FLAGS> <OBJECTS>")
+  set(CMAKE_CLC_ARCHIVE_APPEND "${CLC_AR} q <TARGET> <OBJECTS>")
 endif()
+
 if(NOT DEFINED CMAKE_CLC_ARCHIVE_FINISH)
-  set(CMAKE_CLC_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>")
+  set(CMAKE_CLC_ARCHIVE_FINISH "${CLC_RANLIB} <TARGET>")
 endif()
 
 set(CMAKE_CLC_USE_LINKER_INFORMATION FALSE)
diff --git a/libclc/cmake/modules/CMakeDetermineCLCCompiler.cmake 
b/libclc/cmake/modules/CMakeDetermineCLCCompiler.cmake
index 2138ad85d0059..0ff60eb1b1144 100644
--- a/libclc/cmake/modules/CMakeDetermineCLCCompiler.cmake
+++ b/libclc/cmake/modules/CMakeDetermineCLCCompiler.cmake
@@ -4,7 +4,22 @@ if(NOT CMAKE_CLC_COMPILER)
       "The CLC language requires the C compiler (CMAKE_C_COMPILER) to be "
       "Clang, but CMAKE_C_COMPILER_ID is '${CMAKE_C_COMPILER_ID}'.")
   endif()
-  set(CMAKE_CLC_COMPILER "${CMAKE_C_COMPILER}" CACHE FILEPATH "CLC compiler")
+
+  # Use the regular clang driver if the C compiler is clang-cl.
+  if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND 
CMAKE_C_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
+    cmake_path(GET CMAKE_C_COMPILER PARENT_PATH llvm_bin_dir)
+    find_program(clang_exe clang
+      HINTS "${llvm_bin_dir}"
+      NO_DEFAULT_PATH
+    )
+    if(NOT clang_exe)
+      message(FATAL_ERROR "clang-cl detected, but clang not found in 
${llvm_bin_dir}")
+    endif()
+    set(clc_compiler "${clang_exe}")
+  else()
+    set(clc_compiler "${CMAKE_C_COMPILER}")
+  endif()
+  set(CMAKE_CLC_COMPILER "${clc_compiler}" CACHE FILEPATH "libclc: CLC 
compiler")
 endif()
 
 mark_as_advanced(CMAKE_CLC_COMPILER)
diff --git a/llvm/runtimes/CMakeLists.txt b/llvm/runtimes/CMakeLists.txt
index fba0c7a01f972..21c7b05b3443a 100644
--- a/llvm/runtimes/CMakeLists.txt
+++ b/llvm/runtimes/CMakeLists.txt
@@ -554,7 +554,7 @@ if(build_runtimes)
 
   # TODO: We need to consider passing it as 
'-DRUNTIMES_x86_64_LLVM_ENABLE_RUNTIMES'.
   if("libclc" IN_LIST LLVM_ENABLE_RUNTIMES)
-    foreach(dep clang llvm-as llvm-link opt)
+    foreach(dep clang llvm-as llvm-link opt llvm-ar)
       if(TARGET ${dep})
         list(APPEND extra_deps ${dep})
       endif()

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to