From 7bd23db9b3f262b4881677272a40563e94c83b1c Mon Sep 17 00:00:00 2001 From: Christophe Date: Wed, 6 Sep 2023 18:59:53 +0200 Subject: [PATCH] cmake: Add a function to setup all build targets with the same strictness --- CMakeLists.txt | 16 ------- include/CMakeLists.txt | 7 +++ scripts/CMakeLists.txt | 43 +++++++++++++++++++ src/layer/CMakeLists.txt | 6 +-- src/layer/vk_layer_settings.cpp | 2 +- .../add_subdirectory/vk_enum_string_helper.c | 2 +- tests/generated/CMakeLists.txt | 11 ++--- tests/layer/CMakeLists.txt | 12 ++++++ tests/layer/test_setting_env.cpp | 33 ++++++++------ tests/layer/test_setting_util.cpp | 2 +- tests/vul_dispatch_table/CMakeLists.txt | 8 ++-- 11 files changed, 93 insertions(+), 49 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30a688f..66c497d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,22 +24,6 @@ add_subdirectory(scripts) find_package(VulkanHeaders CONFIG QUIET) option(VUL_WERROR "Treat compiler warnings as errors") -if (VUL_WERROR) - add_compile_options("$,/WX,-Werror>") - if (MSVC) - add_link_options(/WX) - endif() -endif() - -if(${CMAKE_CXX_COMPILER_ID} MATCHES "(GNU|Clang)") - add_compile_options( - -Wall - -Wextra - ) -endif() - -add_library(VulkanUtilityHeaders INTERFACE) -add_library(Vulkan::UtilityHeaders ALIAS VulkanUtilityHeaders) add_subdirectory(src) add_subdirectory(include) diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 1960011..6e1f26e 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -11,6 +11,13 @@ target_sources(VulkanLayerSettings PRIVATE vulkan/layer/vk_layer_settings_ext.h ) +set(CMAKE_FOLDER "${CMAKE_FOLDER}/VulkanUtilityHeaders") + +add_library(VulkanUtilityHeaders INTERFACE) +add_library(Vulkan::UtilityHeaders ALIAS VulkanUtilityHeaders) + +lunarg_target_compiler_configurations(VulkanUtilityHeaders VUL_WERROR) + # https://cmake.org/cmake/help/latest/release/3.19.html#other if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.19") target_sources(VulkanUtilityHeaders PRIVATE diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt index 261d582..db0a7ae 100644 --- a/scripts/CMakeLists.txt +++ b/scripts/CMakeLists.txt @@ -101,6 +101,49 @@ if (UPDATE_DEPS) include("${UPDATE_DEPS_DIR}/helper.cmake") endif() endif() + +# Configure compiler build options common to all targets +function(lunarg_target_compiler_configurations TARGET_NAME WERROR_OPTION) + if (${WERROR_OPTION}) + if (MSVC) + target_compile_options(${TARGET_NAME} INTERFACE /WX) + target_link_options(${TARGET_NAME} INTERFACE /WX) + else() + target_compile_options(${TARGET_NAME} INTERFACE -Werror) + endif() + endif() + + if(${CMAKE_CXX_COMPILER_ID} MATCHES "(GNU|Clang)") + target_compile_options(${TARGET_NAME} INTERFACE + -Wpedantic + -Wall + -Wextra + -Wpointer-arith + ) + if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") + target_compile_options(${TARGET_NAME} INTERFACE + -Wconversion + -Wimplicit-fallthrough + -Wstring-conversion + ) + endif() + elseif(MSVC) + target_compile_options(${TARGET_NAME} INTERFACE + /W4 + /we5038 # Enable warning about MIL ordering in constructors + ) + + # Enforce stricter ISO C++ + target_compile_options(${TARGET_NAME} INTERFACE /permissive-) + + target_compile_options(${TARGET_NAME} INTERFACE $<$:/MP>) # Speed up Visual Studio builds + + # Allow usage of unsafe CRT functions and minimize what Windows.h leaks + target_compile_definitions(${TARGET_NAME} INTERFACE _CRT_SECURE_NO_WARNINGS NOMINMAX WIN32_LEAN_AND_MEAN) + endif() +endfunction() + +# Find Packages if (GOOGLETEST_INSTALL_DIR) list(APPEND CMAKE_PREFIX_PATH ${GOOGLETEST_INSTALL_DIR}) endif() diff --git a/src/layer/CMakeLists.txt b/src/layer/CMakeLists.txt index e28e0a1..d82605d 100644 --- a/src/layer/CMakeLists.txt +++ b/src/layer/CMakeLists.txt @@ -8,7 +8,7 @@ set(CMAKE_FOLDER "${CMAKE_FOLDER}/VulkanLayerSettings") add_library(VulkanLayerSettings STATIC) add_library(Vulkan::LayerSettings ALIAS VulkanLayerSettings) -target_compile_features(VulkanLayerSettings PRIVATE cxx_std_17) +lunarg_target_compiler_configurations(VulkanLayerSettings VUL_WERROR) target_sources(VulkanLayerSettings PRIVATE vk_layer_settings.cpp @@ -22,7 +22,3 @@ target_sources(VulkanLayerSettings PRIVATE # NOTE: Because Vulkan::Headers header files are exposed in the public facing interface # we must expose this library as public to users. target_link_Libraries(VulkanLayerSettings PUBLIC Vulkan::Headers) - -if(WIN32) - target_compile_definitions(VulkanLayerSettings PRIVATE _CRT_SECURE_NO_WARNINGS) -endif() diff --git a/src/layer/vk_layer_settings.cpp b/src/layer/vk_layer_settings.cpp index 6fd652f..b5576b6 100644 --- a/src/layer/vk_layer_settings.cpp +++ b/src/layer/vk_layer_settings.cpp @@ -351,7 +351,7 @@ VkResult vlGetLayerSettingValues(VlLayerSettingSet layerSettingSet, const char * for (std::size_t i = 0, n = values.size(); i < n; ++i) { const std::string &setting_value = vl::ToLower(settings[i]); if (vl::IsFloat(setting_value)) { - values[i] = std::atof(setting_value.c_str()); + values[i] = static_cast(std::atof(setting_value.c_str())); } else { const std::string &message = vl::FormatString("The data provided (%s) is not a floating-point value.", setting_value.c_str()); diff --git a/tests/add_subdirectory/vk_enum_string_helper.c b/tests/add_subdirectory/vk_enum_string_helper.c index 0325fca..c122273 100644 --- a/tests/add_subdirectory/vk_enum_string_helper.c +++ b/tests/add_subdirectory/vk_enum_string_helper.c @@ -11,4 +11,4 @@ const char* foobar() { return string_VkResult(VK_SUCCESS); } // Ensure string_VkPipelineStageFlagBits2 is callable by C users const char* vk_format_feature_2_sampled_image_bit() { return string_VkPipelineStageFlagBits2(VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT); -} \ No newline at end of file +} diff --git a/tests/generated/CMakeLists.txt b/tests/generated/CMakeLists.txt index 68d9f67..1dcb214 100644 --- a/tests/generated/CMakeLists.txt +++ b/tests/generated/CMakeLists.txt @@ -10,16 +10,11 @@ find_package(GTest REQUIRED CONFIG) include(GoogleTest) -if(${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)") - add_compile_options(-Wpedantic -Wall -Wextra -Werror) -endif() - -if (MSVC) - add_compile_options(/W4 /permissive- /WX) -endif() - # Test vk_enum_string_helper.h add_executable(vk_enum_string_helper vk_enum_string_helper.cpp) + +lunarg_target_compiler_configurations(vk_enum_string_helper VUL_WERROR) + target_include_directories(vk_enum_string_helper PRIVATE ${VUL_SOURCE_DIR}/include) target_link_libraries(vk_enum_string_helper PRIVATE Vulkan::Headers diff --git a/tests/layer/CMakeLists.txt b/tests/layer/CMakeLists.txt index 0bb4c39..31751f8 100644 --- a/tests/layer/CMakeLists.txt +++ b/tests/layer/CMakeLists.txt @@ -12,6 +12,8 @@ include(GoogleTest) # test_layer_setting_util add_executable(test_layer_settings_util) +lunarg_target_compiler_configurations(test_layer_settings_util VUL_WERROR) + target_include_directories(test_layer_settings_util PRIVATE ${CMAKE_SOURCE_DIR}/src/layer ) @@ -32,6 +34,8 @@ gtest_discover_tests(test_layer_settings_util) # test_layer_setting_api add_executable(test_layer_settings_api) +lunarg_target_compiler_configurations(test_layer_settings_api VUL_WERROR) + target_include_directories(test_layer_settings_api PRIVATE ${CMAKE_SOURCE_DIR}/src/layer ) @@ -52,6 +56,8 @@ gtest_discover_tests(test_layer_settings_api) # test_layer_setting_cpp add_executable(test_layer_settings_cpp) +lunarg_target_compiler_configurations(test_layer_settings_cpp VUL_WERROR) + target_include_directories(test_layer_settings_cpp PRIVATE ${CMAKE_SOURCE_DIR}/src/layer ) @@ -72,6 +78,8 @@ gtest_discover_tests(test_layer_settings_cpp) # test_layer_setting_env add_executable(test_layer_settings_env) +lunarg_target_compiler_configurations(test_layer_settings_env VUL_WERROR) + target_include_directories(test_layer_settings_env PRIVATE ${CMAKE_SOURCE_DIR}/src/layer ) @@ -92,6 +100,8 @@ gtest_discover_tests(test_layer_settings_env) # test_layer_setting_file add_executable(test_layer_setting_file) +lunarg_target_compiler_configurations(test_layer_setting_file VUL_WERROR) + target_include_directories(test_layer_setting_file PRIVATE ${CMAKE_SOURCE_DIR}/src/layer ) @@ -112,6 +122,8 @@ gtest_discover_tests(test_layer_setting_file) # test_layer_setting_cast add_executable(test_layer_setting_cast) +lunarg_target_compiler_configurations(test_layer_setting_cast VUL_WERROR) + target_include_directories(test_layer_setting_cast PRIVATE ${CMAKE_SOURCE_DIR}/src/layer ) diff --git a/tests/layer/test_setting_env.cpp b/tests/layer/test_setting_env.cpp index 227e54b..f701a37 100644 --- a/tests/layer/test_setting_env.cpp +++ b/tests/layer/test_setting_env.cpp @@ -6,14 +6,23 @@ // // Author(s): // - Christophe Riccio + #include #include "vulkan/layer/vk_layer_settings.h" #include #include +static void SetEnv(const char* value) { +#ifdef _WIN32 + _putenv(value); +#else + putenv(const_cast(value)); +#endif +} + TEST(test_layer_setting_env, EnvVar_TrimNone) { - putenv(const_cast("VK_LUNARG_TEST_MY_SETTING_A=true,false")); + SetEnv("VK_LUNARG_TEST_MY_SETTING_A=true,false"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); @@ -38,7 +47,7 @@ TEST(test_layer_setting_env, EnvVar_TrimNone) { } TEST(test_layer_setting_env, EnvVar_TrimVendor) { - putenv(const_cast("VK_TEST_MY_SETTING_B=true,false")); + SetEnv("VK_TEST_MY_SETTING_B=true,false"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); @@ -63,7 +72,7 @@ TEST(test_layer_setting_env, EnvVar_TrimVendor) { } TEST(test_layer_setting_env, EnvVar_TrimNamespace) { - putenv(const_cast("VK_MY_SETTING_C=true,false")); + SetEnv("VK_MY_SETTING_C=true,false"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); @@ -88,7 +97,7 @@ TEST(test_layer_setting_env, EnvVar_TrimNamespace) { } TEST(test_layer_setting_env, vlGetLayerSettingValues_Bool) { - putenv(const_cast("VK_LUNARG_TEST_MY_SETTING=true,false")); + SetEnv("VK_LUNARG_TEST_MY_SETTING=true,false"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); @@ -123,7 +132,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Bool) { } TEST(test_layer_setting_env, vlGetLayerSettingValues_Int32) { - putenv(const_cast("VK_LUNARG_TEST_MY_SETTING=76,-82")); + SetEnv("VK_LUNARG_TEST_MY_SETTING=76,-82"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); @@ -158,7 +167,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Int32) { } TEST(test_layer_setting_env, vlGetLayerSettingValues_Int64) { - putenv(const_cast("VK_LUNARG_TEST_MY_SETTING=76,-82")); + SetEnv("VK_LUNARG_TEST_MY_SETTING=76,-82"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); @@ -193,7 +202,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Int64) { } TEST(test_layer_setting_env, vlGetLayerSettingValues_Uint32) { - putenv(const_cast("VK_LUNARG_TEST_MY_SETTING=76,82")); + SetEnv("VK_LUNARG_TEST_MY_SETTING=76,82"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); @@ -228,7 +237,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Uint32) { } TEST(test_layer_setting_env, vlGetLayerSettingValues_Uint64) { - putenv(const_cast("VK_LUNARG_TEST_MY_SETTING=76,82")); + SetEnv("VK_LUNARG_TEST_MY_SETTING=76,82"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); @@ -263,7 +272,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Uint64) { } TEST(test_layer_setting_env, vlGetLayerSettingValues_Float) { - putenv(const_cast("VK_LUNARG_TEST_MY_SETTING=76.1f,-82.5f")); + SetEnv("VK_LUNARG_TEST_MY_SETTING=76.1f,-82.5f"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); @@ -297,7 +306,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Float) { } TEST(test_layer_setting_env, vlGetLayerSettingValues_Double) { - putenv(const_cast("VK_LUNARG_TEST_MY_SETTING=76.1,-82.5")); + SetEnv("VK_LUNARG_TEST_MY_SETTING=76.1,-82.5"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); @@ -331,7 +340,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Double) { } TEST(test_layer_setting_env, vlGetLayerSettingValues_Frameset) { - putenv(const_cast("VK_LUNARG_TEST_MY_SETTING=76-100-10,1-100-1")); + SetEnv("VK_LUNARG_TEST_MY_SETTING=76-100-10,1-100-1"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); @@ -371,7 +380,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Frameset) { } TEST(test_layer_setting_env, vlGetLayerSettingValues_String) { - putenv(const_cast("VK_LUNARG_TEST_MY_SETTING=VALUE_A,VALUE_B")); + SetEnv("VK_LUNARG_TEST_MY_SETTING=VALUE_A,VALUE_B"); VlLayerSettingSet layerSettingSet = VK_NULL_HANDLE; vlCreateLayerSettingSet("VK_LAYER_LUNARG_test", nullptr, nullptr, nullptr, &layerSettingSet); diff --git a/tests/layer/test_setting_util.cpp b/tests/layer/test_setting_util.cpp index 024dde5..d18991a 100644 --- a/tests/layer/test_setting_util.cpp +++ b/tests/layer/test_setting_util.cpp @@ -340,7 +340,7 @@ TEST(test_layer_settings_util, ToInt32) { EXPECT_EQ(24, vl::ToInt32("24")); EXPECT_EQ(-24, vl::ToInt32("-24")); EXPECT_EQ(2147483647, vl::ToInt32("2147483647")); - EXPECT_EQ(-2147483648, vl::ToInt32("-2147483648")); + EXPECT_EQ(-2147483647, vl::ToInt32("-2147483647")); EXPECT_EQ(65535, vl::ToInt32("0xFFFF")); EXPECT_EQ(-65535, vl::ToInt32("-0xFFFF")); EXPECT_EQ(15, vl::ToInt32("0xF")); diff --git a/tests/vul_dispatch_table/CMakeLists.txt b/tests/vul_dispatch_table/CMakeLists.txt index ef426a6..bbc1456 100644 --- a/tests/vul_dispatch_table/CMakeLists.txt +++ b/tests/vul_dispatch_table/CMakeLists.txt @@ -3,6 +3,7 @@ # Copyright 2023 LunarG, Inc. # # SPDX-License-Identifier: Apache-2.0 +set(CMAKE_FOLDER "${CMAKE_FOLDER}/VulkanUtilityHeaders/tests") find_package(GTest REQUIRED CONFIG) @@ -10,17 +11,14 @@ include(GoogleTest) add_executable(test_vul_dispatch_table test_interface.cpp) +lunarg_target_compiler_configurations(test_vul_dispatch_table VUL_WERROR) + target_link_libraries(test_vul_dispatch_table PRIVATE GTest::gtest GTest::gtest_main Vulkan::UtilityHeaders ) -if(${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)") - add_compile_options(-Wpedantic -Wall -Wextra -Werror) -endif() - - target_include_directories(test_vul_dispatch_table PRIVATE $) gtest_discover_tests(test_vul_dispatch_table)