Jelajahi Sumber

CMake: Enable using clang-cl on windows

detect clang with MSVC frontend using CMAKE_CXX_COMPILER_FRONTEND_VARIANT
Samuel Nicholas 8 bulan lalu
induk
melakukan
ef9778a392
2 mengubah file dengan 26 tambahan dan 4 penghapusan
  1. 1 0
      CMakeLists.txt
  2. 25 4
      cmake/common_compiler_flags.cmake

+ 1 - 0
CMakeLists.txt

@@ -50,6 +50,7 @@ project( godot-cpp
         HOMEPAGE_URL "https://github.com/godotengine/godot-cpp"
         HOMEPAGE_URL "https://github.com/godotengine/godot-cpp"
         LANGUAGES CXX)
         LANGUAGES CXX)
 
 
+compiler_detection()
 godotcpp_generate()
 godotcpp_generate()
 
 
 # Test Example
 # Test Example

+ 25 - 4
cmake/common_compiler_flags.cmake

@@ -2,9 +2,10 @@
 Common Compiler Flags
 Common Compiler Flags
 ---------------------
 ---------------------
 
 
-This file contains a single function to configure platform agnostic compiler
-flags like optimization levels, warnings, and features. For platform specific
-flags look to each of the ``cmake/<platform>.cmake`` files.
+This file contains host platform toolchain and target platform agnostic
+configuration. It includes flags like optimization levels, warnings, and
+features. For target platform specific flags look to each of the
+``cmake/<platform>.cmake`` files.
 
 
 ]=======================================================================]
 ]=======================================================================]
 
 
@@ -24,6 +25,25 @@ set( GNU_GT_V11 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,11>" )
 set( GNU_LT_V11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" )
 set( GNU_LT_V11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" )
 set( GNU_GE_V12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12>" )
 set( GNU_GE_V12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12>" )
 
 
+#[[ Check for clang-cl with MSVC frontend
+The compiler is tested and set when the project command is called.
+The variable CXX_COMPILER_FRONTEND_VARIANT was introduced in 3.14
+The generator expression $<CXX_COMPILER_FRONTEND_VARIANT> wasn't introduced
+until CMake 3.30 so we can't use it yet.
+
+So to support clang downloaded from llvm.org which uses the MSVC frontend
+by default, we need to test for it. ]]
+function( compiler_detection )
+    if( ${CMAKE_CXX_COMPILER_ID} STREQUAL Clang )
+        if( ${CMAKE_CXX_COMPILER_FRONTEND_VARIANT} STREQUAL MSVC )
+            message( "Using clang-cl" )
+            set( IS_CLANG   "0" PARENT_SCOPE )
+            set( IS_MSVC    "1" PARENT_SCOPE )
+            set( NOT_MSVC   "0" PARENT_SCOPE )
+        endif ()
+    endif ()
+endfunction(  )
+
 function( common_compiler_flags TARGET_NAME )
 function( common_compiler_flags TARGET_NAME )
 
 
     target_compile_features(${TARGET_NAME}
     target_compile_features(${TARGET_NAME}
@@ -60,7 +80,8 @@ function( common_compiler_flags TARGET_NAME )
 
 
         # MSVC only
         # MSVC only
         $<${IS_MSVC}:
         $<${IS_MSVC}:
-            "/MP ${PROC_N}"
+            # /MP isn't valid for clang-cl with msvc frontend
+            $<$<CXX_COMPILER_ID:MSVC>:/MP${PROC_N}>
             /W4
             /W4
 
 
             # Disable warnings which we don't plan to fix.
             # Disable warnings which we don't plan to fix.