make it possible to have include/applets.h-esque entries in .c files
[oweals/busybox.git] / scripts / gen_build_files.sh
1 #!/bin/sh
2
3 test $# -ge 2 || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
4
5 # cd to objtree
6 cd -- "$2" || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
7
8 srctree="$1"
9
10 # (Re)generate include/applets.h
11 src="$srctree/include/applets.src.h"
12 dst="include/applets.h"
13 s=`sed -n 's@^//applet:@@p' -- */*.c */*/*.c`
14 echo "/* DO NOT EDIT. This file is generated from applets.src.h */" >"$dst.$$.tmp"
15 # Why "IFS='' read -r REPLY"??
16 # This atrocity is needed to read lines without mangling.
17 # IFS='' prevents whitespace trimming,
18 # -r suppresses backslash handling.
19 while IFS='' read -r REPLY; do
20         test x"$REPLY" = x"INSERT" && REPLY="$s"
21         printf "%s\n" "$REPLY"
22 done <"$src" >>"$dst.$$.tmp"
23 if test -f "$dst" && cmp -s "$dst.$$.tmp" "$dst"; then
24         rm -- "$dst.$$.tmp"
25 else
26         echo "  GEN     $dst"
27         mv -- "$dst.$$.tmp" "$dst"
28 fi
29
30 # (Re)generate */Kbuild and */Config.in
31 find -type d | while read -r d; do
32         d="${d#./}"
33         src="$srctree/$d/Kbuild.src"
34         dst="$d/Kbuild"
35         if test -f "$src"; then
36                 #echo "  CHK     $dst"
37
38                 s=`sed -n 's@^//kbuild:@@p' -- "$srctree/$d"/*.c`
39
40                 echo "# DO NOT EDIT. This file is generated from Kbuild.src" >"$dst.$$.tmp"
41                 while IFS='' read -r REPLY; do
42                         test x"$REPLY" = x"INSERT" && REPLY="$s"
43                         printf "%s\n" "$REPLY"
44                 done <"$src" >>"$dst.$$.tmp"
45                 if test -f "$dst" && cmp -s "$dst.$$.tmp" "$dst"; then
46                         rm -- "$dst.$$.tmp"
47                 else
48                         echo "  GEN     $dst"
49                         mv -- "$dst.$$.tmp" "$dst"
50                 fi
51         fi
52
53         src="$srctree/$d/Config.src"
54         dst="$d/Config.in"
55         if test -f "$src"; then
56                 #echo "  CHK     $dst"
57
58                 s=`sed -n 's@^//config:@@p' -- "$srctree/$d"/*.c`
59
60                 echo "# DO NOT EDIT. This file is generated from Config.src" >"$dst.$$.tmp"
61                 while IFS='' read -r REPLY; do
62                         test x"$REPLY" = x"INSERT" && REPLY="$s"
63                         printf "%s\n" "$REPLY"
64                 done <"$src" >>"$dst.$$.tmp"
65                 if test -f "$dst" && cmp -s "$dst.$$.tmp" "$dst"; then
66                         rm -- "$dst.$$.tmp"
67                 else
68                         echo "  GEN     $dst"
69                         mv -- "$dst.$$.tmp" "$dst"
70                 fi
71         fi
72 done
73
74 # Last read failed. This is normal. Don't exit with its error code:
75 exit 0