Refactor CMake compiler options
authorCharles Giessen <charles@lunarg.com>
Wed, 3 Nov 2021 19:55:29 +0000 (13:55 -0600)
committerCharles Giessen <46324611+charles-lunarg@users.noreply.github.com>
Wed, 9 Mar 2022 22:06:30 +0000 (15:06 -0700)
Create the target loader_common_options which will contain the compiler
commands, options, and definitions that is shared across the code base.
This includes linking to platform_wsi_defines and moving many of the defines
found in the loader/CMakelists.txt up to this location.

Setting the language standard through an interface target is not possible, so
to avoid using the global state modifiers, each target needing to be on a
specific language standard must specify it for themselves.

Note that this commit is in a series and is not intended to work by itself.

CMakeLists.txt
loader/CMakeLists.txt

index cdc870b421fa6f8f20061416d18a75b870f3dad8..03fc7c9a50c7e2f87cf696825e9edb783e3ddf4c 100644 (file)
@@ -29,8 +29,6 @@ project(Vulkan-Loader)
 
 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS API_NAME="Vulkan")
 
-# Enable beta Vulkan extensions
-add_definitions(-DVK_ENABLE_BETA_EXTENSIONS)
 
 if (UPDATE_DEPS)
     find_package(PythonInterp 3 REQUIRED)
@@ -268,37 +266,41 @@ else()
     message(FATAL_ERROR "Unsupported Platform!")
 endif()
 
+add_library(loader_common_options INTERFACE)
+target_link_libraries(loader_common_options INTERFACE platform_wsi_defines)
+
+# Enable beta Vulkan extensions
+target_compile_definitions(loader_common_options INTERFACE VK_ENABLE_BETA_EXTENSIONS)
+
 if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
-    set(COMMON_COMPILE_FLAGS "-Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
-    set(COMMON_COMPILE_FLAGS "${COMMON_COMPILE_FLAGS} -fno-strict-aliasing -fno-builtin-memcmp")
+    target_compile_options(loader_common_options INTERFACE -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -fno-strict-aliasing -fno-builtin-memcmp)
+
+    target_compile_options(loader_common_options INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
 
     # For GCC version 7.1 or greater, we need to disable the implicit fallthrough warning since there's no consistent way to satisfy
     # all compilers until they all accept the C++17 standard
     if(CMAKE_COMPILER_IS_GNUCC)
-        set(COMMON_COMPILE_FLAGS "${COMMON_COMPILE_FLAGS} -Wno-stringop-truncation -Wno-stringop-overflow")
+        target_compile_options(loader_common_options INTERFACE -Wno-stringop-truncation -Wno-stringop-overflow)
         if(CMAKE_CXX_COMPILER_VERSION GREATER_EQUAL 7.1)
-            set(COMMON_COMPILE_FLAGS "${COMMON_COMPILE_FLAGS} -Wimplicit-fallthrough=0")
+            target_compile_options(loader_common_options INTERFACE -Wimplicit-fallthrough=0)
         endif()
     endif()
 
-    if(APPLE)
-        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_COMPILE_FLAGS}")
-               set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_COMPILE_FLAGS} -std=c++11 -fno-rtti")
-    #clang-cl on Windows
-    elseif((CMAKE_C_COMPILER_ID MATCHES "Clang") AND (CMAKE_CXX_SIMULATE_ID MATCHES "MSVC"))
-        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Xclang -std=c99 ${COMMON_COMPILE_FLAGS}")
-        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -std=c++11 -fno-rtti")
-    # clang (not clang-cl) or gcc
-    else()
-        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 ${COMMON_COMPILE_FLAGS}")
-        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-rtti")
+    # clang-cl on Windows
+    if((CMAKE_C_COMPILER_ID MATCHES "Clang") AND (CMAKE_CXX_SIMULATE_ID MATCHES "MSVC"))
+        target_compile_options(loader_common_options INTERFACE -Xclang )
     endif()
 
-    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_COMPILE_FLAGS}")
-
     if(UNIX)
-        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
-        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
+        target_compile_options(loader_common_options INTERFACE -fvisibility=hidden)
+    endif()
+
+    target_compile_options(loader_common_options INTERFACE -Wpointer-arith)
+
+    # Clang (and not gcc) warns about redefining a typedef with the same types, so disable that warning. Note that it will still
+    # throw an error if a typedef is redefined with a different type.
+    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
+        target_compile_options(loader_common_options INTERFACE -Wno-typedef-redefinition)
     endif()
 endif()
 
@@ -309,7 +311,10 @@ if(MSVC)
     # /w34701, /w34703: Warn about potentially uninitialized variables
     # /w34057: Warn about different indirection types.
     # /w34245: Warn about signed/unsigned mismatch.
-    set(MSVC_LOADER_COMPILE_OPTIONS /WX /GR- /w34456 /w34701 /w34703 /w34057 /w34245)
+    target_compile_options(loader_common_options INTERFACE /WX /GR- /w34456 /w34701 /w34703 /w34057 /w34245)
+
+    # Prevent <windows.h> from polluting the code. guards against things like MIN and MAX
+    target_compile_definitions(loader_common_options INTERFACE WIN32_LEAN_AND_MEAN)
 
     # Replace /GR with an empty string, prevents warnings of /GR being overriden by /GR-
     # Newer CMake versions (3.20) have better solutions for this through policy - using the old
index 8721ab73be7ed372653101b184485b65b746a900..9621840b4a360954a1dcbe9990b67d88a4ad1c08 100644 (file)
@@ -34,7 +34,6 @@ endif()
 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/loader_cmake_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/loader_cmake_config.h)
 
 if(WIN32)
-    set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS VK_USE_PLATFORM_WIN32_KHR WIN32_LEAN_AND_MEAN)
     if(MSVC AND NOT MSVC_VERSION LESS 1900)
         # Enable control flow guard
         message(STATUS "Building loader with control flow guard")
@@ -279,15 +278,6 @@ if(WIN32)
     add_dependencies(vulkan loader_asm_gen_files)
 
 else()
-    # Linux and MacOS
-    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpointer-arith")
-
-    # Clang (and not gcc) warns about redefining a typedef with the same types, so disable that warning. Note that it will still
-    # throw an error if a typedef is redefined with a different type.
-    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
-        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-typedef-redefinition")
-    endif()
-
     if(APPLE AND BUILD_STATIC_LOADER)
         add_library(vulkan STATIC ${NORMAL_LOADER_SRCS} ${OPT_LOADER_SRCS})
     else()