blob: 8417d25ff26b82863b8fbb333d01fa53609dbaa3 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001# 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
24eval "$(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)"
31CONFIG_PACKAGES=$(sed -n 's/^CONFIG_PACKAGE_\(.*\)=y$/\1/p' .config | tr '\n' ' ')
32
33TARGET=${TARGET:-$CONFIG_TARGET_BOARD}
34SUBTARGET=${SUBTARGET:-$CONFIG_TARGET_SUBTARGET}
35ARCH=${ARCH:-$CONFIG_TARGET_ARCH_PACKAGES}
36PACKAGES=${PACKAGES:-$CONFIG_PACKAGES}
37BIN_DIR=${CONFIG_BINARY_FOLDER:-./bin}
38BASE_URL="${BASE_URL:-https://downloads.openwrt.org/snapshots}"
39CHECK_INSTALLED="${CHECK_INSTALLED:-y}"
40
41TARGET_URL="$BASE_URL/targets/$TARGET/$SUBTARGET/packages/Packages.gz"
42CONFIG_URL="$BASE_URL/targets/$TARGET/$SUBTARGET/config.buildinfo"
43PACKAGES_URL="$BASE_URL/packages/$ARCH/base/Packages.gz"
44
45if command -v curl > /dev/null; then
46 DOWNLOAD_METHOD="curl"
47else
48 DOWNLOAD_METHOD="wget --output-document=-"
49fi
50
51help() {
52 sed -rn 's/^### ?//;T;p' "$0"
53}
54
55package_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
77compare_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
110if [ "$1" = "-h" ]; then
111 help
112 exit 0
113fi
114
115if [ "$1" = "-p" ]; then
116 CHECK_INSTALLED=
117fi
118
119echo "Compare packages of $TARGET/$SUBTARGET/$ARCH":
120echo "$PACKAGES"
121echo
122
123echo "Checking configuration difference"
124TMP_CONFIG=$(mktemp /tmp/config.XXXXXX)
125sed -n 's/^ \+config \(.*\)/\1/p' config/Config-build.in config/Config-devel.in > "${TMP_CONFIG}-FOCUS"
126sort .config | grep -f "${TMP_CONFIG}-FOCUS" | grep -v "^#" | sort > "${TMP_CONFIG}-LOCAL"
127mv .config .config.bak
128"$DOWNLOAD_METHOD" "$CONFIG_URL" > .config
129make defconfig > /dev/null 2> /dev/null
130grep -f "${TMP_CONFIG}-FOCUS" .config | grep -v "^#" | sort > "${TMP_CONFIG}-UPSTREAM"
131mv .config.bak .config
132
133echo
134echo " --- start config diff ---"
135diff -u "${TMP_CONFIG}-LOCAL" "${TMP_CONFIG}-UPSTREAM"
136echo " --- end config diff ---"
137rm "${TMP_CONFIG}-FOCUS" "${TMP_CONFIG}-UPSTREAM" "${TMP_CONFIG}-LOCAL"
138echo
139
140if [ -z "$CHECK_INSTALLED" ]; then
141 echo "Checking IPK package size"
142else
143 echo "Checking installed size"
144fi
145echo
146
147echo "Fetching latest package indexes..."
148TMP_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
151echo
152
153echo "Comparing package sizes..."
154echo "Change Local Remote Package"
155compare_sizes | sort -g -r
156
157rm "$TMP_INDEX"