1#!/usr/bin/bash 2# Copyright 2020 The Bazel Authors. All rights reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16# queryview-bottom-up.sh: A query script to strategize the bottom-up migration 17# approach of any target in the queryview workspace. 18# 19# Usage: ./queryview-bottom-up.sh <label of target in queryview> 20# 21# You can generate the queryview workspace using "m queryview". Note that the 22# queryview targets represent the fully configured build graph of Soong, so the 23# results may differ based on the product configuration and environment which 24# the queryview workspace was created in. 25# 26# This script obtains the transitive closure of a target, sorts its dependencies 27# based on minrank (shortest path from root to dep), and gets their module type. 28# This can help to figure out the exact module types we need to work on 29# converting first, 30# 31# This script produces the following (a sample): 32# 33# 0 //bionic/libc:libc--android_arm_armv7-a-neon_shared cc_library 34# 1 //bionic/libc:bionic_libc_license license 35# 1 //bionic/libc:libc--android_arm_armv7-a-neon_shared_29 cc_library 36# 1 //bionic/libc:libc--android_arm_armv7-a-neon_shared_30 cc_library 37# 1 //bionic/libc:libc--android_arm_armv7-a-neon_shared_current cc_library 38# 1 //bionic/libc:libc--android_arm_armv7-a-neon_static cc_library 39# 1 //bionic/libc:libc.arm.map genrule 40# 1 //bionic/libc:libc_common_shared--android_arm_armv7-a-neon_static cc_library_static 41# 1 //bionic/libc:libc_defaults cc_defaults 42# 1 //bionic/libc:libc_headers--android_arm_armv7-a-neon cc_library_headers 43# 1 //bionic/libc:libc_init_dynamic--android_arm_armv7-a-neon_static cc_library_static 44# 1 //bionic/libc:libc_native_allocator_defaults cc_defaults 45# 1 //bionic/libc:libc_sources_shared_arm filegroup 46# 1 //bionic/libc:libc_sources_shared filegroup 47# 1 //bionic/libc:libc_sources_static filegroup 48# 1 //bionic/libdl:libdl_android--android_arm_armv7-a-neon_static cc_library 49# 1 //bionic/libdl:libdl--android_arm_armv7-a-neon_shared cc_library 50# 1 //bionic/linker:ld-android--android_arm_armv7-a-neon_shared cc_library 51# 1 //external/gwp_asan:gwp_asan--android_arm_armv7-a-neon_static cc_library_static 52# 1 //external/gwp_asan:gwp_asan_headers--android_arm_armv7-a-neon cc_library_headers 53# 1 //external/scudo:libscudo--android_arm_armv7-a-neon_static cc_library_static 54# 1 //prebuilts/clang/host/linux-x86:libclang_rt.builtins-arm-android-exported--android_arm_armv7-a-neon_static libclang_rt_prebuilt_library_static 55# 1 //prebuilts/clang/host/linux-x86:libunwind-exported--android_arm_armv7-a-neon_static llvm_prebuilt_library_static 56# 1 //prebuilts/runtime/mainline/runtime/sdk:prebuilt_libc--android_arm_armv7-a-neon_shared cc_prebuilt_library_shared 57# 2 //bionic/libc:bionic_libc_platform_headers--android_arm_armv7-a-neon cc_library_headers 58# 2 //bionic/libc:crtbegin_so--android_arm_armv7-a-neon cc_object 59# 2 //bionic/libc:crtend_so--android_arm_armv7-a-neon cc_object 60# 2 //bionic/libc:libc_common--android_arm_armv7-a-neon_static cc_library_static 61# 2 //bionic/libc:libc_common_static--android_arm_armv7-a-neon_static cc_library_static 62# 2 //bionic/libc:libc_dynamic_dispatch--android_arm_armv7-a-neon_static cc_library_static 63# ... 64 65set -euo pipefail 66 67T=${1:-//bionic/libc:libc--android_arm_armv7-a-neon_shared} 68COMMON_BAZEL_OPTS="--noshow_loading_progress --color=no --curses=no" 69 70declare -a label_array 71declare -a minrank_array 72declare -a label_kind_array 73 74# Obtain the sorted list of labels in the transitive dependency closure of T. 75label_array=$(bazel query --config=queryview "deps($T)" --output=label $COMMON_BAZEL_OPTS 2>/dev/null | sort) 76 77# Obtain the sorted list of ranks based on the label as sort key. The rank value is the minimum distance between the root node to the dep. 78minrank_array=$(bazel query --config=queryview "deps($T)" --output=minrank $COMMON_BAZEL_OPTS 2>/dev/null | awk -e '{print $2 " " $1}' | sort | awk -e '{print $2}') 79 80# Obtain the sorted list of module types (label kinds) based on the label as sort key. 81label_kind_array=$(bazel query --config=queryview "deps($T)" --output=label_kind $COMMON_BAZEL_OPTS 2>/dev/null | awk -e '{print $3 " " $1}' | sort | awk -e '{print $2}') 82 83# Zip the lists together, tabularize it, and sort the list based on the rank 84# value of the dep. A rank value of 0 represents the root node. 85paste <(echo "${minrank_array[*]}") <(echo "${label_array[*]}") <(echo "${label_kind_array[*]}") | column -t | sort -n 86