[First written by Steve Guo, please keep the mark if forwarding.]
Device/docs/design/build-system.html is a good start point to understand Android build system. In this topic, I will describe the behind details using mm to compile an executable and shared library.
Basic
In envsetup.sh, mm macro is defined.
function mm()
{
# If we're sitting in the root of the build tree, just do a
# normal make.
if [ -f config/envsetup.make -a -f Makefile ]; then
make $@
else
# Find the closest Android.mk file.
T=$(gettop)
M=$(findmakefile)
if [ ! "$T" ]; then
echo "Couldn't locate the top of the tree. Try setting TOP."
elif [ ! "$M" ]; then
echo "Couldn't locate a makefile from the current directory."
else
ONE_SHOT_MAKEFILE=$M make -C $T files $@
fi
fi
}
In top layer Makefile
ifneq ($(ONE_SHOT_MAKEFILE),)
# We've probably been invoked by the "mm" shell function
# with a subdirectory's makefile.
include $(ONE_SHOT_MAKEFILE)
# Change CUSTOM_MODULES to include only modules that were
# defined by this makefile; this will install all of those
# modules as a side-effect. Do this after including ONE_SHOT_MAKEFILE
# so that the modules will be installed in the same place they
# would have been with a normal make.
CUSTOM_MODULES := $(sort $(call get-tagged-modules,$(ALL_MODULE_TAGS),))
FULL_BUILD :=
INTERNAL_DEFAULT_DOCS_TARGETS :=
# Stub out the notice targets, which probably aren't defined
# when using ONE_SHOT_MAKEFILE.
NOTICE-HOST-%: ;
NOTICE-TARGET-%: ;
So if we type mm in a directory, it will finally include our own Android.mk. Android will put every Android.mk into one huge Makefile.
In top layer Makefile, it includes base_rules.make, while in base_rules.make it defines a target for LOCAL_MODULE which must be specified in our own Android.mk.
# Provide a short-hand for building this module.
# We name both BUILT and INSTALLED in case
# LOCAL_UNINSTALLABLE_MODULE is set.
.PHONY: $(LOCAL_MODULE)
$(LOCAL_MODULE): $(LOCAL_BUILT_MODULE) $(LOCAL_INSTALLED_MODULE)
definitions.make contains the most important macros for building source file. Here lists the two macros for building C++ and C source files.
###########################################################
## Commands for running gcc to compile a C++ file
###########################################################
define transform-cpp-to-o
@mkdir -p $(dir $@)
@echo "target $(PRIVATE_ARM_MODE) C++: $(PRIVATE_MODULE) <= $<"
$(hide) $(PRIVATE_CXX) /
$(foreach incdir, /
$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, /
$(TARGET_PROJECT_INCLUDES) /
$(TARGET_C_INCLUDES) /
) /
$(PRIVATE_C_INCLUDES) /
, /
-I $(incdir) /
) /
-c /
$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, /
$(TARGET_GLOBAL_CFLAGS) /
$(TARGET_GLOBAL_CPPFLAGS) /
$(PRIVATE_ARM_CFLAGS) /
) /
$(PRIVATE_CFLAGS) /
$(PRIVATE_CPPFLAGS) /
$(PRIVATE_DEBUG_CFLAGS) /
-fno-rtti /
-MD -o $@ $<
$(hide) $(transform-d-to-p)
endef
###########################################################
## Commands for running gcc to compile a C file
###########################################################
# $(1): extra flags
define transform-c-or-s-to-o-no-deps
@mkdir -p $(dir $@)
$(hide) $(PRIVATE_CC) /
$(foreach incdir, /
$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, /
$(TARGET_PROJECT_INCLUDES) /
$(TARGET_C_INCLUDES) /
) /
$(PRIVATE_C_INCLUDES) /
, /
-I $(incdir) /
) /
-c /
$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, /
$(TARGET_GLOBAL_CFLAGS) /
$(PRIVATE_ARM_CFLAGS) /
) /
$(PRIVATE_CFLAGS) /
$(1) /
$(PRIVATE_DEBUG_CFLAGS) /
-MD -o $@ $<
endef
Executable
In our own Android.mk we should add two lines.
LOCAL_MODULE := ***
include $(BUILD_EXECUTABLE)
BUILD_EXECUTALE is defined in config.make.
BUILD_EXECUTABLE:= $(BUILD_SYSTEM)/executable.make
In executable.make
include $(BUILD_SYSTEM)/dynamic_binary.make
ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
$(linked_module): $(TARGET_CRTBEGIN_STATIC_O) $(all_objects) $(all_libraries) $(TARGET_CRTEND_O)
$(transform-o-to-static-executable)
else
$(linked_module): $(TARGET_CRTBEGIN_DYNAMIC_O) $(all_objects) $(all_libraries) $(TARGET_CRTEND_O)
$(transform-o-to-executable)
Endif
So here defined a new target $(linked_module).
transform-o-to-exeuctable macro is defined in defintions.make.
define transform-o-to-executable
@mkdir -p $(dir $@)
@echo "target Executable: $(PRIVATE_MODULE) ($@)"
$(hide) $(transform-o-to-executable-inner)
endef
combo/linux-arm.make contains macros to transform o to executable for ARM.
define transform-o-to-executable-inner
$(TARGET_CXX) -nostdlib -Bdynamic -Wl,-T,$(BUILD_SYSTEM)/armelf.x /
-Wl,-dynamic-linker,/system/bin/linker /
-Wl,--gc-sections /
-Wl,-z,nocopyreloc /
-o $@ /
$(TARGET_GLOBAL_LD_DIRS) /
-Wl,-rpath-link=$(TARGET_OUT_INTERMEDIATE_LIBRARIES) /
$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) /
$(TARGET_CRTBEGIN_DYNAMIC_O) /
$(PRIVATE_ALL_OBJECTS) /
$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) /
$(PRIVATE_LDFLAGS) /
$(TARGET_LIBGCC) /
$(TARGET_CRTEND_O)
endef
binary.make contains some PRIVATE_* definitions used by the above macros.
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_YACCFLAGS := $(LOCAL_YACCFLAGS)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ASFLAGS := $(LOCAL_ASFLAGS)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_CFLAGS := $(LOCAL_CFLAGS)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_CPPFLAGS := $(LOCAL_CPPFLAGS)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_DEBUG_CFLAGS := $(debug_cflags)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_C_INCLUDES := $(LOCAL_C_INCLUDES)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_LDFLAGS := $(LOCAL_LDFLAGS)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_LDLIBS := $(LOCAL_LDLIBS)
combo/linux-arm.make contains default CFLAGS/CPPFLAGS/C_INCLUDES definitions.
$(combo_target)GLOBAL_CFLAGS += /
-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 include/arch/linux-arm/AndroidConfig.h
$(combo_target)GLOBAL_CPPFLAGS += -fvisibility-inlines-hidden
$(combo_target)RELEASE_CFLAGS := /
-DSK_RELEASE -DNDEBUG /
-O2 -g /
-Wstrict-aliasing=2 /
-finline-functions /
-fno-inline-functions-called-once /
-fgcse-after-reload /
-frerun-cse-after-loop /
-frename-registers
# unless CUSTOM_KERNEL_HEADERS is defined, we're going to use
# symlinks located in out/ to point to the appropriate kernel
# headers. see 'config/kernel_headers.make' for more details
#
KERNEL_HEADERS_COMMON := system/bionic/kernel/common
KERNEL_HEADERS_ARCH := system/bionic/kernel/arch-$(TARGET_ARCH)
ifneq ($(CUSTOM_KERNEL_HEADERS),)
KERNEL_HEADERS_COMMON := $(CUSTOM_KERNEL_HEADERS)
KERNEL_HEADERS_ARCH := $(CUSTOM_KERNEL_HEADERS)
endif
KERNEL_HEADERS := $(KERNEL_HEADERS_COMMON) $(KERNEL_HEADERS_ARCH)
$(combo_target)C_INCLUDES := /
system/bionic/arch-arm/include /
system/bionic/include /
system/libstdc++/include /
$(KERNEL_HEADERS) /
system/libm/include /
system/libm/include/arch/arm /
system/libthread_db/include
Shared Library
In our own Android.mk we should add two lines.
LOCAL_MODULE := ***
include $(BUILD_SHARED_LIBRARY)
BUILD_SHARED_LIBRARY is defined in config.make.
BUILD_SHARED_LIBRARY:= $(BUILD_SYSTEM)/shared_library.make
In shared_library.make
include $(BUILD_SYSTEM)/dynamic_binary.make
$(linked_module): $(all_objects) $(all_libraries) $(LOCAL_ADDITIONAL_DEPENDENCIES)
$(transform-o-to-shared-lib)
So here defined a new target $(linked_module).
transform-o-to-shared-lib macro is defined in defintions.make.
define transform-o-to-shared-lib
@mkdir -p $(dir $@)
@echo "target SharedLib: $(PRIVATE_MODULE) ($@)"
$(hide) $(transform-o-to-shared-lib-inner)
endef
combo/linux-arm.make contains macro to transform o to shared lib for ARM.
define transform-o-to-shared-lib-inner
$(TARGET_CXX) /
-nostdlib -Wl,-soname,$(notdir $@) -Wl,-T,$(BUILD_SYSTEM)/armelf.xsc /
-Wl,--gc-sections /
-Wl,-shared,-Bsymbolic /
$(TARGET_GLOBAL_LD_DIRS) /
$(PRIVATE_ALL_OBJECTS) /
-Wl,--whole-archive /
$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) /
-Wl,--no-whole-archive /
$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) /
$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) /
-o $@ /
$(PRIVATE_LDFLAGS) /
$(TARGET_LIBGCC)
endef
Tips: “make *** showcommands”can let build system show the original compile commands.
相关推荐
因Android的Makefile (build system)文件众多,架构复杂,分析起来较为困难。本文梳理了build system的大致脉络,希望对build system感兴趣的同学们有所帮助
Android 编译文档
Gradle Recipes for Android Master the New Build System for Android 英文pdf
对android下的build system的详细描述,图文并茂,是理解android 架构的好资料。
Who This Book Is For, If you are an experienced Android developer wanting to enhance your skills with the Gradle Android build system, then this book is for you. As a prerequisite, you will need some...
If you are an experienced Android developer wanting to enhance your skills with the Gradle Android build system, then this book is for you. As a prerequisite, you will need some knowledge of the ...
1 Android build system 4 1.1 Basic 4 1.2 Executable 6 1.3 Shared Library 9 2 Android Initialization Process 11 3 Android JAVA AIDL Analysis 15 4 Android JAVA Binder IPC System 20 5 Android Media ...
Android Build System 4 In this document 4 Building the Android Platform 6 Building the Android Kernel 8 Build Variants 9 Configuring a New Product 10 In this document 10 Detailed Instructions 11 New ...
Android build system.pdf Bluetooth.pdf Audio.pdf Android Media Scanner Process.pdf Talking about Android process.pdf Android Initialization Process.pdf surfaceflinger.pdf Android Build System by ...
The First Practical, Hands-On Guide to Embedded System Programming for Android Today, embedded systems programming is a more valuable discipline than ever, driven by fast-growing, new fields such ...
以下是 Android 手机 build.prop 信息,可同时在java 和 NDK获取到 下面是通过 adb shell cat /system/build.prop 获取到的信息 # begin build properties开始设置系统性能 # autogenerated by buildinfo.sh{通过...
Chapters also cover advanced features of Android Studio such as Gradle build configuration and the implementation of build variants to target multiple Android device types from a single project code ...
Day 3_ Graphics and Camera Android Bootcamp 2017 - Color Management Android Bootcamp 2017 — deqp Android Bootcamp 2017 — Updateable Graphics Drivers ...Android Bootcamp 2017—Soong Build System
Android Gradle插件 原始源代码,请参见
Android 中build.prop 文件与 getprop 命令 在android系统中,它的根文件系统下有几个用于启动系统时需要的配置文件: /init.rc /default.prop ...root@android:/system # cat build.prop cat build.p