diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 11:19:16 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 12:07:37 +0000 |
commit | b485aab7e71c1625cfc27e0f92c9509f42378458 (patch) | |
tree | ae9abe108601079d1679194de237c9a435ae5b55 /packaging/cmake/Modules/NetdataCompilerFlags.cmake | |
parent | Adding upstream version 1.44.3. (diff) | |
download | netdata-b485aab7e71c1625cfc27e0f92c9509f42378458.tar.xz netdata-b485aab7e71c1625cfc27e0f92c9509f42378458.zip |
Adding upstream version 1.45.3+dfsg.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'packaging/cmake/Modules/NetdataCompilerFlags.cmake')
-rw-r--r-- | packaging/cmake/Modules/NetdataCompilerFlags.cmake | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/packaging/cmake/Modules/NetdataCompilerFlags.cmake b/packaging/cmake/Modules/NetdataCompilerFlags.cmake new file mode 100644 index 000000000..894e244ce --- /dev/null +++ b/packaging/cmake/Modules/NetdataCompilerFlags.cmake @@ -0,0 +1,75 @@ +# Functions to simplify handling of extra compiler flags. +# +# Copyright (c) 2024 Netdata Inc. +# SPDX-License-Identifier: GPL-3.0-or-later + +include(CheckCCompilerFlag) +include(CheckCXXCompilerFlag) + +# Construct a pre-processor safe name +# +# This takes a specified value, and assigns the generated name to the +# specified target. +function(make_cpp_safe_name value target) + string(REPLACE "-" "_" tmp "${value}") + string(REPLACE "=" "_" tmp "${tmp}") + set(${target} "${tmp}" PARENT_SCOPE) +endfunction() + +# Conditionally add an extra compiler flag to C and C++ flags. +# +# If the language flags already match the `match` argument, skip this flag. +# Otherwise, check for support for `flag` and if support is found, add it to +# the language-specific `target` flag group. +function(add_simple_extra_compiler_flag match flag target) + set(CMAKE_REQUIRED_FLAGS "-Werror") + + make_cpp_safe_name("${flag}" flag_name) + + if(NOT ${CMAKE_C_FLAGS} MATCHES ${match}) + check_c_compiler_flag("${flag}" HAVE_C_${flag_name}) + if(HAVE_C_${flag_name}) + set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag}" PARENT_SCOPE) + endif() + endif() + + if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match}) + check_cxx_compiler_flag("${flag}" HAVE_CXX_${flag_name}) + if(HAVE_CXX_${flag_name}) + set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag}" PARENT_SCOPE) + endif() + endif() +endfunction() + +# Same as add_simple_extra_compiler_flag, but check for a second flag if the +# first one is unsupported. +function(add_double_extra_compiler_flag match flag1 flag2 target) + set(CMAKE_REQUIRED_FLAGS "-Werror") + + make_cpp_safe_name("${flag1}" flag1_name) + make_cpp_safe_name("${flag2}" flag2_name) + + if(NOT ${CMAKE_C_FLAGS} MATCHES ${match}) + check_c_compiler_flag("${flag1}" HAVE_C_${flag1_name}) + if(HAVE_C_${flag1_name}) + set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag1}" PARENT_SCOPE) + else() + check_c_compiler_flag("${flag2}" HAVE_C_${flag2_name}) + if(HAVE_C_${flag2_name}) + set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag2}" PARENT_SCOPE) + endif() + endif() + endif() + + if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match}) + check_cxx_compiler_flag("${flag1}" HAVE_CXX_${flag1_name}) + if(HAVE_CXX_${flag1_name}) + set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag1}" PARENT_SCOPE) + else() + check_cxx_compiler_flag("${flag2}" HAVE_CXX_${flag2_name}) + if(HAVE_CXX_${flag2_name}) + set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag2}" PARENT_SCOPE) + endif() + endif() + endif() +endfunction() |