Android编译系统 - findleaves.sh


#!/bin/bash

 

#  build/tools/findleaves.sh

set -o nounset  # fail when dereferencing unset variables

扩展一个未设置的变量时显示一个错误信息

set -o errexit   # fail if any subcommand fails

当命令返回一个非零退出状态(失败)时退出

progName=`basename $0`

# ` `之间是执行的shell命令,basename - 用于去掉文件名的目录和后缀

function warn() {

    echo "$progName: $@" >&2

}

输出警告信息到错误终端

function trace() {

    echo "$progName: $@"

}

function usage() {

if [[ $# > 0 ]] # $#表示shell参数个数, $0表示shell后跟的第一个参数

    then

        warn $@

    fi

    cat <<-EOF

Usage: $progName [<options>] <dirlist> <filename>

Options:

       --mindepth=<mindepth>

       --maxdepth=<maxdepth>

       Both behave in the same way as their find(1) equivalents.

       --prune=<glob>

       Avoids returning results from any path matching the given glob-style

       pattern (e.g., "*/out/*"). May be used multiple times.

       # --prune 表示忽略一些目录,不需要在这些目录中查找目标文件

EOF

    exit 1

}

function fail() {

    warn $@

    exit 1

}

if [ $# -lt 2 ] 如果参数总个数小于2

then

    usage

fi

findargs=""

while [[ "${1:0:2}" == "--" ]]  # 参数解析

取第二个参数字符串的前两个字符,${1:0:2} 1表示第二个参数,0表示第一个参数的起

始下标,2表示需要截取的字符个数

do

    arg=${1:2}

    name=${arg%%=*}

    value=${arg##*=} # 获取参数名字和值

    if [[ "$name" == "mindepth" || "$name" == "maxdepth" ]]

    then

        # Add to beginning of findargs; these must come before the expression.

        findargs="-$name $value $findargs"

    elif [[ "$name" == "prune" ]]

    then

        # Add to end of findargs; these are part of the expression.

        findargs="$findargs -path $value -prune -or"

    fi

Shift

#  linuxfind命令可以加上参数: 

#  -mindepth value -maxdepth value -path */out/* -prune -or

done

nargs=$#

# The filename is the last argument

filename="${!nargs}"

find "${@:0:$nargs}" $findargs -type f -name "$filename" -print |

# Only pass along the directory of each match.

sed -e 's/\/[^\/]*$/\//' |

#  关于sed的用法参考后面网址,这里只要是接收前面find后的结果。

#  /在命令行中是分隔符,如果引用真正的/话,那么需要利用转义字符\,  \/

#  通配符“*”表示在它之前的字符重复任意次,“.”则表示任意字符, ^$表示空行

#  锚定行的结束 如:/sed$/匹配所有以sed结尾的行

#  对结果中的符号进行规则化

LC_ALL=C sort | 

排序后输出

awk -v "filename=$filename" '

    (NR == 1) || (index($0, last) != 1) {

        last = $0;

        printf("%s%s\n", $0, filename);

}

'

Android编译系统中,该脚本findleaves.sh典型应用如下:

main.mk中调用了findleaves.sh,得到所有子目录下Android.mk文件的路径。

subdir_makefiles := \
    $(shell build/tools/findleaves.sh --prune=out --prune=.repo --prune=.git --prune=.svn  $(subdirs)  Android.mk)

$(subdirs)一般编译中取值$(TOP)

该语句依次选取$(subdirs)中的目录,然后遍历所有子目录查找Android.mk文件,如果有,则将其路径加入到返回列表中。


makefile的模式规则和自动化变量

模式规则中,至少在规则的目标定义中要包含"%",否则,就是一般的规则。目标中的"%"定义表示对文件名的匹配,"%"表示长度任意的非空字符串。

如果"%"定义在目标中,那么,目标中的"%"的值决定了依赖目标中的"%"的值,也就是说,目标中的模式的"%"决定了依赖目标中"%"的样子。例如有一个模式规则如下:

    %.o : %.c 

    <command ......>

其含义是,指出了怎么从所有的[.c]文件生成相应的[.o]文件的规则。如果要生成的目标是"a.o b.o",那么"%c"就是"a.c b.c"。一旦依赖目标中的"%"模式被确定,那么,make会被要求去匹配当前目录下所有的文件名

下面这个例子表示了,把所有的[.c]文件都编译成[.o]文件.

    %.o : %.c

    $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

其中,"$@"表示所有的目标的挨个值,"$<"表示了所有依赖目标的挨个值。这些奇怪的变量我们叫"自动化变量"

$@
    表示规则中的目标文件集。在模式规则中,如果有多个目标,那么,"$@"就是匹配于目标中模式定义的集合。
$<
    依赖目标中的第一个目标名字。如果依赖目标是以模式(即"%")定义的,那么"$<"将是符合模式的一系列的文件集。注意,其是一个一个取出来的。

相关内容