Alex Deymo 71e2ea7734 build: support building all tools in a single binary
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.
2014-07-13 14:23:09 +01:00

158 lines
5.1 KiB
Bash
Executable File

#!/bin/sh
# Verify behavior of env.
# Copyright (C) 2009-2014 Free Software Foundation, Inc.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ env
# A simple shebang program to call "echo" from symlinks like "./-u" or "./--".
echo "#!$abs_top_builddir/src/echo simple_echo" > simple_echo \
|| framework_failure_
chmod a+x simple_echo || framework_failure_
# Verify clearing the environment
a=1
export a
env - > out || fail=1
compare /dev/null out || fail=1
env -i > out || fail=1
compare /dev/null out || fail=1
env -u a -i -u a -- > out || fail=1
compare /dev/null out || fail=1
env -i -- a=b > out || fail=1
echo a=b > exp || framework_failure_
compare exp out || fail=1
# These tests verify exact status of internal failure.
env --- # unknown option
test $? = 125 || fail=1
env -u # missing option argument
test $? = 125 || fail=1
env sh -c 'exit 2' # exit status propagation
test $? = 2 || fail=2
env . # invalid command
test $? = 126 || fail=1
env no_such # no such command
test $? = 127 || fail=1
# POSIX is clear that environ may, but need not be, sorted.
# Environment variable values may contain newlines, which cannot be
# observed by merely inspecting output from env.
# Cygwin requires a minimal environment to launch new processes: execve
# adds missing variables SYSTEMROOT and WINDIR, which show up in a
# subsequent env. Cygwin also requires /bin to always be part of PATH,
# and attempts to unset or reduce PATH may cause execve to fail.
#
# For these reasons, it is more portable to grep that our desired changes
# took place, rather than comparing output of env over an entire environment.
if env | grep '^ENV_TEST' >/dev/null ; then
skip_ "environment has potential interference from ENV_TEST*"
fi
ENV_TEST1=a
export ENV_TEST1
>out || framework_failure_
env ENV_TEST2= > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
env -u ENV_TEST1 ENV_TEST3=c > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
env ENV_TEST1=b > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
env ENV_TEST2= env > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
env -u ENV_TEST1 ENV_TEST3=c env > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
env ENV_TEST1=b env > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
cat <<EOF >exp || framework_failure_
ENV_TEST1=a
ENV_TEST2=
ENV_TEST3=c
ENV_TEST1=b
ENV_TEST1=a
ENV_TEST2=
ENV_TEST3=c
ENV_TEST1=b
EOF
compare exp out || fail=1
# PATH modifications affect exec.
mkdir unlikely_name || framework_failure_
cat <<EOF > unlikely_name/also_unlikely || framework_failure_
#!/bin/sh
echo pass
EOF
chmod +x unlikely_name/also_unlikely || framework_failure_
env also_unlikely && fail=1
test x$(PATH=$PATH:unlikely_name env also_unlikely) = xpass || fail=1
test x$(env PATH="$PATH":unlikely_name also_unlikely) = xpass || fail=1
# Explicitly put . on the PATH for the rest of this test.
PATH=$PATH:
export PATH
# Use -- to end options (but not variable assignments).
# On some systems, execve("-i") invokes a shebang script ./-i on PATH as
# '/bin/sh -i', rather than '/bin/sh -- -i', which doesn't do what we want.
# Avoid the issue by using a shebang to 'echo' passing a second parameter
# before the '-i'. See the definition of simple_echo before.
# Test -u, rather than -i, to minimize PATH problems.
ln -s "simple_echo" ./-u || framework_failure_
case $(env -u echo echo good) in
good) ;;
*) fail=1 ;;
esac
case $(env -u echo -- echo good) in
good) ;;
*) fail=1 ;;
esac
case $(env -- -u pass) in
*pass) ;;
*) fail=1 ;;
esac
# After options have ended, the first argument not containing = is a program.
env a=b -- true
test $? = 127 || fail=1
ln -s "simple_echo" ./-- || framework_failure_
case $(env a=b -- true || echo fail) in
*true) ;;
*) fail=1 ;;
esac
# No way to directly invoke program name containing =.
cat <<EOF >./c=d || framework_failure_
#!/bin/sh
echo pass
EOF
chmod +x c=d || framework_failure_
test "x$(env c=d echo fail)" = xfail || fail=1
test "x$(env -- c=d echo fail)" = xfail || fail=1
test "x$(env ./c=d echo fail)" = xfail || fail=1
test "x$(env sh -c 'exec "$@"' sh c=d echo fail)" = xpass || fail=1
test "x$(sh -c '\c=d echo fail')" = xpass && #dash 0.5.4 fails so check first
{ test "x$(env sh -c '\c=d echo fail')" = xpass || fail=1; }
# catch unsetenv failure, broken through coreutils 8.0
env -u a=b true && fail=1
test $? = 125 || fail=1
env -u '' true && fail=1
test $? = 125 || fail=1
Exit $fail