Skip to content

Commit 3f33c98

Browse files
committed
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".
1 parent 5cbef61 commit 3f33c98

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

CMakeLists.txt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ include(cmake/xsdk.cmake)
1313

1414
option(USE_XSDK_DEFAULTS "enable the XDSK v0.3.0 default configuration" NO)
1515

16-
#requre c++11 without extensions
17-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
18-
set(CMAKE_CXX_EXTENSION OFF)
19-
if(NOT ENABLE_CGNS)
20-
set(CMAKE_CXX_STANDARD 11)
21-
endif()
16+
bob_set_cxx_standard(11)
2217

2318
xsdk_begin_package()
2419
bob_begin_package()
@@ -27,20 +22,22 @@ if(USE_XSDK_DEFAULTS)
2722
xsdk_compiler_flags()
2823
endif()
2924

30-
# require c++14
3125
option(ENABLE_CGNS "Enable the CGNS reader: requires c++14 extensions" OFF)
3226
message(STATUS "ENABLE_CGNS: ${ENABLE_CGNS}")
27+
if(ENABLE_CGNS)
28+
message(STATUS "enabling cxx14")
29+
bob_set_cxx_standard(14)
30+
endif()
3331

3432
# Set some default compiler flags that should always be used
3533
if(NOT USE_XSDK_DEFAULTS)
3634
bob_set_shared_libs()
3735
bob_begin_cxx_flags()
3836
bob_end_cxx_flags()
3937
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS}")
40-
if(ENABLE_CGNS) #takes precedence over SCOREC_ENABLE_CXX11
41-
message(STATUS "enabling cxx14")
38+
if(ENABLE_CGNS)
4239
bob_cxx14_flags()
43-
elseif(SCOREC_ENABLE_CXX11)
40+
else()
4441
bob_cxx11_flags()
4542
endif()
4643
endif()
@@ -193,9 +190,6 @@ add_library(core INTERFACE)
193190
target_link_libraries(core INTERFACE ${SCOREC_EXPORTED_TARGETS})
194191
if(ENABLE_CGNS)
195192
target_link_libraries(core INTERFACE ${CMAKE_DL_LIBS}) #HDF5 uses dlopen
196-
target_compile_features(core INTERFACE cxx_std_14)
197-
else()
198-
target_compile_features(core INTERFACE cxx_std_11)
199193
endif()
200194
scorec_export_library(core)
201195

cmake/bob.cmake

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,27 @@ function(bob_begin_cxx_flags)
7777
set(CMAKE_CXX_FLAGS "${FLAGS}" PARENT_SCOPE)
7878
endfunction(bob_begin_cxx_flags)
7979

80-
function(bob_cxx11_flags)
81-
set(FLAGS "${CMAKE_CXX_FLAGS}")
82-
if(CMAKE_CXX_COMPILER_ID MATCHES "PGI")
83-
set(FLAGS "${FLAGS} -std=c++11")
80+
# The following is from the book,"Professional CMake: 19th edition"
81+
macro(bob_set_cxx_standard standard)
82+
# Require C++<standard>, but let a parent project ask for something higher
83+
if(DEFINED CMAKE_CXX_STANDARD)
84+
if(CMAKE_CXX_STANDARD EQUAL 98 OR CMAKE_CXX_STANDARD LESS ${standard})
85+
message(FATAL_ERROR "This project requires at least C++${standard}")
86+
endif()
8487
else()
85-
set(FLAGS "${FLAGS} --std=c++11")
88+
set(CMAKE_CXX_STANDARD ${standard})
89+
endif()
90+
message(STATUS "CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}")
91+
# Always enforce the language constraint
92+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
93+
# We don't need compiler extensions, but let a parent ask for them
94+
if(NOT DEFINED CMAKE_CXX_EXTENSIONS)
95+
set(CMAKE_CXX_EXTENSIONS OFF)
8696
endif()
97+
endmacro()
98+
99+
function(bob_cxx11_flags)
100+
set(FLAGS "${CMAKE_CXX_FLAGS}")
87101
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
88102
if (${PROJECT_NAME}_CXX_WARNINGS)
89103
set(FLAGS "${FLAGS} -Wno-c++98-compat-pedantic -Wno-c++98-compat")
@@ -94,8 +108,7 @@ endfunction(bob_cxx11_flags)
94108

95109
function(bob_cxx14_flags)
96110
set(FLAGS "${CMAKE_CXX_FLAGS}")
97-
# clang only: -Werror=return-stack-address -Werror=mismatched-tags
98-
set(FLAGS "${FLAGS} --std=c++14 -Wall -Wextra -Wpedantic -Werror -Wno-extra-semi -Werror=unused-parameter -Wno-error=deprecated-declarations")
111+
set(FLAGS "${FLAGS} -Wall -Wextra -Wpedantic -Werror -Wno-extra-semi -Werror=unused-parameter -Wno-error=deprecated-declarations")
99112
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
100113
if (${PROJECT_NAME}_CXX_WARNINGS)
101114
set(FLAGS "${FLAGS} -Wno-c++98-compat-pedantic -Wno-c++98-compat")

0 commit comments

Comments
 (0)