blob: 1ebf4035bf6f3d4356f6fd06201c63fac48908b3 [file] [log] [blame]
#!/bin/bash
#
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# 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.
# Find all the debian dependencies of a series of debian package,
# using a docker instance as base image. Just run the script to see its
# usage.
function get_transitive_deps() {
local deps="$*"
local transitive_deps="$(apt-cache depends -i $deps | grep 'Depends: ' | cut -d ':' -f 2)"
(echo "$deps"; echo "$transitive_deps") | tr ' ' '\n' | grep -v '^<' | sort -u
}
function list_dependencies() {
local deps="${1//,/ }"
local parent_deps="${2-}"
local transitive="$(get_transitive_deps $deps)"
while [ "$transitive" != "$deps" ]; do
deps="$transitive"
transitive="$(get_transitive_deps $deps)"
done
# Strip out already present dependencies
for dep in $transitive; do
if ! [[ "$parent_deps" =~ (^| )$dep( |$) ]]; then
echo -n "$dep "
fi
done
}
function get_package_info() {
apt-cache show $2 | grep '^'$1':' | head -1 | cut -d ':' -f 2 | xargs
}
function generate_workspace_dep() {
local package=$1
local sha256=$(get_package_info 'SHA256' $package)
local file=$(get_package_info 'Filename' $package)
echo " [\"${package}\", \"${file}\", \"${sha256}\"],"
}
function generate_list() {
local rule_name=$1
local loaded_packages="${2//,/ }"
shift 1
echo " '${rule_name}': ["
for i in "$@"; do
generate_workspace_dep $i
done
cat <<EOF
],
EOF
}
if (( $# < 2 )); then
echo "Usage: $0 [docker:image] rule_name package1,...,packageN [rule2:parent_rule package1,...,packageN]*" >&2
echo "If docker:image is specified then image is interpreted as the name of" >&2
echo "a docker image to run this script within. If it is not specified, then" >&2
echo "this script runs the debian package search on this machine it is invoked." >&2
echo "Additional image based on the generated image can be generatex by using the rule2:parent_rule syntax." >&2
exit 1
fi
if [[ "$1" =~ ^docker:(.*)$ ]]; then
# Run inside docker
shift 1
script_dir="$(cd $(dirname ${BASH_SOURCE[0]}) && pwd -P)"
docker run --rm -v "${script_dir}":/opt "${BASH_REMATCH[1]}" /bin/bash "/opt/$(basename "${BASH_SOURCE[0]}")" "$@"
exit $?
fi
apt-get update &>/dev/null
echo "# Generated by $0 $*"
echo
echo "DEBS = {"
while (( $# > 1 )); do
rule_name=${1%%:*}
parent_rule=${1##*:}
deps="$2"
shift 2
parent_deps=""
if [[ -n "$parent_rule" ]]; then
parent="DEPS_${parent_rule//-/_}"
parent_deps="${!parent}"
fi
deps=$(list_dependencies "$deps" "$parent_deps")
echo " # Dependencies for ${rule_name}."
eval "DEPS_${rule_name//-/_}='$deps $parent_deps'"
generate_list $rule_name $deps
done
echo "}"