blob: aa7bf3e8a1924d7ecc85d6228bdfb1e2d0b2ec4e [file] [log] [blame]
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +01001#!/bin/sh
2
Ron Yorston71df2d32018-11-27 14:34:25 +00003. ./.config || exit 1
4
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +01005target="$1"
Ron Yorston37788982018-11-17 17:48:14 +00006custom_loc="$2"
7applet_loc="$3"
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +01008
9test "$target" || exit 1
10test "$SED" || SED=sed
11test "$DD" || DD=dd
12
Ron Yorston71df2d32018-11-27 14:34:25 +000013if [ x"$CONFIG_FEATURE_SH_EMBEDDED_SCRIPTS" != x"y" ]
14then
15 printf '#define NUM_SCRIPTS 0\n' >"$target"
16 exit 0
17fi
18
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010019# Some people were bitten by their system lacking a (proper) od
20od -v -b </dev/null >/dev/null
21if test $? != 0; then
22 echo 'od tool is not installed or cannot accept "-v -b" options'
23 exit 1
24fi
25
Ron Yorston37788982018-11-17 17:48:14 +000026custom_scripts=""
27if [ -d "$custom_loc" ]
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010028then
Ron Yorston37788982018-11-17 17:48:14 +000029 custom_scripts=$(cd $custom_loc; ls * 2>/dev/null)
30fi
Ron Yorstonaf694a42018-11-18 07:40:40 +000031all_scripts=$($srctree/applets/busybox.mkscripts)
Ron Yorston37788982018-11-17 17:48:14 +000032
33# all_scripts includes applet scripts and custom scripts, sort them out
34applet_scripts=""
35for i in $all_scripts
36do
37 found=0
38 for j in $custom_scripts
39 do
40 if [ "$i" = "$j" ]
41 then
42 found=1
43 break;
44 fi
45 done
46 if [ $found -eq 0 ]
47 then
48 # anything that isn't a custom script is an applet script
49 applet_scripts="$applet_scripts $i"
50 fi
51done
52
53# we know the custom scripts are present but applet scripts might have
54# become detached from their configuration
55for i in $applet_scripts
56do
57 #if [ ! -f "$applet_loc/$i" -a ! -f "$custom_loc/$i" ]
58 if [ ! -f "$applet_loc/$i" ]
59 then
60 echo "missing applet script $i"
61 exit 1
62 fi
63done
64
65n=$(echo $custom_scripts $applet_scripts | wc -w)
66nall=$(echo $all_scripts | wc -w)
67
68if [ $n -ne $nall ]
69then
70 echo "script mismatch $n != $nall"
71 exit 1
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010072fi
73
Ron Yorston37788982018-11-17 17:48:14 +000074concatenate_scripts() {
75 for i in $custom_scripts
76 do
77 cat $custom_loc/$i
78 printf '\000'
79 done
80 for i in $applet_scripts
81 do
82 cat $applet_loc/$i
83 printf '\000'
84 done
85}
86
87exec >"$target.$$"
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010088
89if [ $n -ne 0 ]
90then
Ron Yorston37788982018-11-17 17:48:14 +000091 printf '#ifdef DEFINE_SCRIPT_DATA\n'
Ron Yorston7b42f8f2018-11-21 10:11:01 +000092 printf 'const uint16_t applet_numbers[] = {\n'
93 for i in $custom_scripts $applet_scripts
94 do
95 # TODO support applets with names including invalid characters
96 printf '\tAPPLET_NO_%s,\n' $i
97 done
98 printf '};\n'
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010099 printf '#else\n'
Ron Yorston7b42f8f2018-11-21 10:11:01 +0000100 printf 'extern const uint16_t applet_numbers[];\n'
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100101 printf '#endif\n'
102fi
Ron Yorston37788982018-11-17 17:48:14 +0000103
104printf "\n"
105printf '#define NUM_SCRIPTS %d\n' $n
106printf "\n"
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100107
108if [ $n -ne 0 ]
109then
110 printf '#define UNPACKED_SCRIPTS_LENGTH '
Ron Yorston37788982018-11-17 17:48:14 +0000111 concatenate_scripts | wc -c
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100112
113 printf '#define PACKED_SCRIPTS \\\n'
Ron Yorston37788982018-11-17 17:48:14 +0000114 concatenate_scripts | bzip2 -1 | $DD bs=2 skip=1 2>/dev/null | \
115 od -v -b \
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100116 | grep -v '^ ' \
117 | $SED -e 's/^[^ ]*//' \
118 -e 's/ //g' \
119 -e '/^$/d' \
120 -e 's/\(...\)/0\1,/g' \
121 -e 's/$/ \\/'
122 printf '\n'
123fi
124
125mv -- "$target.$$" "$target"