mirror of
https://github.com/microsoft/GSL.git
synced 2025-04-01 08:56:29 -04:00
Simplify CMake test code
- Use FetchContent to grab googletest - Simplify find_package testing so it's done as a part of ctest - Use the GTest::gtest_main target directly
This commit is contained in:
parent
87f9d76886
commit
607753a657
25
.github/workflows/cmake_find_package.yml
vendored
25
.github/workflows/cmake_find_package.yml
vendored
@ -1,25 +0,0 @@
|
||||
name: cmake_find_package
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
cmake-find-package:
|
||||
name: Build ${{ matrix.os }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ ubuntu-latest, macos-latest ]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: lukka/get-cmake@latest
|
||||
with:
|
||||
cmakeVersion: 3.14.0
|
||||
- name: Configure GSL
|
||||
run: cmake -S . -B build -G "Ninja" -D GSL_TEST=OFF -D CMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/build/install
|
||||
- name: Install GSL
|
||||
run: cmake --build build --target install
|
||||
- name: Test GSL find_package support
|
||||
run: cmake -S tests/ -B build/tests_find_package -G "Ninja" -D CMAKE_PREFIX_PATH=${GITHUB_WORKSPACE}/build/install -D CMAKE_BUILD_TYPE=Release
|
2
.github/workflows/compilers.yml
vendored
2
.github/workflows/compilers.yml
vendored
@ -99,6 +99,6 @@ jobs:
|
||||
gsl_cxx_standard: ${{ matrix.cxx_version }}
|
||||
extra_cmake_args: ${{ matrix.extra_args }}
|
||||
build_cmd: msbuild GSL.sln
|
||||
test_cmd: ctest . --output-on-failure --no-compress-output
|
||||
test_cmd: ctest . --output-on-failure --no-compress-output -C ${{ matrix.build_type }}
|
||||
shell: pwsh
|
||||
|
||||
|
@ -1,16 +1,9 @@
|
||||
cmake_minimum_required(VERSION 3.14...3.16)
|
||||
|
||||
project(GSLTests LANGUAGES CXX)
|
||||
|
||||
set(GSL_CXX_STANDARD "14" CACHE STRING "Use c++ standard")
|
||||
|
||||
set(CMAKE_CXX_STANDARD ${GSL_CXX_STANDARD})
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
include(FindPkgConfig)
|
||||
include(ExternalProject)
|
||||
|
||||
# will make visual studio generated project group files
|
||||
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||
|
||||
@ -22,50 +15,14 @@ if(IOS)
|
||||
add_compile_definitions(GTEST_HAS_DEATH_TEST=1 IOS_PROCESS_DELAY_WORKAROUND=1)
|
||||
endif()
|
||||
|
||||
pkg_search_module(GTestMain gtest_main)
|
||||
if (NOT GTestMain_FOUND)
|
||||
# No pre-installed GTest is available, try to download it using Git.
|
||||
find_package(Git REQUIRED QUIET)
|
||||
include(FetchContent)
|
||||
|
||||
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
|
||||
RESULT_VARIABLE result
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
|
||||
)
|
||||
if(result)
|
||||
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
|
||||
endif()
|
||||
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} --build .
|
||||
RESULT_VARIABLE result
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
|
||||
)
|
||||
if(result)
|
||||
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
|
||||
endif()
|
||||
|
||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||
set(GTestMain_LIBRARIES gtest_main)
|
||||
|
||||
add_subdirectory(
|
||||
${CMAKE_CURRENT_BINARY_DIR}/googletest-src
|
||||
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
|
||||
EXCLUDE_FROM_ALL
|
||||
)
|
||||
endif()
|
||||
|
||||
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
|
||||
find_package(Microsoft.GSL CONFIG REQUIRED)
|
||||
enable_testing()
|
||||
|
||||
if (NOT DEFINED Microsoft.GSL_VERSION)
|
||||
message(FATAL_ERROR "Microsoft.GSL_VERSION not defined!")
|
||||
endif()
|
||||
|
||||
message(STATUS "Microsoft.GSL_VERSION = ${Microsoft.GSL_VERSION}")
|
||||
endif()
|
||||
FetchContent_Declare(googletest
|
||||
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||
GIT_TAG 1b18723e874b256c1e39378c6774a90701d70f7a)
|
||||
# For Windows: Prevent overriding the parent project's compiler/linker settings
|
||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||
FetchContent_MakeAvailable(googletest)
|
||||
|
||||
if (MSVC AND (GSL_CXX_STANDARD GREATER_EQUAL 17))
|
||||
set(GSL_CPLUSPLUS_OPT -Zc:__cplusplus -permissive-)
|
||||
@ -193,11 +150,6 @@ if (WARN_UNSAFE_BUFFER)
|
||||
target_compile_options(gsl_tests_config INTERFACE "-Wno-unsafe-buffer-usage")
|
||||
endif()
|
||||
|
||||
# for tests to find the gtest header
|
||||
target_include_directories(gsl_tests_config SYSTEM INTERFACE
|
||||
googletest/googletest/include
|
||||
)
|
||||
|
||||
add_executable(gsl_tests
|
||||
algorithm_tests.cpp
|
||||
assertion_tests.cpp
|
||||
@ -216,7 +168,7 @@ add_executable(gsl_tests
|
||||
target_link_libraries(gsl_tests
|
||||
Microsoft.GSL::GSL
|
||||
gsl_tests_config
|
||||
${GTestMain_LIBRARIES}
|
||||
GTest::gtest_main
|
||||
)
|
||||
add_test(gsl_tests gsl_tests)
|
||||
|
||||
@ -315,3 +267,21 @@ target_link_libraries(gsl_noexcept_tests
|
||||
gsl_tests_config_noexcept
|
||||
)
|
||||
add_test(gsl_noexcept_tests gsl_noexcept_tests)
|
||||
|
||||
# Test installation
|
||||
set(test_install_dir "${CMAKE_CURRENT_BINARY_DIR}/install")
|
||||
add_test(NAME integration.install
|
||||
COMMAND ${CMAKE_COMMAND} --install ${GSL_BINARY_DIR} --prefix ${test_install_dir} --config $<CONFIG>
|
||||
)
|
||||
|
||||
# Test find_package suppport
|
||||
add_test(NAME integration.find_package
|
||||
COMMAND ${CMAKE_CTEST_COMMAND}
|
||||
--build-and-test ${CMAKE_CURRENT_LIST_DIR}/integration
|
||||
${CMAKE_CURRENT_BINARY_DIR}/find_package
|
||||
--build-generator ${CMAKE_GENERATOR}
|
||||
--build-options -DCMAKE_PREFIX_PATH=${test_install_dir}
|
||||
)
|
||||
|
||||
# Installing comes before testing
|
||||
set_tests_properties(integration.find_package PROPERTIES DEPENDS integration.install)
|
||||
|
@ -1,14 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.0.2)
|
||||
project(googletest-download NONE)
|
||||
|
||||
include(ExternalProject)
|
||||
ExternalProject_Add(googletest
|
||||
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||
GIT_TAG 1b18723e874b256c1e39378c6774a90701d70f7a
|
||||
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
|
||||
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_COMMAND ""
|
||||
INSTALL_COMMAND ""
|
||||
TEST_COMMAND ""
|
||||
)
|
29
tests/integration/CMakeLists.txt
Normal file
29
tests/integration/CMakeLists.txt
Normal file
@ -0,0 +1,29 @@
|
||||
# ~~~
|
||||
# Copyright 2022-2023 The Khronos Group Inc.
|
||||
# Copyright 2022-2023 Valve Corporation
|
||||
# Copyright 2022-2023 LunarG, Inc.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# ~~~
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(GSL_USER LANGUAGES CXX)
|
||||
|
||||
find_package(Microsoft.GSL CONFIG REQUIRED)
|
||||
|
||||
# Users expect this exact target name!
|
||||
if (NOT TARGET Microsoft.GSL::GSL)
|
||||
message(FATAL_ERROR "VMicrosoft.GSL::GSL target not defined")
|
||||
endif()
|
||||
|
||||
if (NOT DEFINED Microsoft.GSL_VERSION)
|
||||
message(FATAL_ERROR "Microsoft.GSL_VERSION not defined!")
|
||||
endif()
|
||||
message(STATUS "Microsoft.GSL_VERSION = ${Microsoft.GSL_VERSION}")
|
||||
|
||||
# The whole point of building this exectuable is to show that linking against
|
||||
# Microsoft.GSL::GSL properly progagates the include directories.
|
||||
#
|
||||
# Users of the library should not have to manually add the include directory.
|
||||
add_executable(foo main.cpp)
|
||||
target_link_libraries(foo PRIVATE Microsoft.GSL::GSL)
|
3
tests/integration/main.cpp
Normal file
3
tests/integration/main.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
#include <gsl/gsl>
|
||||
|
||||
int main() { return 0; }
|
Loading…
x
Reference in New Issue
Block a user