Skip to content

Commit

Permalink
More concretely utilize local modules in our CMake code. (netdata#17022)
Browse files Browse the repository at this point in the history
* 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
Ferroin authored Feb 26, 2024
1 parent cd145f8 commit ca01640
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 105 deletions.
72 changes: 4 additions & 68 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ project(netdata
DESCRIPTION "Netdata real-time monitoring"
HOMEPAGE_URL "https://www.netdata.cloud"
LANGUAGES C CXX)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/packaging/cmake/Modules")

find_package(PkgConfig REQUIRED)

Expand Down Expand Up @@ -165,8 +166,7 @@ endif()
# handling of extra compiler flags
#

include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(NetdataCompilerFlags)

# Disable hardening for debug builds by default.
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
Expand All @@ -175,71 +175,6 @@ else()
option(DISABLE_HARDENING "disable adding extra compiler flags for hardening" FALSE)
endif()

# Construct a pre-processor safe name
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()

set(EXTRA_HARDENING_C_FLAGS "")
set(EXTRA_HARDENING_CXX_FLAGS "")

Expand Down Expand Up @@ -1524,7 +1459,8 @@ set_source_files_properties(JudyLTables.c PROPERTIES COMPILE_OPTIONS "-I${CMAKE_
# build libnetdata
#

include(packaging/cmake/systemd.cmake)
include(NetdataDetectSystemd)
detect_systemd()

add_library(libnetdata STATIC ${LIBNETDATA_FILES})

Expand Down
75 changes: 75 additions & 0 deletions packaging/cmake/Modules/NetdataCompilerFlags.cmake
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()
42 changes: 42 additions & 0 deletions packaging/cmake/Modules/NetdataDetectSystemd.cmake
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()
37 changes: 0 additions & 37 deletions packaging/cmake/systemd.cmake

This file was deleted.

0 comments on commit ca01640

Please sign in to comment.