Correctly find builtins library with clang-cl
authorTobias Hieta <tobias@hieta.se>
Tue, 1 Mar 2022 07:22:53 +0000 (08:22 +0100)
committerTobias Hieta <tobias@hieta.se>
Mon, 14 Mar 2022 06:49:29 +0000 (07:49 +0100)
When using COMPILER_RT_USE_BUILTINS_LIBRARY=ON and clang-cl there
where several places where it didn't work as expected.

First -print-libgcc-file-name has to be prefixed with /clang:

Then the regex that matched the builtins library was wrong because
the builtins library is called clang_rt.builtins_<arch>.lib
and the regex only matched libclang_rt.builtins_arch.a

With this commit you can use a runtime build on Windows with this
option enabled.

Reviewed By: phosek

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

cmake/Modules/HandleCompilerRT.cmake

index 178e4e5..51409d6 100644 (file)
@@ -54,8 +54,12 @@ function(find_compiler_rt_library name variable)
     get_property(cxx_flags CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
     string(REPLACE " " ";" cxx_flags "${cxx_flags}")
     list(APPEND clang_command ${cxx_flags})
+    set(cmd_prefix "")
+    if(MSVC AND ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
+      set(cmd_prefix "/clang:")
+    endif()
     execute_process(
-      COMMAND ${clang_command} "--rtlib=compiler-rt" "-print-libgcc-file-name"
+      COMMAND ${clang_command} "${cmd_prefix}--rtlib=compiler-rt" "${cmd_prefix}-print-libgcc-file-name"
       RESULT_VARIABLE had_error
       OUTPUT_VARIABLE library_file
     )
@@ -72,7 +76,7 @@ function(find_compiler_rt_library name variable)
       set(dirname "${resource_dir}/lib/darwin")
     endif()
     get_filename_component(basename ${library_file} NAME)
-    if(basename MATCHES "libclang_rt\.([a-z0-9_\-]+)\.a")
+    if(basename MATCHES ".*clang_rt\.([a-z0-9_\-]+)\.(a|lib)")
       set(from_name ${CMAKE_MATCH_1})
       get_component_name(${CMAKE_MATCH_1} to_name)
       string(REPLACE "${from_name}" "${to_name}" basename "${basename}")
@@ -90,7 +94,7 @@ function(find_compiler_rt_library name variable)
     # path and then checking if the resultant path exists. The result of
     # this check is also cached by cache_compiler_rt_library.
     set(library_file "${COMPILER_RT_LIBRARY_builtins_${target}}")
-    if(library_file MATCHES ".*libclang_rt\.([a-z0-9_\-]+)\.a")
+    if(library_file MATCHES ".*clang_rt\.([a-z0-9_\-]+)\.(a|lib)")
       set(from_name ${CMAKE_MATCH_0})
       get_component_name(${name} to_name)
       string(REPLACE "${from_name}" "${to_name}" library_file "${library_file}")