Hi!
I'm attaching the v3 of this patch (also v2 for reference).

In short, what's new: "make check" with symlinks, --help/--version, NEWS,
README, changes to bootstrap.conf; ls/dir/vdir case.

The remaining points are:
1. Add ATTRIBUTTE_NORETURN to main() (probably on src/system.h), and fix
all the mains that return.
2. Add another option to configure for exclude some binaries from the
coreutils single-binary option. I don't see much value on this, but's not
hard.
3. Fix the tests/misc/env.sh test case. No idea why it is not working.

deymo.
From 9a6a486e6503520fd2581f2d3356b7149f1b225d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email protected]>
Date: Tue, 3 Jun 2014 10:50:09 +0100
Subject: [PATCH] tests: consolidate tests for true and false

* src/true.c (main): Add a comment about the possibility
of true returning EXIT_FAILURE due to write failure.
* tests/misc/false-status.sh: Fix so we're testing
the tool and not the shell builtin.  Add a case for true(1).
* tests/misc/help-version.sh: Skip /dev/full test
for true as well as false since the exit status is tested separately.
Also remove the iterations for different LC_MESSAGES, as this was only
applied for false(1).  Translations are not honored in the test dir
and so would need separate handling in any case.
---
 src/true.c                 |  2 ++
 tests/misc/false-status.sh | 12 ++++++---
 tests/misc/help-version.sh | 65 +++++++++++++++++++++-------------------------
 3 files changed, 41 insertions(+), 38 deletions(-)

diff --git a/src/true.c b/src/true.c
index 219f98c..8a1c4f9 100644
--- a/src/true.c
+++ b/src/true.c
@@ -64,6 +64,8 @@ main (int argc, char **argv)
       bindtextdomain (PACKAGE, LOCALEDIR);
       textdomain (PACKAGE);
 
+      /* Note true(1) will return EXIT_FAILURE in the
+         edge case where writes fail with GNU specific options.  */
       atexit (close_stdout);
 
       if (STREQ (argv[1], "--help"))
diff --git a/tests/misc/false-status.sh b/tests/misc/false-status.sh
index b943040..77bea45 100755
--- a/tests/misc/false-status.sh
+++ b/tests/misc/false-status.sh
@@ -1,5 +1,6 @@
 #!/bin/sh
 # ensure that false exits nonzero even with --help or --version
+# and ensure that true exits nonzero when it can't write --help or --version
 
 # Copyright (C) 2003-2014 Free Software Foundation, Inc.
 
@@ -17,9 +18,14 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
-print_ver_ false
+print_ver_ false true
 
-false --version > /dev/null && fail=1
-false --help > /dev/null && fail=1
+env false --version > /dev/null && fail=1
+env false --help > /dev/null && fail=1
+
+if test -w /dev/full && test -c /dev/full; then
+  env true --version > /dev/full && fail=1
+  env true --help > /dev/full && fail=1
+fi
 
 Exit $fail
diff --git a/tests/misc/help-version.sh b/tests/misc/help-version.sh
index 4bc02d8..b4939f7 100755
--- a/tests/misc/help-version.sh
+++ b/tests/misc/help-version.sh
@@ -70,43 +70,38 @@ done
 test "x$v" = "x$VERSION" \
   || fail_ "--version-\$VERSION mismatch"
 
-for lang in C fr da; do
-  for i in $built_programs; do
-
-    # Skip 'test'; it doesn't accept --help or --version.
-    test $i = test && continue;
-
-    # false fails even when invoked with --help or --version.
-    if test $i = false; then
-      env LC_MESSAGES=$lang $i --help >/dev/null && fail=1
-      env LC_MESSAGES=$lang $i --version >/dev/null && fail=1
-      continue
-    fi
+for i in $built_programs; do
 
-    # The just-built install executable is always named 'ginstall'.
-    test $i = install && i=ginstall
-
-    # Make sure they exit successfully, under normal conditions.
-    env $i --help    >/dev/null || fail=1
-    env $i --version >/dev/null || fail=1
-
-    # Make sure they fail upon 'disk full' error.
-    if test -w /dev/full && test -c /dev/full; then
-      env $i --help    >/dev/full 2>/dev/null && fail=1
-      env $i --version >/dev/full 2>/dev/null && fail=1
-      status=$?
-      test $i = [ && prog=lbracket || prog=$(echo $i|sed "s/$EXEEXT$//")
-      eval "expected=\$expected_failure_status_$prog"
-      test x$expected = x && expected=1
-      if test $status = $expected; then
-        : # ok
-      else
-        fail=1
-        echo "*** $i: bad exit status '$status' (expected $expected)," 1>&2
-        echo "  with --help or --version output redirected to /dev/full" 1>&2
-      fi
+  # Skip 'test'; it doesn't accept --help or --version.
+  test $i = test && continue
+
+  # false fails even when invoked with --help or --version.
+  # true and false are tested with these options separately.
+  test $i = false || test $i = true && continue
+
+  # The just-built install executable is always named 'ginstall'.
+  test $i = install && i=ginstall
+
+  # Make sure they exit successfully, under normal conditions.
+  env $i --help    >/dev/null || fail=1
+  env $i --version >/dev/null || fail=1
+
+  # Make sure they fail upon 'disk full' error.
+  if test -w /dev/full && test -c /dev/full; then
+    env $i --help    >/dev/full 2>/dev/null && fail=1
+    env $i --version >/dev/full 2>/dev/null && fail=1
+    status=$?
+    test $i = [ && prog=lbracket || prog=$(echo $i|sed "s/$EXEEXT$//")
+    eval "expected=\$expected_failure_status_$prog"
+    test x$expected = x && expected=1
+    if test $status = $expected; then
+      : # ok
+    else
+      fail=1
+      echo "*** $i: bad exit status '$status' (expected $expected)," 1>&2
+      echo "  with --help or --version output redirected to /dev/full" 1>&2
     fi
-  done
+  fi
 done
 
 bigZ_in=bigZ-in.Z
-- 
2.0.0.526.g5318336

From 126914441df4965547beb4a38df9a43d209216c9 Mon Sep 17 00:00:00 2001
From: Alex Deymo <[email protected]>
Date: Thu, 5 Jun 2014 19:50:32 -0700
Subject: [PATCH] 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
  1207992    6316   94384 1308692  13f814 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 symlinks from each
configured tool to a single "coreutils" binary installed on the
same directory.

* .gitignore: Added new generated files.
* Makefile.am: New rules to generate build-aux/gen-single-binary.sh
  and install symlinks.
* NEWS: Mention the new feature.
* README: Added "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.
* man/coreutils.x: Manpage hook.
* man/local.mk: Added manpage hook.
* src/coreutils.c: Multicall implementation.
* src/local.mk: New rules for the single binary option.
* tests/misc/help-version.sh: Added exception for coreutils.
---
 .gitignore                         |   3 +
 Makefile.am                        |  13 ++++
 NEWS                               |   7 ++
 README                             |  18 ++---
 bootstrap.conf                     |   7 ++
 build-aux/gen-lists-of-programs.sh |   7 ++
 build-aux/gen-single-binary.sh     |  95 +++++++++++++++++++++++
 configure.ac                       |  47 ++++++++++++
 man/coreutils.x                    |   4 +
 man/local.mk                       |   3 +-
 src/coreutils.c                    | 152 +++++++++++++++++++++++++++++++++++++
 src/local.mk                       |  55 ++++++++++++++
 tests/misc/help-version.sh         |   2 +-
 13 files changed, 402 insertions(+), 11 deletions(-)
 create mode 100755 build-aux/gen-single-binary.sh
 create mode 100644 man/coreutils.x
 create mode 100644 src/coreutils.c

diff --git a/.gitignore b/.gitignore
index 558577d..e5c07fe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -155,10 +155,13 @@
 /po/remove-potcdate.sed
 /po/remove-potcdate.sin
 /po/stamp-po
+/src/coreutils.h
 /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
diff --git a/Makefile.am b/Makefile.am
index e88dc9c..1351434 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -50,6 +50,7 @@ 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				\
@@ -57,6 +58,7 @@ EXTRA_DIST =				\
   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
@@ -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
 
@@ -180,6 +186,13 @@ check-git-hook-script-sync:
 	rm -rf $$t;							\
 	test $$fail = 0
 
+# If we are building a single-binary, create symlinks for them when installing.
+install-exec-hook:
+	@for i in $(single_binary_progs); do \
+		rm -f $(DESTDIR)$(bindir)/$$i$(EXEEXT); \
+		$(LN_S) -s coreutils $(DESTDIR)$(bindir)/$$i$(EXEEXT); \
+	done
+
 noinst_LIBRARIES =
 MOSTLYCLEANFILES =
 CLEANFILES =
diff --git a/NEWS b/NEWS
index fa1aab8..c7d2f60 100644
--- a/NEWS
+++ b/NEWS
@@ -82,6 +82,13 @@ 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" with symlinks to it
+  for each program. 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. This flag considerably reduces the overall size of the
+  installed binaries.
+
 ** Changes in behavior
 
   chroot with an argument of "/" no longer implicitly changes the current
diff --git a/README b/README
index fa1a429..bd0bce5 100644
--- a/README
+++ b/README
@@ -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.
 
diff --git a/bootstrap.conf b/bootstrap.conf
index 98e83bb..c0b5f02 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -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)
 }
 
 
diff --git a/build-aux/gen-lists-of-programs.sh b/build-aux/gen-lists-of-programs.sh
index bf63ee3..c95e598 100755
--- a/build-aux/gen-lists-of-programs.sh
+++ b/build-aux/gen-lists-of-programs.sh
@@ -17,6 +17,7 @@ set -e
 # use "--enable-install-program=A,B" when invoking configure.
 disabled_by_default_progs='
     arch
+    coreutils
     hostname
 '
 
@@ -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
     ;;
diff --git a/build-aux/gen-single-binary.sh b/build-aux/gen-single-binary.sh
new file mode 100755
index 0000000..c57475b
--- /dev/null
+++ b/build-aux/gen-single-binary.sh
@@ -0,0 +1,95 @@
+#!/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 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 [ "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!"
+
+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}" \
+       "-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
diff --git a/configure.ac b/configure.ac
index 7c210d9..a8f7b8f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -221,6 +221,18 @@ 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],
+     [Compile all the tools in a single binary (reduces overall size)])],
+  [case $enableval in
+     yes|no) ;;
+     *)      AC_MSG_ERROR([bad value $enableval for single-binary option]) ;;
+   esac
+   gl_single_binary=$enableval],
+  [gl_single_binary=no]
+)
+AM_CONDITIONAL([SINGLE_BINARY], [test "$gl_single_binary" = yes])
+
 AC_FUNC_FORK
 
 optional_bin_progs=
@@ -487,6 +499,41 @@ 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 simlinks if specified.
+single_binary_progs=
+single_binary_libs=
+single_binary_deps=
+if test "$gl_single_binary" = yes; then
+  single_binary_progs=`echo $optional_bin_progs`
+  # 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`
+  optional_bin_progs="coreutils"
+
+  # We need to compile main() functions on src/*.c files with a different
+  # name passing for example -Dmain=_single_binary_main_sort. This function
+  # may issue a suggested attribute "noreturn" warning if it always calls
+  # exit(), warning which otherwise is suppressed for main().
+  gl_WARN_ADD([-Wno-suggest-attribute=noreturn])
+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])
+
+
 # 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
diff --git a/man/coreutils.x b/man/coreutils.x
new file mode 100644
index 0000000..a7a4879
--- /dev/null
+++ b/man/coreutils.x
@@ -0,0 +1,4 @@
+[NAME]
+coreutils \- single binary for coreutils programs
+[DESCRIPTION]
+.\" Add any additional description here
diff --git a/man/local.mk b/man/local.mk
index 7ce426b..ac19190 100644
--- a/man/local.mk
+++ b/man/local.mk
@@ -77,6 +77,7 @@ man/chown.1:     src/chown
 man/chroot.1:    src/chroot
 man/cksum.1:     src/cksum
 man/comm.1:      src/comm
+man/coreutils.1: src/coreutils
 man/cp.1:        src/cp
 man/csplit.1:    src/csplit
 man/cut.1:       src/cut
@@ -189,7 +190,7 @@ man/yes.1:       src/yes
 	  && $(run_help2man)						\
 		     --source='$(PACKAGE_STRING)'			\
 		     --include=$(srcdir)/man/$$name.x			\
-		     --output=$$t/$$name.1 $$t/$$name			\
+		     --output=$$t/$$name.1 '$(abs_top_builddir)/src/'$$prog \
 		     --info-page='coreutils \(aq'$$name' invocation\(aq' \
 	  && sed \
 	       -e 's|$*\.td/||g' \
diff --git a/src/coreutils.c b/src/coreutils.c
new file mode 100644
index 0000000..d5f4202
--- /dev/null
+++ b/src/coreutils.c
@@ -0,0 +1,152 @@
+/* Copyright (C) 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/>.  */
+
+/* coreutils.c aggregates the functionality of every other tool into a single
+   binary multiplexed by the value of argv[0]. This is enabled by passing
+   --enable-single-binary to configure.
+
+   By Alex Deymo <[email protected]> */
+
+#include <config.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "dirname.h"
+#include "system.h"
+#include "error.h"
+
+/* Declare the main() function on each one of the selected tools. This name
+ * needs to match the one passed as CFLAGS on single-binary.mk (generated
+ * byt gen-single-binary.sh). */
+#define SINGLE_BINARY_PROGRAM(prog_name_str, main_name) \
+  int _single_binary_main_##main_name(int argc, char** argv, char** envp);
+#include "coreutils.h"
+#undef SINGLE_BINARY_PROGRAM
+
+void usage (int status) ATTRIBUTE_NORETURN;
+
+/* The official name of this program (e.g., no 'g' prefix).  */
+#define PROGRAM_NAME "coreutils"
+
+#define AUTHORS \
+  proper_name ("Alex Deymo")
+
+
+void
+usage (int status)
+{
+  if (status != EXIT_SUCCESS)
+    emit_try_help ();
+  else
+    {
+      printf (_("\
+Usage: %s PROGRAM_NAME [PARAMETERS]... \n"),
+              program_name);
+      fputs (_("\
+Execute the PROGRAM_NAME built-in program with the given PARAMETERS.\n\
+\n"), stdout);
+      fputs (HELP_OPTION_DESCRIPTION, stdout);
+      fputs (VERSION_OPTION_DESCRIPTION, stdout);
+
+      printf ("\n\
+Built-in programs:\n"
+#define SINGLE_BINARY_PROGRAM(prog_name_str, main_name) " " prog_name_str
+#include "coreutils.h"
+#undef SINGLE_BINARY_PROGRAM
+  "\n");
+
+      printf (_("\
+\n\
+Try: '%s PROGRAM_NAME --help' for help on the particular program.\n"),
+              program_name);
+      emit_ancillary_info ();
+    }
+  exit (status);
+}
+
+int
+main (int argc, char **argv, char** envp)
+{
+  static struct option const long_options[] =
+  {
+    {GETOPT_HELP_OPTION_DECL},
+    {GETOPT_VERSION_OPTION_DECL},
+    {NULL, 0, NULL, 0}
+  };
+
+  char *prog_name = last_component(argv[0]);
+  int prog_argc = argc;
+  char **prog_argv = argv;
+
+  int opt;
+
+  /* If this program is called directly as "coreutils" instead of using a
+   * symlink, we use argv[1] as the name of the program, shifting all the
+   * arguments one position. */
+  if (STREQ(prog_name, "coreutils"))
+  {
+    prog_argv++;
+    prog_argc--;
+    prog_name = prog_argv[0]; /* Don't use last_component() in this case. */
+  }
+
+  /* Ensure that at least a parameter was passed to coreutils. */
+  if (prog_argc < 1 || !prog_argv[0])
+  {
+    initialize_main (&argc, &argv);
+    set_program_name (argv[0]);
+    setlocale (LC_ALL, "");
+    bindtextdomain (PACKAGE, LOCALEDIR);
+    textdomain (PACKAGE);
+    atexit (close_stdout);
+
+    usage(EXIT_FAILURE);
+  }
+
+  /* Lookup the right main program */
+#define SINGLE_BINARY_PROGRAM(prog_name_str, main_name) \
+  if (STREQ(prog_name_str, prog_name)) \
+    return _single_binary_main_##main_name(prog_argc, prog_argv, envp);
+#include "coreutils.h"
+#undef SINGLE_BINARY_PROGRAM
+
+  /* No known program was selected. From here on, we behave like any other
+   * coreutils program. Handle the flags passed to this program. */
+
+  initialize_main (&argc, &argv);
+  set_program_name (argv[0]);
+  setlocale (LC_ALL, "");
+  bindtextdomain (PACKAGE, LOCALEDIR);
+  textdomain (PACKAGE);
+  atexit (close_stdout);
+
+  if ((opt = getopt_long (argc, argv, "", long_options, NULL)) != -1)
+  {
+    switch (opt)
+    {
+      case_GETOPT_HELP_CHAR;
+
+      case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
+
+      default:
+        usage (EXIT_FAILURE);
+    }
+  }
+
+  printf ("%s: program not found.\n", prog_name);
+  usage(EXIT_FAILURE);
+}
diff --git a/src/local.mk b/src/local.mk
index 865dd74..afae2db 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -42,6 +42,7 @@ noinst_PROGRAMS =		\
 noinst_HEADERS =		\
   src/chown-core.h		\
   src/copy.h			\
+  src/coreutils.h		\
   src/cp-hash.h			\
   src/dircolors.h		\
   src/fiemap.h			\
@@ -150,6 +151,7 @@ src_mv_LDADD = $(LDADD)
 src_nice_LDADD = $(LDADD)
 src_nl_LDADD = $(LDADD)
 src_nohup_LDADD = $(LDADD)
+src_numfmt_LDADD = $(LDADD)
 src_od_LDADD = $(LDADD)
 src_paste_LDADD = $(LDADD)
 src_pathchk_LDADD = $(LDADD)
@@ -395,6 +397,43 @@ src_libstdbuf_so_LDADD = $(LIBINTL)
 src_libstdbuf_so_LDFLAGS = -shared
 src_libstdbuf_so_CFLAGS = -fPIC $(AM_CFLAGS)
 
+if SINGLE_BINARY
+# Single binary dependencies
+src_coreutils_SOURCES = src/coreutils.c src/coreutils.h
+src_coreutils_CFLAGS = $(AM_CFLAGS)
+src_coreutils_LDFLAGS = $(AM_LDFLAGS)
+src_coreutils_LDADD = $(single_binary_deps) $(LDADD) $(single_binary_libs)
+src_coreutils_DEPENDENCIES = $(LDADD) $(single_binary_deps)
+
+include $(top_srcdir)/src/single-binary.mk
+
+# Special case for programs that have multiple sources sharing global
+# initialized variables between the sources, and that the sources are shared
+# among more than one binary. These extra rules rename those global variables
+# such that the name from one program doesn't conflict the variable on the
+# other program.
+# Programs with global variables private to a .c are not a problem since the
+# references are resolved locally on each .c file.
+src_libsinglebin_arch_a_CFLAGS += -Duname_mode=_libsinglebin_arch_uname_mode
+src_libsinglebin_uname_a_CFLAGS += -Duname_mode=_libsinglebin_uname_uname_mode
+
+src_libsinglebin_ls_a_CFLAGS += -Dls_mode=_libsinglebin_ls_ls_mode
+src_libsinglebin_dir_a_CFLAGS += -Dls_mode=_libsinglebin_dir_ls_mode
+src_libsinglebin_vdir_a_CFLAGS += -Dls_mode=_libsinglebin_vdir_ls_mode
+
+endif SINGLE_BINARY
+
+# Creates symlinks to the installed programs when building coreutils single
+# binary. This doesn't do anything when $(single_binary_progs) is empty.
+all-local: src_coreutils_links
+.PHONY: src_coreutils_links
+src_coreutils_links:
+	@for i in $(single_binary_progs); do \
+		rm -f $(srcdir)/src/$$i$(EXEEXT); \
+		$(LN_S) -s coreutils $(srcdir)/src/$$i$(EXEEXT); \
+	done
+
+
 BUILT_SOURCES += src/dircolors.h
 src/dircolors.h: src/dcgen src/dircolors.hin
 	$(AM_V_GEN)rm -f $@ $@-t
@@ -515,6 +554,22 @@ src/version.h: Makefile
 	$(AM_V_at)chmod a-w $@t
 	$(AM_V_at)mv $@t $@
 
+# Generates a list of macro invocations like:
+#   SINGLE_BINARY_PROGRAM(program_name_str, main_name)
+# once for each program list on $(single_binary_progs). Note that
+# for [ the macro invocation is:
+#   SINGLE_BINARY_PROGRAM("[", _)
+BUILT_SOURCES += src/coreutils.h
+src/coreutils.h: Makefile
+	$(AM_V_GEN)rm -f $@
+	$(AM_V_at)for prog in $(single_binary_progs); do		       \
+		prog="$$(basename $$prog)" 				     ; \
+		main="$$(echo $$prog | tr '[' '_')" 			     ; \
+		echo "SINGLE_BINARY_PROGRAM(\"$$prog\", $$main)"	     ; \
+	done | sort > $@t
+	$(AM_V_at)chmod a-w $@t
+	$(AM_V_at)mv $@t $@
+
 DISTCLEANFILES += src/version.c src/version.h
 MAINTAINERCLEANFILES += $(BUILT_SOURCES)
 
diff --git a/tests/misc/help-version.sh b/tests/misc/help-version.sh
index b4939f7..0598557 100755
--- a/tests/misc/help-version.sh
+++ b/tests/misc/help-version.sh
@@ -239,7 +239,7 @@ parted_setup () { args="-s $tmp_in mklabel gpt"
 # something more than --help or --version.
 for i in $built_programs; do
   # Skip these.
-  case $i in chroot|stty|tty|false|chcon|runcon) continue;; esac
+  case $i in chroot|stty|tty|false|chcon|runcon|coreutils) continue;; esac
 
   rm -rf $tmp_in $tmp_in2 $tmp_dir $tmp_out $bigZ_in $zin $zin2
   echo z |gzip > $zin
-- 
2.0.0.526.g5318336

Reply via email to