Skip to content

Commit 088dd6f

Browse files
committed
Shrink musl images by stripping binaries
Signed-off-by: Ross Younger <[email protected]>
1 parent 50ccc96 commit 088dd6f

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

docker/lib.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,62 @@ docker_to_linux_arch() {
174174

175175
eval "${oldstate}"
176176
}
177+
178+
find_argument() {
179+
# Extracts the value from an argument of the form VARIABLE=VALUE
180+
local needle="$1"
181+
local return_var="$2"
182+
shift 2
183+
local prefix="${needle}="
184+
for var in "${@}"; do
185+
case "$var" in
186+
"$prefix"*)
187+
eval "$return_var=${var#"${prefix}"}"
188+
return 0 ;;
189+
*) ;;
190+
esac
191+
done
192+
echo "Missing argument ${needle}"
193+
exit 1
194+
}
195+
196+
symlinkify_if_same() {
197+
local file1="$1"
198+
local file2="$2"
199+
# Only make a symlink if the files are identical, and the destination file isn't already a symlink
200+
if [ ! -L "${file2}" ] && cmp "$file1" "$file2"; then
201+
ln -sf "$file1" "$file2"
202+
fi
203+
}
204+
205+
symlinkify_and_strip_toolchain() {
206+
local target="$1"
207+
local gcc_ver="$2"
208+
209+
local target_bin="/usr/local/${target}/bin"
210+
local local_bin="/usr/local/bin"
211+
212+
# The first set of tools appear as /usr/local/bin/<target>-<tool> and /usr/local/<target>/bin/<tool>
213+
214+
# Special case: ld is itself usually hardlinked to ld.bfd
215+
symlinkify_if_same "${local_bin}/ld" "${local_bin}/ld.bfd"
216+
217+
# Turn hard links or otherwise identical files into symlinks
218+
for tool in ar as ld ld.bfd nm objcopy objdump ranlib readelf strip; do
219+
local src="${local_bin}/${target}-${tool}"
220+
local dest="${target_bin}/${tool}"
221+
symlinkify_if_same "${src}" "${dest}"
222+
strip "${src}"
223+
done
224+
225+
# The second set of tools only appear as /usr/local/bin/<target>-<tool>
226+
227+
# Special case: c++ and g++ are usually the same file
228+
symlinkify_if_same "${local_bin}/${target}-c++" "${local_bin}/${target}-g++"
229+
# Special case: gcc and gcc-<version>
230+
symlinkify_if_same "${local_bin}/${target}-gcc" "${local_bin}/${target}-gcc-${gcc_ver}"
231+
232+
for tool in addr2line c++ c++filt cpp elfedit g++ gcc gcc-${gcc_ver} gcc-ar gcc-nm gcc-ranlib gcov gcov-dump gcov-tool gfortran gprof size strings; do
233+
strip "${local_bin}/${target}-${tool}"
234+
done
235+
}

docker/musl.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ main() {
3636
# Don't depend on the mirrors of sabotage linux that musl-cross-make uses.
3737
local linux_headers_site=https://ci-mirrors.rust-lang.org/rustc/sabotage-linux-tarballs
3838
local linux_ver=headers-4.19.88
39+
local gcc_ver=9.2.0
40+
local target
41+
find_argument TARGET target "${@}"
3942

4043
# alpine GCC is built with `--enable-default-pie`, so we want to
4144
# ensure we use that. we want support for shared runtimes except for
@@ -44,7 +47,7 @@ main() {
4447
# linked, so our behavior has maximum portability, and is consistent
4548
# with popular musl distros.
4649
hide_output make install "-j$(nproc)" \
47-
GCC_VER=9.2.0 \
50+
GCC_VER=${gcc_ver} \
4851
MUSL_VER=1.2.3 \
4952
BINUTILS_VER=2.33.1 \
5053
DL_CMD='curl --retry 3 -sSfL -C - -o' \
@@ -58,6 +61,14 @@ main() {
5861

5962
popd
6063

64+
symlinkify_and_strip_toolchain "${target}" "${gcc_ver}"
65+
66+
for dir in /usr/local/libexec/gcc/"${target}"/*; do
67+
pushd "${dir}" || exit 1
68+
strip cc1 cc1plus collect2 f951 lto1 lto-wrapper liblto_plugin.so.0.0.0
69+
popd || exit 1
70+
done
71+
6172
rm -rf "${td}"
6273
rm "${0}"
6374
}

0 commit comments

Comments
 (0)