Tweaks to build process for embedded scripts
[oweals/busybox.git] / scripts / embedded_scripts
1 #!/bin/sh
2
3 target="$1"
4 custom_loc="$2"
5 applet_loc="$3"
6
7 test "$target" || exit 1
8 test "$SED" || SED=sed
9 test "$DD" || DD=dd
10
11 # Some people were bitten by their system lacking a (proper) od
12 od -v -b </dev/null >/dev/null
13 if test $? != 0; then
14         echo 'od tool is not installed or cannot accept "-v -b" options'
15         exit 1
16 fi
17
18 custom_scripts=""
19 if [ -d "$custom_loc" ]
20 then
21         custom_scripts=$(cd $custom_loc; ls * 2>/dev/null)
22 fi
23 all_scripts=$($srctree/applets/busybox.mkscripts)
24
25 # all_scripts includes applet scripts and custom scripts, sort them out
26 applet_scripts=""
27 for i in $all_scripts
28 do
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
43 done
44
45 # we know the custom scripts are present but applet scripts might have
46 # become detached from their configuration
47 for i in $applet_scripts
48 do
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
55 done
56
57 n=$(echo $custom_scripts $applet_scripts | wc -w)
58 nall=$(echo $all_scripts | wc -w)
59
60 if [ $n -ne $nall ]
61 then
62         echo "script mismatch $n != $nall"
63         exit 1
64 fi
65
66 concatenate_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
79 exec >"$target.$$"
80
81 if [ $n -ne 0 ]
82 then
83         printf '#ifdef DEFINE_SCRIPT_DATA\n'
84         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'
91         printf '#else\n'
92         printf 'extern const uint16_t applet_numbers[];\n'
93         printf '#endif\n'
94 fi
95
96 printf "\n"
97 printf '#define NUM_SCRIPTS %d\n' $n
98 printf "\n"
99
100 if [ $n -ne 0 ]
101 then
102         printf '#define UNPACKED_SCRIPTS_LENGTH '
103         concatenate_scripts | wc -c
104
105         printf '#define PACKED_SCRIPTS \\\n'
106         concatenate_scripts | bzip2 -1 | $DD bs=2 skip=1 2>/dev/null | \
107         od -v -b \
108         | 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'
115 fi
116
117 mv -- "$target.$$" "$target"