From bedbde277e80cbca3d67c4f07fe405e966486d01 Mon Sep 17 00:00:00 2001 From: Juan Ramos Date: Fri, 31 Mar 2023 12:37:22 -0600 Subject: [PATCH] cmake: Add/test find_package/add_subdirectory support --- .github/workflows/vul.yml | 16 ++++++-- CMakeLists.txt | 39 ++++++++++--------- include/CMakeLists.txt | 19 +++++++++ .../vulkan/layer}/vk_layer_settings.h | 7 ++-- {layer_utils => src}/CMakeLists.txt | 14 +++---- {layer_utils => src}/vk_layer_settings.cpp | 3 +- {layer_utils/tests => tests}/CMakeLists.txt | 14 +++---- tests/add_subdirectory/CMakeLists.txt | 18 +++++++++ tests/add_subdirectory/client.cpp | 3 ++ tests/find_package/CMakeLists.txt | 20 ++++++++++ {layer_utils/tests => tests}/test_util.cpp | 5 +-- 11 files changed, 113 insertions(+), 45 deletions(-) create mode 100644 include/CMakeLists.txt rename {layer_utils => include/vulkan/layer}/vk_layer_settings.h (94%) rename {layer_utils => src}/CMakeLists.txt (71%) rename {layer_utils => src}/vk_layer_settings.cpp (99%) rename {layer_utils/tests => tests}/CMakeLists.txt (76%) create mode 100644 tests/add_subdirectory/CMakeLists.txt create mode 100644 tests/add_subdirectory/client.cpp create mode 100644 tests/find_package/CMakeLists.txt rename {layer_utils/tests => tests}/test_util.cpp (89%) diff --git a/.github/workflows/vul.yml b/.github/workflows/vul.yml index 4f8d8f6..f549528 100644 --- a/.github/workflows/vul.yml +++ b/.github/workflows/vul.yml @@ -22,13 +22,13 @@ on: - main jobs: - build_test: + build_and_test: runs-on: ${{matrix.os}} strategy: fail-fast: false matrix: config: [Debug, Release] - os: [ ubuntu-latest, windows-latest, macos-latest ] + os: [ ubuntu-20.04, ubuntu-22.04, windows-latest, macos-latest ] steps: - uses: actions/checkout@v3 - uses: actions/setup-python@v4 @@ -37,7 +37,17 @@ jobs: - name: Configure run: cmake -S. -B build -D VUL_WERROR=ON -D VUL_TESTS=ON -D CMAKE_BUILD_TYPE=${{matrix.config}} -D UPDATE_DEPS=ON - name: Build - run: cmake --build build --config ${{matrix.config}} + run: cmake --build build --config ${{matrix.config}} --verbose - name: Tests working-directory: ./build run: ctest -C ${{matrix.config}} --output-on-failure + - name: Install + run: cmake --install build --prefix ${{ github.workspace }}/install --config ${{matrix.config}} + - name: Test find_package support + run: | + cmake -S tests/find_package -B tests/find_package/build -D CMAKE_PREFIX_PATH="${{ github.workspace }}/install;${{ github.workspace }}/external/Vulkan-Headers/build/install" -D CMAKE_BUILD_TYPE=${{matrix.config}} + cmake --build tests/find_package/build --config ${{matrix.config}} --verbose + - name: Test add_subdirectory support + run: | + cmake -S tests/add_subdirectory -B tests/add_subdirectory/build -D CMAKE_BUILD_TYPE=${{matrix.config}} -D GITHUB_VULKAN_HEADER_SOURCE_DIR=${{ github.workspace }}/external/Vulkan-Headers/ + cmake --build tests/add_subdirectory/build --config ${{matrix.config}} --verbose diff --git a/CMakeLists.txt b/CMakeLists.txt index ef357e7..4b17b9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,21 +16,16 @@ cmake_minimum_required(VERSION 3.17.2) project(VUL LANGUAGES CXX) -include(GNUInstallDirs) +string(COMPARE EQUAL ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR} VUL_IS_TOP_LEVEL) # Remove when min is 3.21 -set_property(GLOBAL PROPERTY USE_FOLDERS ON) # Remove when min is 3.26, see CMP0143 +set_property(GLOBAL PROPERTY USE_FOLDERS ON) # Remove when min is 3.26, see CMP0143 -option(VUL_TESTS "Build utility libraries tests") - -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_POSITION_INDEPENDENT_CODE ON) -set(CMAKE_CXX_VISIBILITY_PRESET "hidden") -set(CMAKE_VISIBILITY_INLINES_HIDDEN "YES") -# NOTE: The idiom for open source projects is to not enable warnings as errors. -# This reduces problems for users who simply want to build the repository. +add_subdirectory(scripts) + +find_package(VulkanHeaders CONFIG QUIET) + option(VUL_WERROR "Treat compiler warnings as errors") if (VUL_WERROR) add_compile_options("$,/WX,-Werror>") @@ -39,14 +34,20 @@ if (VUL_WERROR) endif() endif() -find_package(PythonInterp 3.7.2 REQUIRED) +add_subdirectory(src) +add_subdirectory(include) -add_subdirectory(scripts) +if (VUL_IS_TOP_LEVEL) + option(VUL_TESTS "Build tests") + if (VUL_TESTS) + enable_testing() + add_subdirectory(tests) + endif() -find_package(VulkanHeaders REQUIRED CONFIG QUIET) - -if (VUL_TESTS) - enable_testing() + include(GNUInstallDirs) + + install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/vulkan" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + set_target_properties(VulkanLayerUtils PROPERTIES EXPORT_NAME "LayerUtils") + install(TARGETS VulkanLayerUtils EXPORT VulkanUtilityLibrariesConfig INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + install(EXPORT VulkanUtilityLibrariesConfig DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/VulkanUtilityLibraries NAMESPACE Vulkan::) endif() - -add_subdirectory(layer_utils) diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt new file mode 100644 index 0000000..e349bff --- /dev/null +++ b/include/CMakeLists.txt @@ -0,0 +1,19 @@ +# Copyright (c) 2023 Valve Corporation +# Copyright (c) 2023 LunarG, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +target_include_directories(VulkanLayerUtils PUBLIC $) + +target_compile_features(VulkanLayerUtils PUBLIC cxx_std_17) + +target_sources(VulkanLayerUtils PRIVATE vulkan/layer/vk_layer_settings.h) diff --git a/layer_utils/vk_layer_settings.h b/include/vulkan/layer/vk_layer_settings.h similarity index 94% rename from layer_utils/vk_layer_settings.h rename to include/vulkan/layer/vk_layer_settings.h index bf2d568..ebb8469 100644 --- a/layer_utils/vk_layer_settings.h +++ b/include/vulkan/layer/vk_layer_settings.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2015-2023 The Khronos Group Inc. - * Copyright (c) 2015-2023 Valve Corporation - * Copyright (c) 2015-2023 LunarG, Inc. + * Copyright (c) 2023 The Khronos Group Inc. + * Copyright (c) 2023 Valve Corporation + * Copyright (c) 2023 LunarG, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,4 +60,5 @@ Strings GetLayerSettingStrings(const char *layer_key, const char *setting_key); // Query setting data for LIST setting type in the layer manifest List GetLayerSettingList(const char *layer_key, const char *setting_key); + } // namespace vku diff --git a/layer_utils/CMakeLists.txt b/src/CMakeLists.txt similarity index 71% rename from layer_utils/CMakeLists.txt rename to src/CMakeLists.txt index 21d4a15..6ff8390 100644 --- a/layer_utils/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,21 +12,19 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +set(CMAKE_FOLDER "${CMAKE_FOLDER}/libs") add_library(VulkanLayerUtils STATIC) add_library(Vulkan::LayerUtils ALIAS VulkanLayerUtils) target_sources(VulkanLayerUtils PRIVATE vk_layer_settings.cpp - vk_layer_settings.h ) -target_link_Libraries(VulkanLayerUtils PRIVATE Vulkan::Headers) + +# 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(VulkanLayerUtils PUBLIC Vulkan::Headers) if(WIN32) - target_compile_definitions(VulkanLayerUtils PUBLIC _CRT_SECURE_NO_WARNINGS) -endif() - -if(VUL_TESTS) - enable_testing() - add_subdirectory(tests) + target_compile_definitions(VulkanLayerUtils PRIVATE _CRT_SECURE_NO_WARNINGS) endif() diff --git a/layer_utils/vk_layer_settings.cpp b/src/vk_layer_settings.cpp similarity index 99% rename from layer_utils/vk_layer_settings.cpp rename to src/vk_layer_settings.cpp index 173eeb3..28663a2 100644 --- a/layer_utils/vk_layer_settings.cpp +++ b/src/vk_layer_settings.cpp @@ -22,8 +22,7 @@ * - Courtney Goeltzenleuchter * - Tobin Ehlis */ - -#include "vk_layer_settings.h" +#include "vulkan/layer/vk_layer_settings.h" #include #include diff --git a/layer_utils/tests/CMakeLists.txt b/tests/CMakeLists.txt similarity index 76% rename from layer_utils/tests/CMakeLists.txt rename to tests/CMakeLists.txt index 85cc6f5..b1ee20f 100644 --- a/layer_utils/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,23 +12,23 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +set(CMAKE_FOLDER "${CMAKE_FOLDER}/tests") find_package(GTest REQUIRED CONFIG) -include(GoogleTest) -add_executable(tests_util) +add_executable(vul_tests) -target_sources(tests_util PRIVATE +target_sources(vul_tests PRIVATE test_util.cpp ) -target_link_libraries(tests_util PRIVATE +target_link_libraries(vul_tests PRIVATE GTest::gtest GTest::gtest_main Vulkan::Headers - VulkanLayerUtils + Vulkan::LayerUtils ) -set_target_properties(tests_util PROPERTIES FOLDER "VulkanLayerUtils-tests") +include(GoogleTest) -gtest_discover_tests(tests_util) +gtest_discover_tests(vul_tests) diff --git a/tests/add_subdirectory/CMakeLists.txt b/tests/add_subdirectory/CMakeLists.txt new file mode 100644 index 0000000..298f049 --- /dev/null +++ b/tests/add_subdirectory/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.17) + +project(TEST_FIND_PACKAGE LANGUAGES CXX) + +add_library(add_subdirectory_example STATIC) + +target_sources(add_subdirectory_example PRIVATE client.cpp) + +# NOTE: Because VulkanHeaders is a PUBLIC dependency it needs to be found prior to VulkanUtilityLibraries +add_subdirectory(${GITHUB_VULKAN_HEADER_SOURCE_DIR} ${PROJECT_BINARY_DIR}/headers) + +add_subdirectory(${PROJECT_SOURCE_DIR}/../../ ${PROJECT_BINARY_DIR}/vul) + +if (NOT TARGET Vulkan::LayerUtils) + message(FATAL_ERROR "Vulkan::LayerUtils target not defined!") +endif() + +target_link_libraries(add_subdirectory_example PRIVATE Vulkan::LayerUtils) diff --git a/tests/add_subdirectory/client.cpp b/tests/add_subdirectory/client.cpp new file mode 100644 index 0000000..c754873 --- /dev/null +++ b/tests/add_subdirectory/client.cpp @@ -0,0 +1,3 @@ +#include + +bool foobar() { return vku::IsLayerSetting("layer_name", "setting_key"); } \ No newline at end of file diff --git a/tests/find_package/CMakeLists.txt b/tests/find_package/CMakeLists.txt new file mode 100644 index 0000000..e219f27 --- /dev/null +++ b/tests/find_package/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.17) + +project(TEST_FIND_PACKAGE LANGUAGES CXX) + +add_library(find_package_example STATIC) + +target_sources(find_package_example PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/../add_subdirectory/client.cpp +) + +# NOTE: Because VulkanHeaders is a PUBLIC dependency it needs to be found prior to VulkanUtilityLibraries +find_package(VulkanHeaders REQUIRED CONFIG) + +find_package(VulkanUtilityLibraries REQUIRED CONFIG) + +if (NOT TARGET Vulkan::LayerUtils) + message(FATAL_ERROR "Vulkan::LayerUtils target not defined!") +endif() + +target_link_libraries(find_package_example PRIVATE Vulkan::LayerUtils) diff --git a/layer_utils/tests/test_util.cpp b/tests/test_util.cpp similarity index 89% rename from layer_utils/tests/test_util.cpp rename to tests/test_util.cpp index c89f05e..a0d6b4b 100644 --- a/layer_utils/tests/test_util.cpp +++ b/tests/test_util.cpp @@ -18,12 +18,11 @@ * - Christophe Riccio */ -#include #include -#include "../vk_layer_settings.h" +#include "vulkan/layer/vk_layer_settings.h" -TEST(test_library_util, Something) { +TEST(test_library_util, Something) { EXPECT_TRUE(true); EXPECT_FALSE(vku::IsLayerSetting("layer_name", "setting_key")); }