| #!/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 |
| ## |
| |
| 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" |
| internal_sdk_deps_file="${TEMPDIR}/internal_sdk_deps_list" |
| non_sdk_deps_file="${TEMPDIR}/non_sdk_deps_list" |
| |
| touch ${dep_list_file} |
| touch ${partner_sdk_deps_file} |
| touch ${internal_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"* ]]; 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" ) |
| ;; |
| 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 core_deps=() |
| local platform_deps=() |
| local non_sdk=() |
| |
| local partner_sdk_deps="${TEMPDIR}/partner_sdk_all_deps" |
| local internal_sdk_deps="${TEMPDIR}/internal_sdk_all_deps" |
| |
| fx-info "Collecting Core SDK dependencies..." |
| fx-command-run gn desc "${build_dir}" "//sdk:core" deps --all > ${partner_sdk_deps} & |
| fx-info "Collecting Driver SDK dependencies..." |
| fx-command-run gn desc "${build_dir}" "//sdk:driver" deps --all >> ${partner_sdk_deps} & |
| fx-info "Collecting Platform SDK dependencies..." |
| fx-command-run gn desc "${build_dir}" "//sdk:platform" deps --all > ${internal_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 ${dep_sdk} ${partner_sdk_deps} | wc -l) -gt 0 ]]; then |
| echo "${dep}" >> "${partner_sdk_deps_file}" |
| continue |
| fi |
| |
| if [[ $(grep ${dep_sdk} ${internal_sdk_deps} | wc -l) -gt 0 ]]; then |
| echo "${dep}" >> "${internal_sdk_deps_file}" |
| continue |
| fi |
| |
| echo "${dep}" >> "${non_sdk_deps_file}" |
| done |
| |
| fx-info "Result:" |
| echo |
| echo " Partner (Including //sdk:core and //sdk:driver):" |
| sed -i -e 's/^/ /' ${partner_sdk_deps_file} |
| echo " Total items:" $(cat ${partner_sdk_deps_file} | wc -l) |
| cat ${partner_sdk_deps_file} |
| echo |
| echo " Internal (Including //sdk:platform):" |
| sed -i -e 's/^/ /' ${internal_sdk_deps_file} |
| echo " Total items:" $(cat ${internal_sdk_deps_file} | wc -l) |
| cat ${internal_sdk_deps_file} |
| echo |
| echo " Not in SDK:" |
| sed -i -e 's/^/ /' ${non_sdk_deps_file} |
| echo " Total items:" $(cat ${non_sdk_deps_file} | wc -l) |
| echo " Items containing 'banjo':" $(cat ${non_sdk_deps_file} | grep banjo | wc -l) |
| cat ${non_sdk_deps_file} |
| } |
| |
| main "$@" |