Skip to content

Commit d95706d

Browse files
Merge pull request #191 from cjee21/cmake
CMake: Prefer latest C++ standard with min C++11
2 parents ec88b2b + 6274a87 commit d95706d

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

Project/CMake/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,10 @@ set_target_properties(zen PROPERTIES
166166
VERSION ${ZenLib_VERSION}
167167
SOVERSION ${ZenLib_MAJOR_VERSION}
168168
PUBLIC_HEADER "${ZenLib_HDRS}"
169-
CXX_STANDARD 20
170-
CXX_EXTENSIONS NO
171-
CXX_STANDARD_REQUIRED NO
172169
)
173-
target_compile_features(zen PRIVATE cxx_std_11)
170+
171+
include(EnableLatestCXX)
172+
enable_latest_cxx(zen)
174173

175174
install(TARGETS zen EXPORT zen-export
176175
PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/ZenLib
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function(enable_latest_cxx target)
2+
3+
set_target_properties(${target} PROPERTIES
4+
CXX_STANDARD_REQUIRED ON
5+
CXX_EXTENSIONS OFF
6+
)
7+
8+
# User override
9+
if(DEFINED CMAKE_CXX_STANDARD)
10+
target_compile_features(${target} PRIVATE cxx_std_${CMAKE_CXX_STANDARD})
11+
return()
12+
endif()
13+
14+
# Select highest available from list of supported
15+
set(_known_standards 26 23 20 17 14 11)
16+
foreach(std ${_known_standards})
17+
if("cxx_std_${std}" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
18+
target_compile_features(${target} PRIVATE cxx_std_${std})
19+
message(STATUS "'${target}' is using latest available C++ standard: C++${std}")
20+
return()
21+
endif()
22+
endforeach()
23+
24+
# Unsupported C++ version
25+
message(FATAL_ERROR "Compiler ${CMAKE_CXX_COMPILER_ID} does not support C++11 or newer.")
26+
endfunction()

0 commit comments

Comments
 (0)