forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More concretely utilize local modules in our CMake code. (netdata#17022)
* Properly handle systemd.cmake as a module. * Split CMake compiler flag handling functions to their own module. * Add include guards to modules. * Prefix module names with Netdata. This ensures that we end up using our local modules instead of possibly using a system module with the same name. * Drop include guards. And shift systemd detection code to a macro so it’s less dangerous to import the module multiple times.
- Loading branch information
Showing
4 changed files
with
121 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# CMake Module to handle all the systemd-related checks for Netdata. | ||
# | ||
# Copyright (c) 2024 Netdata Inc. | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
macro(detect_systemd) | ||
find_library(SYSTEMD_LIBRARY NAMES systemd) | ||
|
||
set(ENABLE_DSYSTEMD_DBUS NO) | ||
pkg_check_modules(SYSTEMD libsystemd) | ||
|
||
if(SYSTEMD_FOUND) | ||
set(CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD "${CMAKE_REQUIRED_LIBRARIES}") | ||
set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES};${SYSTEMD_LIBRARIES}") | ||
|
||
check_c_source_compiles(" | ||
#include <systemd/sd-journal.h> | ||
int main() { | ||
int x = SD_JOURNAL_OS_ROOT; | ||
return 0; | ||
}" HAVE_SD_JOURNAL_OS_ROOT) | ||
|
||
check_symbol_exists(SD_JOURNAL_OS_ROOT "systemd/sd-journal.h" HAVE_SD_JOURNAL_OS_ROOT) | ||
check_symbol_exists(sd_journal_open_files_fd "systemd/sd-journal.h" HAVE_SD_JOURNAL_OPEN_FILES_FD) | ||
check_symbol_exists(sd_journal_restart_fields "systemd/sd-journal.h" HAVE_SD_JOURNAL_RESTART_FIELDS) | ||
check_symbol_exists(sd_journal_get_seqnum "systemd/sd-journal.h" HAVE_SD_JOURNAL_GET_SEQNUM) | ||
|
||
check_symbol_exists(sd_bus_default_system "systemd/sd-bus.h" HAVE_SD_BUS_DEFAULT_SYSTEM) | ||
check_symbol_exists(sd_bus_call_method "systemd/sd-bus.h" HAVE_SD_BUS_CALL_METHOD) | ||
check_symbol_exists(sd_bus_message_enter_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_ENTER_CONTAINER) | ||
check_symbol_exists(sd_bus_message_read "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_READ) | ||
check_symbol_exists(sd_bus_message_exit_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_EXIT_CONTAINER) | ||
|
||
set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD}") | ||
|
||
set(HAVE_SYSTEMD True) | ||
if(HAVE_SD_BUS_DEFAULT_SYSTEM AND HAVE_SD_BUS_CALL_METHOD AND HAVE_SD_BUS_MESSAGE_ENTER_CONTAINER AND HAVE_SD_BUS_MESSAGE_READ AND HAVE_SD_BUS_MESSAGE_EXIT_CONTAINER) | ||
set(ENABLE_SYSTEMD_DBUS YES) | ||
endif() | ||
endif() | ||
endmacro() |
This file was deleted.
Oops, something went wrong.