]> git.proxmox.com Git - mirror_lxcfs.git/commitdiff
build: use jinja2 templating
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 5 Nov 2021 12:30:28 +0000 (13:30 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 5 Nov 2021 12:49:40 +0000 (13:49 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
13 files changed:
config/init/meson.build [new file with mode: 0644]
config/init/systemd/lxcfs.service.in
lxcfs.spec.in
meson.build
share/00-lxcfs.conf.in
share/lxc.mount.hook.in
share/meson.build
tests/main.sh.in
tests/meson.build
tests/test_confinement.sh.in
tests/test_read_proc.sh.in
tests/test_reload.sh.in
tools/meson-render-jinja2.py [new file with mode: 0755]

diff --git a/config/init/meson.build b/config/init/meson.build
new file mode 100644 (file)
index 0000000..0a265b7
--- /dev/null
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+if init_script == 'systemd'
+        systemd_service = custom_target(
+                                'lxcfs.service',
+                                input : 'systemd/lxcfs.service.in',
+                                output : 'lxcfs.service',
+                               command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'],
+                               install: true,
+                               install_dir: '/lib/systemd/system')
+elif init_script == 'upstart'
+        install_data('upstart/lxcfs.conf', install_dir : join_paths(sysconfdir, 'init'))
+elif init_script == 'openrc'
+        install_data('sysvinit/lxcfs', install_dir : join_paths(sysconfdir, 'rc.d/init.d/lxcfs'))
+elif init_script == 'sysvinit'
+        install_data('sysvinit/lxcfs', install_dir : join_paths(sysconfdir, 'rc.d/init.d/lxcfs'))
+endif
index 9cd4e59878f38b2256f34a86a52aea624baf9f5c..9da02f6ce7481bced50efcc6a075e8290a2fbf1d 100644 (file)
@@ -5,10 +5,10 @@ Before=lxc.service
 Documentation=man:lxcfs(1)
 
 [Service]
-ExecStart=/usr/bin/lxcfs @LXCFSTARGETDIR@
+ExecStart=/usr/bin/lxcfs {{LXCFSTARGETDIR}}
 KillMode=process
 Restart=on-failure
-ExecStopPost=-/bin/fusermount -u @LXCFSTARGETDIR@
+ExecStopPost=-/bin/fusermount -u {{LXCFSTARGETDIR}}
 Delegate=yes
 ExecReload=/bin/kill -USR1 $MAINPID
 
index 8431e743d6c4947da7a128fdf5c13851b46f562d..90bb033b36fd83419e2c7c5b82d7a73eeae9716b 100644 (file)
@@ -19,8 +19,8 @@ BuildRequires: systemd
 %endif
 
 Summary: Linux Containers File System
-Name: @PACKAGE@
-Version: @PACKAGE_VERSION@
+Name: {{PROJECT}}
+Version: {{PROJECT_VERSION}}
 Release: 1%{?dist}
 URL: https://linuxcontainers.org/lxcfs/downloads/
 Source0: %{name}-%{version}.tar.gz
index 7c4e627181ab194c02c9ca287523edb99fce60b5..6bf147b3eb3abbb617ddd63a038c2c21770f7b36 100644 (file)
@@ -14,8 +14,9 @@ project('lxcfs', 'c',
        )
 
 conf = configuration_data()
+conf.set_quoted('PROJECT', meson.project_name())
 conf.set_quoted('PROJECT_URL', 'https://linuxcontainers.org/lxcfs/introduction/')
-conf.set('PROJECT_VERSION',    meson.project_version(),
+conf.set_quoted('PROJECT_VERSION',    meson.project_version(),
          description : 'Numerical project version (used where a simple number is expected)')
 project_source_root = meson.current_source_dir()
 project_build_root = meson.current_build_dir()
@@ -53,6 +54,12 @@ init_script = get_option('init-script')
 cc = meson.get_compiler('c')
 meson_build_sh = find_program('tools/meson-build.sh')
 
+if run_command('python3', '-c', 'import jinja2').returncode() != 0
+        error('python3 jinja2 missing')
+endif
+
+meson_render_jinja2 = find_program('tools/meson-render-jinja2.py')
+
 want_tests = get_option('tests')
 
 possible_cc_flags = [
@@ -152,6 +159,7 @@ config_include = include_directories('.')
 
 add_project_arguments('-include', 'config.h', language : 'c')
 
+test_programs = []
 subdir('tests')
 
 public_programs = []
@@ -220,31 +228,15 @@ if want_tests == true
                c_args : '-DRELOADTEST -DDEBUG')
 endif
 
-if init_script == 'systemd'
-        systemd_service_data = configuration_data()
-       systemd_service_data.set_quoted('LXCFSTARGETDIR', join_paths(localstatedir, 'lib/lxcfs'))
-        systemd_service = configure_file(
-                               configuration : systemd_service_data,
-                                input : 'config/init/systemd/lxcfs.service.in',
-                                output : 'lxcfs.service')
-        install_data(join_paths(project_build_root, 'lxcfs.service'), install_dir : '/lib/systemd/system')
-elif init_script == 'upstart'
-        install_data('config/init/upstart/lxcfs.conf', install_dir : join_paths(sysconfdir, 'init'))
-elif init_script == 'openrc'
-        install_data('config/init/sysvinit/lxcfs', install_dir : join_paths(sysconfdir, 'rc.d/init.d/lxcfs'))
-elif init_script == 'sysvinit'
-        install_data('config/init/sysvinit/lxcfs', install_dir : join_paths(sysconfdir, 'rc.d/init.d/lxcfs'))
-endif
-
-lxcfs_spec_data = configuration_data()
-lxcfs_spec_data.set('PACKAGE_VERSION', meson.project_version())
-lxcfs_spec_data.set('PACKAGE', meson.project_name())
-lxcfs_spec_data = configure_file(
-                       configuration : lxcfs_spec_data,
-                        input : 'lxcfs.spec.in',
-                        output : 'lxcfs.spec')
+lxcfs_spec = custom_target(
+       'lxcfs.spec',
+       build_by_default : true,
+        input : 'lxcfs.spec.in',
+        output : 'lxcfs.spec',
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'])
 
 subdir('share')
+subdir('config/init')
 
 status = [
         '@0@ @1@'.format(meson.project_name(), meson.project_version()),
index 5c88d4719efab32dd7e8553e9f9c6c857f515878..e7e261d00ae77ec49f9819a1ab0c6c2f7456a0c3 100644 (file)
@@ -1,2 +1,2 @@
-lxc.hook.mount = @LXCFSSHAREDIR@/lxc.mount.hook
-lxc.hook.post-stop = @LXCFSSHAREDIR@/lxc.reboot.hook
+lxc.hook.mount = {{LXCFSSHAREDIR}}/lxc.mount.hook
+lxc.hook.post-stop = {{LXCFSSHAREDIR}}/lxc.reboot.hook
index 465f4e33ef46145dfdfce35efadf8846e4b0a406..cf81d2c635b28c287147c616261a9f814122c20d 100755 (executable)
@@ -15,8 +15,8 @@ done
 LXC_ROOTFS_MOUNT=$(readlink -f "${LXC_ROOTFS_MOUNT}")
 
 # /proc files
-if [ -d @LXCFSTARGETDIR@/proc/ ]; then
-    for entry in @LXCFSTARGETDIR@/proc/*; do
+if [ -d {{LXCFSTARGETDIR}}/proc/ ]; then
+    for entry in {{LXCFSTARGETDIR}}/proc/*; do
         DEST=$(basename "$entry")
         [ -e "${LXC_ROOTFS_MOUNT}/proc/${DEST}" ] || continue
         mount -n --bind "$entry" "${LXC_ROOTFS_MOUNT}/proc/${DEST}"
@@ -24,11 +24,11 @@ if [ -d @LXCFSTARGETDIR@/proc/ ]; then
 fi
 
 # /sys/devices/system/cpu
-if [ -d @LXCFSTARGETDIR@/sys/devices/system/cpu ] ; then
-    if [ -f @LXCFSTARGETDIR@/sys/devices/system/cpu/uevent ]; then
-        mount -n --bind @LXCFSTARGETDIR@/sys/devices/system/cpu "${LXC_ROOTFS_MOUNT}/sys/devices/system/cpu"
+if [ -d {{LXCFSTARGETDIR}}/sys/devices/system/cpu ] ; then
+    if [ -f {{LXCFSTARGETDIR}}/sys/devices/system/cpu/uevent ]; then
+        mount -n --bind {{LXCFSTARGETDIR}}/sys/devices/system/cpu "${LXC_ROOTFS_MOUNT}/sys/devices/system/cpu"
     else
-        for entry in @LXCFSTARGETDIR@/sys/devices/system/cpu/*; do
+        for entry in {{LXCFSTARGETDIR}}/sys/devices/system/cpu/*; do
             DEST=$(basename "$entry")
             [ -e "${LXC_ROOTFS_MOUNT}/sys/devices/system/cpu/${DEST}" ] || continue
             mount -n --bind "$entry" "${LXC_ROOTFS_MOUNT}/sys/devices/system/cpu/${DEST}"
@@ -37,8 +37,8 @@ if [ -d @LXCFSTARGETDIR@/sys/devices/system/cpu ] ; then
 fi
 
 # Allow nesting lxcfs
-if [ -d "${LXC_ROOTFS_MOUNT}@LXCFSTARGETDIR@/" ]; then
-    mount -n --bind @LXCFSTARGETDIR@ "${LXC_ROOTFS_MOUNT}@LXCFSTARGETDIR@/"
+if [ -d "${LXC_ROOTFS_MOUNT}{{LXCFSTARGETDIR}}/" ]; then
+    mount -n --bind {{LXCFSTARGETDIR}} "${LXC_ROOTFS_MOUNT}{{LXCFSTARGETDIR}}/"
 fi
 
 # no need for lxcfs cgroups if we have cgroup namespaces
@@ -59,7 +59,7 @@ fi
 
 # /sys/fs/cgroup files
 if [ -d "${LXC_ROOTFS_MOUNT}/sys/fs/cgroup" ]; then
-    if [ -d @LXCFSTARGETDIR@/cgroup ]; then
+    if [ -d {{LXCFSTARGETDIR}}/cgroup ]; then
         # Cleanup existing mounts
         for entry in "${LXC_ROOTFS_MOUNT}/sys/fs/cgroup"/*; do
             DEST=$(basename "$entry")
@@ -83,7 +83,7 @@ if [ -d "${LXC_ROOTFS_MOUNT}/sys/fs/cgroup" ]; then
         done
 
         # Mount the new entries
-        for entry in @LXCFSTARGETDIR@/cgroup/*; do
+        for entry in {{LXCFSTARGETDIR}}/cgroup/*; do
             DEST=$(basename "$entry")
             if [ "$DEST" = "name=systemd" ]; then
                 DEST="systemd"
index edeb5b91526726b09434fca23a3f71e64b715e3f..9b780eff652541457ef8d569c1811b76f6070ea0 100644 (file)
@@ -1,24 +1,25 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 
-lxcfs_conf_data = configuration_data()
-lxcfs_conf_data.set('LXCFSSHAREDIR', lxcfssharedir)
-lxcfs_conf_data = configure_file(
-                       configuration : lxcfs_conf_data,
-                        input : '00-lxcfs.conf.in',
-                        output : '00-lxcfs.conf',
-                       install: true,
-                       install_dir: lxcconfdir)
+lxcfs_conf_data = custom_target(
+       '00-lxcfs.conf',
+       input : '00-lxcfs.conf.in',
+       output : '00-lxcfs.conf',
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'],
+       install : true,
+       install_dir : lxcconfdir)
 
-lxcfs_hook_mount_data = configure_file(
-                       configuration : conf,
-                        input : 'lxc.mount.hook.in',
-                        output : 'lxc.mount.hook',
-                       install: true,
-                       install_dir: lxcfssharedir)
+lxcfs_hook_mount_data = custom_target(
+        'lxc.mount.hook',
+        input : 'lxc.mount.hook.in',
+        output : 'lxc.mount.hook',
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'],
+       install : true,
+       install_dir : lxcfssharedir)
 
-lxcfs_hook_reboot_data = configure_file(
-                       configuration : configuration_data(),
-                        input : 'lxc.reboot.hook.in',
-                        output : 'lxc.reboot.hook',
-                       install: true,
-                       install_dir: lxcfssharedir)
+lxcfs_hook_reboot_data = custom_target(
+        'lxc.reboot.hook',
+        input : 'lxc.reboot.hook.in',
+        output : 'lxc.reboot.hook',
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'],
+       install : true,
+       install_dir : lxcfssharedir)
index 261fc9935d8347d1deca74266302d9510164ef64..656bb6a03378cd61c6f05bf391be2afc10d22afe 100755 (executable)
@@ -36,20 +36,20 @@ cleanup() {
 }
 
 TESTCASE="setup"
-lxcfs="@LXCFS_BUILD_ROOT@/lxcfs"
+lxcfs="{{LXCFS_BUILD_ROOT}}/lxcfs"
 
 if [ -x ${lxcfs} ]; then
        if [ -n "${LD_LIBRARY_PATH:-}" ]; then
-               export LD_LIBRARY_PATH="@LXCFS_BUILD_ROOT@:${LD_LIBRARY_PATH}"
+               export LD_LIBRARY_PATH="{{LXCFS_BUILD_ROOT}}:${LD_LIBRARY_PATH}"
        else
-               export LD_LIBRARY_PATH="@LXCFS_BUILD_ROOT@"
+               export LD_LIBRARY_PATH="{{LXCFS_BUILD_ROOT}}"
        fi
        echo "=> Spawning ${lxcfs} ${LXCFSDIR}"
        ${lxcfs} -p ${pidfile} ${LXCFSDIR} &
        LXCFSPID=$!
 else
        UNSHARE=0
-       LXCFSPID=$(cat "@RUNTIME_PATH@/lxcfs.pid")
+       LXCFSPID=$(cat "{{RUNTIME_PATH}}/lxcfs.pid")
        echo "=> Re-using host lxcfs"
        rmdir $LXCFSDIR
        export LXCFSDIR=/var/lib/lxcfs
index 9179c454c11beefa1897a1d65ee56ef5b4cb699b..5670a81cc7ed475d5afa8510bb749fd72743a28a 100644 (file)
@@ -1,58 +1,76 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 
-test_main = configure_file(
+test_programs += custom_target(
+        'main.sh',
+       build_by_default : want_tests != false,
         input : 'main.sh.in',
         output : 'main.sh',
-        configuration : conf)
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'])
 
-test_main = configure_file(
+test_programs += custom_target(
+        'test_cgroup',
+       build_by_default : want_tests != false,
         input : 'test_cgroup.in',
         output : 'test_cgroup',
-        configuration : conf)
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'])
 
-test_main = configure_file(
+test_programs += custom_target(
+        'test_confinement.sh',
+       build_by_default : want_tests != false,
         input : 'test_confinement.sh.in',
         output : 'test_confinement.sh',
-        configuration : conf)
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'])
 
-test_main = configure_file(
+test_programs += custom_target(
+        'test_meminfo_hierarchy.sh',
+       build_by_default : want_tests != false,
         input : 'test_meminfo_hierarchy.sh.in',
         output : 'test_meminfo_hierarchy.sh',
-        configuration : conf)
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'])
 
-test_main = configure_file(
+test_programs += custom_target(
+        'test_proc',
+       build_by_default : want_tests != false,
         input : 'test_proc.in',
         output : 'test_proc',
-        configuration : conf)
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'])
 
-test_main = configure_file(
+test_programs += custom_target(
+        'test_read_proc.sh',
+       build_by_default : want_tests != false,
         input : 'test_read_proc.sh.in',
         output : 'test_read_proc.sh',
-        configuration : conf)
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'])
 
-test_main = configure_file(
+test_programs += custom_target(
+        'test_readdir',
+       build_by_default : want_tests != false,
         input : 'test_readdir.in',
         output : 'test_readdir',
-        configuration : conf)
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'])
 
-test_main = configure_file(
+test_programs += custom_target(
+        'test_reload.sh',
+       build_by_default : want_tests != false,
         input : 'test_reload.sh.in',
         output : 'test_reload.sh',
-        configuration : conf)
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'])
 
-test_main = configure_file(
+test_programs += custom_target(
+        'test_sigusr2.sh',
+       build_by_default : want_tests != false,
         input : 'test_sigusr2.sh.in',
         output : 'test_sigusr2.sh',
-        configuration : conf)
+       command : [meson_render_jinja2, config_h, '@INPUT@', '@OUTPUT@'])
 
-test_read = executable(
+test_programs += executable(
         'test-read',
         'test-read.c',
        include_directories: config_include,
        install: false,
         build_by_default : want_tests != false)
 
-test_syscalls = executable(
+test_programs += executable(
         'test-syscalls',
         'test_syscalls.c',
        include_directories: config_include,
@@ -64,7 +82,7 @@ test_cpusetrange_sources = files(
                '../src/cpuset_parse.c',
                '../src/cpuset_parse.h')
 
-test_cpusetrange = executable(
+test_programs += executable(
         'test-cpusetrange',
         test_cpusetrange_sources,
        include_directories: config_include,
index 345ae1282550cfa23858daf0f601bc18e254cfe5..1aaa2c7fd883f51ccb10306e0d886d12046b2ee7 100755 (executable)
@@ -23,7 +23,7 @@ dirname=$(dirname ${cmdline})
 
 trap cleanup EXIT HUP INT TERM
 
-lxcfs="@LXCFS_BUILD_ROOT@/lxcfs" $d &
+lxcfs="{{LXCFS_BUILD_ROOT}}/lxcfs" $d &
 pid=$!
 
 # put ourselves into x1
index e94c62d91665a297257bf4b218a4d9d5835ec2b7..1c766cf4259c92253ca0327ad9826dc3f3bd1e39 100755 (executable)
@@ -12,12 +12,12 @@ if ! mountpoint -q $DIR; then
 fi
 
 echo "==> Testing /proc/cpuinfo"
-@LXCFS_BUILD_ROOT@/tests/test-read $DIR/proc/cpuinfo 3 >/dev/null
+{{LXCFS_BUILD_ROOT}}/tests/test-read $DIR/proc/cpuinfo 3 >/dev/null
 
 echo "==> Testing /proc/stat"
-@LXCFS_BUILD_ROOT@/tests/test-read $DIR/proc/stat 3 >/dev/null
+{{LXCFS_BUILD_ROOT}}/tests/test-read $DIR/proc/stat 3 >/dev/null
 
 echo "==> Testing /proc/meminfo"
-@LXCFS_BUILD_ROOT@/tests/test-read $DIR/proc/meminfo 3 >/dev/null
+{{LXCFS_BUILD_ROOT}}/tests/test-read $DIR/proc/meminfo 3 >/dev/null
 
 exit 0
index 322ed5cb5496bc22c2eca952b49b0ec3bd6ff4b7..6bb90f499c25bc634c58ff1875c22b2055ddbcdf 100755 (executable)
@@ -7,12 +7,12 @@ set -eu
 [ $(id -u) -eq 0 ]
 
 cmdline=$(realpath $0)
-topdir=@LXCFS_BUILD_ROOT@
+topdir={{LXCFS_BUILD_ROOT}}
 
 testdir=`mktemp -t -d libs.XXX`
 installdir=`mktemp -t -d libs.XXX`
 pidfile=$(mktemp)
-libdir=${installdir}/@LIBDIR@
+libdir=${installdir}/{{LIBDIR}}
 bindir=${installdir}/usr/bin
 lxcfspid=-1
 FAILED=1
@@ -39,7 +39,7 @@ cleanup() {
 trap cleanup EXIT HUP INT TERM
 
 echo "==> Installing lxcfs to temporary path"
-cd @LXCFS_BUILD_ROOT@
+cd {{LXCFS_BUILD_ROOT}}
 DESTDIR=${installdir} meson install
 if [ -n "${LD_LIBRARY_PATH:-}" ]; then
     export LD_LIBRARY_PATH="${libdir}:${LD_LIBRARY_PATH}"
@@ -66,7 +66,7 @@ cat ${testdir}/proc/uptime
 
 [ ! -f /tmp/lxcfs-iwashere ]
 rm -f ${libdir}/liblxcfs.so* ${libdir}/liblxcfs.la
-cp @LXCFS_BUILD_ROOT@/liblxcfstest.so ${libdir}/liblxcfs.so
+cp {{LXCFS_BUILD_ROOT}}/liblxcfstest.so ${libdir}/liblxcfs.so
 
 kill -USR1 ${lxcfspid}
 sleep 1
diff --git a/tools/meson-render-jinja2.py b/tools/meson-render-jinja2.py
new file mode 100755 (executable)
index 0000000..0f9fc43
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+import ast
+import os
+import re
+import sys
+
+import jinja2
+
+def parse_config_h(filename):
+    # Parse config.h file generated by meson.
+    ans = {}
+    for line in open(filename):
+        m = re.match(r'#define\s+(\w+)\s+(.*)', line)
+        if not m:
+            continue
+        a, b = m.groups()
+        if b and b[0] in '0123456789"':
+            b = ast.literal_eval(b)
+        ans[a] = b
+    return ans
+
+def render(filename, defines):
+    text = open(filename).read()
+    template = jinja2.Template(text, trim_blocks=True, undefined=jinja2.StrictUndefined)
+    return template.render(defines)
+
+if __name__ == '__main__':
+    defines = parse_config_h(sys.argv[1])
+    output = render(sys.argv[2], defines)
+    with open(sys.argv[3], 'w') as f:
+        f.write(output)
+    info = os.stat(sys.argv[2])
+    os.chmod(sys.argv[3], info.st_mode)