1#!/bin/sh
2
3# Generate the list of rules for the single-binary option based on all the other
4# binaries found in src/local.mk.
5#
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.
13#
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.
17
18if test "x$1" = "x"; then
19  echo "Usage: $0 path/to/src/local.mk" >&2
20  exit 1
21fi
22
23set -e
24
25LOCAL_MK=$1
26GEN_LISTS_OF_PROGRAMS="`dirname "$0"`/gen-lists-of-programs.sh"
27
28ALL_PROGRAMS=$($GEN_LISTS_OF_PROGRAMS --list-progs \
29    | grep -v -F -e coreutils -e libstdbuf.so \
30    | tr '[' '_')
31
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.
37for cmd in $ALL_PROGRAMS; do
38  eval "src_${cmd}_SOURCES=src/${cmd}.c"
39done
40
41# Load actual values from src/local.mk. This will read all the variables from
42# the local.mk matching the src_${cmd}_... case.
43while 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}"
51      fi
52      value=$(echo "$value" | sed "s/'/'\"'\"'/g")
53      eval "$var='$oldvalue "$value"'"
54    fi
55  fi
56done < $LOCAL_MK
57
58me=`echo "$0" | sed 's,.*/,,'`
59echo "## Automatically generated by $me.  DO NOT EDIT BY HAND!"
60
61# Override the sources for some tools, to use smaller variants
62override_single() {
63  from="$1"; to="$2";
64
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"
70}
71override_single dir ls
72override_single vdir ls
73override_single arch uname
74
75for 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"
83
84  # LDADD
85  var=src_${cmd}_LDADD
86  eval "value=\$$var"
87  if [ "x$value" != "x" ]; then
88    echo "${base}_ldadd = $value"
89  fi
90
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)"
96  fi
97
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"
108  fi
109
110  # CPPFLAGS
111  var=src_${cmd}_CPPFLAGS
112  eval "value=\$$var"
113  if [ "x$value" != "x" ]; then
114    echo "${base}_CPPFLAGS = $value"
115  fi
116done
117
118exit 0
119