Add an cmake option which respect `CMAKE_CXX_STANDARD` for cxx standard. (#188)

This commit is contained in:
Rui Chen 2023-01-31 22:51:38 -05:00 committed by GitHub
parent 74ca58fdcf
commit 5c7e155c9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 19 deletions

View File

@ -16,10 +16,6 @@ jobs:
os: [ubuntu-20.04] os: [ubuntu-20.04]
etcd: [v3.4.13] etcd: [v3.4.13]
steps: steps:
- name: Get time
run: |
date +'%Y-%m' > snapshot.txt
- name: Install dependencies for Linux - name: Install dependencies for Linux
run: | run: |
# switch to centos stream # switch to centos stream
@ -44,15 +40,11 @@ jobs:
with: with:
submodules: true submodules: true
- name: Get time
run: |
date +'%Y-%m' > snapshot.txt
- name: Cache for cccahe - name: Cache for cccahe
uses: actions/cache@v3 uses: actions/cache@v3
with: with:
path: /home/runner/.ccache path: /home/runner/.ccache
key: ${{ runner.os }}-centos-ccache-${{ hashFiles('**/snapshot.txt') }} key: ${{ runner.os }}-centos-ccache-${{ hashFiles('/CMakeLists.txt') }}
restore-keys: | restore-keys: |
${{ runner.os }}-centos-ccache- ${{ runner.os }}-centos-ccache-

View File

@ -30,6 +30,16 @@ option(BUILD_SHARED_LIBS "Build etcd-cpp-apiv3 shared libraries" ON)
option(BUILD_ETCD_TESTS "Build etcd-cpp-apiv3 test cases" OFF) option(BUILD_ETCD_TESTS "Build etcd-cpp-apiv3 test cases" OFF)
option(CMAKE_POSITION_INDEPENDENT_CODE "Build etcd-cpp-apiv3 with -fPIC" ON) option(CMAKE_POSITION_INDEPENDENT_CODE "Build etcd-cpp-apiv3 with -fPIC" ON)
option(ETCD_W_STRICT "Build etcd-cpp-apiv3 with -Werror" ON) option(ETCD_W_STRICT "Build etcd-cpp-apiv3 with -Werror" ON)
option(ETCD_CMAKE_CXX_STANDARD "Build etcd-cpp-apiv3 with specified C++ standard, e.g., 11, 14, 17, 20" "")
if(NOT "${ETCD_CMAKE_CXX_STANDARD}")
if(NOT "${CMAKE_CXX_STANDARD}")
set(ETCD_CMAKE_CXX_STANDARD 11)
else()
set(ETCD_CMAKE_CXX_STANDARD ${CMAKE_CXX_STANDARD})
endif()
endif()
message("Building etcd-cpp-apiv3 with C++${ETCD_CMAKE_CXX_STANDARD}")
# reference: https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling#always-full-rpath # reference: https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling#always-full-rpath
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
@ -60,20 +70,20 @@ if(NOT (CMAKE_CXX_COMPILER_LAUNCHER MATCHES "ccache") AND NOT (CMAKE_C_COMPILER_
endif(ccache_EXECUTABLE) endif(ccache_EXECUTABLE)
endif() endif()
macro(use_cxx11 target) macro(use_cxx target)
if(CMAKE_VERSION VERSION_LESS "3.1") if(CMAKE_VERSION VERSION_LESS "3.1")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(${target} PRIVATE "-std=gnu++11") target_compile_options(${target} PRIVATE "-std=gnu++${ETCD_CMAKE_CXX_STANDARD}")
else() else()
target_compile_options(${target} PRIVATE "-std=c++11") target_compile_options(${target} PRIVATE "-std=c++${ETCD_CMAKE_CXX_STANDARD}")
endif() endif()
else() else()
set_target_properties(${target} PROPERTIES set_target_properties(${target} PROPERTIES
CXX_STANDARD 11 CXX_STANDARD ${ETCD_CMAKE_CXX_STANDARD}
CXX_STANDARD_REQUIRED ON CXX_STANDARD_REQUIRED ON
) )
endif() endif()
endmacro(use_cxx11) endmacro(use_cxx)
find_package(Boost REQUIRED COMPONENTS system thread random) find_package(Boost REQUIRED COMPONENTS system thread random)
if(APPLE) if(APPLE)

View File

@ -18,13 +18,13 @@ file(GLOB_RECURSE CPP_CLIENT_CORE_SRC
) )
add_library(etcd-cpp-api-core-objects OBJECT ${CPP_CLIENT_CORE_SRC} ${PROTOBUF_GENERATES}) add_library(etcd-cpp-api-core-objects OBJECT ${CPP_CLIENT_CORE_SRC} ${PROTOBUF_GENERATES})
use_cxx11(etcd-cpp-api-core-objects) use_cxx(etcd-cpp-api-core-objects)
add_dependencies(etcd-cpp-api-core-objects protobuf_generates) add_dependencies(etcd-cpp-api-core-objects protobuf_generates)
include_generated_protobuf_files(etcd-cpp-api-core-objects) include_generated_protobuf_files(etcd-cpp-api-core-objects)
# add the core library, includes the sycnhronous client only # add the core library, includes the sycnhronous client only
add_library(etcd-cpp-api-core $<TARGET_OBJECTS:etcd-cpp-api-core-objects>) add_library(etcd-cpp-api-core $<TARGET_OBJECTS:etcd-cpp-api-core-objects>)
use_cxx11(etcd-cpp-api-core) use_cxx(etcd-cpp-api-core)
target_link_libraries(etcd-cpp-api-core PUBLIC target_link_libraries(etcd-cpp-api-core PUBLIC
${Boost_LIBRARIES} ${Boost_LIBRARIES}
${PROTOBUF_LIBRARIES} ${PROTOBUF_LIBRARIES}
@ -36,7 +36,7 @@ include_generated_protobuf_files(etcd-cpp-api-core)
# add the client with asynchronus client # add the client with asynchronus client
add_library(etcd-cpp-api $<TARGET_OBJECTS:etcd-cpp-api-core-objects> add_library(etcd-cpp-api $<TARGET_OBJECTS:etcd-cpp-api-core-objects>
"${CMAKE_CURRENT_SOURCE_DIR}/Client.cpp") "${CMAKE_CURRENT_SOURCE_DIR}/Client.cpp")
use_cxx11(etcd-cpp-api) use_cxx(etcd-cpp-api)
target_link_libraries(etcd-cpp-api PUBLIC target_link_libraries(etcd-cpp-api PUBLIC
${Boost_LIBRARIES} ${Boost_LIBRARIES}
${CPPREST_LIB} # n.b.: the asynchronous client requires pplx in cpprestsdk ${CPPREST_LIB} # n.b.: the asynchronous client requires pplx in cpprestsdk
@ -56,4 +56,3 @@ else()
install(TARGETS etcd-cpp-api-core etcd-cpp-api install(TARGETS etcd-cpp-api-core etcd-cpp-api
EXPORT etcd-targets) EXPORT etcd-targets)
endif() endif()

View File

@ -18,7 +18,7 @@ foreach(testfile ${TEST_FILES})
else() else()
add_executable(${test_name} EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/${testfile}) add_executable(${test_name} EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/${testfile})
endif() endif()
use_cxx11(${test_name}) use_cxx(${test_name})
add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>) add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>)
target_include_directories(${test_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../proto/gen) target_include_directories(${test_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../proto/gen)