| 1 | #!/bin/sh |
| 2 | # |
| 3 | # generate_core.sh extracts the core part from buildgcc script, |
| 4 | # most importantly the checksum calculation/verification functions. |
| 5 | # |
| 6 | # Copyright (C) 2019 Mike Banon <[email protected]> |
| 7 | # |
| 8 | ############################################################################## |
| 9 | # |
| 10 | # USAGE: |
| 11 | # ./generate_core.sh <buildgcc> <corescript> prepare_before_patch |
| 12 | # where |
| 13 | # buildgcc - path to input buildgcc script |
| 14 | # corescript - path to output core part script |
| 15 | # prepare_before_patch - optional argument to insert prepare_${package} |
| 16 | # call into the unpack_and_patch function, e.g. |
| 17 | # for removing some files with rm command |
| 18 | # in order to reduce the size of patch file |
| 19 | # |
| 20 | ############################################################################## |
| 21 | |
| 22 | buildgcc="$1" |
| 23 | corescript="$2" |
| 24 | prepare_before_patch="$3" |
| 25 | |
| 26 | # |
| 27 | # Imports the source file fragment between start and end into the |
| 28 | # destination file, optionally excluding the last line if not needed |
| 29 | # |
| 30 | |
| 31 | import_from_file() { |
| 32 | source="$1" |
| 33 | destination="$2" |
| 34 | start="$3" |
| 35 | end="$4" |
| 36 | last_line_disabled="$5" |
| 37 | if [ -z "${last_line_disabled}" ] ; then |
| 38 | sed -n "/^${start}/,/^${end}/{/^${start}/{p;n};{p}}" "$source" >> "$destination" |
| 39 | else |
| 40 | sed -n "/^${start}/,/^${end}/{/^${start}/{p;n};/^${end}/{q};{p}}" "$source" >> "$destination" |
| 41 | fi |
| 42 | } |
| 43 | |
| 44 | # |
| 45 | # Import the color defines together with UNAME/HALT_FOR_TOOLS variables |
| 46 | # |
| 47 | |
| 48 | import_from_file "$buildgcc" "$corescript" "red=" "HALT_FOR_TOOLS=0" || exit "$?" |
| 49 | |
| 50 | # |
| 51 | # Import the core functions |
| 52 | # |
| 53 | |
| 54 | FUNCTIONS="please_install searchtool download compute_hash error_hash_mismatch verify_hash unpack_and_patch" |
| 55 | |
| 56 | for F in $FUNCTIONS ; do |
| 57 | import_from_file "$buildgcc" "$corescript" "$F()" "}" || exit "$?" |
| 58 | done |
| 59 | |
| 60 | # |
| 61 | # Import a fragment where we find tar/patch/make and other essential tools |
| 62 | # |
| 63 | |
| 64 | import_from_file "$buildgcc" "$corescript" "# Find all the required tools" "# Allow" "last_line_disabled" || exit "$?" |
| 65 | |
| 66 | # |
| 67 | # Import a fragment with conditional exit if some required tools were not found |
| 68 | # |
| 69 | |
| 70 | import_from_file "$buildgcc" "$corescript" "if \[ \"\$HALT_FOR_TOOLS" "fi" || exit "$?" |
| 71 | |
| 72 | # |
| 73 | # Avoid the unnecessary subdirectories holding a single file each |
| 74 | # |
| 75 | |
| 76 | sed -i -e "s/patches\///g" "$corescript" |
| 77 | sed -i -e "s/sum\///g" "$corescript" |
| 78 | sed -i -e "s/tarballs\///g" "$corescript" |
| 79 | sed -i -e "s/cd tarballs || exit 1//g" "$corescript" |
| 80 | sed -i -e "s/cd \.\.//g" "$corescript" |
| 81 | |
| 82 | # |
| 83 | # Get the known checksum without using a dedicated single-line file |
| 84 | # |
| 85 | |
| 86 | sed -i -e "s/\tknown_hash=\"\$(get_known_hash.*/\tknown_hash=\"\$2\"/g" "$corescript" |
| 87 | |
| 88 | # |
| 89 | # Update the paths printed at the error messages |
| 90 | # |
| 91 | |
| 92 | sed -i -e "s/util\/crossgcc\///g" "$corescript" |
| 93 | |
| 94 | # |
| 95 | # Insert prepare_${package} function call between the unpack and patch operations |
| 96 | # |
| 97 | |
| 98 | if [ ! -z "${prepare_before_patch}" ] ; then |
| 99 | sed -i -e "/\$TAR \$FLAGS \"\$(basename \"\$archive\")\"/a prepare_\${package} || exit \"\$?\"" "$corescript" |
| 100 | sed -i -e "/\$TAR \$FLAGS \"\$archive\"/a prepare_\${package} || exit \"\$?\"" "$corescript" |
| 101 | fi |
| 102 | |
| 103 | # |