dd: fix ‘error’ name issue without macros
[coreutils.git] / build-aux / gen-single-binary.sh
blob1e8f495b4ab642a7f5e53a13918378d273ed5591
1 #!/bin/sh
3 # Generate the list of rules for the single-binary option based on all the other
4 # binaries found in src/local.mk.
6 # We need to duplicate the specific rules to build each program into a new
7 # static library target. We can't reuse the existing target since we need to
8 # create a .a file instead of linking the program. We can't do this at
9 # ./configure since the file names need to be available when automake runs
10 # to let it generate all the required rules in Makefile.in. The configure
11 # step will select which ones will be used to build, but they need to be
12 # generated beforehand.
14 # Instead of maintaining a duplicated list of rules, we generate the
15 # single-binary required rules based on the normal configuration found on
16 # src/local.mk with this script.
18 if test "x$1" = "x"; then
19 echo "Usage: $0 path/to/src/local.mk" >&2
20 exit 1
23 set -e
25 LOCAL_MK=$1
26 GEN_LISTS_OF_PROGRAMS="`dirname "$0"`/gen-lists-of-programs.sh"
28 ALL_PROGRAMS=$($GEN_LISTS_OF_PROGRAMS --list-progs \
29 | grep -v -F -e coreutils -e libstdbuf.so \
30 | tr '[' '_')
32 # Compute default SOURCES. automake will assume the source file for the
33 # src_${cmd} target to be src/${cmd}.c, but we will add rules to generate
34 # the lib src_libsinglebin_${cmd}_a which won't match the autogenerated source
35 # file. This loop will initialize the default source file and will be reset
36 # later if needed.
37 for cmd in $ALL_PROGRAMS; do
38 eval "src_${cmd}_SOURCES=src/${cmd}.c"
39 done
41 # Load actual values from src/local.mk. This will read all the variables from
42 # the local.mk matching the src_${cmd}_... case.
43 while read l; do
44 if echo "$l" | grep -E '^src_[_[:alnum:]]+ +\+?=' > /dev/null; then
45 var=$(echo $l | cut -f 1 -d ' ')
46 value=$(echo $l | cut -f 2- -d =)
47 if [ "$value" != " \$(LDADD)" ]; then
48 oldvalue=""
49 if echo $l | grep -F '+=' >/dev/null; then
50 eval "oldvalue=\${$var}"
52 value=$(echo "$value" | sed "s/'/'\"'\"'/g")
53 eval "$var='$oldvalue "$value"'"
56 done < $LOCAL_MK
58 me=`echo "$0" | sed 's,.*/,,'`
59 echo "## Automatically generated by $me. DO NOT EDIT BY HAND!"
61 # Override the sources for some tools, to use smaller variants
62 override_single() {
63 from="$1"; to="$2";
65 eval "src_${from}_SOURCES='src/coreutils-${from}.c'"
66 eval "src_from_LDADD=\$src_${from}_LDADD"
67 eval "src_${from}_LDADD='$src_from_LDADD src/libsinglebin_${to}.a'"
68 eval "src_libsinglebin_${from}_a_DEPENDENCIES='src/libsinglebin_${to}.a'"
69 echo "src_libsinglebin_${from}_a_DEPENDENCIES = src/libsinglebin_${to}.a"
71 override_single dir ls
72 override_single vdir ls
73 override_single arch uname
75 for cmd in $ALL_PROGRAMS; do
76 echo "# Command $cmd"
77 echo noinst_LIBRARIES += src/libsinglebin_${cmd}.a
78 base="src_libsinglebin_${cmd}_a"
79 # SOURCES
80 var=src_${cmd}_SOURCES
81 eval "value=\$$var"
82 echo "${base}_SOURCES = $value"
84 # LDADD
85 var=src_${cmd}_LDADD
86 eval "value=\$$var"
87 if [ "x$value" != "x" ]; then
88 echo "${base}_ldadd = $value"
91 # DEPENDENCIES
92 var=src_libsinglebin_${cmd}_a_DEPENDENCIES
93 eval "value=\$$var"
94 if [ "x$value" = "x" ]; then
95 echo "$var = \$(src_${cmd}_DEPENDENCIES)"
98 # CFLAGS
99 # Hack any other program defining a main() replacing its main by
100 # single_binary_main_$PROGRAM_NAME.
101 echo "${base}_CFLAGS = \"-Dmain=single_binary_main_${cmd} (int, char **);" \
102 " int single_binary_main_${cmd}\" " \
103 "-Dusage=_usage_${cmd} \$(src_coreutils_CFLAGS)"
104 var=src_${cmd}_CFLAGS
105 eval "value=\$$var"
106 if [ "x$value" != "x" ]; then
107 echo "${base}_CFLAGS += $value"
110 # CPPFLAGS
111 var=src_${cmd}_CPPFLAGS
112 eval "value=\$$var"
113 if [ "x$value" != "x" ]; then
114 echo "${base}_CPPFLAGS = $value"
116 done
118 exit 0