1、 Android NDK 的编译脚本系统,实际上并不是什么新东西,就是对 GNU MakeFile 系统的封装。Android NDK 自带了很多 .mk 文件(保存在ndk的 NDK_ROOT/build 目录中),文件中预定义了很多 Make 脚本函数和变量,以供用户编写自己的编译脚本时使用。另外,NDK为所有应用统一提供了一个固定的编译入口-即大家熟悉的ndk-build。 我们首先从每个应用编译时都需要用到的编译入口开始分析。一、ndk-build分析(一)、脚本内容分析 该文件存在于ndk安装根目录中,实际上是一个 shell 脚本。其有效内容如下:PROGDIR=dirname $
2、0PROGDIR=cd $PROGDIR && pwd# If GNUMAKE is defined, check that it points to a valid fileif -n $GNUMAKE ; then ABS_GNUMAKE=which $GNUMAKE 2> /dev/null if $? != 0 ; echo ERROR: Your GNUMAKE variable is defined to an invalid name: $GNUMAKEPlease fix it to point to a valid make executable (e.g. /
3、usr/bin/make) exit 1 fi GNUMAKE=$ABS_GNUMAKEelse # Otherwise, use make and check that it is available GNUMAKE=which make 2& Cannot find program. Please install Cygwin make packageor define the GNUMAKE variable to point to it.fi# On Windows, when running under cygwin, check that we are# invoking a cy
4、gwin-compatible GNU Make binary. It is unfortunately# common for app developers to have another non-cygwin compatible#if $OSTYPE = cygwin GNUMAKE=cygpath -u $GNUMAKE PROGDIR_MIXED=cygpath -m $PROGDIR CYGWIN_GNUMAKE=$GNUMAKE -f $PROGDIR_MIXED/build/core/check-cygwin-make.mk 2&1 You are using a non-Cy
5、gwin compatible Make program.Currently using: cygpath -m $GNUMAKETo solve the issue, follow these steps:1. Ensure that the Cygwin package is installed. NOTE: You will need GNU Make 3.81 or later!2. Define the GNUMAKE environment variable to point to it, as in: export GNUMAKE=/usr/bin/make3. Call ndk
6、-build again.fi$GNUMAKE -f $PROGDIR/build/core/build-local.mk $ 下面我们对这些有效代码逐一进行分析: pwd 在GNU makefile中,两个反引号相当于 shell 函数,反引号括起来的内容相当于 shell 函数的参数。而shell函数的功能就是调用操作系统的命令。 linux 命令 dirname, 功能是获取后面参数的路径。例如:#dirname ./././ndk-build 执行的结果是:././.。顺便提一下,这里不能用pwd命令,pwd 命令,是获得当前路径,获取的是当前路径的绝对路径。所以,上面两条语句的结果就
7、是在 PROGDIR 变量中保存 ndk-build 所在的路径。有了这两条语句之后,就可以在任何位置执行 ndk-build ,而不必非要在 ndk 根目录中执行。 再之后的两段语句,是用来确定不同平台下正确的 make 工具。# If GNUMAKE is defined, check that it points to a valid filefi 最后一条语句,是调用:$GNUMAKE -f $PROGDIR/build/core/build-local.mk 这是真正开始执行 make ,指定 Makefile 为 build-local.mk 文件。同时,将 ndk-build 的所有参数($)原封不动地都传递进去。 总之ndk-build 是对 make 指令的封装,并不对各个参数作处理,具体参数原封不动地传给 make 和Makefile。由此可见,ndk-build,只是对make命令的一个封装,为编译提供一个统一的且适应性更强的编译入口。(二)、技巧二、 build-local.mk文件分析=一、Android.mk文件内容分析 典型的 Android.mk 文件一般会包含如下内容:L
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1