From a76b2c5229ec82c4c98fe416d3d6ec76ad40e874 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Mon, 9 May 2022 19:03:22 +0200 Subject: [PATCH] Add CMake function to associate macros with C++ standards Add and use the following CMake function: json_test_add_standard_keyphrases( PHRASES ... CXX_STANDARDS ...) Create a mapping between C++ standard versions and key phrases. json_test_add_test_for() will search for these key phrases and build tests for associated C++ standard versions. --- cmake/test.cmake | 42 ++++++++++++++++++++++++++++++++++++++++-- tests/CMakeLists.txt | 16 ++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/cmake/test.cmake b/cmake/test.cmake index bb840c6c0..351119225 100644 --- a/cmake/test.cmake +++ b/cmake/test.cmake @@ -42,6 +42,37 @@ endforeach() # test functions ############################################################################# +############################################################################# +# json_test_add_standard_keyphrases( +# PHRASES ... +# CXX_STANDARDS ...) +# +# Create a mapping between C++ standard versions and key phrases. +# +# json_test_add_test_for() will search for these key phrases and build tests +# for associated C++ standard versions. +############################################################################# + +function(json_test_add_standard_keyphrases) + cmake_parse_arguments(args "" "" + "CXX_STANDARDS;PHRASES" + ${ARGN}) + + if(NOT args_PHRASES) + message(FATAL_ERROR "At least one key phrase is required.") + endif() + + if(NOT args_CXX_STANDARDS) + message(FATAL_ERROR "At least one C++ standard version is required.") + endif() + + foreach(cxx_standard ${args_CXX_STANDARDS}) + set(var _JSON_TEST_STANDARD_KEYPHRASES_CPP_${cxx_standard}) + list(APPEND ${var} ${args_PHRASES}) + set(${var} CACHE INTERNAL "Mapping of key phrases to C++ standard version ${cxx_standard}") + endforeach() +endfunction() + ############################################################################# # json_test_set_test_options( # all| @@ -233,8 +264,15 @@ function(json_test_add_test_for file) # add unconditionally if C++11 (default) or forced if(NOT ("${cxx_standard}" STREQUAL 11 OR args_FORCE)) - string(FIND "${file_content}" JSON_HAS_CPP_${cxx_standard} has_cpp_found) - if(${has_cpp_found} EQUAL -1) + set(build FALSE) + foreach(phrase JSON_HAS_CPP_${cxx_standard} ${_JSON_TEST_STANDARD_KEYPHRASES_CPP_${cxx_standard}}) + string(FIND "${file_content}" ${phrase} phrase_found) + if(NOT ${phrase_found} EQUAL -1) + set(build TRUE) + break() + endif() + endforeach() + if(NOT build) continue() endif() endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 65b610f0e..2a3866bc6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -94,6 +94,22 @@ json_test_set_test_options(test-disabled_exceptions # raise timeout of expensive Unicode test json_test_set_test_options(test-unicode4 TEST_PROPERTIES TIMEOUT 3000) +############################################################################# +# define additional macro to C++ standard version mappings +############################################################################# + +json_test_add_standard_keyphrases( + PHRASES + JSON_HAS_EXPERIMENTAL_FILESYSTEM + JSON_HAS_FILESYSTEM + CXX_STANDARDS 17) + +json_test_add_standard_keyphrases( + PHRASES + JSON_HAS_RANGES + JSON_HAS_THREE_WAY_COMPARISON + CXX_STANDARDS 20) + ############################################################################# # add unit tests #############################################################################