blob: 205ac591a563e6125775cc355a77fd72b4bb7640 [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
Denys Vlasenkoed2af2e2022-01-04 14:32:41 +010026bzip2 </dev/null >/dev/null
27if test $? != 0; then
28 echo 'bzip2 is not installed'
29 exit 1
30fi
31
Ron Yorston37788982018-11-17 17:48:14 +000032custom_scripts=""
33if [ -d "$custom_loc" ]
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010034then
Ron Yorston37788982018-11-17 17:48:14 +000035 custom_scripts=$(cd $custom_loc; ls * 2>/dev/null)
36fi
Ron Yorstonaf694a42018-11-18 07:40:40 +000037all_scripts=$($srctree/applets/busybox.mkscripts)
Ron Yorston37788982018-11-17 17:48:14 +000038
39# all_scripts includes applet scripts and custom scripts, sort them out
40applet_scripts=""
41for i in $all_scripts
42do
43 found=0
44 for j in $custom_scripts
45 do
46 if [ "$i" = "$j" ]
47 then
48 found=1
49 break;
50 fi
51 done
52 if [ $found -eq 0 ]
53 then
54 # anything that isn't a custom script is an applet script
55 applet_scripts="$applet_scripts $i"
56 fi
57done
58
59# we know the custom scripts are present but applet scripts might have
60# become detached from their configuration
61for i in $applet_scripts
62do
63 #if [ ! -f "$applet_loc/$i" -a ! -f "$custom_loc/$i" ]
64 if [ ! -f "$applet_loc/$i" ]
65 then
66 echo "missing applet script $i"
67 exit 1
68 fi
69done
70
71n=$(echo $custom_scripts $applet_scripts | wc -w)
72nall=$(echo $all_scripts | wc -w)
73
74if [ $n -ne $nall ]
75then
76 echo "script mismatch $n != $nall"
77 exit 1
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010078fi
79
Ron Yorston37788982018-11-17 17:48:14 +000080concatenate_scripts() {
81 for i in $custom_scripts
82 do
83 cat $custom_loc/$i
84 printf '\000'
85 done
86 for i in $applet_scripts
87 do
88 cat $applet_loc/$i
89 printf '\000'
90 done
91}
92
93exec >"$target.$$"
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010094
95if [ $n -ne 0 ]
96then
Ron Yorston37788982018-11-17 17:48:14 +000097 printf '#ifdef DEFINE_SCRIPT_DATA\n'
Ron Yorston7b42f8f2018-11-21 10:11:01 +000098 printf 'const uint16_t applet_numbers[] = {\n'
99 for i in $custom_scripts $applet_scripts
100 do
101 # TODO support applets with names including invalid characters
102 printf '\tAPPLET_NO_%s,\n' $i
103 done
104 printf '};\n'
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100105 printf '#else\n'
Ron Yorston7b42f8f2018-11-21 10:11:01 +0000106 printf 'extern const uint16_t applet_numbers[];\n'
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100107 printf '#endif\n'
108fi
Ron Yorston37788982018-11-17 17:48:14 +0000109
110printf "\n"
111printf '#define NUM_SCRIPTS %d\n' $n
112printf "\n"
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100113
114if [ $n -ne 0 ]
115then
116 printf '#define UNPACKED_SCRIPTS_LENGTH '
Ron Yorston37788982018-11-17 17:48:14 +0000117 concatenate_scripts | wc -c
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100118
119 printf '#define PACKED_SCRIPTS \\\n'
Ron Yorston37788982018-11-17 17:48:14 +0000120 concatenate_scripts | bzip2 -1 | $DD bs=2 skip=1 2>/dev/null | \
121 od -v -b \
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100122 | grep -v '^ ' \
123 | $SED -e 's/^[^ ]*//' \
124 -e 's/ //g' \
125 -e '/^$/d' \
126 -e 's/\(...\)/0\1,/g' \
127 -e 's/$/ \\/'
128 printf '\n'
129fi
130
131mv -- "$target.$$" "$target"