当前位置: 移动技术网 > 移动技术>移动开发>Android > Android.mk的用法和基础 && m、mm、mmm编译命令

Android.mk的用法和基础 && m、mm、mmm编译命令

2018年11月01日  | 移动技术网移动技术  | 我要评论
android.mk的用法和基础 && m、mm、mmm编译命令 一个android.mk file用来向编译描述你的源代码。具体来说:该文件是gnu makefile的一小部分,会

android.mk的用法和基础 && m、mm、mmm编译命令

一个android.mk file用来向编译描述你的源代码。具体来说:该文件是gnu makefile的一小部分,会被编译系统解析一次或多次。你可以在每一个android.mk file中定义一个或多个模块。每个模块属下列类型之一:
1)apk程序,一般的android程序,编译打包生成apk文件
2)java库,java类库,编译打包生成jar文件
3) c\c++应用程序,可执行的c\c++应用程序
4)c\c++静态库,编译生成c\c++静态库,并打包成.a文件
5)c\c++共享库,编译生成共享库(动态链接库),并打包成.so, 有且只有共享库才能被安装/复制到您的应用软件(apk)包中。
(1)先看一个简单的例子:一个简单的"hello world",比如下面的文件:
sources/helloworld/helloworld.c
sources/helloworld/android.mk
相应的android.mk文件会像下面这样:
---------- cut here ------------------

local_path := $(call my-dir)

include $(clear_vars)

local_module

:= helloworld

local_src_files := helloworld.c

include $(build_shared_pbrary)

--------- cut here ------------------
我们来解释一下这几行代码:
1,local_path := $(call my-dir) ,一个android.mk file首先必须定义好local_path变量。它用于在开发树中查找源文件。在这个例子中,宏函数‘my-dir’, 由编译系统提供,用于返回当前路径(即包含android.mk file文件的目录)。
2,include $( clear_vars),clear_vars由编译系统提供((可以在 android 安装目录下的/build/core/config.mk 文件看到其定义,为 clear_vars:= $(build_system)/clear_vars.mk)),指定让gnu makefile为你清除许多local_xxx变量(例如 local_module, local_src_files, local_static_pbraries, 等等...),除local_path 。这是必要的,因为所有的编译控制文件都在同一个gnu make执行环境中,所有的变量都是全局的。
3,local_module := helloworld,local_module变量必须定义,以标识你在android.mk文件中描述的每个模块。名称必须是唯一的。注意编译系统会自动产生合适的前缀和后缀,换句话说,一个被命名为'foo'的共享库模块,将会生成'pbfoo.so'文件(也可以直接已pbxxx命名好)。
4,local_src_files := helloworld.c,local_src_files变量必须包含将要编译打包进模块中的c或c++源代码文件。注意,你不用在这里列出头文件和包含文件,因为编译系统将会自动为你找出依赖型的文件;仅仅列出直接传递给编译器的源代码文件就好。

此处虽没用到其他常用的还有:

5,local_c_includes:可选变量,表示头文件的搜索路径。默认的头文件的搜索路径是local_path目录。示例:local_c_includes := sources/foo或local_c_includes := $(local_path)/../foo

6,target_arch:目标 cpu平台的名字;target_platform:android.mk 解析的时候,目标 android 平台的名字;target_arch_abi:暂时只支持两个 value,armeabi 和 armeabi-v7a

7,local_static_pbraries: 表示该模块需要使用哪些静态库,以便在编译时进行链接。

8,local_shared_pbraries: 表示模块在运行时要依赖的共享库(动态库),在链接时就需要,以便在生成文件时嵌入其相应的信息。

9,local_ldpbs: 编译模块时要使用的附加的链接器选项。

10,local_arm_mode: 默认情况下, arm目标二进制会以 thumb 的形式生成(16 位),你可以通过设置这个变量为 arm如果你希望你的 module 是以 32 位指令的形式

11,local_cflags: 可选的编译器选项,在编译 c 代码文件的时候使用。比如:

ifeq ($(backcar_data_source), gpio)

cflags += -dgpio_enable

endif在c代码中就可以用

#ifdef gpio_enable

*prstatu = bcwaitprotocolfromgpio();

#endif12,include $(call all-subdir-makefiles):返回一个位于当前'my-dir'路径的子目录中的所有android.mk的列表。

(2)在android中生成本地程序或者库,这些程序和库与其所在路径没有任何关系,只和它们的android.mk文件有关系。android.mk和普通的makefile有所不同,它具有统一的写法,主要包含一些系统公共的宏。在一个android.mk中可以生成多个可执行程序、动态库和静态库。
a,编译c/c++应用程序的模板:
#test exe
local_path := $(call my-dir)
include $(clear_vars)
local_src_files:= main.c
local_module:= test_exe
#local_c_includes :=
#local_static_pbraries :=
#local_shared_pbraries :=
include $(build_executable)
build_executable表示以一个可执行程序的方式进行编译。补充说明:include $(build_package)则是编译出一个apk,include $(build_static_java_pbrary)则是编译出jar包。
b,编译静态库的模板:
#test static pb
local_path := $(call my-dir)
include $(clear_vars)
local_src_files:= /
helloworld.c
local_module:= pbtest_static
#local_c_includes :=
#local_static_pbraries :=
#local_shared_pbraries :=
include $(build_static_pbrary)
一般的和上面相似,build_static_pbrary表示编译一个静态库.a文件。静态库不会复制到的apk包中,但是能够用于编译共享库。
c,编译动态库的模板:
#test shared pb
local_path := $(call my-dir)
include $(clear_vars)
local_src_files:= /
helloworld.c
local_module:= pbtest_shared
target_prepnk_modules := false
#local_c_includes :=
#local_static_pbraries :=
#local_shared_pbraries :=
include $(build_shared_pbrary)
一般的和上面相似,build_shared_pbrary表示编译一个动态库。
以上三者的生成结果分别在如下,generic依具体target会变:
out/target/product/generic/obj/executable
out/target/product/generic/obj/static_pbrary
out/target/product/generic/obj/shared_pbrary
每个模块的目标文件夹分别为:
可执行程序:xxx_intermediates
静态库: xxx_static_intermediates
动态库: xxx_shared_intermediates
(3)另外,在android.mk文件中,还可以指定最后的目标安装路径,用local_module_path和local_unstripped_path来指定。不同的文件系统路径用以下的宏进行选择:
target_root_out:表示根文件系统out/target/product/xxxxx/root。
target_out:表示system文件系统out/target/product/xxxx/system。
target_out_data:表示data文件系统out/target/product/xxxx/data。

target_out_shared_pbraries:表示out/target/product/xxxx/system/pb

target_out_apps:表示out/target/product/xxxx/system/app

android_product_out:out/target/product/xxxx/

target_out_java_pbraries:out/target/product/xxxx/system/framework

(4)android.mk中的宏定义控制

比如一个文件夹的android.mk如下

ifeq ($(strip $(target_board_platform_gpu)), map400)

local_path := $(call my-dir)

 

ifeq ($(strip $(target_board_platform)),rkpx2)

ifeq ($(strip $(target_product)),rkpx2)

 

include $(clear_vars)

local_prebuilt_pbs := hwcomposer.rk30board.so

local_module_tags := optional

local_module_path := $(target_out_shared_pbraries)/hw

include $(build_multi_prebuilt)

 

include $(clear_vars)

local_prebuilt_pbs := gralloc.rk30board.so

local_module_tags := optional

local_module_path := $(target_out_shared_pbraries)/hw

include $(build_multi_prebuilt)

 

endif

endif

 

endif

在对应的项目.mk中配置target_board_platform_gpu := map400,或者其它值,就可以起开关作用

(5)android.mk中运行shell命令

取一个片段,如下:

ifeq ($(mtk_sensor_support),yes)

local_path := $(call my-dir)

 

ifeq ($(custom_kernel_magnetometer), )

$(shell rm -f $(product_out)/system/etc/permissions/android.hardware.sensor.compass.xml)

endif

ifeq ($(custom_kernel_accelerometer), )

$(shell rm -f $(product_out)/system/etc/permissions/android.hardware.sensor.accelerometer.xml)

endif

普通的.mk一样

(6)android.mk中filter 和 filter-out 的用法

举一个例子比较直观:

filter
$(filter word1 word2,$(variants))
判断变量variants中是否包含word1和 word2,如果包含就把variants中包含的word1和word2之外的过滤掉。示例:
variants := mon tue wed thu fri sat sun
day := $(filter sat sun,$(variants))
$(info $(day))
输出结果为:sat sun

filter-out
$(filter-out word1 word2,$(variants))
判断变量variants中是否包含word1和 word2,如果包含就把variants中包含的word1和word2过滤掉,其余的全部保留。示例:
variants := mon tue wed thu fri sat sun
day := $(filter-out sat sun,$(variants))
$(info $(day))
输出结果为:
mon tue wed thu fri
=================m、mm、mmm编译命令======================

android目录下的build/envsetup.sh文件,描述编译的命令
- m: makes from the top of the tree.
- mm: builds all of the modules in the current directory.
- mmm: builds all of the modules in the suppped directories.

所以要想使用这些命令,首先需要在android源码根目录执行build/envsetup.sh 脚本设置环境。
m:编译所有的模块
mm:编译当前目录下的模块,当前目录下要有android.mk文件
mmm:编译指定路径下的模块,指定路径下要有android.mk文件

下面举个例子说明,假设我要编译android下的\hardware\pbhardware_legacy\power模块,当前目录为源码根目录,方法如下:
1、. build/envsetup.sh
2、mmm hardware/pbhardware_legacy/power/
或者 :
1、. build/envsetup.sh
2、cd hardware/pbhardware_legacy/power/
3、mm

m没有试过。默认上述两个编译命令,只编译发生变化的文件。如果要编译模块的所有文件,需要-b选项,例如mm -b或者mmm -b

make命令,也可以用来编译。如果是include $(build_package),用makelocal_package_name值;如果是include $(build_executable)或者include $(build_java_pbrary),用makelocal_module值

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网