Ensure build works when KBUILD_OUTPUT is set, closes 11511
[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         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
94         printf '#else\n'
95         if [ $n -ne 0 ]
96         then
97                 printf 'extern const uint16_t applet_numbers[];\n'
98         fi
99         printf '#endif\n'
100 fi
101
102 printf "\n"
103 printf '#define NUM_SCRIPTS %d\n' $n
104 printf "\n"
105
106 if [ $n -ne 0 ]
107 then
108         printf '#define UNPACKED_SCRIPTS_LENGTH '
109         concatenate_scripts | wc -c
110
111         printf '#define PACKED_SCRIPTS \\\n'
112         concatenate_scripts | bzip2 -1 | $DD bs=2 skip=1 2>/dev/null | \
113         od -v -b \
114         | 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'
121 fi
122
123 mv -- "$target.$$" "$target"