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 的交叉编译器。
thumb来进行源码编译。
thumb,则强制使用 thumb的交互方式。
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建议不要修改这些文件,但我们可以修改这些文件来达到特殊目的。 |