diff --git a/CMakeLists.txt b/CMakeLists.txt index f60142b207..ab6a763b24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,27 +40,6 @@ if(protobuf_DEPRECATED_CMAKE_SUBDIRECTORY_USAGE) get_filename_component(protobuf_SOURCE_DIR ${protobuf_SOURCE_DIR} DIRECTORY) endif() -# Add C++14 flags -if(NOT CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 14) -endif() -if(CYGWIN) - string(REGEX_MATCH "-std=gnu\\+\\+([0-9]+)" _protobuf_CXX_STD "${CMAKE_CXX_FLAGS}") -endif() -if(NOT _protobuf_CXX_STD) - set(_protobuf_CXX_STD "${CMAKE_CXX_STANDARD}") -endif() -if(_protobuf_CXX_STD LESS "14") - message(FATAL_ERROR "Protocol Buffers requires at least C++14, but is configured for C++${_protobuf_CXX_STD}") -endif() -if(CYGWIN) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++${_protobuf_CXX_STD}") -else() - set(CMAKE_CXX_STANDARD ${_protobuf_CXX_STD}) - set(CMAKE_CXX_STANDARD_REQUIRED ON) - set(CMAKE_CXX_EXTENSIONS OFF) -endif() - # The Intel compiler isn't able to deal with noinline member functions of # template classes defined in headers. As such it spams the output with # warning #2196: routine is both "inline" and "noinline" diff --git a/cmake/README.md b/cmake/README.md index 396d495525..4bb2e05dd3 100644 --- a/cmake/README.md +++ b/cmake/README.md @@ -8,6 +8,19 @@ currently support CMake 3.5 and newer on both [Windows](#windows-builds) and Most of the instructions will be given using CMake's command-line interface, but the same actions can be performed using appropriate GUI tools. +# CMake Flags + +## C++ Version + +By default, CMake will use whatever C++ version is the system default. Since +protobuf requires C++14 or newer, sometimes you will need to explicitly override +this. For example, the following: + + cmake . -DCMAKE_CXX_STANDARD=14 + cmake --build + +will build protobuf using C++14 (see [CXX_STANDARD](https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html#prop_tgt:CXX_STANDARD){.external} for all available options). + # Windows Builds On Windows, you can build the project from *Command Prompt* and using an diff --git a/kokoro/common/cmake.sh b/kokoro/common/cmake.sh index 99be3bef29..934170bcc3 100755 --- a/kokoro/common/cmake.sh +++ b/kokoro/common/cmake.sh @@ -60,7 +60,8 @@ mkdir -p "${BUILD_DIR}" caplog 01_configure \ cmake -S "${SOURCE_DIR}" \ ${CMAKE_BUILD_TYPE_FLAG} \ - ${CAPLOG_CMAKE_ARGS:-} + ${CAPLOG_CMAKE_ARGS:-} \ + -DCMAKE_CXX_STANDARD=14 ) if [[ -n ${CAPLOG_DIR:-} ]]; then # Save configuration logs. diff --git a/kokoro/linux/32-bit/test_php.sh b/kokoro/linux/32-bit/test_php.sh index 250e9ad625..ef973379e6 100644 --- a/kokoro/linux/32-bit/test_php.sh +++ b/kokoro/linux/32-bit/test_php.sh @@ -38,7 +38,7 @@ build_php_c() { mkdir -p build pushd build -cmake .. +cmake .. -DCMAKE_CXX_STANDARD=14 cmake --build . -- -j20 ctest --verbose --parallel 20 export PROTOC=$(pwd)/protoc diff --git a/kokoro/linux/cmake/build.sh b/kokoro/linux/cmake/build.sh index 5abe392006..67aea1b698 100755 --- a/kokoro/linux/cmake/build.sh +++ b/kokoro/linux/cmake/build.sh @@ -8,7 +8,7 @@ set -eux cd $(dirname $0)/../../.. GIT_REPO_ROOT=`pwd` -CONTAINER_IMAGE=gcr.io/protobuf-build/cmake/linux@sha256:79e6ed9d7f3f8e56167a3309a521e5b7e6a212bfb19855c65ee1cbb6f9099671 +CONTAINER_IMAGE=gcr.io/protobuf-build/cmake/linux@sha256:bd17c13255c8c7af2821c6597ad96568d714c0ea9d9e27d81b108fcc195ca858 # Update git submodules and regenerate files git submodule update --init --recursive @@ -22,7 +22,10 @@ docker run \ --cidfile $tmpfile \ -v $GIT_REPO_ROOT:/workspace \ $CONTAINER_IMAGE \ - /test.sh -Dprotobuf_BUILD_CONFORMANCE=ON -Dprotobuf_BUILD_EXAMPLES=ON + /test.sh \ + -Dprotobuf_BUILD_CONFORMANCE=ON \ + -Dprotobuf_BUILD_EXAMPLES=ON \ + -DCMAKE_CXX_STANDARD=14 # Save logs for Kokoro docker cp \ diff --git a/kokoro/linux/cmake_install/build.sh b/kokoro/linux/cmake_install/build.sh index 9efc110ac6..bdd7ae6818 100755 --- a/kokoro/linux/cmake_install/build.sh +++ b/kokoro/linux/cmake_install/build.sh @@ -8,7 +8,7 @@ set -eux cd $(dirname $0)/../../.. GIT_REPO_ROOT=`pwd` -CONTAINER_IMAGE=gcr.io/protobuf-build/cmake/linux@sha256:79e6ed9d7f3f8e56167a3309a521e5b7e6a212bfb19855c65ee1cbb6f9099671 +CONTAINER_IMAGE=gcr.io/protobuf-build/cmake/linux@sha256:bd17c13255c8c7af2821c6597ad96568d714c0ea9d9e27d81b108fcc195ca858 # Update git submodules and regenerate files git submodule update --init --recursive @@ -22,10 +22,11 @@ docker run \ --cidfile $tmpfile \ -v $GIT_REPO_ROOT:/workspace \ $CONTAINER_IMAGE \ - "/install.sh && /test.sh \ - -Dprotobuf_REMOVE_INSTALLED_HEADERS=ON \ - -Dprotobuf_BUILD_PROTOBUF_BINARIES=OFF \ - -Dprotobuf_BUILD_CONFORMANCE=ON" + "/install.sh -DCMAKE_CXX_STANDARD=14 && /test.sh \ + -Dprotobuf_REMOVE_INSTALLED_HEADERS=ON \ + -Dprotobuf_BUILD_PROTOBUF_BINARIES=OFF \ + -Dprotobuf_BUILD_CONFORMANCE=ON \ + -DCMAKE_CXX_STANDARD=14" # Save logs for Kokoro diff --git a/kokoro/linux/cmake_ninja/build.sh b/kokoro/linux/cmake_ninja/build.sh index 133d97b201..6ee3c61114 100755 --- a/kokoro/linux/cmake_ninja/build.sh +++ b/kokoro/linux/cmake_ninja/build.sh @@ -8,7 +8,7 @@ set -eux cd $(dirname $0)/../../.. GIT_REPO_ROOT=`pwd` -CONTAINER_IMAGE=gcr.io/protobuf-build/cmake/linux@sha256:79e6ed9d7f3f8e56167a3309a521e5b7e6a212bfb19855c65ee1cbb6f9099671 +CONTAINER_IMAGE=gcr.io/protobuf-build/cmake/linux@sha256:bd17c13255c8c7af2821c6597ad96568d714c0ea9d9e27d81b108fcc195ca858 # Update git submodules and regenerate files git submodule update --init --recursive @@ -22,7 +22,7 @@ docker run \ --cidfile $tmpfile \ -v $GIT_REPO_ROOT:/workspace \ $CONTAINER_IMAGE \ - /test.sh -G Ninja -Dprotobuf_BUILD_CONFORMANCE=ON + /test.sh -G Ninja -Dprotobuf_BUILD_CONFORMANCE=ON -DCMAKE_CXX_STANDARD=14 # Save logs for Kokoro docker cp \ diff --git a/kokoro/linux/cmake_shared/build.sh b/kokoro/linux/cmake_shared/build.sh index f3d7534f94..22369261e3 100755 --- a/kokoro/linux/cmake_shared/build.sh +++ b/kokoro/linux/cmake_shared/build.sh @@ -8,7 +8,7 @@ set -eux cd $(dirname $0)/../../.. GIT_REPO_ROOT=`pwd` -CONTAINER_IMAGE=gcr.io/protobuf-build/cmake/linux@sha256:79e6ed9d7f3f8e56167a3309a521e5b7e6a212bfb19855c65ee1cbb6f9099671 +CONTAINER_IMAGE=gcr.io/protobuf-build/cmake/linux@sha256:bd17c13255c8c7af2821c6597ad96568d714c0ea9d9e27d81b108fcc195ca858 # Update git submodules and regenerate files git submodule update --init --recursive @@ -22,7 +22,10 @@ docker run \ --cidfile $tmpfile \ -v $GIT_REPO_ROOT:/workspace \ $CONTAINER_IMAGE \ - /test.sh -Dprotobuf_BUILD_CONFORMANCE=ON -Dprotobuf_BUILD_SHARED_LIBS=ON + /test.sh \ + -Dprotobuf_BUILD_CONFORMANCE=ON \ + -Dprotobuf_BUILD_SHARED_LIBS=ON \ + -DCMAKE_CXX_STANDARD=14 # Save logs for Kokoro docker cp \