当前位置:操作系统 > 安卓/Android >>

Android编译过程详解(二)

通过上篇文章,我们分析了编译android时source build/envsetup.sh和lunch命令,在执行完上述两个命令后, 我们就可以进行编译android了。

 


1. make

执行make命令的结果就是去执行当前目录下的Makefile文件,我们来看下它的内容:


[html]  1 ### DO NOT EDIT THIS FILE ### 
2 include build/core/main.mk 
3 ### DO NOT EDIT THIS FILE ### 
  1 ### DO NOT EDIT THIS FILE ###
  2 include build/core/main.mk
  3 ### DO NOT EDIT THIS FILE ###

呵呵,看到上面 的内容,我们都会笑,这是我见过最简单的Makefile了,我们再看下build/core/main.mk

在main.mk里,定义了变量TOPDIR,TOP为当前目录,BUILD_SYSTEM为build/core目录。

在49行,包含了build/core/config.mk文件。

后面的代码是check环境 变量,所有的Makefile都通过build/core/main.mk这个文件组织在一起,它定义了一个默认goals:droid,当我们在TOP目录下,敲Make实际上就等同于我们执行make droid。当Make include所有的文件,完成对所有make我文件的解析以后就会寻找生成droid的规则,依次生成它的依赖,直到所有满足的模块被编译好,然后使用相应的工具打包成相应的img。这儿不是我们的重点,不再多说。

 


2. build/core/config.mk

该文件被main.mk包含。

定义了以下环境变量:


[plain] 16 SRC_HEADERS := \ 
17     $(TOPDIR)system/core/include \ 
18     $(TOPDIR)hardware/libhardware/include \ 
19     $(TOPDIR)hardware/libhardware_legacy/include \ 
20     $(TOPDIR)hardware/ril/include \ 
21     $(TOPDIR)dalvik/libnativehelper/include \ 
22     $(TOPDIR)frameworks/base/include \ 
23     $(TOPDIR)frameworks/base/opengl/include \ 
24     $(TOPDIR)external/skia/include 
25 SRC_HOST_HEADERS:=$(TOPDIR)tools/include 
26 SRC_LIBRARIES:= $(TOPDIR)libs 
27 SRC_SERVERS:= $(TOPDIR)servers 
28 SRC_TARGET_DIR := $(TOPDIR)build/target 
29 SRC_API_DIR := $(TOPDIR)frameworks/base/api 
 16 SRC_HEADERS := \
 17     $(TOPDIR)system/core/include \
 18     $(TOPDIR)hardware/libhardware/include \
 19     $(TOPDIR)hardware/libhardware_legacy/include \
 20     $(TOPDIR)hardware/ril/include \
 21     $(TOPDIR)dalvik/libnativehelper/include \
 22     $(TOPDIR)frameworks/base/include \
 23     $(TOPDIR)frameworks/base/opengl/include \
 24     $(TOPDIR)external/skia/include
 25 SRC_HOST_HEADERS:=$(TOPDIR)tools/include
 26 SRC_LIBRARIES:= $(TOPDIR)libs
 27 SRC_SERVERS:= $(TOPDIR)servers
 28 SRC_TARGET_DIR := $(TOPDIR)build/target
 29 SRC_API_DIR := $(TOPDIR)frameworks/base/api
[plain] .....然后定义了下面几个重要的编译命令 
.....然后定义了下面几个重要的编译命令[plain] 43 CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk 
 44 BUILD_HOST_STATIC_LIBRARY:= $(BUILD_SYSTEM)/host_static_library.mk 
 45 BUILD_HOST_SHARED_LIBRARY:= $(BUILD_SYSTEM)/host_shared_library.mk 
 46 BUILD_STATIC_LIBRARY:= $(BUILD_SYSTEM)/static_library.mk 
 47 BUILD_RAW_STATIC_LIBRARY := $(BUILD_SYSTEM)/raw_static_library.mk 
 48 BUILD_SHARED_LIBRARY:= $(BUILD_SYSTEM)/shared_library.mk 
 49 BUILD_EXECUTABLE:= $(BUILD_SYSTEM)/executable.mk 
 50 BUILD_RAW_EXECUTABLE:= $(BUILD_SYSTEM)/raw_executable.mk 
 51 BUILD_HOST_EXECUTABLE:= $(BUILD_SYSTEM)/host_executable.mk 
 52 BUILD_PACKAGE:= $(BUILD_SYSTEM)/package.mk 
 53 BUILD_HOST_PREBUILT:= $(BUILD_SYSTEM)/host_prebuilt.mk 
 54 BUILD_PREBUILT:= $(BUILD_SYSTEM)/prebuilt.mk 
 55 BUILD_MULTI_PREBUILT:= $(BUILD_SYSTEM)/multi_prebuilt.mk 
 56 BUILD_JAVA_LIBRARY:= $(BUILD_SYSTEM)/java_library.mk 
 57 BUILD_STATIC_JAVA_LIBRARY:= $(BUILD_SYSTEM)/static_java_library.mk 
 58 BUILD_HOST_JAVA_LIBRARY:= $(BUILD_SYSTEM)/host_java_library.mk 
 59 BUILD_DROIDDOC:= $(BUILD_SYSTEM)/droiddoc.mk 
 60 BUILD_COPY_HEADERS := $(BUILD_SYSTEM)/copy_headers.mk 
 61 BUILD_KEY_CHAR_MAP := $(BUILD_SYSTEM)/key_char_map.mk 
 43 CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk
 44 BUILD_HOST_STATIC_LIBRARY:= $(BUILD_SYSTEM)/host_static_library.mk
 45 BUILD_HOST_SHARED_LIBRARY:= $(BUILD_SYSTEM)/host_shared_library.mk
 46 BUILD_STATIC_LIBRARY:= $(BUILD_SYSTEM)/static_library.mk
 47 BUILD_RAW_STATIC_LIBRARY := $(BUILD_SYSTEM)/raw_static_library.mk
 48 BUILD_SHARED_LIBRARY:= $(BUILD_SYSTEM)/shared_library.mk
 49 BUILD_EXECUTABLE:= $(BUILD_SYSTEM)/executable.mk
 50 BUILD_RAW_EXECUTABLE:= $(BUILD_SYSTEM)/raw_executable.mk
 51 BUILD_HOST_EXECUTABLE:= $(BUILD_SYSTEM)/host_executable.mk
 52 BUILD_PACKAGE:= $(BUILD_SYSTEM)/package.mk
 53 BUILD_HOST_PREBUILT:= $(BUILD_SYSTEM)/host_prebuilt.mk
 54 BUILD_PREBUILT:= $(BUILD_SYSTEM)/prebuilt.mk
 55 BUILD_MULTI_PREBUILT:= $(BUILD_SYSTEM)/multi_prebuilt.mk
 56 BUILD_JAVA_LIBRARY:= $(BUILD_SYSTEM)/java_library.mk
 57 BUILD_STATIC_JAVA_LIBRARY:= $(BUILD_SYSTEM)/static_java_library.mk
 58 BUILD_HOST_JAVA_LIBRARY:= $(BUILD_SYSTEM)/host_java_library.mk
 59 BUILD_DROIDDOC:= $(BUILD_SYSTEM)/droiddoc.mk
 60 BUILD_COPY_HEADERS := $(BUILD_SYSTEM)/copy_headers.mk
 61 BUILD_KEY_CHAR_MAP := $(BUILD_SYSTEM)/key_char_map.mk
上述命令变量其实是对应的mk文件名,所有的Android.mk文件里基本上都包含上述命令变量,如:

CLEAR_VARS:用来清除之前定义的环境变量

BUILD_SHARED_LIBRARY:用来指定编译动态库过程

 

[plain] 109 # --------------------------------------------------------------- 
110 # Define most of the global variables.  These are the ones that 
111 # are specific to the user's build configuration. 
112 include $(BUILD_SYSTEM)/envsetup.mk 
113  
114 # Boards may be defined under $(SRC_TARGET_DIR)/board/$(TARGET_DEVICE) 
115 # or under vendor/*/$(TARGET_DEVICE).  Search in both places, but 
116 # make sure only one exists. 
117 # Real boards should always be associated with an OEM vendor. 
118 board_config_mk := \ 
119     $(strip $(wildcard \ 
120         $(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)/BoardConfig.mk \ 
121         vendor/*/$(TARGET_DEVICE)/BoardConfig.mk \ 
122     )) 
123 ifeq ($(board_config_mk),) 
124   $(error No config file found for TARGET_DEVICE $(TARGET_DEVICE)) 
125 endif 
126 ifneq ($(words $(board_config_mk)),1) 
127   $(error Multiple board config files for TARGET_DEVICE $(TARGET_DEVICE): $(board_config_mk)) 
128 endif 
129 include $(board_config_mk) 
130 TARGET_DEVICE_DIR := $(patsubst %/,%,$(dir $(board_config_mk))) 
131 board_config_mk := 
109 # ---------------------------------------------------------------
110 # Define most of the global variables.  These are the ones that
111 # are specific to the user's build configuration.
112 include $(BUILD_SYSTEM)/envsetup.mk
113
114 # Boards may be defined under $(SRC_

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,