From 3f33c98d57814aca8568bbc0de43a2aa29a2c25a Mon Sep 17 00:00:00 2001 From: Cameron Smith Date: Mon, 21 Oct 2024 16:26:10 -0400 Subject: [PATCH 1/2] simplify and improve logic for setting C++ standard Added a 'bob' cmake helper function to set the C++ standard. The modifications do not change the default behavior of using C++11. The user is given the ability to increase the standard to > C++11, or enable the use of extensions, by passing CMAKE_CXX_STANDARD and CMAKE_CXX_EXTENSIONS, respectively, to cmake. Following approach given in "Professional CMake: 19th Edition". --- CMakeLists.txt | 20 +++++++------------- cmake/bob.cmake | 27 ++++++++++++++++++++------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f1ab11c3..dad94b45d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,12 +13,7 @@ include(cmake/xsdk.cmake) option(USE_XSDK_DEFAULTS "enable the XDSK v0.3.0 default configuration" NO) -#requre c++11 without extensions -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSION OFF) -if(NOT ENABLE_CGNS) - set(CMAKE_CXX_STANDARD 11) -endif() +bob_set_cxx_standard(11) xsdk_begin_package() bob_begin_package() @@ -27,9 +22,12 @@ if(USE_XSDK_DEFAULTS) xsdk_compiler_flags() endif() -# require c++14 option(ENABLE_CGNS "Enable the CGNS reader: requires c++14 extensions" OFF) message(STATUS "ENABLE_CGNS: ${ENABLE_CGNS}") +if(ENABLE_CGNS) + message(STATUS "enabling cxx14") + bob_set_cxx_standard(14) +endif() # Set some default compiler flags that should always be used if(NOT USE_XSDK_DEFAULTS) @@ -37,10 +35,9 @@ if(NOT USE_XSDK_DEFAULTS) bob_begin_cxx_flags() bob_end_cxx_flags() set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS}") - if(ENABLE_CGNS) #takes precedence over SCOREC_ENABLE_CXX11 - message(STATUS "enabling cxx14") + if(ENABLE_CGNS) bob_cxx14_flags() - elseif(SCOREC_ENABLE_CXX11) + else() bob_cxx11_flags() endif() endif() @@ -193,9 +190,6 @@ add_library(core INTERFACE) target_link_libraries(core INTERFACE ${SCOREC_EXPORTED_TARGETS}) if(ENABLE_CGNS) target_link_libraries(core INTERFACE ${CMAKE_DL_LIBS}) #HDF5 uses dlopen - target_compile_features(core INTERFACE cxx_std_14) -else() - target_compile_features(core INTERFACE cxx_std_11) endif() scorec_export_library(core) diff --git a/cmake/bob.cmake b/cmake/bob.cmake index 4cbd4f84c..4bf5dcce9 100644 --- a/cmake/bob.cmake +++ b/cmake/bob.cmake @@ -77,13 +77,27 @@ function(bob_begin_cxx_flags) set(CMAKE_CXX_FLAGS "${FLAGS}" PARENT_SCOPE) endfunction(bob_begin_cxx_flags) -function(bob_cxx11_flags) - set(FLAGS "${CMAKE_CXX_FLAGS}") - if(CMAKE_CXX_COMPILER_ID MATCHES "PGI") - set(FLAGS "${FLAGS} -std=c++11") +# The following is from the book,"Professional CMake: 19th edition" +macro(bob_set_cxx_standard standard) + # Require C++, but let a parent project ask for something higher + if(DEFINED CMAKE_CXX_STANDARD) + if(CMAKE_CXX_STANDARD EQUAL 98 OR CMAKE_CXX_STANDARD LESS ${standard}) + message(FATAL_ERROR "This project requires at least C++${standard}") + endif() else() - set(FLAGS "${FLAGS} --std=c++11") + set(CMAKE_CXX_STANDARD ${standard}) + endif() + message(STATUS "CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}") + # Always enforce the language constraint + set(CMAKE_CXX_STANDARD_REQUIRED ON) + # We don't need compiler extensions, but let a parent ask for them + if(NOT DEFINED CMAKE_CXX_EXTENSIONS) + set(CMAKE_CXX_EXTENSIONS OFF) endif() +endmacro() + +function(bob_cxx11_flags) + set(FLAGS "${CMAKE_CXX_FLAGS}") if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") if (${PROJECT_NAME}_CXX_WARNINGS) set(FLAGS "${FLAGS} -Wno-c++98-compat-pedantic -Wno-c++98-compat") @@ -94,8 +108,7 @@ endfunction(bob_cxx11_flags) function(bob_cxx14_flags) set(FLAGS "${CMAKE_CXX_FLAGS}") - # clang only: -Werror=return-stack-address -Werror=mismatched-tags - set(FLAGS "${FLAGS} --std=c++14 -Wall -Wextra -Wpedantic -Werror -Wno-extra-semi -Werror=unused-parameter -Wno-error=deprecated-declarations") + set(FLAGS "${FLAGS} -Wall -Wextra -Wpedantic -Werror -Wno-extra-semi -Werror=unused-parameter -Wno-error=deprecated-declarations") if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") if (${PROJECT_NAME}_CXX_WARNINGS) set(FLAGS "${FLAGS} -Wno-c++98-compat-pedantic -Wno-c++98-compat") From 16aa1112ab7c006defe15341b58503024e0a95e6 Mon Sep 17 00:00:00 2001 From: Cameron Smith Date: Tue, 22 Oct 2024 13:49:59 -0400 Subject: [PATCH 2/2] bob_set_cxx_standard should only be called once calling it more than once could be done if the standard was decreased, otherwise, the check against CMAKE_CXX_STANDARD will fail --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dad94b45d..ae43799ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,8 +13,6 @@ include(cmake/xsdk.cmake) option(USE_XSDK_DEFAULTS "enable the XDSK v0.3.0 default configuration" NO) -bob_set_cxx_standard(11) - xsdk_begin_package() bob_begin_package() @@ -24,7 +22,9 @@ endif() option(ENABLE_CGNS "Enable the CGNS reader: requires c++14 extensions" OFF) message(STATUS "ENABLE_CGNS: ${ENABLE_CGNS}") -if(ENABLE_CGNS) +if(NOT ENABLE_CGNS) + bob_set_cxx_standard(11) +else() message(STATUS "enabling cxx14") bob_set_cxx_standard(14) endif()