一般来说编译一个sdk或者一个比较大的工程项目,第一步都是执行 envsetup.sh这个脚本,比如编译android,qt源码以及其他一些嵌入式的sdk。
而且执行的时候需要特别注意使用
source build/envsetup.sh
或者
. ./build/envsetup.sh
特别注意两个点之间是有空格的,这个作用相当于source。
==========================================补课开始=====================================================================
一般执行shell脚本时直接用./来执行,此时是系统启动一个子shell进程来执行命令,执行完后返回最后一条指令的结果(可以用echo $?来获取执行结果值(0-255)),其结果并不会对当前的BASH产生影响。
source命令的用法:
source FileName
他的作用是在当前bash环境下读取并执行FileName中的指令,这个命令执行完之后会把FileName执行过程中的一些输出设置到当前的BASH中,也就设置环境变量。
需要注意的是用source执行新产生的这些变量的作用范围(作用域)只是在当前终端有用,如果你关闭当前终端了,或者在其他终端是无法使用这些新增的变量的。
==========================================补课结束=====================================================================
当我们用source命令执行完encsetup.sh的时候,里面设置的一些变量(方法)就会输出到当前的BASH环境中,在后续的过程中我们就可以在当前终端使用这些变量,比如用android的envsetup.sh来说,执行完后会增加一些shell cmd及环境变量可以使用
输入hmm可以看到以下帮助信息
Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- lunch: lunch <product_name>-<build_variant>
- tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5] [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.
- 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.
- cgrep: Greps on all local C/C++ files.
- jgrep: Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- godir: Go to the directory containing a file.
Look at the source to view more functions. The complete list is:
addcompletions add_lunch_combo cgrep check_product check_variant choosecombo chooseproduct choosetype choosevariant cproj croot findmakefile gdbclient gdbwrapper get_abs_build_var getbugreports get_build_var getlastscreenshot getprebuilt getscreenshotpath getsdcardpath gettargetarch gettop godir hmm isviewserverstarted jgrep key_back key_home key_menu lunch _lunch m mangrep mm mma mmm mmma pez pid printconfig print_lunch_menu qpid resgrep runhat runtest sepgrep set_java_home setpaths set_sequence_number set_stuff_for_environment settitle smoketest stacks startviewserver stopviewserver systemstack tapas tracedmdump
liuxueneng@airfly-dev:~/workCode/allwinner_h2_2018-0907/android$ source build/envsetup.sh find Makefile including device/generic/mips/vendorsetup.sh including device/generic/armv7-a-neon/vendorsetup.sh including device/generic/x86/vendorsetup.sh including device/softwinner/common/vendorsetup.sh including device/softwinner/dolphin-fvd-p1/vendorsetup.sh liuxueneng@airfly-dev:~/workCode/allwinner_h2_2018-0907/android$ hmm Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment: - lunch: lunch <product_name>-<build_variant> - tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5] [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. - 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. - cgrep: Greps on all local C/C++ files. - jgrep: Greps on all local Java files. - resgrep: Greps on all local res/*.xml files. - godir: Go to the directory containing a file. Look at the source to view more functions. The complete list is: addcompletions add_lunch_combo cgrep check_product check_variant choosecombo chooseproduct choosetype choosevariant cproj croot findmakefile gdbclient gdbwrapper get_abs_build_var getbugreports get_build_var getlastscreenshot getprebuilt getscreenshotpath getsdcardpath gettargetarch gettop godir hmm isviewserverstarted jgrep key_back key_home key_menu lunch _lunch m mangrep mm mma mmm mmma pez pid printconfig print_lunch_menu qpid resgrep runhat runtest sepgrep set_java_home setpaths set_sequence_number set_stuff_for_environment settitle smoketest stacks startviewserver stopviewserver systemstack tapas tracedmdump liuxueneng@airfly-dev:~/workCode/allwinner_h2_2018-0907/android$
这些命令都是在android/build/envsetup.sh中定义输出的,可以打开看下
export ROOT_DIR=${PWD} MAKEFILE=$ROOT_DIR/Makefile if [ -f $MAKEFILE ]; then echo "find Makefile" else echo "copy Makefile" cp $ROOT_DIR/build/core/root.mk $ROOT_DIR/Makefile fi function hmm() { cat <<EOF Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment: - lunch: lunch <product_name>-<build_variant> - tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5] [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. - 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. - cgrep: Greps on all local C/C++ files. - jgrep: Greps on all local Java files. - resgrep: Greps on all local res/*.xml files. - godir: Go to the directory containing a file. Look at the source to view more functions. The complete list is: EOF T=$(gettop) local A A="" for i in `cat $T/build/envsetup.sh | sed -n "/^function /s/function \([a-z_]*\).*/\1/p" | sort`; do A="$A $i" done echo $A } # Get the value of a build variable as an absolute path. function get_abs_build_var() { T=$(gettop) if [ ! "$T" ]; then echo "Couldn't locate the top of the tree. Try setting TOP." >&2 return fi (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \ make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-abs-$1) }
这些方法还有一些变量都会输出到当前的BASH环境中,输入env指令就可以打印出当前BASH下的所有环境变量
liuxueneng@airfly-dev:~/workCode/allwinner_h2_2018-0907/android$ env XDG_SESSION_ID=13509 TERM=xterm SHELL=/bin/bash SSH_CLIENT=192.168.11.102 1097 22 OLDPWD=/home/liuxueneng SSH_TTY=/dev/pts/26 USER=liuxueneng LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36: ROOT_DIR=/home/liuxueneng/workCode/allwinner_h2_2018-0907/android MAIL=/var/mail/liuxueneng PATH=/home/liuxueneng/bin:/home/liuxueneng/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin PWD=/home/liuxueneng/workCode/allwinner_h2_2018-0907/android LANG=en_HK.UTF-8 SHLVL=1 HOME=/home/liuxueneng LANGUAGE=en_HK:en LOGNAME=liuxueneng XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop SSH_CONNECTION=192.168.11.102 1097 192.168.11.111 22 LESSOPEN=| /usr/bin/lesspipe %s XDG_RUNTIME_DIR=/run/user/1006 LESSCLOSE=/usr/bin/lesspipe %s %s _=/usr/bin/env
其中我们可以看到至少以下两个变量是执行刚才的envsetup.sh输出来的
ROOT_DIR=/home/liuxueneng/workCode/allwinner_h2_2018-0907/android
联系以上的的croot指令就是改变当前工作目录的哦ROOT_DIR中。
另外还可以查看输出到当前BASH环境中的临时shell方法,输入set指令来查看(小心会有一大堆东西喷出来)
tracedmdump () { T=$(gettop); if [ ! "$T" ]; then echo "Couldn't locate the top of the tree. Try setting TOP."; return; fi; local prebuiltdir=$(getprebuilt); local arch=$(gettargetarch); local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu; local TRACE=$1; if [ ! "$TRACE" ]; then echo "usage: tracedmdump tracename"; return; fi; if [ ! -r "$KERNEL" ]; then echo "Error: cannot find kernel: '$KERNEL'"; return; fi; local BASETRACE=$(basename $TRACE); if [ "$BASETRACE" = "$TRACE" ]; then TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE; fi; echo "post-processing traces..."; rm -f $TRACE/qtrace.dexlist; post_trace $TRACE; if [ $? -ne 0 ]; then echo "***"; echo "*** Error: malformed trace. Did you remember to exit the emulator?"; echo "***"; return; fi; echo "generating dexlist output..."; /bin/ls $ANDROID_PRODUCT_OUT/system/framework/*.jar $ANDROID_PRODUCT_OUT/system/app/*.apk $ANDROID_PRODUCT_OUT/data/app/*.apk 2> /dev/null | xargs dexlist > $TRACE/qtrace.dexlist; echo "generating dmtrace data..."; q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return; echo "generating html file..."; dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return; echo "done, see $TRACE/dmtrace.html for details"; echo "or run:"; echo " traceview $TRACE/dmtrace" } treegrep () { find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@" } verity_file_init () { echo "verity_file_init"; cp -rf $DEVICE/verity ${OUT}; $DEVICE/verity/gen_file_verify_block.sh ${OUT}/system; cp -f ${OUT}/verity/verity_block ${OUT}/verity_block.img } verity_key_init () { $DEVICE/verity/gen_dm_verity_key.sh; cp -f $DEVICE/verity/rsa_key/verity_key $OUT/root }
以上treegrep tracedmdump等shell方法都是从envsetup.sh输出来的。
后续讲解下输出的m mm mmm mmma croot cgrep 等作用及用法。