blob: c2e7c6961968b363af608c09fbb27531fc02635c [file] [log] [blame]
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +01001#!/bin/sh
2
3target="$1"
Ron Yorston37788982018-11-17 17:48:14 +00004custom_loc="$2"
5applet_loc="$3"
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +01006
7test "$target" || exit 1
8test "$SED" || SED=sed
9test "$DD" || DD=dd
10
11# Some people were bitten by their system lacking a (proper) od
12od -v -b </dev/null >/dev/null
13if test $? != 0; then
14 echo 'od tool is not installed or cannot accept "-v -b" options'
15 exit 1
16fi
17
Ron Yorston37788982018-11-17 17:48:14 +000018custom_scripts=""
19if [ -d "$custom_loc" ]
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010020then
Ron Yorston37788982018-11-17 17:48:14 +000021 custom_scripts=$(cd $custom_loc; ls * 2>/dev/null)
22fi
Ron Yorstonaf694a42018-11-18 07:40:40 +000023all_scripts=$($srctree/applets/busybox.mkscripts)
Ron Yorston37788982018-11-17 17:48:14 +000024
25# all_scripts includes applet scripts and custom scripts, sort them out
26applet_scripts=""
27for i in $all_scripts
28do
29 found=0
30 for j in $custom_scripts
31 do
32 if [ "$i" = "$j" ]
33 then
34 found=1
35 break;
36 fi
37 done
38 if [ $found -eq 0 ]
39 then
40 # anything that isn't a custom script is an applet script
41 applet_scripts="$applet_scripts $i"
42 fi
43done
44
45# we know the custom scripts are present but applet scripts might have
46# become detached from their configuration
47for i in $applet_scripts
48do
49 #if [ ! -f "$applet_loc/$i" -a ! -f "$custom_loc/$i" ]
50 if [ ! -f "$applet_loc/$i" ]
51 then
52 echo "missing applet script $i"
53 exit 1
54 fi
55done
56
57n=$(echo $custom_scripts $applet_scripts | wc -w)
58nall=$(echo $all_scripts | wc -w)
59
60if [ $n -ne $nall ]
61then
62 echo "script mismatch $n != $nall"
63 exit 1
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010064fi
65
Ron Yorston37788982018-11-17 17:48:14 +000066concatenate_scripts() {
67 for i in $custom_scripts
68 do
69 cat $custom_loc/$i
70 printf '\000'
71 done
72 for i in $applet_scripts
73 do
74 cat $applet_loc/$i
75 printf '\000'
76 done
77}
78
79exec >"$target.$$"
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010080
81if [ $n -ne 0 ]
82then
Ron Yorston37788982018-11-17 17:48:14 +000083 printf '#ifdef DEFINE_SCRIPT_DATA\n'
84 if [ $n -ne 0 ]
85 then
86 printf 'const uint16_t applet_numbers[] = {\n'
87 for i in $custom_scripts $applet_scripts
88 do
89 # TODO support applets with names including invalid characters
90 printf '\tAPPLET_NO_%s,\n' $i
91 done
92 printf '};\n'
93 fi
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010094 printf '#else\n'
Ron Yorston37788982018-11-17 17:48:14 +000095 if [ $n -ne 0 ]
96 then
97 printf 'extern const uint16_t applet_numbers[];\n'
98 fi
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010099 printf '#endif\n'
100fi
Ron Yorston37788982018-11-17 17:48:14 +0000101
102printf "\n"
103printf '#define NUM_SCRIPTS %d\n' $n
104printf "\n"
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100105
106if [ $n -ne 0 ]
107then
108 printf '#define UNPACKED_SCRIPTS_LENGTH '
Ron Yorston37788982018-11-17 17:48:14 +0000109 concatenate_scripts | wc -c
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100110
111 printf '#define PACKED_SCRIPTS \\\n'
Ron Yorston37788982018-11-17 17:48:14 +0000112 concatenate_scripts | bzip2 -1 | $DD bs=2 skip=1 2>/dev/null | \
113 od -v -b \
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100114 | grep -v '^ ' \
115 | $SED -e 's/^[^ ]*//' \
116 -e 's/ //g' \
117 -e '/^$/d' \
118 -e 's/\(...\)/0\1,/g' \
119 -e 's/$/ \\/'
120 printf '\n'
121fi
122
123mv -- "$target.$$" "$target"