BastionGuard/cmake/BastionGuardInit.cmake
specialworld83 588e87590d add multi-init support and Gentoo/Alpine packaging
* add support for systemd, OpenRC, SysVinit, and Dinit
* add automatic init-system detection through CMake
* make libsystemd optional for non-systemd builds
* add native service definitions for all supported init systems
* add Gentoo ebuild and Alpine APKBUILD packaging support
* publish the official BastionGuard source repository
* update the README with supported distributions, init systems, repository information, and build documentation
2026-07-21 14:15:32 +02:00

85 lines
3.4 KiB
CMake

# BastionGuard init-system selection.
#
# BASTIONGUARD_INIT_SYSTEM may be AUTO, SYSTEMD, OPENRC, SYSVINIT or DINIT.
# AUTO is suitable for local builds. Distribution packages should pass an
# explicit value because the build host/chroot init may differ from the target.
set(BASTIONGUARD_INIT_SYSTEM "AUTO" CACHE STRING
"Init system used by BastionGuard: AUTO, SYSTEMD, OPENRC, SYSVINIT or DINIT")
set_property(CACHE BASTIONGUARD_INIT_SYSTEM PROPERTY STRINGS
AUTO SYSTEMD OPENRC SYSVINIT DINIT)
function(bastionguard_detect_init_system out_var)
string(TOUPPER "${BASTIONGUARD_INIT_SYSTEM}" _requested)
if(NOT _requested MATCHES "^(AUTO|SYSTEMD|OPENRC|SYSVINIT|DINIT)$")
message(FATAL_ERROR
"Invalid BASTIONGUARD_INIT_SYSTEM='${BASTIONGUARD_INIT_SYSTEM}'. "
"Use AUTO, SYSTEMD, OPENRC, SYSVINIT or DINIT.")
endif()
if(NOT _requested STREQUAL "AUTO")
set(${out_var} "${_requested}" PARENT_SCOPE)
return()
endif()
find_program(_BG_RC_SERVICE rc-service)
find_program(_BG_OPENRC_RUN openrc-run)
find_program(_BG_SYSTEMCTL systemctl)
find_program(_BG_DINITCTL dinitctl)
find_program(_BG_SERVICE service)
find_program(_BG_UPDATE_RC_D update-rc.d)
find_program(_BG_CHKCONFIG chkconfig)
# Prefer runtime markers, then the configured PID 1 implementation.
# Tool presence alone is only a fallback because compatibility commands
# can coexist with another init system.
set(_BG_INIT_REAL "")
if(EXISTS "/sbin/init")
get_filename_component(_BG_INIT_REAL "/sbin/init" REALPATH)
elseif(EXISTS "/usr/sbin/init")
get_filename_component(_BG_INIT_REAL "/usr/sbin/init" REALPATH)
endif()
string(TOLOWER "${_BG_INIT_REAL}" _BG_INIT_REAL_LOWER)
if(EXISTS "/run/openrc")
set(_detected "OPENRC")
elseif(EXISTS "/run/systemd/system")
set(_detected "SYSTEMD")
elseif(EXISTS "/dev/dinitctl" OR EXISTS "/run/dinitctl")
set(_detected "DINIT")
elseif(_BG_INIT_REAL_LOWER MATCHES "openrc")
set(_detected "OPENRC")
elseif(_BG_INIT_REAL_LOWER MATCHES "systemd")
set(_detected "SYSTEMD")
elseif(_BG_INIT_REAL_LOWER MATCHES "(^|/)dinit($|[-.])")
set(_detected "DINIT")
elseif(_BG_INIT_REAL_LOWER MATCHES "sysvinit")
set(_detected "SYSVINIT")
elseif(_BG_RC_SERVICE AND _BG_OPENRC_RUN)
set(_detected "OPENRC")
elseif(_BG_DINITCTL AND NOT _BG_SYSTEMCTL)
set(_detected "DINIT")
message(WARNING
"No active init marker found; selecting DINIT because dinitctl is "
"available and systemctl is not. Set -DBASTIONGUARD_INIT_SYSTEM "
"explicitly for reproducible packages.")
elseif(_BG_SYSTEMCTL)
# Packaging fallback when configuring in a chroot without a running
# PID 1. Pass an explicit value for reproducible packages.
set(_detected "SYSTEMD")
message(WARNING
"No active init marker found; selecting SYSTEMD because systemctl "
"is available. Set -DBASTIONGUARD_INIT_SYSTEM explicitly for "
"reproducible packages.")
elseif(EXISTS "/etc/init.d" AND
(_BG_SERVICE OR _BG_UPDATE_RC_D OR _BG_CHKCONFIG))
set(_detected "SYSVINIT")
else()
message(FATAL_ERROR
"Unable to detect the init system. Set "
"-DBASTIONGUARD_INIT_SYSTEM=SYSTEMD|OPENRC|SYSVINIT|DINIT.")
endif()
set(${out_var} "${_detected}" PARENT_SCOPE)
endfunction()