Skip to content

Commit

Permalink
[feat][cmake] Support c++20, c++23. default c++17.
Browse files Browse the repository at this point in the history
  • Loading branch information
Haijun Yu committed Feb 29, 2024
1 parent 25c0c10 commit 9b9a52b
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ option(BRPC_ENABLE_CPU_PROFILER "Enable brpc cpu profiler" OFF)
option(XDPROCKS_PATH "Enable xdprocks raw engine")
option(BUILD_PYTHON_SDK "Build python sdk" OFF)


message(STATUS CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
message(STATUS THIRD_PARTY_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE})

SET(CMAKE_C_FLAGS_DEBUG "$ENV{CFLAGS} -O0 -g3 -ggdb")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -g3 -ggdb")

message(STATUS "CMAKE_CXX_STANDARD: ${CMAKE_CXX_STANDARD}")

# xdprocks
if(XDPROCKS_PATH)
message(STATUS "Enable xdprocks, path: ${XDPROCKS_PATH}")
Expand Down Expand Up @@ -104,19 +107,44 @@ if(ENABLE_COVERAGE)
endif()

include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-std=c++17" COMPILER_SUPPORTS_CXX17)
if(COMPILER_SUPPORTS_CXX17)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
if (CMAKE_CXX_STANDARD EQUAL 23)
check_cxx_compiler_flag("-std=c++23" COMPILER_SUPPORTS_CXX23)
if(COMPILER_SUPPORTS_CXX23)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++23 support. Please use a different C++ compiler.")
endif()
elseif(CMAKE_CXX_STANDARD EQUAL 20)
check_cxx_compiler_flag("-std=c++20" COMPILER_SUPPORTS_CXX20)
if(COMPILER_SUPPORTS_CXX20)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++20 support. Please use a different C++ compiler.")
endif()
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++17 support. Please use a different C++ compiler.")
message(STATUS "CMAKE_CXX_STANDARD: ${CMAKE_CXX_STANDARD} change to c++17")
check_cxx_compiler_flag("-std=c++17" COMPILER_SUPPORTS_CXX17)
if(COMPILER_SUPPORTS_CXX17)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++17 support. Please use a different C++ compiler.")
endif()
endif()

if(THIRD_PARTY_BUILD_TYPE MATCHES "Debug")
set(CMAKE_STATIC_LIBRARY_SUFFIX "d.a")
endif()

set(CMAKE_CXX_STANDARD 17)
if (CMAKE_CXX_STANDARD EQUAL 23)
set(CMAKE_CXX_STANDARD 23)
elseif(CMAKE_CXX_STANDARD EQUAL 20)
set(CMAKE_CXX_STANDARD 20)
else()
set(CMAKE_CXX_STANDARD 17)
endif()
# set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
Expand Down

0 comments on commit 9b9a52b

Please sign in to comment.