b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | # |
| 3 | # Copyright (C) 2020 Paul Spooren <mail@aparcar.org> |
| 4 | # |
| 5 | ### |
| 6 | ### size_compare - compare size of OpenWrt packages against upstream |
| 7 | ### |
| 8 | ### The script compares locally compiled package with the package indexes |
| 9 | ### available upstream. This way the storage impact of optimizations or |
| 10 | ### feature modifications is easy to see. |
| 11 | ### |
| 12 | ### If no environmental variables are set the script reads the current |
| 13 | ### .config file. The evaluated env variables are the following: |
| 14 | ### |
| 15 | ### TARGET SUBTARGET ARCH PACKAGES BIN_DIR BASE_URL CHECK_INSTALLED |
| 16 | ### |
| 17 | ### Usage: |
| 18 | ### ./scripts/size_compare.sh |
| 19 | ### |
| 20 | ### Options: |
| 21 | ### -p --package-size Check IPK package size and not installed size |
| 22 | ### -h --help This message |
| 23 | |
| 24 | eval "$(grep \ |
| 25 | -e ^CONFIG_TARGET_BOARD= \ |
| 26 | -e ^CONFIG_TARGET_SUBTARGET= \ |
| 27 | -e ^CONFIG_TARGET_ARCH_PACKAGES= \ |
| 28 | -e ^CONFIG_BINARY_FOLDER= \ |
| 29 | .config 2>/dev/null \ |
| 30 | )" |
| 31 | CONFIG_PACKAGES=$(sed -n 's/^CONFIG_PACKAGE_\(.*\)=y$/\1/p' .config | tr '\n' ' ') |
| 32 | |
| 33 | TARGET=${TARGET:-$CONFIG_TARGET_BOARD} |
| 34 | SUBTARGET=${SUBTARGET:-$CONFIG_TARGET_SUBTARGET} |
| 35 | ARCH=${ARCH:-$CONFIG_TARGET_ARCH_PACKAGES} |
| 36 | PACKAGES=${PACKAGES:-$CONFIG_PACKAGES} |
| 37 | BIN_DIR=${CONFIG_BINARY_FOLDER:-./bin} |
| 38 | BASE_URL="${BASE_URL:-https://downloads.openwrt.org/snapshots}" |
| 39 | CHECK_INSTALLED="${CHECK_INSTALLED:-y}" |
| 40 | |
| 41 | TARGET_URL="$BASE_URL/targets/$TARGET/$SUBTARGET/packages/Packages.gz" |
| 42 | CONFIG_URL="$BASE_URL/targets/$TARGET/$SUBTARGET/config.buildinfo" |
| 43 | PACKAGES_URL="$BASE_URL/packages/$ARCH/base/Packages.gz" |
| 44 | |
| 45 | if command -v curl > /dev/null; then |
| 46 | DOWNLOAD_METHOD="curl" |
| 47 | else |
| 48 | DOWNLOAD_METHOD="wget --output-document=-" |
| 49 | fi |
| 50 | |
| 51 | help() { |
| 52 | sed -rn 's/^### ?//;T;p' "$0" |
| 53 | } |
| 54 | |
| 55 | package_size () { |
| 56 | FOUND_PACKAGE= |
| 57 | if [ -z "$CHECK_INSTALLED" ]; then |
| 58 | SEARCH_PATTERN="Size" |
| 59 | else |
| 60 | SEARCH_PATTERN="Installed-Size" |
| 61 | fi |
| 62 | while IFS= read -r line; do |
| 63 | if [ "$line" = "Package: $2" ]; then |
| 64 | FOUND_PACKAGE=y |
| 65 | fi |
| 66 | if [ -n "$FOUND_PACKAGE" ]; then |
| 67 | case $line in |
| 68 | "$SEARCH_PATTERN"*) |
| 69 | echo "$line" | cut -d ' ' -f 2 |
| 70 | break |
| 71 | ;; |
| 72 | esac |
| 73 | fi |
| 74 | done < "$1" |
| 75 | } |
| 76 | |
| 77 | compare_sizes () { |
| 78 | TOTAL_DIFF="0" |
| 79 | for PACKAGE in $PACKAGES; do |
| 80 | if [ "$PACKAGE" = "libc" ]; then |
| 81 | continue |
| 82 | fi |
| 83 | PACKAGE_FILE=$(find "$BIN_DIR/packages/$ARCH/" \ |
| 84 | "$BIN_DIR/targets/$TARGET/$SUBTARGET/" \ |
| 85 | -name "${PACKAGE}_*.ipk" 2>/dev/null | head -n1) |
| 86 | |
| 87 | if [ -z "$PACKAGE_FILE" ]; then |
| 88 | continue |
| 89 | fi |
| 90 | if [ -z "$CHECK_INSTALLED" ]; then |
| 91 | SIZE_LOCAL=$(stat -c '%s' "$PACKAGE_FILE") |
| 92 | else |
| 93 | SIZE_LOCAL=$(tar tzvf "$PACKAGE_FILE" ./data.tar.gz | awk '{ print $3 }') |
| 94 | fi |
| 95 | SIZE_UPSTREAM=$(package_size "$TMP_INDEX" "$PACKAGE") |
| 96 | SIZE_DIFF="$((SIZE_LOCAL - SIZE_UPSTREAM))" |
| 97 | TOTAL_DIFF="$((TOTAL_DIFF + SIZE_DIFF))" |
| 98 | if [ "$SIZE_DIFF" -gt 0 ]; then |
| 99 | SIZE_DIFF="+$SIZE_DIFF" |
| 100 | fi |
| 101 | if [ -n "$SIZE_UPSTREAM" ]; then |
| 102 | echo "${SIZE_DIFF} ${SIZE_LOCAL} ${SIZE_UPSTREAM} $PACKAGE" |
| 103 | else |
| 104 | echo "$PACKAGE is missing upstream" |
| 105 | fi |
| 106 | done |
| 107 | echo "~~~~~~~ total change ${TOTAL_DIFF}" |
| 108 | } |
| 109 | |
| 110 | if [ "$1" = "-h" ]; then |
| 111 | help |
| 112 | exit 0 |
| 113 | fi |
| 114 | |
| 115 | if [ "$1" = "-p" ]; then |
| 116 | CHECK_INSTALLED= |
| 117 | fi |
| 118 | |
| 119 | echo "Compare packages of $TARGET/$SUBTARGET/$ARCH": |
| 120 | echo "$PACKAGES" |
| 121 | echo |
| 122 | |
| 123 | echo "Checking configuration difference" |
| 124 | TMP_CONFIG=$(mktemp /tmp/config.XXXXXX) |
| 125 | sed -n 's/^ \+config \(.*\)/\1/p' config/Config-build.in config/Config-devel.in > "${TMP_CONFIG}-FOCUS" |
| 126 | sort .config | grep -f "${TMP_CONFIG}-FOCUS" | grep -v "^#" | sort > "${TMP_CONFIG}-LOCAL" |
| 127 | mv .config .config.bak |
| 128 | "$DOWNLOAD_METHOD" "$CONFIG_URL" > .config |
| 129 | make defconfig > /dev/null 2> /dev/null |
| 130 | grep -f "${TMP_CONFIG}-FOCUS" .config | grep -v "^#" | sort > "${TMP_CONFIG}-UPSTREAM" |
| 131 | mv .config.bak .config |
| 132 | |
| 133 | echo |
| 134 | echo " --- start config diff ---" |
| 135 | diff -u "${TMP_CONFIG}-LOCAL" "${TMP_CONFIG}-UPSTREAM" |
| 136 | echo " --- end config diff ---" |
| 137 | rm "${TMP_CONFIG}-FOCUS" "${TMP_CONFIG}-UPSTREAM" "${TMP_CONFIG}-LOCAL" |
| 138 | echo |
| 139 | |
| 140 | if [ -z "$CHECK_INSTALLED" ]; then |
| 141 | echo "Checking IPK package size" |
| 142 | else |
| 143 | echo "Checking installed size" |
| 144 | fi |
| 145 | echo |
| 146 | |
| 147 | echo "Fetching latest package indexes..." |
| 148 | TMP_INDEX=$(mktemp /tmp/size_compare_package_index.XXXXXX) |
| 149 | "$DOWNLOAD_METHOD" "$TARGET_URL" | gzip -d > "$TMP_INDEX" || exit 1 |
| 150 | "$DOWNLOAD_METHOD" "$PACKAGES_URL" | gzip -d >> "$TMP_INDEX" || exit 1 |
| 151 | echo |
| 152 | |
| 153 | echo "Comparing package sizes..." |
| 154 | echo "Change Local Remote Package" |
| 155 | compare_sizes | sort -g -r |
| 156 | |
| 157 | rm "$TMP_INDEX" |