From 62054a43fa0c5a7f708b636806ccc09636b985f5 Mon Sep 17 00:00:00 2001 From: Steven Moreland Date: Thu, 6 Dec 2018 10:11:40 -0800 Subject: [PATCH] Add functions to jump to specific modules. Usage: ...aosp/$ gomod libbase ...aosp/system/core/base$ Using in alias: alias g=gomod complete -F _complete-android-module-names g ...aosp/$ g libbase ...aosp/system/core/base$ Nothing in Android keeps an up to date index of all Android modules and their locations between builds. However, there is a target 'module-info.json' which is used to contain some of that information. This module can be updated using the command 'refreshmod' added here, but it will also be updated during a normal Android build. This also adds auto-complete for m. Bug: N/A Test: manual Change-Id: I06b0b87c308b9fe2798dbc29225906cc30fe59fe --- envsetup.sh | 121 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 99 insertions(+), 22 deletions(-) diff --git a/envsetup.sh b/envsetup.sh index 400e7f03c..383b4e246 100644 --- a/envsetup.sh +++ b/envsetup.sh @@ -4,28 +4,31 @@ cat <- - Selects as the product to build, and as the variant to - build, and stores those selections in the environment to be read by subsequent - invocations of 'm' etc. -- tapas: tapas [ ...] [arm|x86|mips|arm64|x86_64|mips64] [eng|userdebug|user] -- croot: Changes directory to the top of the tree. -- m: Makes from the top of the tree. -- mm: Builds all of the modules in the current directory, but not their dependencies. -- mmm: Builds all of the modules in the supplied directories, but not their dependencies. - To limit the modules being built use the syntax: mmm dir/:target1,target2. -- mma: Builds all of the modules in the current directory, and their dependencies. -- mmma: Builds all of the modules in the supplied directories, and their dependencies. -- provision: Flash device with all required partitions. Options will be passed on to fastboot. -- cgrep: Greps on all local C/C++ files. -- ggrep: Greps on all local Gradle files. -- jgrep: Greps on all local Java files. -- resgrep: Greps on all local res/*.xml files. -- mangrep: Greps on all local AndroidManifest.xml files. -- mgrep: Greps on all local Makefiles files. -- sepgrep: Greps on all local sepolicy files. -- sgrep: Greps on all local source files. -- godir: Go to the directory containing a file. +- lunch: lunch - + Selects as the product to build, and as the variant to + build, and stores those selections in the environment to be read by subsequent + invocations of 'm' etc. +- tapas: tapas [ ...] [arm|x86|mips|arm64|x86_64|mips64] [eng|userdebug|user] +- croot: Changes directory to the top of the tree. +- m: Makes from the top of the tree. +- mm: Builds all of the modules in the current directory, but not their dependencies. +- mmm: Builds all of the modules in the supplied directories, but not their dependencies. + To limit the modules being built use the syntax: mmm dir/:target1,target2. +- mma: Builds all of the modules in the current directory, and their dependencies. +- mmma: Builds all of the modules in the supplied directories, and their dependencies. +- provision: Flash device with all required partitions. Options will be passed on to fastboot. +- cgrep: Greps on all local C/C++ files. +- ggrep: Greps on all local Gradle files. +- jgrep: Greps on all local Java files. +- resgrep: Greps on all local res/*.xml files. +- mangrep: Greps on all local AndroidManifest.xml files. +- mgrep: Greps on all local Makefiles files. +- sepgrep: Greps on all local sepolicy files. +- sgrep: Greps on all local source files. +- godir: Go to the directory containing a file. +- allmod: List all modules. +- gomod: Go to the directory containing a module. +- refreshmod: Refresh list of modules for allmod/gomod. Environment options: - SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that @@ -359,6 +362,9 @@ function addcompletions() complete -C "bit --tab" bit fi complete -F _lunch lunch + + complete -F _complete-android-module-names gomod + complete -F _complete-android-module-names m } function choosetype() @@ -1463,6 +1469,77 @@ function godir () { \cd $T/$pathname } +# Update module-info.json in out. +function refreshmod() { + if [ ! "$ANDROID_PRODUCT_OUT" ]; then + echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2 + return 1 + fi + + echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2 + + # for the output of the next command + mkdir -p $ANDROID_PRODUCT_OUT || return 1 + + # Note, can't use absolute path because of the way make works. + m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \ + > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1 +} + +# List all modules for the current device, as cached in module-info.json. If any build change is +# made and it should be reflected in the output, you should run 'refreshmod' first. +function allmod() { + if [ ! "$ANDROID_PRODUCT_OUT" ]; then + echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2 + return 1 + fi + + if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then + echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2 + refreshmod || return 1 + fi + + python -c "import json; print '\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys()))" +} + +# Go to a specific module in the android tree, as cached in module-info.json. If any build change +# is made, and it should be reflected in the output, you should run 'refreshmod' first. +function gomod() { + if [ ! "$ANDROID_PRODUCT_OUT" ]; then + echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2 + return 1 + fi + + if [[ $# -ne 1 ]]; then + echo "usage: gomod " >&2 + return 1 + fi + + if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then + echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2 + refreshmod || return 1 + fi + + local relpath=$(python -c "import json, os +module = '$1' +module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')) +if module not in module_info: + exit(1) +print module_info[module]['path'][0]" 2>/dev/null) + + if [ -z "$relpath" ]; then + echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2 + return 1 + else + cd $ANDROID_BUILD_TOP/$relpath + fi +} + +function _complete-android-module-names() { + local word=${COMP_WORDS[COMP_CWORD]} + COMPREPLY=( $(allmod | grep -E "^$word") ) +} + # Print colored exit condition function pez { "$@"