Commit 1d0d1c3b authored by Harris Hancock's avatar Harris Hancock

Use target_compile_features to request C++11

Projects which use Cap'n Proto require C++11, because they must be able to
compile our headers. Old gcc defaults to C++98, forcing users to manually
specify the -std=gnu++11 flag in their CMake projects. CMake 3.1
introduced the target_compile_features command which removes this
necessity by automatically communicating the C++ standard level
requirement to client projects.

Specifically, if target `kj` requires a C++11 feature publicly, then all
targets which link to `kj` will also require that C++11 feature, and get
the -std=gnu++11 flag for free. If that target is a library, such as
`kj-async`, and `kj-async` links publicly to `kj`, the requirement is also
transitive to all targets which link to `kj-async`.

Note that CMake's default behavior is to request compiler-specific
extensions, such as those provided by the -std=gnu++11 flag. You must
specifically opt out of these extensions. I'm not aware of any way to
propagate this nuance in CMake, but added a check to at least prevent
users from compiling Cap'n Proto without extensions.

With this change, the c++/samples/CMakeLists.txt will build with MinGW
with no change beyond requiring a later version of CMake.
parent ba0f2a31
project("Cap'n Proto" CXX)
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.1)
set(VERSION 0.7-dev)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
......@@ -63,10 +63,11 @@ else()
# recognize as safe.
# * sign-compare: Low S/N ratio.
# * unused-parameter: Low S/N ratio.
#
# We have to use -std=gnu++0x isntead of -std=c++11 because otherwise we lose
# GNU extensions that we need.
add_compile_options(-std=gnu++0x -Wall -Wextra -Wno-strict-aliasing -Wno-sign-compare -Wno-unused-parameter)
add_compile_options(-Wall -Wextra -Wno-strict-aliasing -Wno-sign-compare -Wno-unused-parameter)
if(DEFINED CMAKE_CXX_EXTENSIONS AND NOT CMAKE_CXX_EXTENSIONS)
message(SEND_ERROR "Cap'n Proto requires compiler-specific extensions (e.g., -std=gnu++11). Please leave CMAKE_CXX_EXTENSIONS undefined or ON.")
endif()
if (NOT ANDROID)
add_compile_options(-pthread)
......
......@@ -58,7 +58,7 @@ set(capnp_schemas
)
add_library(capnp ${capnp_sources})
add_library(CapnProto::capnp ALIAS capnp)
target_link_libraries(capnp kj)
target_link_libraries(capnp PUBLIC kj)
#make sure external consumers don't need to manually set the include dirs
target_include_directories(capnp INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
......@@ -96,7 +96,7 @@ set(capnp-rpc_schemas
if(NOT CAPNP_LITE)
add_library(capnp-rpc ${capnp-rpc_sources})
add_library(CapnProto::capnp-rpc ALIAS capnp-rpc)
target_link_libraries(capnp-rpc capnp kj-async kj)
target_link_libraries(capnp-rpc PUBLIC capnp kj-async kj)
install(TARGETS capnp-rpc ${INSTALL_TARGETS_DEFAULT_ARGS})
install(FILES ${capnp-rpc_headers} ${capnp-rpc_schemas} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/capnp")
endif()
......@@ -117,7 +117,7 @@ set(capnp-json_schemas
if(NOT CAPNP_LITE)
add_library(capnp-json ${capnp-json_sources})
add_library(CapnProto::capnp-json ALIAS capnp-json)
target_link_libraries(capnp-json capnp kj-async kj)
target_link_libraries(capnp-json PUBLIC capnp kj-async kj)
install(TARGETS capnp-json ${INSTALL_TARGETS_DEFAULT_ARGS})
install(FILES ${capnp-json_headers} ${capnp-json_schemas} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/capnp/compat")
endif()
......@@ -138,7 +138,7 @@ set(capnpc_sources
)
if(NOT CAPNP_LITE)
add_library(capnpc ${capnpc_sources})
target_link_libraries(capnpc capnp kj)
target_link_libraries(capnpc PUBLIC capnp kj)
install(TARGETS capnpc ${INSTALL_TARGETS_DEFAULT_ARGS})
install(FILES ${capnpc_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/capnp")
endif()
......
......@@ -59,6 +59,9 @@ set(kj-std_headers
)
add_library(kj ${kj_sources})
add_library(CapnProto::kj ALIAS kj)
target_compile_features(kj PUBLIC cxx_constexpr)
# Requiring the cxx_std_11 metafeature would be preferable, but that doesn't exist until CMake 3.8.
if(UNIX AND NOT ANDROID)
target_link_libraries(kj PUBLIC pthread)
endif()
......@@ -85,7 +88,7 @@ set(kj-test-compat_headers
)
add_library(kj-test ${kj-test_sources})
add_library(CapnProto::kj-test ALIAS kj-test)
target_link_libraries(kj-test kj)
target_link_libraries(kj-test PUBLIC kj)
install(TARGETS kj-test ${INSTALL_TARGETS_DEFAULT_ARGS})
install(FILES ${kj-test_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/kj")
install(FILES ${kj-test-compat_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/kj/compat")
......@@ -133,7 +136,7 @@ set(kj-http_headers
if(NOT CAPNP_LITE)
add_library(kj-http ${kj-http_sources})
add_library(CapnProto::kj-http ALIAS kj-http)
target_link_libraries(kj-http kj-async kj)
target_link_libraries(kj-http PUBLIC kj-async kj)
install(TARGETS kj-http ${INSTALL_TARGETS_DEFAULT_ARGS})
install(FILES ${kj-http_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/kj/compat")
endif()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment