From 69ffedfe527aab014e83a84cf8d95b12ca75b066 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Thu, 24 Nov 2022 23:33:42 -0600 Subject: [PATCH] Use `target_compile_features` to specify C++ standard requirement --- CMakeLists.txt | 16 ++----- support/cmake/cxx14.cmake | 54 ----------------------- test/CMakeLists.txt | 2 +- test/add-subdirectory-test/CMakeLists.txt | 2 +- test/compile-error-test/CMakeLists.txt | 4 +- test/find-package-test/CMakeLists.txt | 2 +- test/fuzzing/CMakeLists.txt | 2 +- test/gtest/CMakeLists.txt | 8 ++-- test/static-export-test/CMakeLists.txt | 2 +- 9 files changed, 16 insertions(+), 76 deletions(-) delete mode 100644 support/cmake/cxx14.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index e846b8cd..71ffdd6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.1...3.18) +cmake_minimum_required(VERSION 3.8...3.18) # Fallback for using newer policies on CMake <3.12. if(${CMAKE_VERSION} VERSION_LESS 3.12) @@ -122,17 +122,9 @@ endif () set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/support/cmake") -include(cxx14) +include(CheckCXXCompilerFlag) include(JoinPaths) -list(FIND CMAKE_CXX_COMPILE_FEATURES "cxx_variadic_templates" index) -if (${index} GREATER -1) - # Use cxx_variadic_templates instead of more appropriate cxx_std_11 for - # compatibility with older CMake versions. - set(FMT_REQUIRED_FEATURES cxx_variadic_templates) -endif () -message(STATUS "Required features: ${FMT_REQUIRED_FEATURES}") - if (FMT_MASTER_PROJECT AND NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET) set_verbose(CMAKE_CXX_VISIBILITY_PRESET hidden CACHE STRING "Preset for the export of private symbols") @@ -239,7 +231,7 @@ if (FMT_MODULE) enable_module(fmt) endif () -target_compile_features(fmt INTERFACE ${FMT_REQUIRED_FEATURES}) +target_compile_features(fmt PUBLIC cxx_std_11) target_include_directories(fmt ${FMT_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC $ @@ -270,7 +262,7 @@ add_library(fmt-header-only INTERFACE) add_library(fmt::fmt-header-only ALIAS fmt-header-only) target_compile_definitions(fmt-header-only INTERFACE FMT_HEADER_ONLY=1) -target_compile_features(fmt-header-only INTERFACE ${FMT_REQUIRED_FEATURES}) +target_compile_features(fmt-header-only INTERFACE cxx_std_11) target_include_directories(fmt-header-only ${FMT_SYSTEM_HEADERS_ATTRIBUTE} INTERFACE $ diff --git a/support/cmake/cxx14.cmake b/support/cmake/cxx14.cmake deleted file mode 100644 index deb1e26f..00000000 --- a/support/cmake/cxx14.cmake +++ /dev/null @@ -1,54 +0,0 @@ -# C++14 feature support detection - -include(CheckCXXCompilerFlag) -function (fmt_check_cxx_compiler_flag flag result) - if (NOT MSVC) - check_cxx_compiler_flag("${flag}" ${result}) - endif () -endfunction () - -if (NOT CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 11) -endif() -message(STATUS "CXX_STANDARD: ${CMAKE_CXX_STANDARD}") - -if (CMAKE_CXX_STANDARD EQUAL 20) - fmt_check_cxx_compiler_flag(-std=c++20 has_std_20_flag) - fmt_check_cxx_compiler_flag(-std=c++2a has_std_2a_flag) - - if (has_std_20_flag) - set(CXX_STANDARD_FLAG -std=c++20) - elseif (has_std_2a_flag) - set(CXX_STANDARD_FLAG -std=c++2a) - endif () - -elseif (CMAKE_CXX_STANDARD EQUAL 17) - fmt_check_cxx_compiler_flag(-std=c++17 has_std_17_flag) - fmt_check_cxx_compiler_flag(-std=c++1z has_std_1z_flag) - - if (has_std_17_flag) - set(CXX_STANDARD_FLAG -std=c++17) - elseif (has_std_1z_flag) - set(CXX_STANDARD_FLAG -std=c++1z) - endif () - -elseif (CMAKE_CXX_STANDARD EQUAL 14) - fmt_check_cxx_compiler_flag(-std=c++14 has_std_14_flag) - fmt_check_cxx_compiler_flag(-std=c++1y has_std_1y_flag) - - if (has_std_14_flag) - set(CXX_STANDARD_FLAG -std=c++14) - elseif (has_std_1y_flag) - set(CXX_STANDARD_FLAG -std=c++1y) - endif () - -elseif (CMAKE_CXX_STANDARD EQUAL 11) - fmt_check_cxx_compiler_flag(-std=c++11 has_std_11_flag) - fmt_check_cxx_compiler_flag(-std=c++0x has_std_0x_flag) - - if (has_std_11_flag) - set(CXX_STANDARD_FLAG -std=c++11) - elseif (has_std_0x_flag) - set(CXX_STANDARD_FLAG -std=c++0x) - endif () -endif () diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ce2b77d3..dcbac6fb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -109,7 +109,7 @@ if (FMT_MODULE) # If module support is present the module tests require a # test-only module to be built from {fmt} add_library(test-module OBJECT ${CMAKE_SOURCE_DIR}/src/fmt.cc) - target_compile_features(test-module PUBLIC ${FMT_REQUIRED_FEATURES}) + target_compile_features(test-module PUBLIC cxx_std_11) target_include_directories(test-module PUBLIC $) enable_module(test-module) diff --git a/test/add-subdirectory-test/CMakeLists.txt b/test/add-subdirectory-test/CMakeLists.txt index e08d0cf7..d7c47aa8 100644 --- a/test/add-subdirectory-test/CMakeLists.txt +++ b/test/add-subdirectory-test/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.1...3.18) +cmake_minimum_required(VERSION 3.8...3.18) project(fmt-test CXX) diff --git a/test/compile-error-test/CMakeLists.txt b/test/compile-error-test/CMakeLists.txt index 9c65dfdc..b03a3320 100644 --- a/test/compile-error-test/CMakeLists.txt +++ b/test/compile-error-test/CMakeLists.txt @@ -1,6 +1,6 @@ # Test if compile errors are produced where necessary. -cmake_minimum_required(VERSION 3.1...3.18) +cmake_minimum_required(VERSION 3.8...3.18) project(compile-error-test CXX) set(fmt_headers " @@ -64,7 +64,7 @@ function (run_tests) ") file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test/CMakeLists.txt" " - cmake_minimum_required(VERSION 3.1...3.18) + cmake_minimum_required(VERSION 3.8...3.18) project(tests CXX) add_subdirectory(${FMT_DIR} fmt) ${cmake_targets} diff --git a/test/find-package-test/CMakeLists.txt b/test/find-package-test/CMakeLists.txt index 93d686e6..9cbba1cd 100644 --- a/test/find-package-test/CMakeLists.txt +++ b/test/find-package-test/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.1...3.18) +cmake_minimum_required(VERSION 3.8...3.18) project(fmt-test) diff --git a/test/fuzzing/CMakeLists.txt b/test/fuzzing/CMakeLists.txt index 0280c5cd..afcf9df1 100644 --- a/test/fuzzing/CMakeLists.txt +++ b/test/fuzzing/CMakeLists.txt @@ -22,7 +22,7 @@ function(add_fuzzer source) if (FMT_FUZZ_LDFLAGS) target_link_libraries(${name} PRIVATE ${FMT_FUZZ_LDFLAGS}) endif () - target_compile_features(${name} PRIVATE cxx_generic_lambdas) + target_compile_features(${name} PRIVATE cxx_std_14) endfunction() foreach (source chrono-duration.cc chrono-timepoint.cc float.cc named-arg.cc one-arg.cc two-args.cc) diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt index ed0e59d5..1ef42da0 100644 --- a/test/gtest/CMakeLists.txt +++ b/test/gtest/CMakeLists.txt @@ -9,6 +9,7 @@ add_library(gtest STATIC gmock-gtest-all.cc gmock/gmock.h gtest/gtest.h gtest/gtest-spi.h) target_compile_definitions(gtest PUBLIC GTEST_HAS_STD_WSTRING=1) target_include_directories(gtest SYSTEM PUBLIC .) +target_compile_features(gtest PUBLIC cxx_std_11) find_package(Threads) if (Threads_FOUND) @@ -17,9 +18,10 @@ else () target_compile_definitions(gtest PUBLIC GTEST_HAS_PTHREAD=0) endif () -# Workaround GTest bug https://github.com/google/googletest/issues/705. -fmt_check_cxx_compiler_flag( - -fno-delete-null-pointer-checks HAVE_FNO_DELETE_NULL_POINTER_CHECKS) +# Workaround GTest bug https://github.com/google/googletest/issues/705 +if (NOT MSVC) + check_cxx_compiler_flag(-fno-delete-null-pointer-checks HAVE_FNO_DELETE_NULL_POINTER_CHECKS) +endif () if (HAVE_FNO_DELETE_NULL_POINTER_CHECKS) target_compile_options(gtest PUBLIC -fno-delete-null-pointer-checks) endif () diff --git a/test/static-export-test/CMakeLists.txt b/test/static-export-test/CMakeLists.txt index 7690d7b5..7e1b3e54 100644 --- a/test/static-export-test/CMakeLists.txt +++ b/test/static-export-test/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.1...3.18) +cmake_minimum_required(VERSION 3.8...3.18) project(fmt-link CXX)