From affe3026c44960036c65dda62948534810c15ead Mon Sep 17 00:00:00 2001 From: "Henrique F. Simoes" Date: Tue, 16 Apr 2024 08:41:20 -0300 Subject: [PATCH] ioc: prune module directories from non-static builds. Modules may contain several artifacts, including configuration files, graphical interface files and other repository artifacts that do not need to be in the IOC image. Remove them all except the ones containing EPICS database (`.db` and `.template`) or autosave requirement (`.req`) files, besides shared libraries. `bin` is also removed, as only $REPONAME and $RUNDIR should contain target executables. --- base/lnls-prune-artifacts.sh | 38 +++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/base/lnls-prune-artifacts.sh b/base/lnls-prune-artifacts.sh index a120ffb..be3694d 100755 --- a/base/lnls-prune-artifacts.sh +++ b/base/lnls-prune-artifacts.sh @@ -19,6 +19,14 @@ get_linked_libraries() { done | sort -u } +find_shared_libs() { + search_paths=$@ + + libs=$(find $search_paths -type f -executable -name *.so*) + + echo "$libs" | grep -E "*.so(.[0-9]+)*$" | sort -u +} + get_used_epics_modules() { targets=$@ @@ -62,9 +70,7 @@ 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) + remove_libs=$(find_shared_libs /opt /usr/local) for lib in $used_libs; do remove_libs=$(echo "$remove_libs" | grep -v $lib) @@ -78,7 +84,27 @@ remove_unused_shared_libs() { done } -remove_unused_epics_modules() { +prune_module_dirs() { + module=$1 + + keep_paths=" + $(find_shared_libs $module) + $(find $module -type f -regex ".*\.\(db\|template\|req\)" -printf "%h\n" | sort -u) + " + + for candidate in $(find $module -type d); do + [ -d $candidate ] || continue + + if [[ ! $keep_paths =~ "$candidate".* ]]; then + size=$(du -hs $candidate | cut -f 1) + + printf "Removing directory '$candidate' ($size)...\n" + rm -rf $candidate + fi + done +} + +clean_up_epics_modules() { targets=$@ used_modules=$(get_used_epics_modules $targets) @@ -89,10 +115,12 @@ remove_unused_epics_modules() { echo "Removing module '$module' ($size)..." rm -rf $module + elif [ -d $module/lib ]; then + prune_module_dirs $module fi done } -remove_unused_epics_modules $@ +clean_up_epics_modules $@ remove_static_libs /opt remove_unused_shared_libs $@