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