From f7da2e41f058bbde382d042c447699f6edd16aa8 Mon Sep 17 00:00:00 2001 From: jpr42 <109434725+jpr42@users.noreply.github.com> Date: Mon, 6 Feb 2023 12:16:37 -0700 Subject: [PATCH] Raise CMake min to 3.14 (#1083) - Explicitly test package manager support with CMake 3.14 and Ninja - Remove gsl_install.cmake - Simplify installation code --- .github/workflows/cmake_find_package.yml | 25 ++++++++++++++++++++++ CMakeLists.txt | 27 +++++++++++++++++++----- cmake/gsl_install.cmake | 26 ----------------------- tests/CMakeLists.txt | 17 +++++++++------ 4 files changed, 57 insertions(+), 38 deletions(-) create mode 100644 .github/workflows/cmake_find_package.yml delete mode 100644 cmake/gsl_install.cmake diff --git a/.github/workflows/cmake_find_package.yml b/.github/workflows/cmake_find_package.yml new file mode 100644 index 0000000..5155d9c --- /dev/null +++ b/.github/workflows/cmake_find_package.yml @@ -0,0 +1,25 @@ +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@v3 + - 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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 52077a1..bc1f38c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.8...3.16) +cmake_minimum_required(VERSION 3.14...3.16) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/") include(gsl_functions) @@ -22,11 +22,28 @@ add_subdirectory(include) # Add natvis file gsl_add_native_visualizer_support() -if (GSL_INSTALL) - include(gsl_install) -endif() - if (GSL_TEST) enable_testing() add_subdirectory(tests) endif() + +if (GSL_INSTALL) + include(GNUInstallDirs) + include(CMakePackageConfigHelpers) + + install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/gsl" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + + set(export_name "Microsoft.GSLConfig") + set(namespace "Microsoft.GSL::") + set(cmake_files_install_dir ${CMAKE_INSTALL_DATADIR}/cmake/Microsoft.GSL) + + install(TARGETS GSL EXPORT ${export_name} INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + install(EXPORT ${export_name} NAMESPACE ${namespace} DESTINATION ${cmake_files_install_dir}) + export(TARGETS GSL NAMESPACE ${namespace} FILE ${export_name}.cmake) + + set(gls_config_version "${CMAKE_CURRENT_BINARY_DIR}/Microsoft.GSLConfigVersion.cmake") + + write_basic_package_version_file(${gls_config_version} COMPATIBILITY SameMajorVersion ARCH_INDEPENDENT) + + install(FILES ${gls_config_version} DESTINATION ${cmake_files_install_dir}) +endif() diff --git a/cmake/gsl_install.cmake b/cmake/gsl_install.cmake deleted file mode 100644 index 2b9c36d..0000000 --- a/cmake/gsl_install.cmake +++ /dev/null @@ -1,26 +0,0 @@ -include(GNUInstallDirs) -include(CMakePackageConfigHelpers) - -# Install header files -install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/gsl" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) - -set(export_name "Microsoft.GSLConfig") -set(namespace "Microsoft.GSL::") -set(cmake_files_install_dir ${CMAKE_INSTALL_DATADIR}/cmake/Microsoft.GSL) - -# Add find_package() support -target_include_directories(GSL INTERFACE $) -install(TARGETS GSL EXPORT ${export_name}) -install(EXPORT ${export_name} NAMESPACE ${namespace} DESTINATION ${cmake_files_install_dir}) -export(TARGETS GSL NAMESPACE ${namespace} FILE ${export_name}.cmake) - -set(gls_config_version "${CMAKE_CURRENT_BINARY_DIR}/Microsoft.GSLConfigVersion.cmake") - -# Add find_package() versioning support -if(${CMAKE_VERSION} VERSION_LESS "3.14.0") - write_basic_package_version_file(${gls_config_version} COMPATIBILITY SameMajorVersion) -else() - write_basic_package_version_file(${gls_config_version} COMPATIBILITY SameMajorVersion ARCH_INDEPENDENT) -endif() - -install(FILES ${gls_config_version} DESTINATION ${cmake_files_install_dir}) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 657bde8..7a00fec 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,13 +1,12 @@ -cmake_minimum_required(VERSION 3.8) +cmake_minimum_required(VERSION 3.14...3.16) project(GSLTests LANGUAGES CXX) -enable_testing() # again, for support standalone testing set(GSL_CXX_STANDARD "14" CACHE STRING "Use c++ standard") set(CMAKE_CXX_STANDARD ${GSL_CXX_STANDARD}) set(CMAKE_CXX_EXTENSIONS OFF) -set(CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_STANDARD_REQUIRED ON) include(FindPkgConfig) include(ExternalProject) @@ -58,10 +57,14 @@ if (NOT GTestMain_FOUND) endif() if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) - # CMake has been started independently in this directory with tests. Do - # import the globally installed Guidelines Support Library and test it - # instead of the current version from the include/ folder. - find_package(Microsoft.GSL REQUIRED) + 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() if (MSVC AND (GSL_CXX_STANDARD GREATER_EQUAL 17))