CMakeLists.txt: always use highest available C++17/C++14/C++11 standard

Check for the availability of the C++17, C++14, and C++11 language
standards. Prefer the highest available to build any of the feature
tests or applications. Factor out common conditions into a central spot
in the CMake build rules. Rename variables to avoid special chars in
their name.

Setup both the CMAKE_CXX_STANDARD cmake variable which internally is
used transparently, as well explicitly pass the -std= compile flag in
build instructions for feature checks and application code. It's what
the smuview build does, should also work for pulseview, and is assumed
to not harm either in case it's redundant.

This unbreaks operation in most generic ways on platforms like MacOS 12
(system library), and for external libraries of differing degrees of
aggressivness (boost, glibmm, sig++).
This commit is contained in:
Gerhard Sittig 2022-11-24 14:48:00 +01:00
parent 0f76477f61
commit fa8d0fcb4c
1 changed files with 20 additions and 15 deletions

View File

@ -82,7 +82,24 @@ include(memaccess)
find_package(PkgConfig)
check_cxx_compiler_flag("-std=c++17" PV_HAVE_C++17)
check_cxx_compiler_flag("-std=c++17" HAVE_STD_CXX_17)
check_cxx_compiler_flag("-std=c++14" HAVE_STD_CXX_14)
check_cxx_compiler_flag("-std=c++11" HAVE_STD_CXX_11)
if(HAVE_STD_CXX_17)
message(STATUS "Using C++17 for the application build")
set(CMAKE_CXX_STANDARD 17)
set(REQUIRED_STD_CXX_FLAGS "-std=c++17")
elseif(HAVE_STD_CXX_14)
message(STATUS "Using C++14 for the application build")
set(CMAKE_CXX_STANDARD 14)
set(REQUIRED_STD_CXX_FLAGS "-std=c++14")
elseif(HAVE_STD_CXX_11)
message(STATUS "Using C++11 for the application build")
set(CMAKE_CXX_STANDARD 11)
set(REQUIRED_STD_CXX_FLAGS "-std=c++11")
else()
message(FATAL_ERROR "Need modern C++, at least language standard 11")
endif()
list(APPEND PKGDEPS glib-2.0>=2.28.0)
@ -169,13 +186,7 @@ find_package(Threads REQUIRED)
# Helper for checking for atomics
function(check_working_cxx_atomics varname additional_lib)
cmake_push_check_state()
# The OSX 12 (Monteray) stdlib requires C++17 to compile.
if(APPLE AND PV_HAVE_C++17)
message(STATUS "Using C++17 for OSX build")
set(CMAKE_REQUIRED_FLAGS "-std=c++17")
else()
set(CMAKE_REQUIRED_FLAGS "-std=c++11")
endif()
set(CMAKE_REQUIRED_FLAGS "${REQUIRED_STD_CXX_FLAGS}")
set(CMAKE_REQUIRED_LIBRARIES "${additional_lib}")
set(CMAKE_REQUIRED_QUIET 1)
CHECK_CXX_SOURCE_COMPILES("
@ -508,13 +519,7 @@ qt5_add_resources(pulseview_RESOURCES_RCC ${CMAKE_BINARY_DIR}/translations.qrc)
add_definitions(-DQT_NO_KEYWORDS)
add_definitions(-D__STDC_LIMIT_MACROS)
add_definitions(-Wall -Wextra)
# The OSX 12 Monteray stdlib requires C++17 to compile.
if(APPLE AND PV_HAVE_C++17)
add_definitions(-std=c++17)
else()
add_definitions(-std=c++11)
endif()
add_definitions(${REQUIRED_STD_CXX_FLAGS})
add_definitions(-DBOOST_MATH_DISABLE_FLOAT128=1)
if(WIN32)