1#!/bin/sh
2
3# Note: was using sed OPTS CMD -- FILES
4# but users complain that many sed implementations
5# are misinterpreting --.
6
7export LC_ALL=C
8
9test $# -ge 2 || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
10
11# cd to objtree
12cd -- "$2" || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
13# In separate objtree build, include/ might not exist yet
14mkdir include 2>/dev/null
15
16srctree="$1"
17
18status() { printf '  %-8s%s\n' "$1" "$2"; }
19gen() { status "GEN" "$@"; }
20chk() { status "CHK" "$@"; }
21
22# scripts in the 'embed' directory are treated as fake applets
23custom_scripts()
24{
25	custom_loc="$1"
26	if [ -d "$custom_loc" ]
27	then
28		for i in $(cd "$custom_loc"; ls * 2>/dev/null)
29		do
30			printf "IF_FEATURE_SH_EMBEDDED_SCRIPTS(APPLET_SCRIPTED(%s, scripted, BB_DIR_USR_BIN, BB_SUID_DROP, scripted))\n" $i;
31		done
32	fi
33}
34
35generate()
36{
37	# NB: data to be inserted at INSERT line is coming on stdin
38	src="$1"
39	dst="$2"
40	header="$3"
41	loc="$4"
42	#chk "${dst}"
43	{
44		# Need to use printf: different shells have inconsistent
45		# rules re handling of "\n" in echo params.
46		printf "%s\n" "${header}"
47		# print everything up to INSERT line
48		sed -n '/^INSERT$/ q; p' "${src}"
49		# copy stdin to stdout
50		cat
51		if [ -n "$loc" ]
52		then
53			custom_scripts "$loc"
54		fi
55		# print everything after INSERT line
56		sed -n '/^INSERT$/ {
57		:l
58		    n
59		    p
60		    bl
61		}' "${src}"
62	} >"${dst}.tmp"
63	if ! cmp -s "${dst}" "${dst}.tmp"; then
64		gen "${dst}"
65		mv "${dst}.tmp" "${dst}"
66	else
67		rm -f "${dst}.tmp"
68	fi
69}
70
71# (Re)generate include/applets.h
72sed -n 's@^//applet:@@p' "$srctree"/*/*.c "$srctree"/*/*/*.c \
73| generate \
74	"$srctree/include/applets.src.h" \
75	"include/applets.h" \
76	"/* DO NOT EDIT. This file is generated from applets.src.h */" \
77	"$srctree/embed"
78
79# (Re)generate include/usage.h
80# We add line continuation backslash after each line,
81# and insert empty line before each line which doesn't start
82# with space or tab
83TAB="$(printf '\tX')"
84TAB="${TAB%X}"
85LF="$(printf '\nX')"
86LF="${LF%X}"
87sed -n -e 's@^//usage:\([ '"$TAB"'].*\)$@\1 \\@p' \
88       -e 's@^//usage:\([^ '"$TAB"'].*\)$@\'"$LF"'\1 \\@p' \
89	"$srctree"/*/*.c "$srctree"/*/*/*.c \
90| generate \
91	"$srctree/include/usage.src.h" \
92	"include/usage.h" \
93	"/* DO NOT EDIT. This file is generated from usage.src.h */"
94
95# (Re)generate */Kbuild and */Config.in
96# We skip .dotdirs - makes git/svn/etc users happier
97{ cd -- "$srctree" && find . -type d ! '(' -name '.?*' -prune ')'; } \
98| while read -r d; do
99	d="${d#./}"
100
101	src="$srctree/$d/Kbuild.src"
102	dst="$d/Kbuild"
103	if test -f "$src"; then
104		mkdir -p -- "$d" 2>/dev/null
105
106		sed -n 's@^//kbuild:@@p' "$srctree/$d"/*.c \
107		| generate \
108			"${src}" "${dst}" \
109			"# DO NOT EDIT. This file is generated from Kbuild.src"
110	fi
111
112	src="$srctree/$d/Config.src"
113	dst="$d/Config.in"
114	if test -f "$src"; then
115		mkdir -p -- "$d" 2>/dev/null
116
117		sed -n 's@^//config:@@p' "$srctree/$d"/*.c \
118		| generate \
119			"${src}" "${dst}" \
120			"# DO NOT EDIT. This file is generated from Config.src"
121	fi
122done
123
124# Last read failed. This is normal. Don't exit with its error code:
125exit 0
126