From 42dab85728385eaf538b36ed56e9db7ff3f96faa Mon Sep 17 00:00:00 2001 From: William Manley Date: Tue, 30 Aug 2016 20:28:02 +0100 Subject: [PATCH] ostree-prepare-root: Allow building statically with musl If the `--with-static-compiler=musl-gcc` configure flag is given. ostree-prepare-root can be used as init in a system without a populated /lib. To support this use case we need to link statically as we will be unable to locate libc.so at run time if it's not installed in /lib. We support building ostree-prepare-root with a different compiler to the rest of ostree so we can use musl rather than glibc. This reduces the size of the executable significantly: from ~700K -> ~30K. We have to use `_SCRIPTS` here to get autotools to install this as an executable but without generating rules to make it itself which we have specified manually. See https://lists.gnu.org/archive/html/help-gnu-utils/2007-01/msg00007.html for advice on using autotools in this manner. Closes: #477 Approved by: cgwalters --- Makefile-switchroot.am | 28 +++++++++++++++++++++++++--- configure.ac | 10 +++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Makefile-switchroot.am b/Makefile-switchroot.am index f79553eb..10866bd1 100644 --- a/Makefile-switchroot.am +++ b/Makefile-switchroot.am @@ -17,7 +17,6 @@ if BUILDOPT_SYSTEMD -ostree_boot_PROGRAMS += ostree-prepare-root ostree_boot_PROGRAMS += ostree-remount noinst_LTLIBRARIES += libswitchroot-mountutil.la @@ -28,9 +27,32 @@ libswitchroot_mountutil_la_SOURCES = \ src/switchroot/ostree-mount-util.h \ $(NULL) -ostree_prepare_root_SOURCES = src/switchroot/ostree-prepare-root.c -ostree_prepare_root_LDADD = libswitchroot-mountutil.la +ostree_prepare_root_SOURCES = \ + src/switchroot/ostree-mount-util.c \ + src/switchroot/ostree-mount-util.h \ + src/switchroot/ostree-prepare-root.c \ + $(NULL) + +if BUILDOPT_USE_STATIC_COMPILER +# ostree-prepare-root can be used as init in a system without a populated /lib. +# To support this use case we need to link statically as we will be unable to +# locate libc.so at run time if it's not installed in /lib. +# +# We support building ostree-prepare-root with a different compiler to the rest +# of ostree so we can use musl rather than glibc. This reduces the size of the +# executable significantly: from ~700K -> ~30K. We have to use _SCRIPTS here +# to get autotools to install this as an executable but without generating rules +# to make it itself which we have specified manually. See +# https://lists.gnu.org/archive/html/help-gnu-utils/2007-01/msg00007.html +ostree_boot_SCRIPTS = ostree-prepare-root + +ostree-prepare-root : $(ostree_prepare_root_SOURCES) + $(STATIC_COMPILER) -o $@ -static $(ostree_prepare_root_SOURCES) $(AM_CPPFLAGS) $(AM_CFLAGS) +else +ostree_boot_PROGRAMS += ostree-prepare-root + ostree_prepare_root_CFLAGS = $(AM_CFLAGS) -Isrc/switchroot +endif ostree_remount_SOURCES = src/switchroot/ostree-remount.c ostree_remount_LDADD = libswitchroot-mountutil.la diff --git a/configure.ac b/configure.ac index 6d322c9c..51fb13c0 100644 --- a/configure.ac +++ b/configure.ac @@ -295,6 +295,13 @@ AS_IF([test x$with_grub2_mkconfig_path = x], [ ],[GRUB2_MKCONFIG=$with_grub2_mkconfig_path]) AC_DEFINE_UNQUOTED([GRUB2_MKCONFIG_PATH], ["$GRUB2_MKCONFIG"], [The system grub2-mkconfig executible name]) +AC_ARG_WITH(static-compiler, + AS_HELP_STRING([--with-static-compiler], + [Use the given compiler to build ostree-prepare-root statically linked (default: no)]),, + [with_static_compiler=no]) +AM_CONDITIONAL(BUILDOPT_USE_STATIC_COMPILER, test x$with_static_compiler != xno) +AC_SUBST(STATIC_COMPILER, $with_static_compiler) + dnl for tests AS_IF([test "x$found_introspection" = xyes], [ AC_PATH_PROG(GJS, [gjs]) @@ -332,7 +339,8 @@ echo " api docs (gtk-doc): $enable_gtk_doc gjs-based tests: $have_gjs dracut: $with_dracut - mkinitcpio: $with_mkinitcpio" + mkinitcpio: $with_mkinitcpio + Static compiler for ostree-prepare-root: $with_static_compiler" AS_IF([test x$with_builtin_grub2_mkconfig = xyes], [ echo " builtin grub2-mkconfig (instead of system): $with_builtin_grub2_mkconfig" ], [