Skip to content

Commit

Permalink
ioc: prune shared libraries from non-static builds.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
henriquesimoes committed Oct 15, 2024
1 parent 7e3ea8a commit 1a684b3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions base/lnls-prune-artifacts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
set -eu


get_linked_libraries() {
targets=$@

executables=$(find $targets -type f -executable)

# Depend on the glibc-specific behavior of supporting multiple executables
# to be queried at once
libs="$(ldd $executables 2>/dev/null | grep '=>' | cut -d'=' -f 1)"

for lib in $libs; do
# Strip version from soname so that we match any version of the library
# in use
echo ${lib%.so*}.so
done | sort -u
}

get_used_epics_modules() {
targets=$@

Expand Down Expand Up @@ -42,6 +58,26 @@ remove_static_libs() {
done
}

remove_unused_shared_libs() {
targets=$@

used_libs=$(get_linked_libraries $targets)

remove_libs=$(find /opt /usr/local -type f -executable -name *.so*)
remove_libs=$(echo "$remove_libs" | grep -E "*.so(.[0-9]+)*$" | sort -u)

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=$@

Expand All @@ -59,3 +95,4 @@ remove_unused_epics_modules() {

remove_unused_epics_modules $@
remove_static_libs /opt
remove_unused_shared_libs $@

0 comments on commit 1a684b3

Please sign in to comment.