blob: 86ad44d1d68d451547edc567114cc0ccbf6ad00b [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'
Ron Yorston7b42f8f2018-11-21 10:11:01 +000084 printf 'const uint16_t applet_numbers[] = {\n'
85 for i in $custom_scripts $applet_scripts
86 do
87 # TODO support applets with names including invalid characters
88 printf '\tAPPLET_NO_%s,\n' $i
89 done
90 printf '};\n'
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010091 printf '#else\n'
Ron Yorston7b42f8f2018-11-21 10:11:01 +000092 printf 'extern const uint16_t applet_numbers[];\n'
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010093 printf '#endif\n'
94fi
Ron Yorston37788982018-11-17 17:48:14 +000095
96printf "\n"
97printf '#define NUM_SCRIPTS %d\n' $n
98printf "\n"
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +010099
100if [ $n -ne 0 ]
101then
102 printf '#define UNPACKED_SCRIPTS_LENGTH '
Ron Yorston37788982018-11-17 17:48:14 +0000103 concatenate_scripts | wc -c
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100104
105 printf '#define PACKED_SCRIPTS \\\n'
Ron Yorston37788982018-11-17 17:48:14 +0000106 concatenate_scripts | bzip2 -1 | $DD bs=2 skip=1 2>/dev/null | \
107 od -v -b \
Denys Vlasenko4f2ef4a2018-11-01 09:53:25 +0100108 | grep -v '^ ' \
109 | $SED -e 's/^[^ ]*//' \
110 -e 's/ //g' \
111 -e '/^$/d' \
112 -e 's/\(...\)/0\1,/g' \
113 -e 's/$/ \\/'
114 printf '\n'
115fi
116
117mv -- "$target.$$" "$target"