-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add utility to prune unused go library targets #6149
Merged
+158
−0
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#!/bin/bash | ||
# Copyright 2018 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
# Identifies go_library() bazel targets in the workspace with no dependencies. | ||
# Deletes the target and its files. Cleans up any :all-srcs references | ||
|
||
# By default just checks for extra libraries, run with --fix to make changes. | ||
|
||
|
||
set -o nounset | ||
set -o errexit | ||
set -o pipefail | ||
|
||
# Output every go_library rule in the repo | ||
find-go-libraries() { | ||
# Excluding generated //foo:go_default_library~library~ | ||
bazel query \ | ||
'kind("go_library rule", //...)' \ | ||
except 'attr("name", ".*~library~", //...)' | ||
} | ||
|
||
# Output the target if it has only one dependency (itself) | ||
is-unused() { | ||
local n | ||
n="$(bazel query "rdeps(//..., \"$1\", 1)" | wc -l)" | ||
if [[ "$n" -gt "1" ]]; then # Always counts itself | ||
echo "LIVE: $1" >&2 | ||
return 0 | ||
fi | ||
echo "DEAD: $1" >&2 | ||
echo "$1" | ||
} | ||
|
||
# Filter down to unused targets | ||
filter-to-unused() { | ||
for i in "$@"; do | ||
is-unused "$i" | ||
done | ||
} | ||
|
||
# Output sources used by target | ||
target-sources() { | ||
for i in "$@"; do | ||
bazel query "kind(\"source file\", deps(\"$(package-name "$i"):package-srcs\", 1))" | ||
done | ||
} | ||
|
||
# Convert //foo/bar:stuff to //foo/bar | ||
package-name() { | ||
echo $1 | cut -d : -f 1 | ||
} | ||
|
||
# Find every package that directly depends on //foo:all-srcs | ||
all-srcs-refs() { | ||
build-files $(for i in "$@"; do | ||
local package | ||
package="$(dirname "$i")" | ||
bazel query "rdeps(//..., \"${package}:all-srcs\", 1)" | ||
done) | ||
} | ||
|
||
# Convert //foo/bar:stuff to //foo/bar/BUILD | ||
build-files() { | ||
(for i in "$@"; do | ||
echo $(package-name "$i")/BUILD | ||
done) | sort -u | ||
} | ||
|
||
# Remove lines with //something:all-srcs from files | ||
# Usage: | ||
# remove-all-srcs <targets-to-remove> <remove-from-packages> | ||
remove-all-srcs() { | ||
for b in $1; do | ||
sed -i -e "\|$(dirname "$b"):all-srcs|d" $(to-path $2) | ||
done | ||
} | ||
|
||
# For each arg //foo/bar:whatever.sh becomes foo/bar/whatever.sh | ||
to-path() { | ||
for i in "$@"; do | ||
# Format is //foo/bar:whatever.sh | ||
# Output foo/bar/whatever.sh | ||
local base dir | ||
base=$(basename $i) | ||
dir=$(dirname $i) | ||
echo "${dir:2}/${base/:/\/}" | ||
done | ||
} | ||
|
||
# Global variable to track when we are finished | ||
remove_unused_go_libraries_finished= | ||
# Remove every go_library() target from workspace with no one depending on it. | ||
remove-unused-go-libraries() { | ||
local libraries deps | ||
libraries="$(find-go-libraries | sort -u)" | ||
deps="$(filter-to-unused $libraries)" | ||
if [[ -z "$deps" ]]; then | ||
echo "All remaining go libraries are alive" >&2 | ||
remove_unused_go_libraries_finished=true | ||
return 0 | ||
elif [[ "$1" == "--check" || "$1" != "--fix" ]]; then | ||
echo "Found unused libraries:" >&2 | ||
for d in $deps; do | ||
echo $d | ||
done | ||
exit 1 | ||
else | ||
echo Cleaning up unused dependencies... >&2 | ||
fi | ||
local builds files ref_builds | ||
builds=$(build-files $deps) | ||
files=$(target-sources $deps) | ||
ref_builds=$(all-srcs-refs $builds) | ||
|
||
pushd "$(dirname ${BASH_SOURCE})/.." | ||
remove-all-srcs "$builds" "$ref_builds" | ||
git rm $(to-path $files | sort -u) | ||
git rm -f $(to-path $builds | sort -u) | ||
popd | ||
} | ||
|
||
# Continue until we do not remove anything | ||
while [[ -z $remove_unused_go_libraries_finished ]]; do | ||
remove-unused-go-libraries "${1:-}" | ||
done |
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,20 @@ | ||
#!/bin/bash | ||
# Copyright 2018 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
script="$(dirname ${BASH_SOURCE})/prune-libraries.sh" --check | ||
if ! "$script" ; then | ||
echo "Run $script --fix" | ||
exit 1 | ||
fi |
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.
There's probably a potential word-splitting bug here without quotes around
$1
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.
Ack