gen-single-binary.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/sh
  2. # Generate the list of rules for the single-binary option based on all the other
  3. # binaries found in src/local.mk.
  4. #
  5. # We need to duplicate the specific rules to build each program into a new
  6. # static library target. We can't reuse the existing target since we need to
  7. # create a .a file instead of linking the program. We can't do this at
  8. # ./configure since the file names need to be available when automake runs
  9. # to let it generate all the required rules in Makefile.in. The configure
  10. # step will select which ones will be used to build, but they need to be
  11. # generated beforehand.
  12. #
  13. # Instead of maintaining a duplicated list of rules, we generate the
  14. # single-binary required rules based on the normal configuration found on
  15. # src/local.mk with this script.
  16. if test "x$1" = "x"; then
  17. echo "Usage: $0 path/to/src/local.mk" >&2
  18. exit 1
  19. fi
  20. set -e
  21. LOCAL_MK=$1
  22. GEN_LISTS_OF_PROGRAMS="`dirname "$0"`/gen-lists-of-programs.sh"
  23. ALL_PROGRAMS=$($GEN_LISTS_OF_PROGRAMS --list-progs \
  24. | grep -v -F -e coreutils -e libstdbuf.so \
  25. | tr '[' '_')
  26. # Compute default SOURCES. automake will assume the source file for the
  27. # src_${cmd} target to be src/${cmd}.c, but we will add rules to generate
  28. # the lib src_libsinglebin_${cmd}_a which won't match the autogenerated source
  29. # file. This loop will initialize the default source file and will be reset
  30. # later if needed.
  31. for cmd in $ALL_PROGRAMS; do
  32. eval "src_${cmd}_SOURCES=src/${cmd}.c"
  33. done
  34. # Load actual values from src/local.mk. This will read all the variables from
  35. # the local.mk matching the src_${cmd}_... case.
  36. while read l; do
  37. if echo "$l" | grep -E '^src_\w+ +\+?=' > /dev/null; then
  38. var=$(echo $l | cut -f 1 -d ' ')
  39. value=$(echo $l | cut -f 2- -d =)
  40. if [ "$value" != " \$(LDADD)" ]; then
  41. oldvalue=""
  42. if echo $l | grep -F '+=' >/dev/null; then
  43. eval "oldvalue=\${$var}"
  44. fi
  45. value=$(echo "$value" | sed "s/'/'\"'\"'/g")
  46. eval "$var='$oldvalue "$value"'"
  47. fi
  48. fi
  49. done < $LOCAL_MK
  50. me=`echo "$0" | sed 's,.*/,,'`
  51. echo "## Automatically generated by $me. DO NOT EDIT BY HAND!"
  52. # Override the sources for dir and vdir. We use a smaller version of dir and
  53. # vdir that relies on the ls main.
  54. src_dir_SOURCES="src/coreutils-dir.c"
  55. src_dir_LDADD="$src_dir_LDADD src/libsinglebin_ls.a"
  56. echo src_libsinglebin_dir_a_DEPENDENCIES = src/libsinglebin_ls.a
  57. src_vdir_SOURCES="src/coreutils-vdir.c"
  58. src_vdir_LDADD="$src_vdir_LDADD src/libsinglebin_ls.a"
  59. echo src_libsinglebin_vdir_a_DEPENDENCIES = src/libsinglebin_ls.a
  60. # Override the sources for arch likewise, using the main from uname.
  61. src_arch_SOURCES="src/coreutils-arch.c"
  62. src_arch_LDADD="$src_arch_LDADD src/libsinglebin_uname.a"
  63. echo src_libsinglebin_arch_a_DEPENDENCIES = src/libsinglebin_uname.a
  64. for cmd in $ALL_PROGRAMS; do
  65. echo "# Command $cmd"
  66. echo noinst_LIBRARIES += src/libsinglebin_${cmd}.a
  67. base="src_libsinglebin_${cmd}_a"
  68. # SOURCES
  69. var=src_${cmd}_SOURCES
  70. eval "value=\$$var"
  71. echo "${base}_SOURCES = $value"
  72. # LDADD
  73. var=src_${cmd}_LDADD
  74. eval "value=\$$var"
  75. if [ "x$value" != "x" ]; then
  76. echo "${base}_ldadd = $value"
  77. fi
  78. # CFLAGS
  79. # Hack any other program defining a main() replacing its main by
  80. # single_binary_main_$PROGRAM_NAME.
  81. echo "${base}_CFLAGS = \"-Dmain=single_binary_main_${cmd} (int, char **);" \
  82. " int single_binary_main_${cmd}\" " \
  83. "-Dusage=_usage_${cmd} \$(src_coreutils_CFLAGS)"
  84. var=src_${cmd}_CFLAGS
  85. eval "value=\$$var"
  86. if [ "x$value" != "x" ]; then
  87. echo "${base}_CFLAGS += $value"
  88. fi
  89. # CPPFLAGS
  90. var=src_${cmd}_CPPFLAGS
  91. eval "value=\$$var"
  92. if [ "x$value" != "x" ]; then
  93. echo "${base}_CPPFLAGS = $value"
  94. fi
  95. done
  96. exit 0