Skip to content

Commit 1bff1e6

Browse files
committed
CMake: replace CXX_STANDARD by target_compile_features()
3b5314c was wrong, setting per-target CXX_STANDARD did not allow the user to override the C++ standard from the command line. This time, using target_compile_features(), forcing a newer standard works, e.g. for C++17: cmake -DCMAKE_CXX_STANDARD=17
1 parent 34c01c1 commit 1bff1e6

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

CMakeLists.txt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,28 @@ include(CMakePackageConfigHelpers)
1515

1616
find_package(pegtl CONFIG REQUIRED)
1717

18+
# Set the minimum C++ standard required for compiling,
19+
# but allow for user to override on the command line using:
20+
# cmake -DCMAKE_CXX_STANDARD=[11|14|17|...] -DCMAKE_CXX_EXTENSIONS=[ON|OFF]
21+
function(cppgraphqlgen_target_set_cxx_standard target)
22+
if (CMAKE_VERSION VERSION_LESS "3.8")
23+
# The cxx_std_11 abstract compile feature
24+
# is available only starting from CMake 3.8.
25+
# We assume the availability of lambdas
26+
# indicates a C++11-compatible compiler mode.
27+
target_compile_features(${target} PUBLIC cxx_lambdas)
28+
else()
29+
target_compile_features(${target} PUBLIC cxx_std_11)
30+
endif()
31+
endfunction()
32+
1833
add_executable(schemagen
1934
GraphQLTree.cpp
2035
GraphQLResponse.cpp
2136
SchemaGenerator.cpp)
2237
target_link_libraries(schemagen PRIVATE taocpp::pegtl)
2338
target_include_directories(schemagen PRIVATE ${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR})
24-
set_property(TARGET schemagen PROPERTY CXX_STANDARD 11)
39+
cppgraphqlgen_target_set_cxx_standard(schemagen)
2540

2641
add_custom_command(
2742
OUTPUT
@@ -48,7 +63,7 @@ target_include_directories(graphqlservice
4863
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
4964
$<INSTALL_INTERFACE:include>
5065
)
51-
set_property(TARGET graphqlservice PROPERTY CXX_STANDARD 11)
66+
cppgraphqlgen_target_set_cxx_standard(graphqlservice)
5267

5368
option(BUILD_TESTS "Build the tests and sample schema library." ON)
5469
option(UPDATE_SAMPLES "Regenerate the sample schema sources whether or not we're building the tests and the sample library." ON)
@@ -77,14 +92,14 @@ if(BUILD_TESTS OR UPDATE_SAMPLES)
7792
graphqlservice)
7893
target_include_directories(todaygraphql SYSTEM PUBLIC ${RAPIDJSON_INCLUDE_DIRS})
7994
target_include_directories(todaygraphql PRIVATE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
80-
set_property(TARGET todaygraphql PROPERTY CXX_STANDARD 11)
95+
cppgraphqlgen_target_set_cxx_standard(todaygraphql)
8196

8297
add_executable(test_today
8398
test_today.cpp)
8499
target_link_libraries(test_today PRIVATE
85100
todaygraphql)
86101
target_include_directories(test_today PRIVATE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
87-
set_property(TARGET test_today PROPERTY CXX_STANDARD 11)
102+
cppgraphqlgen_target_set_cxx_standard(test_today)
88103

89104
enable_testing()
90105
find_package(GTest MODULE REQUIRED)
@@ -96,7 +111,7 @@ if(BUILD_TESTS OR UPDATE_SAMPLES)
96111
GTest::GTest
97112
GTest::Main)
98113
target_include_directories(tests PRIVATE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
99-
set_property(TARGET tests PROPERTY CXX_STANDARD 11)
114+
cppgraphqlgen_target_set_cxx_standard(tests)
100115

101116
add_test(NAME TodayServiceCase
102117
COMMAND tests --gtest_filter=TodayServiceCase.*

0 commit comments

Comments
 (0)