1# systemd.m4 - Macros to check for and enable systemd          -*- Autoconf -*-
2#
3# Copyright (C) 2014 Luis R. Rodriguez <mcgrof@suse.com>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; If not, see <http://www.gnu.org/licenses/>.
17
18dnl Some optional path options
19AC_DEFUN([AX_SYSTEMD_OPTIONS], [
20	AC_ARG_WITH(systemd,
21		AS_HELP_STRING([--with-systemd=DIR],
22		[set directory for systemd service files [PREFIX/lib/systemd/system]]),
23		[SYSTEMD_DIR="$withval"],[SYSTEMD_DIR=""])
24	AC_SUBST(SYSTEMD_DIR)
25
26	AC_ARG_WITH(systemd-modules-load,
27		AS_HELP_STRING([--with-systemd-modules-load=DIR],
28		[set directory for systemd modules load files [PREFIX/lib/modules-load.d/]]),
29		[SYSTEMD_MODULES_LOAD="$withval"], [SYSTEMD_MODULES_LOAD=""])
30	AC_SUBST(SYSTEMD_MODULES_LOAD)
31])
32
33AC_DEFUN([AX_ENABLE_SYSTEMD_OPTS], [
34	AX_ARG_DEFAULT_ENABLE([systemd], [Disable systemd support])
35	AX_SYSTEMD_OPTIONS()
36])
37
38AC_DEFUN([AX_ALLOW_SYSTEMD_OPTS], [
39	AX_ARG_DEFAULT_DISABLE([systemd], [Enable systemd support])
40	AX_SYSTEMD_OPTIONS()
41])
42
43AC_DEFUN([AX_CHECK_SYSTEMD_LIBS], [
44	PKG_CHECK_MODULES([SYSTEMD], [libsystemd-daemon],,
45		[PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 209])]
46        )
47	dnl pkg-config older than 0.24 does not set these for
48	dnl PKG_CHECK_MODULES() worth also noting is that as of version 208
49	dnl of systemd pkg-config --cflags currently yields no extra flags yet.
50	AC_SUBST([SYSTEMD_CFLAGS])
51	AC_SUBST([SYSTEMD_LIBS])
52
53	AS_IF([test "x$SYSTEMD_DIR" = x], [
54	    dnl In order to use the line below we need to fix upstream systemd
55	    dnl to properly ${prefix} for child variables in
56	    dnl src/core/systemd.pc.in but this is a bit complex at the
57	    dnl moment as they depend on another rootprefix, which can vary
58	    dnl from prefix in practice. We provide our own definition as we
59	    dnl *know* where systemd will dump this to, but this does limit
60	    dnl us to stick to a non custom systemdsystemunitdir, to work
61	    dnl around this we provide the additional configure option
62	    dnl --with-systemd where you can specify the directory for the unit
63	    dnl files. It would also be best to just extend the upstream
64	    dnl pkg-config  pkg.m4 with an AC_DEFUN() to do this neatly.
65	    dnl SYSTEMD_DIR="`$PKG_CONFIG --define-variable=prefix=$PREFIX --variable=systemdsystemunitdir systemd`"
66	    SYSTEMD_DIR="\$(prefix)/lib/systemd/system/"
67	], [])
68
69	AS_IF([test "x$SYSTEMD_DIR" = x], [
70	    AC_MSG_ERROR([SYSTEMD_DIR is unset])
71	], [])
72
73	dnl There is no variable for this yet for some reason
74	AS_IF([test "x$SYSTEMD_MODULES_LOAD" = x], [
75	    SYSTEMD_MODULES_LOAD="\$(prefix)/lib/modules-load.d/"
76	], [])
77
78	AS_IF([test "x$SYSTEMD_MODULES_LOAD" = x], [
79	    AC_MSG_ERROR([SYSTEMD_MODULES_LOAD is unset])
80	], [])
81])
82
83AC_DEFUN([AX_CHECK_SYSTEMD], [
84	dnl Respect user override to disable
85	AS_IF([test "x$enable_systemd" != "xno"], [
86	     AS_IF([test "x$systemd" = "xy" ], [
87		AC_DEFINE([HAVE_SYSTEMD], [1], [Systemd available and enabled])
88			systemd=y
89			AX_CHECK_SYSTEMD_LIBS()
90	    ],[
91		AS_IF([test "x$enable_systemd" = "xyes"],
92			[AC_MSG_ERROR([Unable to find systemd development library])],
93			[systemd=n])
94	    ])
95	],[systemd=n])
96])
97
98AC_DEFUN([AX_CHECK_SYSTEMD_ENABLE_AVAILABLE], [
99	PKG_CHECK_MODULES([SYSTEMD], [libsystemd-daemon], [systemd="y"],[
100		PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 209],
101				  [systemd="y"],[systemd="n"])
102	])
103])
104
105dnl Enables systemd by default and requires a --disable-systemd option flag
106dnl to configure if you want to disable.
107AC_DEFUN([AX_ENABLE_SYSTEMD], [
108	AX_ENABLE_SYSTEMD_OPTS()
109	AX_CHECK_SYSTEMD()
110])
111
112dnl Systemd will be disabled by default and requires you to run configure with
113dnl --enable-systemd to look for and enable systemd.
114AC_DEFUN([AX_ALLOW_SYSTEMD], [
115	AX_ALLOW_SYSTEMD_OPTS()
116	AX_CHECK_SYSTEMD()
117])
118
119dnl Systemd will be disabled by default but if your build system is detected
120dnl to have systemd build libraries it will be enabled. You can always force
121dnl disable with --disable-systemd
122AC_DEFUN([AX_AVAILABLE_SYSTEMD], [
123	AX_ALLOW_SYSTEMD_OPTS()
124	AX_CHECK_SYSTEMD_ENABLE_AVAILABLE()
125	AX_CHECK_SYSTEMD()
126])
127