Skip to content

Commit

Permalink
cmake: Add a function to setup all build targets with the same strict…
Browse files Browse the repository at this point in the history
…ness
  • Loading branch information
christophe-lunarg committed Sep 7, 2023
1 parent 4d4015e commit f2e91ac
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 45 deletions.
15 changes: 2 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,12 @@ add_subdirectory(scripts)
find_package(VulkanHeaders CONFIG QUIET)

option(VUL_WERROR "Treat compiler warnings as errors")
if (VUL_WERROR)
add_compile_options("$<IF:$<CXX_COMPILER_ID:MSVC>,/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)

lunarg_target_compiler_configurations(VulkanUtilityHeaders VUL_WERROR)

add_subdirectory(src)
add_subdirectory(include)

Expand Down
43 changes: 43 additions & 0 deletions scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
target_compile_features(${TARGET_NAME} INTERFACE cxx_std_17)

if (${WERROR_OPTION})
target_compile_options(${TARGET_NAME} INTERFACE "$<IF:$<CXX_COMPILER_ID:MSVC>,/WX,-Werror>")
if (MSVC)
target_link_options(${TARGET_NAME} INTERFACE /WX)
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 $<$<BOOL:${MSVC_IDE}>:/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()
Expand Down
6 changes: 1 addition & 5 deletions src/layer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
2 changes: 1 addition & 1 deletion src/layer/vk_layer_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(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());
Expand Down
11 changes: 3 additions & 8 deletions tests/generated/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/layer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
)
Expand Down
33 changes: 21 additions & 12 deletions tests/layer/test_setting_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@
//
// Author(s):
// - Christophe Riccio <[email protected]>

#include <gtest/gtest.h>

#include "vulkan/layer/vk_layer_settings.h"
#include <vector>
#include <cstdlib>

static void SetEnv(const char* value) {
#ifdef _WIN32
_putenv(value);
#else
putenv(const_cast<char *>(value));
#endif
}

TEST(test_layer_setting_env, EnvVar_TrimNone) {
putenv(const_cast<char *>("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);
Expand All @@ -38,7 +47,7 @@ TEST(test_layer_setting_env, EnvVar_TrimNone) {
}

TEST(test_layer_setting_env, EnvVar_TrimVendor) {
putenv(const_cast<char *>("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);
Expand All @@ -63,7 +72,7 @@ TEST(test_layer_setting_env, EnvVar_TrimVendor) {
}

TEST(test_layer_setting_env, EnvVar_TrimNamespace) {
putenv(const_cast<char *>("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);
Expand All @@ -88,7 +97,7 @@ TEST(test_layer_setting_env, EnvVar_TrimNamespace) {
}

TEST(test_layer_setting_env, vlGetLayerSettingValues_Bool) {
putenv(const_cast<char *>("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);
Expand Down Expand Up @@ -123,7 +132,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Bool) {
}

TEST(test_layer_setting_env, vlGetLayerSettingValues_Int32) {
putenv(const_cast<char *>("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);
Expand Down Expand Up @@ -158,7 +167,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Int32) {
}

TEST(test_layer_setting_env, vlGetLayerSettingValues_Int64) {
putenv(const_cast<char *>("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);
Expand Down Expand Up @@ -193,7 +202,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Int64) {
}

TEST(test_layer_setting_env, vlGetLayerSettingValues_Uint32) {
putenv(const_cast<char *>("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);
Expand Down Expand Up @@ -228,7 +237,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Uint32) {
}

TEST(test_layer_setting_env, vlGetLayerSettingValues_Uint64) {
putenv(const_cast<char *>("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);
Expand Down Expand Up @@ -263,7 +272,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Uint64) {
}

TEST(test_layer_setting_env, vlGetLayerSettingValues_Float) {
putenv(const_cast<char *>("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);
Expand Down Expand Up @@ -297,7 +306,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Float) {
}

TEST(test_layer_setting_env, vlGetLayerSettingValues_Double) {
putenv(const_cast<char *>("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);
Expand Down Expand Up @@ -331,7 +340,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Double) {
}

TEST(test_layer_setting_env, vlGetLayerSettingValues_Frameset) {
putenv(const_cast<char *>("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);
Expand Down Expand Up @@ -371,7 +380,7 @@ TEST(test_layer_setting_env, vlGetLayerSettingValues_Frameset) {
}

TEST(test_layer_setting_env, vlGetLayerSettingValues_String) {
putenv(const_cast<char *>("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);
Expand Down
2 changes: 1 addition & 1 deletion tests/layer/test_setting_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
7 changes: 2 additions & 5 deletions tests/vul_dispatch_table/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

gtest_discover_tests(test_vul_dispatch_table)

0 comments on commit f2e91ac

Please sign in to comment.