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