Android编译环境 相关


1、  # make clean-helloworld
“make clean-$(LOCAL_MODULE)”是Android编译环境提供的make clean的方式。
Android编译环境提供了”showcommands”选项来显示编译命令行,我们可以通过打开这个选项来查看一些编译时的细节。

2、使用showcommands选项重新编译helloworld:
# make helloworld showcommands

build/core/product_config.mk:229: WARNING: adding test OTA key
target thumb C: helloworld <= development/hello/hello.c
prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -I system/core/include -I hardware/libhardware/include -I hardware/ril/include -I dalvik/libnativehelper/include -I frameworks/base/include -I external/skia/include -I out/target/product/generic/obj/include -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libstdc++/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -I bionic/libm/include -I bionic/libm/include/arch/arm -I bionic/libthread_db/include -I development/hello -I out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates -c -fno-exceptions -Wno-multichar -march=armv5te -mtune=xscale -msoft-float -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -include system/core/include/arch/linux-arm/AndroidConfig.h -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -DSK_RELEASE -DNDEBUG -O2 -g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-registers -DNDEBUG -UDEBUG -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -MD -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o development/hello/hello.c
target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)
prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-g++ -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lstdc++ -lm out/target/product/generic/obj/lib/crtbegin_dynamic.o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o -Wl,--no-undefined prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/../lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a out/target/product/generic/obj/lib/crtend_android.o
target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)
out/host/linux-x86/bin/acp -fpt out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld out/target/product/generic/symbols/system/bin/helloworld
target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)
out/host/linux-x86/bin/soslim --strip --shady --quiet out/target/product/generic/symbols/system/bin/helloworld --outfile out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld
Install: out/target/product/generic/system/bin/helloworld
out/host/linux-x86/bin/acp -fpt out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld out/target/product/generic/system/bin/helloworld 

从上面的命令行可以看到,Android编译环境所用的交叉编译工具链是prebuilt/linux-x86/toolchain /arm-eabi-4.2.1/bin/arm-eabi-gcc,-I和-L参数指定了所用的C库头文件和动态库文件路径分别是bionic /libc/include 和out/target/product/generic/obj/lib,其他还包括很多编译选项以及-D所定义的预编译宏。

我们可以利用上面的编译命令,稍加简化来手工编译helloworld程序。先手工删除上次编译得到的helloworld程序:
# rm out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o
# rm out/target/product/generic/system/bin/helloworld
再用gcc编译,生成目标文件:
# prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -c -fno-exceptions -Wno-multichar -march=armv5te -mtune=xscale -msoft-float -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -include system/core/include/arch/linux-arm/AndroidConfig.h -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -DSK_RELEASE -DNDEBUG -O2 -g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-registers -DNDEBUG -UDEBUG -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -MD -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o development/hello/hello.c
与Android.mk编译参数比较,上面主要减少了不必要的-I参数。

接下来生成可执行文件:
# prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lm out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o out/target/product/generic/obj/lib/crtbegin_dynamic.o -Wl,--no-undefined ./prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/../lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a out/target/product/generic/obj/lib/crtend_android.o
这里值得留意的是参数“-Wl,-dynamic-linker,/system/bin/linker”,它指定了Android专用的动态链接器/system/bin/linker,而不是通常所用的ld.so。


3、生成的可执行程序可用file和readelf命令来查看一下:
# file out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld
out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

# readelf -d out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld |grep NEEDED
0x00000001 (NEEDED) Shared library: [libc.so]
0x00000001 (NEEDED) Shared library: [libm.so]
这是ARM格式的动态链接可执行文件,运行时需要libc.so和libm.so。“not stripped”表示它还没被STRIP。嵌入式系统中为节省空间通常将编译完成的可执行文件或动态库进行STRIP,即去掉其中多余的符号表信息。在 前面“make helloworld showcommands”命令的最后我们也可以看到,Android编译环境中使用了out/host/linux-x86/bin/soslim工具 进行STRIP。
有关Android Toolchain的其他一些内容可参考:Android Toolchain与Bionic Libc

4、由linux-arm.mk 开始分析 先看看注释部分(eclair)


1 # Configuration for Linux on ARM.
  2 # Included by combo/select.make
  3
  4 # You can set TARGET_ARCH_VARIANT to use an arch version other
  5 # than ARMv5TE. Each value should correspond to a file named
  6 # $(BUILD_COMBOS)/arch/<name>.mk which must contain
  7 # makefile variable definitions similar to the preprocessor
  8 # defines in system/core/include/arch/<combo>/AndroidConfig.h. Their
  9 # purpose is to allow module Android.mk files to selectively compile
 10 # different versions of code based upon the funtionality and
 11 # instructions available in a given architecture version.
 12 #
 13 # The blocks also define specific arch_variant_cflags, which
 14 # include defines, and compiler settings for the given architecture
 15 # version.
 16 #
 17 ifeq ($(strip $(TARGET_ARCH_VARIANT)),)
 18 TARGET_ARCH_VARIANT := armv5te
 19 endif
你可以通过设定TARGET_ARCH_VARIANT=yournane而不采用android默认使用的ARMv5TE指令集。你的yourname的值应该当和$(BUILD_COMBOS)/arch/<name>.mk中的name名称相同。

system/core/include/arch/linux-arm/AndroidConfig.h中定义了arm编译时的一些定义供C/C++程序引用,这些定义的目的是允许Android.mk 可以根据指定的硬件架构的功能和指令的不同可以有选择性进行编译。

20
 21 # TARGET_ARCH_VARIANT used to be called TARGET_ARCH_VERSION
 22 # to avoid any weirdness, issue an error message if the latter
 23 # is defined.
 24 #
 25 ifneq ($(strip $(TARGET_ARCH_VERSION)),)
 26 $(info Definition for TARGET_ARCH_VERSION encountered !)
 27 $(info This variable has been renamed TARGET_ARCH_VARIANT, please update your build files !!)
 28 $(error Aborting the build.)
 29 endif
 30
 31 TARGET_ARCH_SPECIFIC_MAKEFILE := $(BUILD_COMBOS)/arch/$(TARGET_ARCH)/$(TARGET_ARCH_VARIANT).mk
 32 ifeq ($(strip $(wildcard $(TARGET_ARCH_SPECIFIC_MAKEFILE))),)
 33 $(error Unknown ARM architecture version: $(TARGET_ARCH_VARIANT))
 34 endif
 35
 36 include $(TARGET_ARCH_SPECIFIC_MAKEFILE)

TARGET_ARCH_VARIANT 会被 TARGET_ARCH_VERSION调用, TARGET_ARCH_VERSION不能被直接声明,否则会报错。
根据 TARGET_ARCH_VARIANT的设置加载对应的build/core/combo/arch/arm/armv???.mk文件。

38 # You can set TARGET_TOOLS_PREFIX to get gcc from somewhere else
 39 ifeq ($(strip $($(combo_target)TOOLS_PREFIX)),)
 40 $(combo_target)TOOLS_PREFIX := \
 41     prebuilt/$(HOST_PREBUILT_TAG)/toolchain/arm-eabi-4.4.0/bin/arm-eabi-
 42 endif
 43
 44 $(combo_target)CC := $($(combo_target)TOOLS_PREFIX)gcc$(HOST_EXECUTABLE_SUFFIX)
 45 $(combo_target)CXX := $($(combo_target)TOOLS_PREFIX)g++$(HOST_EXECUTABLE_SUFFIX)
 46 $(combo_target)AR := $($(combo_target)TOOLS_PREFIX)ar$(HOST_EXECUTABLE_SUFFIX)
 47 $(combo_target)OBJCOPY := $($(combo_target)TOOLS_PREFIX)objcopy$(HOST_EXECUTABLE_SUFFIX)
 48 $(combo_target)LD := $($(combo_target)TOOLS_PREFIX)ld$(HOST_EXECUTABLE_SUFFIX)
 49
 50 $(combo_target)NO_UNDEFINED_LDFLAGS := -Wl,--no-undefined
 51
 52 TARGET_arm_CFLAGS :=    -O2 \
 53                         -fomit-frame-pointer \
 54                         -fstrict-aliasing    \
 55                         -funswitch-loops     \
 56                         -finline-limit=300

如果你想采用自己的交叉编译器,可以通过设定 TARGET_TOOLS_PREFIX来达到效果。否则android会采用自己的prebuild 的交叉编译器。


58 # Modules can choose to compile some source as thumb. As
59 # non-thumb enabled targets are supported, this is treated
60 # as a 'hint'. If thumb is not enabled, these files are just
61 # compiled as ARM.
62 ifeq ($(ARCH_ARM_HAVE_THUMB_SUPPORT),true)
63 TARGET_thumb_CFLAGS := -mthumb \
64 -Os \
65 -fomit-frame-pointer \
66 -fno-strict-aliasing \
67 -finline-limit=64
68 else
69 TARGET_thumb_CFLAGS := $(TARGET_arm_CFLAGS)
70 endif

可以设定是有采用 thumb来进行源码编译。

72 # Set FORCE_ARM_DEBUGGING to "true" in your buildspec.mk
 73 # or in your environment to force a full arm build, even for
 74 # files that are normally built as thumb; this can make
 75 # gdb debugging easier.  Don't forget to do a clean build.
 76 #
 77 # NOTE: if you try to build a -O0 build with thumb, several
 78 # of the libraries (libpv, libwebcore, libkjs) need to be built
 79 # with -mlong-calls.  When built at -O0, those libraries are
 80 # too big for a thumb "BL <label>" to go from one end to the other.
 81 ifeq ($(FORCE_ARM_DEBUGGING),true)
 82   TARGET_arm_CFLAGS += -fno-omit-frame-pointer -fno-strict-aliasing
 83   TARGET_thumb_CFLAGS += -marm -fno-omit-frame-pointer
 84 endif
 85
 86 android_config_h := $(call select-android-config-h,linux-arm)
 87 arch_include_dir := $(dir $(android_config_h))
 88
 89 $(combo_target)GLOBAL_CFLAGS += \
 90             -msoft-float -fpic \
 91             -ffunction-sections \
 92             -funwind-tables \
 93             -fstack-protector \
 94             -fno-short-enums \
 95             $(arch_variant_cflags) \
 96             -include $(android_config_h) \
 97             -I $(arch_include_dir)
 98
 99 $(combo_target)GLOBAL_LDFLAGS += \
100             $(arch_variant_ldflags)

在CFLAGS 上加载 system/core/include/linux-arm/AndroidConfig.h 文件 和 头文件的路径
在LDFLAGS 上记载库路径。



102 # We only need thumb interworking in cases where thumb support
103 # is available in the architecture, and just to be sure, (and
104 # since sometimes thumb-interwork appears to be default), we
105 # specifically disable when thumb support is unavailable.
106 ifeq ($(ARCH_ARM_HAVE_THUMB_SUPPORT),true)
107 $(combo_target)GLOBAL_CFLAGS += -mthumb-interwork
108 else
109 $(combo_target)GLOBAL_CFLAGS += -mno-thumb-interwork
110 endif
111
112 $(combo_target)GLOBAL_CPPFLAGS += -fvisibility-inlines-hidden
113
114 $(combo_target)RELEASE_CFLAGS := \
115 -DSK_RELEASE -DNDEBUG \
116 -g \
117 -Wstrict-aliasing=2 \
118 -finline-functions \
119 -fno-inline-functions-called-once \
120 -fgcse-after-reload \
121 -frerun-cse-after-loop \
122 -frename-registers
123
124 libc_root := bionic/libc
125 libm_root := bionic/libm
126 libstdc++_root := bionic/libstdc++
127 libthread_db_root := bionic/libthread_db

如果支持 thumb,则强制使用 thumb的交互方式。



130 ## on some hosts, the target cross-compiler is not available so do not run this command
131 ifneq ($(wildcard $($(combo_target)CC)),)
132 # We compile with the global cflags to ensure that
133 # any flags which affect libgcc are correctly taken
134 # into account.
135 $(combo_target)LIBGCC := $(shell $($(combo_target)CC) $($(combo_target)GLOBAL_CFLAGS) -print-libgcc-file-name)
136 endif

忽略



138 # unless CUSTOM_KERNEL_HEADERS is defined, we're going to use
139 # symlinks located in out/ to point to the appropriate kernel
140 # headers. see 'config/kernel_headers.make' for more details
141 #
142 ifneq ($(CUSTOM_KERNEL_HEADERS),)
143 KERNEL_HEADERS_COMMON := $(CUSTOM_KERNEL_HEADERS)
144 KERNEL_HEADERS_ARCH := $(CUSTOM_KERNEL_HEADERS)
145 else
146 KERNEL_HEADERS_COMMON := $(libc_root)/kernel/common
147 KERNEL_HEADERS_ARCH := $(libc_root)/kernel/arch-$(TARGET_ARCH)
148 endif
149 KERNEL_HEADERS := $(KERNEL_HEADERS_COMMON) $(KERNEL_HEADERS_ARCH)

如果没有对 CUSTOM_KERNEL_HEADERS 进行定义,那么默认的系统头文件位置在out为根目录的位置。

下面就是一些对编译选项的一些具体定义。


总结分析:
可以看出,对于具体的硬件平台移植这里边有一个重要的宏
TARGET_ARCH_VARIANT 和其关联的两个重要的文件 build/core/combo/arch/arm/armv???.mk 以及
system/core/include/arch/linux-arm/AndroidConfig.h, 这些确定你编译的细节。
TARGET_ARCH_VARIANT应该可以设定在你的 product.mk中。
虽然android建议不要修改这些文件,但我们可以修改这些文件来达到特殊目的。

你可能感兴趣的:(android,Build,tools,makefile,wildcard,debugging)