From b1470877be5a2551451e0e724bc700c148da4ddd Mon Sep 17 00:00:00 2001 From: "Henrique F. Simoes" Date: Thu, 11 Apr 2024 17:00:00 -0300 Subject: [PATCH] ioc: prune shared libraries from non-static builds. Shared libraries may be installed either in /opt or /usr/local/lib but may not be used by the target binaries from the IOC image. Remove them during the prune phase before copying both directories to resulting image to shrink the final image size. A regular expression is needed to filter the existing libraries, as Solaris configuration files match the trivial ".so*" expression. --- base/lnls-prune-artifacts.sh | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/base/lnls-prune-artifacts.sh b/base/lnls-prune-artifacts.sh index 7106968..8f327c8 100755 --- a/base/lnls-prune-artifacts.sh +++ b/base/lnls-prune-artifacts.sh @@ -4,6 +4,16 @@ set -eu target_paths=$@ +get_linked_libraries() { + targets=$@ + + executables=$(find $targets -type f -executable -print) + + libs="$(ldd $executables 2>/dev/null | grep '=>' | cut -d'=' -f 1)" + for lib in $libs; do + echo ${lib%.so*}.so + done | sort | uniq +} get_used_epics_modules() { targets=$@ @@ -40,6 +50,26 @@ remove_static_libs() { done } +remove_unused_shared_libs() { + targets=$@ + + remove_libs=$(find /opt /usr/local -type f -executable -name *.so* -print) + remove_libs=$(echo "$remove_libs" | grep -E "*.so(.[0-9]+)*$" | sort | uniq) + + used_libs=$(get_linked_libraries $targets) + + for lib in $used_libs; do + remove_libs=$(echo "$remove_libs" | grep -v $lib) + done + + for lib in $remove_libs; do + size=$(du -hs $lib | cut -f 1) + + echo "Removing shared library '$lib' ($size)" + rm -f $lib + done +} + remove_unused_epics_modules() { targets=$@ @@ -57,3 +87,4 @@ remove_unused_epics_modules() { remove_unused_epics_modules $target_paths remove_static_libs /opt +remove_unused_shared_libs $target_paths