]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
cmake: fix compile warnings for clang-cl
authorzjyhjqs <zjyhjqs@Hotmail.com>
Wed, 16 Oct 2024 16:17:38 +0000 (00:17 +0800)
committerViktor Szakats <commit@vsz.me>
Mon, 21 Oct 2024 10:47:47 +0000 (12:47 +0200)
clang-cl is an alternative command-line interface to Clang, designed
for compatibility with the Visual C++ compiler, `cl.exe`:
https://clang.llvm.org/docs/UsersManual.html#clang-cl

The way to test clang-cl in CMake:
- `CMAKE_<LANGUAGE>_COMPILER_ID`: "Clang"
- `CMAKE_<LANGUAGE>_COMPILER_FRONTEND_VARIANT`: "MSVC"

Note: `CMAKE_<LANGUAGE>_COMPILER_FRONTEND_VARIANT` was introduced since
CMake 3.14, but the variable `MSVC` works fine here.

https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_FRONTEND_VARIANT.html
https://cmake.org/cmake/help/latest/variable/MSVC.html

Closes #15337

CMake/PickyWarnings.cmake
CMakeLists.txt

index 57b176c3fed41c765f27e09a1e6ebd56e5a03ef8..d20908827682f59597c2c4cb9b931c595a2857f2 100644 (file)
@@ -30,17 +30,17 @@ if(CURL_WERROR AND
      NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0 AND
      NOT CMAKE_VERSION VERSION_LESS 3.23.0) OR  # to avoid check_symbol_exists() conflicting with GCC -pedantic-errors
    CMAKE_C_COMPILER_ID MATCHES "Clang"))
-  set(WPICKY "${WPICKY} -pedantic-errors")
+  list(APPEND WPICKY "-pedantic-errors")
 endif()
 
 if(APPLE AND
    (CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
    (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
-  set(WPICKY "${WPICKY} -Werror=partial-availability")  # clang 3.6  appleclang 6.3
+  list(APPEND WPICKY "-Werror=partial-availability")  # clang 3.6  appleclang 6.3
 endif()
 
 if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
-  set(WPICKY "${WPICKY} -Werror-implicit-function-declaration")  # clang 1.0  gcc 2.95
+  list(APPEND WPICKY "-Werror-implicit-function-declaration")  # clang 1.0  gcc 2.95
 endif()
 
 if(PICKY_COMPILER)
@@ -221,7 +221,7 @@ if(PICKY_COMPILER)
     #
 
     foreach(_ccopt IN LISTS WPICKY_ENABLE)
-      set(WPICKY "${WPICKY} ${_ccopt}")
+      list(APPEND WPICKY "${_ccopt}")
     endforeach()
 
     foreach(_ccopt IN LISTS WPICKY_DETECT)
@@ -233,13 +233,24 @@ if(PICKY_COMPILER)
       string(REPLACE "-Wno-" "-W" _ccopt_on "${_ccopt}")
       check_c_compiler_flag(${_ccopt_on} ${_optvarname})
       if(${_optvarname})
-        set(WPICKY "${WPICKY} ${_ccopt}")
+        list(APPEND WPICKY "${_ccopt}")
       endif()
     endforeach()
   endif()
 endif()
 
+# clang-cl
+if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND MSVC)
+  # list(TRANSFORM WPICKY PREPEND "/clang:") # since CMake 3.12
+  set(_wpicky "")
+  foreach(_ccopt IN LISTS WPICKY)
+    list(APPEND _wpicky "/clang:${_ccopt}")
+  endforeach()
+  set(WPICKY ${_wpicky})
+endif()
+
 if(WPICKY)
+  string(REPLACE ";" " " WPICKY "${WPICKY}")
   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WPICKY}")
   message(STATUS "Picky compiler options:${WPICKY}")
 endif()
index 4b13b7d604115fbf25d91a7f9e3d0b3126cc337d..5a75723da3c71d2452718eede35cfa191441a8f0 100644 (file)
@@ -1796,7 +1796,7 @@ if(MSVC)
   endif()
 
   # Use multithreaded compilation on VS 2008+
-  if(MSVC_VERSION GREATER_EQUAL 1500)
+  if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER_EQUAL 1500)
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
   endif()
 endif()