Skip to content

Commit 62afc9e

Browse files
qt5: minimize build and wheel size (#57)
1 parent c55046f commit 62afc9e

File tree

1 file changed

+61
-19
lines changed

1 file changed

+61
-19
lines changed

qt5/build.sh

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ fi
6161
git -C qtbase-src fetch --depth 1 origin "$QT_TAG"
6262
git -C qtbase-src checkout --force FETCH_HEAD
6363

64-
# Build qtbase
64+
# Clean install dir and build artifacts to avoid stale cache issues
65+
rm -rf "$INSTALL_DIR"
66+
67+
# Build qtbase (disable modules cabana doesn't need)
6568
cd qtbase-src
69+
make distclean 2>/dev/null || true
6670
./configure \
6771
-release \
6872
-prefix "$INSTALL_DIR" \
@@ -86,32 +90,70 @@ git -C qtcharts-src checkout --force FETCH_HEAD
8690

8791
# Build qtcharts
8892
cd qtcharts-src
93+
make distclean 2>/dev/null || true
8994
"$INSTALL_DIR/bin/qmake"
9095
make -j"$NJOBS"
9196
make install
9297
cd "$DIR"
9398

94-
# Replace symlinks with copies for wheel compatibility
95-
find "$INSTALL_DIR" -type l | while read -r link; do
96-
target="$(readlink -f "$link")"
97-
if [ -f "$target" ]; then
98-
rm "$link"
99-
cp "$target" "$link"
100-
elif [ -d "$target" ]; then
101-
rm "$link"
102-
cp -r "$target" "$link"
103-
fi
104-
done
105-
106-
# Strip binaries and libraries
107-
find "$INSTALL_DIR" -type f -name '*.so*' -exec strip --strip-unneeded {} + 2>/dev/null || true
108-
find "$INSTALL_DIR" -type f -name '*.dylib' -exec strip -x {} + 2>/dev/null || true
109-
strip "$INSTALL_DIR/bin/moc" "$INSTALL_DIR/bin/rcc" "$INSTALL_DIR/bin/uic" 2>/dev/null || true
110-
111-
# Remove unnecessary files to reduce wheel size
99+
# Cleanup (don't let individual failures kill the build)
100+
set +e
112101
rm -rf "$INSTALL_DIR/doc" "$INSTALL_DIR/mkspecs" "$INSTALL_DIR/lib/cmake" "$INSTALL_DIR/lib/pkgconfig"
113102
find "$INSTALL_DIR/lib" -name '*.prl' -delete 2>/dev/null || true
114103
find "$INSTALL_DIR/lib" -name '*.la' -delete 2>/dev/null || true
115104

105+
# Remove unnecessary binaries (qmake alone is 28MB, only needed for qtcharts build above)
106+
find "$INSTALL_DIR/bin" -not -name moc -not -name rcc -not -name uic -not -type d -delete 2>/dev/null || true
107+
108+
if [[ "$(uname)" == "Linux" ]]; then
109+
# Remove unnecessary shared libs (keep only what cabana links)
110+
KEEP_LIBS="Qt5Core Qt5Gui Qt5Widgets Qt5Charts Qt5OpenGL Qt5XcbQpa Qt5EglFSDeviceIntegration Qt5EglFsKmsSupport"
111+
for f in "$INSTALL_DIR/lib/"lib*.so; do
112+
name="${f##*/lib}"; name="${name%.so}"
113+
echo "$KEEP_LIBS" | grep -qw "$name" || rm -f "$INSTALL_DIR/lib/lib${name}".so*
114+
done
115+
116+
# Remove unnecessary include dirs
117+
KEEP_INCLUDES="QtCore QtGui QtWidgets QtCharts QtOpenGL"
118+
for d in "$INSTALL_DIR/include/"*/; do
119+
name="$(basename "$d")"
120+
echo "$KEEP_INCLUDES" | grep -qw "$name" || rm -rf "$d"
121+
done
122+
123+
# Remove static libs
124+
find "$INSTALL_DIR/lib" -maxdepth 1 -name '*.a' -delete
125+
126+
# Replace symlinks with copies (wheels can't store symlinks)
127+
find "$INSTALL_DIR" -type l | while read -r link; do
128+
target="$(readlink -f "$link")"
129+
if [ -f "$target" ]; then
130+
rm "$link"; cp "$target" "$link"
131+
elif [ -d "$target" ]; then
132+
rm "$link"; cp -r "$target" "$link"
133+
fi
134+
done
135+
136+
# Deduplicate versioned .so: keep only .so and .so.5 (SONAME)
137+
find "$INSTALL_DIR/lib" -maxdepth 1 -name 'lib*.so.5.15.18' -delete
138+
find "$INSTALL_DIR/lib" -maxdepth 1 -name 'lib*.so.5.15' -delete
139+
140+
# Strip
141+
find "$INSTALL_DIR" -type f \( -name '*.so*' -o -path '*/bin/*' \) -exec strip --strip-unneeded {} + 2>/dev/null || true
142+
else
143+
# macOS: replace symlinks, strip frameworks
144+
find "$INSTALL_DIR" -type l | while read -r link; do
145+
target="$(readlink -f "$link")"
146+
if [ -f "$target" ]; then
147+
rm "$link"; cp "$target" "$link"
148+
elif [ -d "$target" ]; then
149+
rm "$link"; cp -r "$target" "$link"
150+
fi
151+
done
152+
find "$INSTALL_DIR" -type f -name '*.dylib' -exec strip -x {} + 2>/dev/null || true
153+
find "$INSTALL_DIR/bin" -type f -exec strip -x {} + 2>/dev/null || true
154+
find "$INSTALL_DIR/lib" -maxdepth 1 -name '*.a' -delete 2>/dev/null || true
155+
fi
156+
157+
set -e
116158
echo "Installed Qt5 to $INSTALL_DIR"
117159
du -sh "$INSTALL_DIR"

0 commit comments

Comments
 (0)