Skip to content

Commit

Permalink
build: support building all tools in a single binary
Browse files Browse the repository at this point in the history
Add the --enable-single-binary option to the configure file.
When enabled, this option builds a single binary file containing
the selected tools.  Which tool gets executed depends on the value
of argv[0] which can be set implicitly through symlinks to the
single program.

This setup reduces significantly the size of a complete coreutils
install, since code from lib/libcoreutils.a is not duplicated in
every one of the more than 100 binaries.  Runtime overhead is
increased due to more dynamic libraries being loaded, and extra
initialization being performed for all utils.  Also initially
a larger binary is loaded from storage, though this is usually
alleviated due to caching and lazy mmaping of unused blocks,
and in fact the single binary should have better caching
characteristics.

Comparing the size of the individual versus single binary on x86_64:
  $ cd src
  $ size coreutils
  $ size -t $(../build-aux/gen-lists-of-programs.sh --list-progs |
              grep -Ev '(coreutils|libstdbuf)') | tail -n1
     text    data     bss     dec     hex filename
  1097416    5388   88432 1191236  122d44 src/coreutils
  4901010  124964  163768 5189742  4f306e (TOTALS)

Storage requirements are reduced similarly:
  $ cd src
  $ du -h coreutils
  $ du -ch $(../build-aux/gen-lists-of-programs.sh --list-progs |
             grep -Ev '(coreutils|libstdbuf)') | tail -n1
  1.2M    coreutils
  5.3M    total

When installing, the makefile will create either symlinks or
shebangs based on the --enable-single-binary setting, for
each configured tool. In this way, all the tools are still
callable individually, but they are all implemented by the same
"coreutils" binary installed on the same directory.

* .gitignore: Add new generated files.
* Makefile.am: New rules to generate build-aux/gen-single-binary.sh
  and install symlinks.
* NEWS: Mention the new feature.
* README: Add "coreutils" to the list of utils.
* bootstrap.conf: Regenerate src/single-binary.mk
* build-aux/gen-lists-of-programs.sh: New --list-progs option.
* build-aux/gen-single-binary.sh: Regenerate
* configure.ac: New --enable-single-binary option and other variables.
Disallow --enable-single-binary=symlinks with --program-prefix et. al.
* man/coreutils.x: Manpage hook.
* man/local.mk: Add manpage hook and fix dependencies.
* src/coreutils.c: Multicall implementation.
* src/local.mk: New rules for the single binary option.
* tests/local.mk: Add $single_binary_progs to support
require_built_() from init.cfg
* tests/misc/env.sh: Avoid the use of symlink to echo.
* tests/misc/help-version.sh: Add exception for coreutils.
* tests/install/basic-1.sh: Really avoid using ginstall strip
functionality if there is an issue with the independent strip command.
* src/kill.c: Changes to call exit() in main.
* src/readlink.c: Likewise.
* src/shuf.c: Likewise.
* src/timeout.c: Likewise.
* src/truncate.c: Likewise.
  • Loading branch information
deymo authored and pixelb committed Jul 13, 2014
1 parent 9c128c0 commit 71e2ea7
Show file tree
Hide file tree
Showing 29 changed files with 744 additions and 149 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
/m4/xsize.m4
/maint.mk
/man/*.1
/man/dynamic-deps.mk
/po/*.gmo
/po/*.po
/po/.gitignore
Expand All @@ -155,10 +156,15 @@
/po/remove-potcdate.sed
/po/remove-potcdate.sin
/po/stamp-po
/src/coreutils.h
/src/coreutils_symlinks
/src/coreutils_shebangs
/src/cu-progs.mk
/src/fs-latest-magic.h
/src/libsinglebin_*.a
/src/make-prime-list
/src/primes.h
/src/single-binary.mk
/src/version.c
/src/version.h
/stamp-h1
Expand Down
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ chown: David MacKenzie, Jim Meyering
chroot: Roland McGrath
cksum: Q. Frank Xia
comm: Richard M. Stallman, David MacKenzie
coreutils: Alex Deymo
cp: Torbjörn Granlund, David MacKenzie, Jim Meyering
csplit: Stuart Kemp, David MacKenzie
cut: David M. Ihnat, David MacKenzie, Jim Meyering
Expand Down
24 changes: 24 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ EXTRA_DIST = \
bootstrap \
bootstrap.conf \
build-aux/gen-lists-of-programs.sh \
build-aux/gen-single-binary.sh \
cfg.mk \
dist-check.mk \
maint.mk \
tests/GNUmakefile \
thanks-gen

gen_progs_lists = $(top_srcdir)/build-aux/gen-lists-of-programs.sh
gen_single_binary = $(top_srcdir)/build-aux/gen-single-binary.sh

# Keep these in sync with bootstrap.conf:bootstrap_post_import_hook().
# Use '$(top_srcdir)/m4' and '$(srcdir)/src' for the benefit of non-GNU
Expand All @@ -70,6 +72,10 @@ $(srcdir)/src/cu-progs.mk: $(gen_progs_lists)
$(AM_V_GEN)rm -f $@ $@-t \
&& $(SHELL) $(gen_progs_lists) --automake >$@-t \
&& chmod a-w $@-t && mv -f $@-t $@
$(srcdir)/src/single-binary.mk: $(gen_single_binary) $(srcdir)/src/local.mk
$(AM_V_GEN)rm -f $@ $@-t \
&& $(SHELL) $(gen_single_binary) $(srcdir)/src/local.mk >$@-t \
&& chmod a-w $@-t && mv -f $@-t $@

ACLOCAL_AMFLAGS = -I m4

Expand Down Expand Up @@ -180,6 +186,24 @@ check-git-hook-script-sync:
rm -rf $$t; \
test $$fail = 0

# If we are building a single-binary, create symlinks or shebangs for
# the selected tools when installing.
install-exec-hook:
$(AM_V_at)ctrans=$$(printf coreutils | sed -e "$(transform)"); \
for p in $(single_binary_progs); do \
ptrans=$$(printf '%s' "$$p" | sed -e "$(transform)"); \
rm -f $(DESTDIR)$(bindir)/$$ptrans$(EXEEXT) || exit $$?; \
if test "x$(single_binary_install_type)" = xshebangs; then \
printf '#!%s --coreutils-prog-shebang=%s\n' \
$(bindir)/$$ctrans$(EXEEXT) $$p \
>$(DESTDIR)$(bindir)/$$ptrans$(EXEEXT) || exit $$?; \
chmod a+x,a-w $(DESTDIR)$(bindir)/$$ptrans$(EXEEXT) || exit $$?;\
else \
$(LN_S) -s $$ctrans$(EXEEXT) \
$(DESTDIR)$(bindir)/$$ptrans$(EXEEXT) || exit $$?; \
fi \
done

noinst_LIBRARIES =
MOSTLYCLEANFILES =
CLEANFILES =
Expand Down
18 changes: 18 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ GNU coreutils NEWS -*- outline -*-
od accepts a new option: --endian=TYPE to handle inputs with different byte
orders, or to provide consistent output on systems with disparate endianness.

configure accepts the new option --enable-single-binary to build all the
selected programs in a single binary called "coreutils". The selected
programs can still be called directly using symlinks to "coreutils" or
shebangs with the option --coreutils-prog= passed to this program. The
install behavior is determined by the option --enable-single-binary=symlinks
or --enable-single-binary=shebangs (the default). With the symlinks option,
you can't make a second symlink to any program because that will change the
name of the called program, which is used by coreutils to determine the
desired program. The shebangs option doesn't suffer from this problem, but
the /proc/$pid/cmdline file might not be updated on all the platforms. The
functionality of each program is not affected but this single binary will
depend on all the required dynamic libraries even to run simple programs.
If you desire to build some tools outside the single binary file, you can
pass the option --enable-single-binary-exceptions=PROG_LIST with the comma
separated list of programs you want to build separately. This flag
considerably reduces the overall size of the installed binaries which makes
it suitable for embedded system.

** Changes in behavior

chroot with an argument of "/" no longer implicitly changes the current
Expand Down
18 changes: 9 additions & 9 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ arbitrary limits.

The programs that can be built with this package are:

[ arch base64 basename cat chcon chgrp chmod chown chroot cksum comm cp
csplit cut date dd df dir dircolors dirname du echo env expand expr
factor false fmt fold groups head hostid hostname id install join kill
link ln logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl nohup
nproc numfmt od paste pathchk pinky pr printenv printf ptx pwd readlink
realpath rm rmdir runcon seq sha1sum sha224sum sha256sum sha384sum sha512sum
shred shuf sleep sort split stat stdbuf stty sum sync tac tail tee test
timeout touch tr true truncate tsort tty uname unexpand uniq unlink
uptime users vdir wc who whoami yes
[ arch base64 basename cat chcon chgrp chmod chown chroot cksum comm
coreutils cp csplit cut date dd df dir dircolors dirname du echo env
expand expr factor false fmt fold groups head hostid hostname id install
join kill link ln logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl
nohup nproc numfmt od paste pathchk pinky pr printenv printf ptx pwd
readlink realpath rm rmdir runcon seq sha1sum sha224sum sha256sum sha384sum
sha512sum shred shuf sleep sort split stat stdbuf stty sum sync tac tail
tee test timeout touch tr true truncate tsort tty uname unexpand uniq
unlink uptime users vdir wc who whoami yes

See the file NEWS for a list of major changes in the current release.

Expand Down
7 changes: 7 additions & 0 deletions bootstrap.conf
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ bootstrap_post_import_hook ()

# Massage lib/gnulib.mk before using it later in the bootstrapping process.
build-aux/prefix-gnulib-mk --lib-name=$gnulib_name lib/$gnulib_mk

# Regenerate src/single-binary.mk
(mkf=src/single-binary.mk tmp=single-binary.tmp \
&& rm -f $mkf $tmp \
&& build-aux/gen-single-binary.sh src/local.mk >$tmp \
&& chmod a-w $tmp \
&& mv -f $tmp $mkf)
}


Expand Down
7 changes: 7 additions & 0 deletions build-aux/gen-lists-of-programs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set -e
# use "--enable-install-program=A,B" when invoking configure.
disabled_by_default_progs='
arch
coreutils
hostname
'

Expand Down Expand Up @@ -178,6 +179,12 @@ END
echo default__progs += $progsdir/$p
done
;;
1,--list-progs)
for p in $disabled_by_default_progs $build_if_possible_progs \
$normal_progs; do
echo $p
done
;;
*)
echo "$0: invalid usage" >&2; exit 2
;;
Expand Down
110 changes: 110 additions & 0 deletions build-aux/gen-single-binary.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/sh

# Generate the list of rules for the single-binary option based on all the other
# binaries found in src/local.mk.
#
# We need to duplicate the specific rules to build each program into a new
# static library target. We can't reuse the existing target since we need to
# create a .a file instead of linking the program. We can't do this at
# ./configure since the file names need to be available when automake runs
# to let it generate all the required rules in Makefile.in. The configure
# step will select which ones will be used to build, but they need to be
# generated beforehand.
#
# Instead of maintaining a duplicated list of rules, we generate the
# single-binary required rules based on the normal configuration found on
# src/local.mk with this script.

if test "x$1" = "x"; then
echo "Usage: $0 path/to/src/local.mk" >&2
exit 1
fi

set -e

LOCAL_MK=$1
GEN_LISTS_OF_PROGRAMS="`dirname "$0"`/gen-lists-of-programs.sh"

ALL_PROGRAMS=$($GEN_LISTS_OF_PROGRAMS --list-progs \
| grep -v -F -e coreutils -e libstdbuf.so \
| tr '[' '_')

# Compute default SOURCES. automake will assume the source file for the
# src_${cmd} target to be src/${cmd}.c, but we will add rules to generate
# the lib src_libsinglebin_${cmd}_a which won't match the autogenerated source
# file. This loop will initialize the default source file and will be reset
# later if needed.
for cmd in $ALL_PROGRAMS; do
eval "src_${cmd}_SOURCES=src/${cmd}.c"
done

# Load actual values from src/local.mk. This will read all the variables from
# the local.mk matching the src_${cmd}_... case.
while read l; do
if echo "$l" | grep -E '^src_\w+ +\+?=' > /dev/null; then
var=$(echo $l | cut -f 1 -d ' ')
value=$(echo $l | cut -f 2- -d =)
if [ "$value" != " \$(LDADD)" ]; then
oldvalue=""
if echo $l | grep -F '+=' >/dev/null; then
eval "oldvalue=\${$var}"
fi
eval "$var='$oldvalue "${value//\'/\'\"\'\"\'}"'"
fi
fi
done < $LOCAL_MK

me=`echo "$0" | sed 's,.*/,,'`
echo "## Automatically generated by $me. DO NOT EDIT BY HAND!"

# Override the sources for dir and vdir. We use a smaller version of dir and
# vdir that relies on the ls main.
src_dir_SOURCES="src/coreutils-dir.c"
src_dir_LDADD+=" src/libsinglebin_ls.a"
echo src_libsinglebin_dir_a_DEPENDENCIES = src/libsinglebin_ls.a
src_vdir_SOURCES="src/coreutils-vdir.c"
src_vdir_LDADD+=" src/libsinglebin_ls.a"
echo src_libsinglebin_vdir_a_DEPENDENCIES = src/libsinglebin_ls.a

# Override the sources for arch likewise, using the main from uname.
src_arch_SOURCES="src/coreutils-arch.c"
src_arch_LDADD+=" src/libsinglebin_uname.a"
echo src_libsinglebin_arch_a_DEPENDENCIES = src/libsinglebin_uname.a

for cmd in $ALL_PROGRAMS; do
echo "# Command $cmd"
echo noinst_LIBRARIES += src/libsinglebin_${cmd}.a
base="src_libsinglebin_${cmd}_a"
# SOURCES
var=src_${cmd}_SOURCES
eval "value=\$$var"
echo "${base}_SOURCES = $value"

# LDADD
var=src_${cmd}_LDADD
eval "value=\$$var"
if [ "x$value" != "x" ]; then
echo "${base}_ldadd = $value"
fi

# CFLAGS
# Hack any other program defining a main() replacing its main by
# _single_binary_main_$PROGRAM_NAME.
echo "${base}_CFLAGS = \"-Dmain=_single_binary_main_${cmd}(int, char**) " \
"ATTRIBUTE_NORETURN; int _single_binary_main_${cmd}\" " \
"-Dusage=_usage_${cmd} \$(src_coreutils_CFLAGS)"
var=src_${cmd}_CFLAGS
eval "value=\$$var"
if [ "x$value" != "x" ]; then
echo "${base}_CFLAGS += $value"
fi

# CPPFLAGS
var=src_${cmd}_CPPFLAGS
eval "value=\$$var"
if [ "x$value" != "x" ]; then
echo "${base}_CPPFLAGS = $value"
fi
done

exit 0
76 changes: 76 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,36 @@ if test "$gl_gcc_warnings" = yes; then
AC_SUBST([GNULIB_TEST_WARN_CFLAGS])
fi

AC_ARG_ENABLE([single-binary],
[AS_HELP_STRING([--enable-single-binary=[shebangs|symlinks]],
[Compile all the tools in a single binary, reducing the overall size.
When compiled this way, shebangs (default when enabled) or symlinks are
installed for each tool that points to the single binary.])],
[gl_single_binary=no ;
case $enableval in
yes) gl_single_binary=shebangs ;;
no|shebangs|symlinks) gl_single_binary=$enableval ;;
*) AC_MSG_ERROR([bad value $enableval for single-binary option.
Options are: symlinks, shebangs, no.]) ;;
esac],
[gl_single_binary=no]
)
AC_ARG_ENABLE([single-binary-exceptions],
[AS_HELP_STRING([--enable-single-binary-exceptions=PROG_LIST],
[When used with --enable-single-binary, exclude the PROG_LIST from
it, so these programs are compiled as separated files
(comma-separated, default none))])],
[gl_single_binary_exceptions=$enableval],
[gl_single_binary_exceptions=]
)
if test "$gl_single_binary" = 'symlinks'; then
if ! test "`echo ls | sed \"$program_transform_name\"`" = 'ls'; then
AC_MSG_ERROR([program name transformations are not currently supported
with --enable-single-binary=symlinks.])
fi
fi
AM_CONDITIONAL([SINGLE_BINARY], [test "$gl_single_binary" != no])

AC_FUNC_FORK

optional_bin_progs=
Expand Down Expand Up @@ -479,6 +509,52 @@ man1_MANS=`
# a distribution tarball.
EXTRA_MANS=`for p in $no_install_progs_default; do echo man/$p.1; done`

# Replace all the programs by the single binary and symlinks if specified.
single_binary_progs=
single_binary_libs=
single_binary_deps=
single_binary_install_type=
if test "$gl_single_binary" != no; then
man1_MANS="$man1_MANS man/coreutils.1"
# Convert the list to a space separated list
gl_single_binary_exceptions=`echo $gl_single_binary_exceptions | tr ',' ' '`

single_binary_progs=`echo $optional_bin_progs`
optional_bin_progs="coreutils"
for prog in $gl_single_binary_exceptions; do
# Fail if requested to exclude a program than was not part of coreutils.
case " $single_binary_progs " in
*" $prog "*)
gl_REMOVE_PROG([single_binary_progs], [$prog]) ;
gl_ADD_PROG([optional_bin_progs], [$prog]) ;;
*) AC_MSG_ERROR(['$prog' is not being compiled.]) ;;
esac
done

# single_binary_libs holds the list of libs required by the selected
# programs, such as for example -lrt.
single_binary_libs=`
for p in $single_binary_progs; do
# Convert '[' to '_'
test x"$p" = x'@<:@' && p='_'
printf '$(src_libsinglebin_%s_a_ldadd) ' "$p"
done`
# single_binary_deps holds the list of libsinglebin_*.a files that have the
# compiled code of each selected program in a "library" format.
single_binary_deps=`
for p in $single_binary_progs; do
# Convert '[' to '_'
test x"$p" = x'@<:@' && p='_'
printf 'src/libsinglebin_%s.a ' "$p"
done`
single_binary_install_type="$gl_single_binary"
fi
AC_SUBST([single_binary_progs], [$single_binary_progs])
AC_SUBST([single_binary_libs], [$single_binary_libs])
AC_SUBST([single_binary_deps], [$single_binary_deps])
AC_SUBST([single_binary_install_type], [$single_binary_install_type])


# The programs built and installed by "make && make install".
# Since this is AC_SUBST'd, Automake won't be able to perform rewrite
# with $(EXEEXT) appending on it, so we have to do it ourselves -- in
Expand Down
Loading

0 comments on commit 71e2ea7

Please sign in to comment.