summaryrefslogtreecommitdiff
path: root/scripts/gen_build_files.sh (plain)
blob: d5f7b9488eb3b00271d138025c6f56707f7ac2dc
1#!/bin/sh
2
3# Note: was using sed OPTS CMD -- FILES
4# but users complain that many sed implementations
5# are misinterpreting --.
6
7test $# -ge 2 || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
8
9# cd to objtree
10cd -- "$2" || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
11# In separate objtree build, include/ might not exist yet
12mkdir include 2>/dev/null
13
14srctree="$1"
15
16status() { printf ' %-8s%s\n' "$1" "$2"; }
17gen() { status "GEN" "$@"; }
18chk() { status "CHK" "$@"; }
19
20# On OSX the sed implementation is not compatible with some of the
21# features in this script, so this uses gsed and warns the user if
22# it does not exist.
23UNAME=$(uname -sm)
24case "$UNAME" in
25*Darwin*|*Macintosh*)
26 SED_IMPL=$(which gsed)
27 if [ $? != 0 ]; then
28 echo "GNU sed is required for Darwin builds, please install and add 'gsed' to the path"
29 exit 1;
30 fi
31 ;;
32*)
33 SED_IMPL=sed
34esac
35
36generate()
37{
38 # NB: data to be inserted at INSERT line is coming on stdin
39 local src="$1" dst="$2" header="$3"
40 #chk "${dst}"
41 {
42 # Need to use printf: different shells have inconsistent
43 # rules re handling of "\n" in echo params.
44 printf "%s\n" "${header}"
45 # print everything up to INSERT line
46 $SED_IMPL -n '/^INSERT$/ q; p' "${src}"
47 # copy stdin to stdout
48 cat
49 # print everything after INSERT line
50 $SED_IMPL -n '/^INSERT$/ { :l; n; p; bl }' "${src}"
51 } >"${dst}.tmp"
52 if ! cmp -s "${dst}" "${dst}.tmp"; then
53 gen "${dst}"
54 mv "${dst}.tmp" "${dst}"
55 else
56 rm -f "${dst}.tmp"
57 fi
58}
59
60# (Re)generate include/applets.h
61$SED_IMPL -n 's@^//applet:@@p' "$srctree"/*/*.c "$srctree"/*/*/*.c \
62| generate \
63 "$srctree/include/applets.src.h" \
64 "include/applets.h" \
65 "/* DO NOT EDIT. This file is generated from applets.src.h */"
66
67# (Re)generate include/usage.h
68# We add line continuation backslash after each line,
69# and insert empty line before each line which doesn't start
70# with space or tab
71$SED_IMPL -n -e 's@^//usage:\([ \t].*\)$@\1 \\@p' -e 's@^//usage:\([^ \t].*\)$@\n\1 \\@p' \
72 "$srctree"/*/*.c "$srctree"/*/*/*.c \
73| generate \
74 "$srctree/include/usage.src.h" \
75 "include/usage.h" \
76 "/* DO NOT EDIT. This file is generated from usage.src.h */"
77
78# (Re)generate */Kbuild and */Config.in
79# We skip .dotdirs - makes git/svn/etc users happier
80{ cd -- "$srctree" && find . -type d -not '(' -name '.?*' -prune ')'; } \
81| while read -r d; do
82 d="${d#./}"
83
84 src="$srctree/$d/Kbuild.src"
85 dst="$d/Kbuild"
86 if test -f "$src"; then
87 mkdir -p -- "$d" 2>/dev/null
88
89 $SED_IMPL -n 's@^//kbuild:@@p' "$srctree/$d"/*.c \
90 | generate \
91 "${src}" "${dst}" \
92 "# DO NOT EDIT. This file is generated from Kbuild.src"
93 fi
94
95 src="$srctree/$d/Config.src"
96 dst="$d/Config.in"
97 if test -f "$src"; then
98 mkdir -p -- "$d" 2>/dev/null
99
100 $SED_IMPL -n 's@^//config:@@p' "$srctree/$d"/*.c \
101 | generate \
102 "${src}" "${dst}" \
103 "# DO NOT EDIT. This file is generated from Config.src"
104 fi
105done
106
107# Last read failed. This is normal. Don't exit with its error code:
108exit 0
109