细说dex2oat - Android.oat.mk分析
有了dex2oat命令行的基础,我们就可以开始分析相关的makefile了。
引用
include art/build/Android.common_build.mk
引用Android.common_build.mk文件,我们用到再说。
默认指令集
ifeq ($(DEX2OAT_HOST_INSTRUCTION_SET_FEATURES),)
DEX2OAT_HOST_INSTRUCTION_SET_FEATURES := default
endif
上面我们之前讲makefile基础的时候已经讲过了。下面这个开始变复杂一点儿了,正好用来消化对宏的理解。
ifeq ($($(HOST_2ND_ARCH_VAR_PREFIX)DEX2OAT_HOST_INSTRUCTION_SET_FEATURES),)
$(HOST_2ND_ARCH_VAR_PREFIX)DEX2OAT_HOST_INSTRUCTION_SET_FEATURES := default
endif
我们先看看HOST_2ND_ARCH_VAR_PREFIX是个什么东西,定义于:build/core/envsetup.mk
# Out for HOST_2ND_ARCH
HOST_2ND_ARCH_VAR_PREFIX := 2ND_
HOST_2ND_ARCH_MODULE_SUFFIX := _32
搞了半天就是在DEX2OAT_HOST_INSTRUCTION_SET_FEATURES前面加了个2ND_。既没有addprefix,也没有join,真是简单粗暴。但是这却真实反应了宏的本质,就是字符串展开。
大函数create-core-oat-host-rules
makefile里一样能写出来近100行的大函数。
参数说明
33# Use dex2oat debug version for better error reporting
34# $(1): compiler - default, optimizing, jit or interpreter.
35# $(2): pic/no-pic
36# $(3): 2ND_ or undefined, 2ND_ for 32-bit host builds.
37# $(4): wrapper, e.g., valgrind.
38# $(5): dex2oat suffix, e.g, valgrind requires 32 right now.
39# NB depending on HOST_CORE_DEX_LOCATIONS so we are sure to have the dex files in frameworks for
40# run-test --no-image
清理宏的定义
前面是参数的说明,下面函数定义正式开始:
41define create-core-oat-host-rules
42 core_compile_options :=
43 core_image_name :=
44 core_oat_name :=
45 core_infix :=
46 core_pic_infix :=
先清理一下宏,毕竟不像是C语言可以定义局部变量那么自由,宏有宏的规则,先统统定义成空。
DEX2OAT_DEPENDENCY
47 core_dex2oat_dependency := $(DEX2OAT_DEPENDENCY)
我们去查DEX2OAT_DEPENDENCY,定义于build/core/dex_preopt_libart.mk中
9# By default, do not run rerun dex2oat if the tool changes.
10# Comment out the | to force dex2oat to rerun on after all changes.
11DEX2OAT_DEPENDENCY := art/runtime/oat.cc # dependency on oat version number
12DEX2OAT_DEPENDENCY += art/runtime/image.cc # dependency on image version number
13DEX2OAT_DEPENDENCY += |
14DEX2OAT_DEPENDENCY += $(DEX2OAT)
DEX2OAT又是什么呢,继续往上查,在第6行。
6DEX2OAT := $(HOST_OUT_EXECUTABLES)/dex2oat$(HOST_EXECUTABLE_SUFFIX)
HOST_EXECUTABLE_SUFFIX又是个啥子呢?就是可执行文件的后缀,在Windows下是.exe,在Linux下不需要。
下面是在windows下的定义,build/core/combo/HOST_windows-x86.mk
79HOST_SHLIB_SUFFIX := .dll
80HOST_EXECUTABLE_SUFFIX := .exe
HOST_OUT_EXECUTABLES是out下面的可执行文件的目录,这个大家都清楚,我们就不多讲了。
48
49 # With the optimizing compiler, we want to rerun dex2oat whenever there is
50 # a dex2oat change to catch regressions early.
51 ifeq ($(ART_USE_OPTIMIZING_COMPILER), true)
52 core_dex2oat_dependency := $(DEX2OAT)
53 endif
54
55 ifeq ($(1),default)
56 core_compile_options += --compiler-backend=Quick
57 endif
58 ifeq ($(1),optimizing)
59 core_compile_options += --compiler-backend=Optimizing
60 core_dex2oat_dependency := $(DEX2OAT)
61 core_infix := -optimizing
62 endif
63 ifeq ($(1),interpreter)
64 core_compile_options += --compiler-filter=interpret-only
65 core_infix := -interpreter
66 endif
67 ifeq ($(1),default)
68 # Default has no infix, no compile options.
69 endif
以上都是配置compiler-backend
70 ifneq ($(filter-out default interpreter jit optimizing,$(1)),)
71 #Technically this test is not precise, but hopefully good enough.
72 $$(error found $(1) expected default, interpreter, jit or optimizing)
73 endif
上面这个filter-out,我们在之前有讲过,是将这些串全部过滤掉,如果是default interpreter jit optimizing之一,过滤就是空,否则非空就走到error这一支了。
74
75 ifeq ($(2),pic)
76 core_compile_options += --compile-pic
77 core_pic_infix := -pic
78 endif
79 ifeq ($(2),no-pic)
80 # No change for non-pic
81 endif
82 ifneq ($(filter-out pic no-pic,$(2)),)
83 # Technically this test is not precise, but hopefully good enough.
84 $$(error found $(2) expected pic or no-pic)
85 endif
86
上面是处理编译pic的情况
87 core_image_name := $($(3)HOST_CORE_IMG_OUT_BASE)$$(core_infix)$$(core_pic_infix)$(4)$(CORE_IMG_SUFFIX)
88 core_oat_name := $($(3)HOST_CORE_OAT_OUT_BASE)$$(core_infix)$$(core_pic_infix)$(4)$(CORE_OAT_SUFFIX)
89
正常情况下,core_pic_infix是空。
67HOST_CORE_IMG_OUT_BASE定义在art/build/Android.common_path.mk中:
67HOST_CORE_IMG_OUT_BASE := $(HOST_OUT_JAVA_LIBRARIES)/$(ART_HOST_ARCH)/core
HOST_CORE_IMG_OUTS和HOST_CORE_OAT_OUTS
90 # Using the bitness suffix makes it easier to add as a dependency for the run-test mk.
91 ifeq ($(3),)
92 $(4)HOST_CORE_IMAGE_$(1)_$(2)_64 := $$(core_image_name)
93 else
94 $(4)HOST_CORE_IMAGE_$(1)_$(2)_32 := $$(core_image_name)
95 endif
96 $(4)HOST_CORE_IMG_OUTS += $$(core_image_name)
97 $(4)HOST_CORE_OAT_OUTS += $$(core_oat_name)
98
然后后面是为wrapper处理的:
99 # If we have a wrapper, make the target phony.
100 ifneq ($(4),)
101.PHONY: $$(core_image_name)
102 endif
103$$(core_image_name): PRIVATE_CORE_COMPILE_OPTIONS := $$(core_compile_options)
104$$(core_image_name): PRIVATE_CORE_IMG_NAME := $$(core_image_name)
105$$(core_image_name): PRIVATE_CORE_OAT_NAME := $$(core_oat_name)
106$$(core_image_name): $$(HOST_CORE_DEX_LOCATIONS) $$(core_dex2oat_dependency)
host dex2oat
这个函数的核心终于出场了。
107 @echo "host dex2oat: $$@ ($$?)"
108 @mkdir -p $$(dir $$@)
109 $$(hide) $(4) $$(DEX2OAT)$(5) --runtime-arg -Xms$(DEX2OAT_IMAGE_XMS) \
110 --runtime-arg -Xmx$(DEX2OAT_IMAGE_XMX) \
111 --image-classes=$$(PRELOADED_CLASSES) $$(addprefix --dex-file=,$$(HOST_CORE_DEX_FILES)) \
112 $$(addprefix --dex-location=,$$(HOST_CORE_DEX_LOCATIONS)) --oat-file=$$(PRIVATE_CORE_OAT_NAME) \
113 --oat-location=$$(PRIVATE_CORE_OAT_NAME) --image=$$(PRIVATE_CORE_IMG_NAME) \
114 --base=$$(LIBART_IMG_HOST_BASE_ADDRESS) --instruction-set=$$($(3)ART_HOST_ARCH) \
115 --instruction-set-features=$$($(3)DEX2OAT_HOST_INSTRUCTION_SET_FEATURES) \
116 --host --android-root=$$(HOST_OUT) --include-patch-information --generate-debug-info \
117 $$(PRIVATE_CORE_COMPILE_OPTIONS)
118
119$$(core_oat_name): $$(core_image_name)
120
121 # Clean up locally used variables.
122 core_dex2oat_dependency :=
123 core_compile_options :=
124 core_image_name :=
125 core_oat_name :=
126 core_infix :=
127 core_pic_infix :=
128endef # create-core-oat-host-rules