blob: beea0c1d3090b59dd5a7982b08d806a42a1ca733 [file] [log] [blame]
#!/bin/bash
# Copyright 2024 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Other
### List the dependencies of a target, and if dependencies are in SDK
## usage: fx sdk-deps [Options]
##
## Options:
## -t < target> : List all the deps of this target
## -d < dep> : Determine if the dependency is in SDK
## -o < filename> : Write output to CSV file. Default: sdk_deps_out.csv
set -o errexit
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh
fx-config-read
readonly TEMPDIR="tmp"
mkdir "${TEMPDIR}"
trap 'rm -rf "$TEMPDIR"' EXIT
dep_list_file="${TEMPDIR}/dep_list"
partner_sdk_deps_file="${TEMPDIR}/partner_sdk_deps_list"
non_sdk_deps_file="${TEMPDIR}/non_sdk_deps_list"
output_csv="sdk_deps_out.csv" # Default CSV filename
touch ${dep_list_file}
touch ${partner_sdk_deps_file}
touch ${non_sdk_deps_file}
collect_deps() {
local target=$1
local prefix=$(echo ${target} | cut -d ":" -f1)
local deps=($(fx-command-run gn desc "${build_dir}" "${target}" deps || continue))
for dep in "${deps[@]}"; do
# We skip some deps
if [[ "${dep}" == *"shlib-allowlist-check"* \
|| "${dep}" == *"static-libc++-deps"* \
|| "${dep}" == *"tools/bindc:bin"* \
|| "${dep}" == *"zircon/public/sysroot"* \
|| "${dep}" != "//"* ]] ; then
continue
fi
if [[ "${dep}" == ${prefix}* ]]; then
collect_deps $dep
else
echo "${dep}" | xargs >> ${dep_list_file}
fi
done
}
main() {
local build_dir=$(fx-command-run get-build-dir)
fx-info "Using build dir: ${build_dir}"
local targets=()
local sdk_deps=()
while [[ $# -ge 1 ]]; do
case "$1" in
-t)
shift
targets+=( "$1" )
;;
-d)
shift
sdk_deps+=( "$1" )
;;
-o)
shift
output_csv="$1"
;;
esac
shift
done
if [[ "${#targets[@]}" -eq 0 && "${#sdk_deps[@]}" -eq 0 ]]; then
echo "Either a target that contains driver or a dependency needs to be passed in."
fx-command-help sdk-deps
exit 1
fi
if [[ "${#targets[@]}" -gt 0 ]]; then
local distribution_manifests=()
# Use `gn meta` to walk the metadata and select all items with
# `driver_package_component_files`. This is mandatory all driver.
for target in "${targets[@]}"; do
distribution_manifests+=( $(fx-command-run gn meta "${build_dir}" --data=driver_package_component_files "${target}" 2>/dev/null | grep "distribution_manifest = " | cut -d "\"" -f2 | sed -e "s|.*|${build_dir}/&|") )
done
# Collect all the drivers from the distribution
if [[ "${#targets[@]}" -gt 0 ]]; then
local drivers=($(fx-command-run jq -r '.[] | select((.destination|startswith("driver"))) | .label' "${distribution_manifests[@]}" | sort -u))
fi
# Collect all the deps
fx-info "Listing all drivers and collect deps..."
echo
for driver in "${drivers[@]}"; do
# Don't analyze the compat driver
if [[ "${driver}" == *"src/devices/misc/drivers/compat:driver"* ]]; then
continue
fi
echo " ${driver}"
collect_deps $driver &
done
echo
fi
# Also collect the deps directly passed in
for dep in "${sdk_deps[@]}"; do
echo "${dep}" | xargs >> ${dep_list_file}
done
local non_sdk=()
local partner_sdk_deps="${TEMPDIR}/partner_sdk_all_deps"
fx-info "Collecting Core SDK dependencies..."
fx-command-run gn desc "${build_dir}" "//sdk:core" deps --all > ${partner_sdk_deps} &
wait $(jobs -rp)
for dep in $(cat ${dep_list_file} | sort -u); do
if [[ "${dep}" == *"sdk/fidl"* ]]; then
fidl="$(echo $dep | cut -d ":" -f1 | cut -d "/" -f5)"
dep_sdk="//sdk/fidl/${fidl}:${fidl}_sdk"
elif [[ "${dep}" == *"src/devices/bind"* ]]; then
bind="$(echo $dep | cut -d ":" -f1 | cut -d "/" -f6)"
dep_sdk="//src/devices/bind/${bind}:${bind}_sdk"
else
dep_sdk="$(echo ${dep} | cut -d "(" -f1)_sdk"
fi
if [[ $(grep -c ${dep_sdk} ${partner_sdk_deps}) -gt 0 ]]; then
echo "${dep}" >> "${partner_sdk_deps_file}"
continue
fi
echo "${dep}" >> "${non_sdk_deps_file}"
done
# Calculate non sdk total items
num_non_sdk_total_items=$(cat ${non_sdk_deps_file} | wc -l)
# Calculate items containing banjo
num_total_banjo=$(cat ${non_sdk_deps_file} | grep banjo | wc -l)
# Calculate total number of drivers
num_drivers="${#drivers[@]}"
# Calculate total number of parter sdk deps
num_partner_sdk_deps=$(cat ${partner_sdk_deps_file} | wc -l)
# Print to console
fx-info "Result:"
echo
echo " In SDK:"
sed -i -e 's/^/ /' ${partner_sdk_deps_file}
echo " Total items:" ${num_partner_sdk_deps}
cat ${partner_sdk_deps_file}
echo
echo " Not in SDK:"
sed -i -e 's/^/ /' ${non_sdk_deps_file}
echo " Total items:" ${num_non_sdk_total_items}
echo " Items containing 'banjo':" ${num_total_banjo}
cat ${non_sdk_deps_file}
# Write to CSV with headers
echo "date_time_run,number_drivers,number_partner_sdk_deps,number_non_sdk_total_items,number_total_banjo" > "${output_csv}"
echo "$(date +"%Y-%m-%d %H:%M:%S"),${num_drivers},${num_partner_sdk_deps},${num_non_sdk_total_items},${num_total_banjo}" >> "${output_csv}"
fx-info "CSV output written to: ${output_csv}"
}
main "$@"