-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prune unused artifacts from non-static builds #59
Draft
henriquesimoes
wants to merge
7
commits into
main
Choose a base branch
from
prune
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e643d8e
ioc: prune modules from non-static builds.
henriquesimoes cc98b04
ioc: remove static libraries from non-static builds.
henriquesimoes 63ee161
ioc: prune shared libraries from non-static builds.
henriquesimoes 1680f69
ioc: prune module directories from non-static builds.
henriquesimoes 2ae76d3
ioc: prune EPICS base directories from non-static builds.
henriquesimoes 2b46de5
ioc: don't prune pre-selected directories
henriquesimoes 0a9c79e
base: don't prune pyDevSup custom python libraries
henriquesimoes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -Eeu | ||
|
||
filter_out_paths() { | ||
list="$1" | ||
exclude_list="$2" | ||
|
||
for path in $exclude_list; do | ||
if [ "${path:0:1}" != "/" ]; then | ||
>&2 echo "error: filter_out_paths() expects absolute paths, but got '$path'" | ||
exit 1 | ||
fi | ||
|
||
while [ "$path" != "/" ]; do | ||
list=$(echo "$list" | grep -xv $path) | ||
|
||
path=$(dirname $path) | ||
done | ||
done | ||
|
||
echo "$list" | ||
} | ||
|
||
find_elf_executables() { | ||
targets=$@ | ||
|
||
# Loop on entire lines to properly handle filenames with spaces | ||
while read -r executable; do | ||
read -r -N 4 magic < "$executable" | ||
|
||
# Output only ELF binaries | ||
if [ "$magic" = $'\x7fELF' ]; then | ||
echo $executable | ||
fi | ||
done < <(find $targets -type f -executable) | ||
} | ||
|
||
find_shared_libs() { | ||
libs=$(find_elf_executables $@) | ||
|
||
echo "$libs" | grep -E "*.so(.[0-9]+)*$" | sort -u | ||
} | ||
|
||
find_linked_libraries() { | ||
executables=$(find_elf_executables $@) | ||
|
||
# Depend on the glibc-specific behavior of supporting multiple executables | ||
# to be queried at once | ||
linked=$(ldd $executables 2>/dev/null | grep '=>') | ||
|
||
# We grep out not found libraries, since they cannot be kept if we don't | ||
# know where they are. | ||
# | ||
# Final binary may be actually runnable, since rpath of another binary may | ||
# pull those not-found libraries | ||
found="$(echo "$linked" | grep -v "not found")" | ||
|
||
# Get their full path | ||
libs=$(echo "$found" | cut -d' ' -f 3) | ||
|
||
echo "$libs" | sort -u | ||
} | ||
|
||
get_all_epics_modules() { | ||
release_defs=$(grep = ${EPICS_RELEASE_FILE} | cut -d'=' -f 2) | ||
|
||
echo "$release_defs" | grep $EPICS_MODULES_PATH | ||
} | ||
|
||
get_used_epics_modules() { | ||
linked_libs=$(find_linked_libraries $@) | ||
all_modules=$(get_all_epics_modules) | ||
|
||
unused_modules=$(filter_out_paths "$all_modules" "$linked_libs") | ||
|
||
filter_out_paths "$all_modules" "$unused_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) | ||
" | ||
|
||
while read -r candidate; 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 < <(find $module -type d) | ||
} | ||
|
||
clean_up_epics_modules() { | ||
targets=$@ | ||
|
||
all_modules=$(get_all_epics_modules) | ||
used_modules=$(get_used_epics_modules $targets) | ||
|
||
keep_dirs="$targets $used_modules" | ||
unused_modules=$(filter_out_paths "$all_modules" "$keep_dirs") | ||
|
||
# Assume module paths do not contain spaces, as we mostly control them | ||
for module in $unused_modules; do | ||
# if we already removed it because of its top-level repository or | ||
# because it is an IOC, move on to the next. | ||
[ ! -d $module ] && continue | ||
|
||
size=$(du -hs $module | cut -f 1) | ||
|
||
echo "Removing module '$module' ($size)..." | ||
rm -rf $module | ||
done | ||
|
||
prune_dirs=$(filter_out_paths "$used_modules" "$targets") | ||
|
||
for dir in $prune_dirs; do | ||
echo "Pruning module '$dir'..." | ||
prune_module_dirs $dir | ||
done | ||
|
||
prune_module_dirs $EPICS_BASE_PATH | ||
} | ||
|
||
remove_static_libs() { | ||
for target; do | ||
libs=$(find $target -type f -name *.a) | ||
|
||
if [ -n "$libs" ]; then | ||
size=$(du -hsc $libs | tail -n 1 | cut -f 1) | ||
|
||
echo "Removing static libraries from $target ($size)" | ||
rm -f $libs | ||
fi | ||
done | ||
} | ||
|
||
remove_unused_shared_libs() { | ||
target_libs=$(find_shared_libs $@) | ||
linked_libs=$(find_linked_libraries $@) | ||
remove_libs=$(find_shared_libs /opt /usr/local) | ||
|
||
for lib in $target_libs $linked_libs; do | ||
remove_libs=$(echo "$remove_libs" | grep -vx $lib) | ||
done | ||
|
||
for lib in $remove_libs; do | ||
size=$(du -hs $lib | cut -f 1) | ||
|
||
echo "Removing shared library '$lib' ($size)" | ||
rm -f ${lib%.so*}.so* | ||
done | ||
} | ||
|
||
clean_up_epics_modules $@ | ||
remove_static_libs /opt | ||
remove_unused_shared_libs $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added this last so that image cache is not invalidated every time for nothing. It can be moved to be side-by-side with
lnls-run
after the script ready to be merged.