Android makefile Analysis

Source: Internet
Author: User
Tags automake

With the development of the mobile Internet, mobile development is becoming more and more popular. Currently, Android is the most popular. No need to mention Android. Android has been sought after by many people since its open source. Of course, one person is pursuing it because it was developed by Google. For a program
Personnel, a system value is not worth the pursuit of code to speak. I am not going to analyze the android code here, but the android makefile. Maybe you already know that in the android source code, we can see a lot of makefile files. At first, I didn't understand them, after a period of research, I gradually understood that I want to analyze
Andorid makefile to show you how to write makefile.
For new programmers, good ide is the target of their pursuit. However, when there is more code, he will gradually find that IDE is not enough, because there are a lot of things that IDE is not easy to do,
For example, automatic compilation, testing, version control, and compilation customization. This is a bit like a political saying: capitalism started to promote productivity development, but later it became a factor hindering productivity development.
. If a program cannot get rid of IDE restrictions (not unnecessary, but selective), it will be difficult to improve. You know, IDE and makefile represent two different ideas.
Thought: ide emphasizes simplifying the interaction between computers and users, while makefile reflects automation.
Makefile may be easier to learn for a person who is familiar with Linux from the very beginning (practice makes perfect). For a person who is familiar with windows from the very beginning
Makefile is hard to learn. This is mainly because you may unconsciously use Visual Studio (
Studio is a good thing, especially its debugging ). I don't know whether it feels like shouting: if a person first contacts C and then Java, it will be easier; if a person first contacts Java,
If you contact C again, you will be disgusted with C.
Here are some of the makefile descriptions provided by Baidu Encyclopedia:
The source files in a project are not counted. They are stored in several directories by type, function, and module. makefile defines a series of rules to specify which files need to be compiled first and which files need to be compiled first.
After compilation is required, which files need to be re-compiled, or even perform more complex functional operations, because makefile is like a shell script, which can also execute operating system commands.
The benefit of makefile is "automatic compilation". Once written, only one make command is required, and the entire project is fully automatically compiled, which greatly improves the efficiency of software development. Make is a command tool that explains commands in makefile. Generally, most ides use this command, such as make in Delphi and nmake in Visual C ++, GNU make in Linux. It can be seen that makefile is a compilation method in Engineering.
The most important and basic function of the make tool is to use the MAKEFILE file to describe the relationship between source programs and automatically maintain the compilation work.
While makefile
The file needs to be written according to a certain syntax. The file must describe how to compile each source file and generate an executable file, and define the dependency between the source files. Makefile
Files are a common method for maintaining compilation information in many compilers, including compilers in Windows NT. They are modified on a friendly interface in the integrated development environment.
MAKEFILE file.
For Android, Android uses GNU make, so its makefile format is also the GNU makefile format. Network Access now
The best document for makefile is Chen Hao's "Write makefile with me". This document describes makefile in detail. Therefore, we recommend that you read this document first (Electronic
The version can be

Http://download.csdn.net/detail/andy_android/3783424

Download.

First, let's take a look at the makefile Writing Method in Android.

(1) In the Android. mk file, you must specify the local_path variable to find the source file. Generally
Android. mk is in the same directory as the source file to be compiled, so it is defined as follows:
Local_path: = $ (call my-DIR)
The preceding statement defines the local_path variable as the directory path of the cost file.

(2) Multiple compilation modules can be defined in Android. mk. Each compilation module starts with include $ (clear_vars ).
End with include $ (build_xxx.
Include $ (clear_vars)
Clear_vars is provided by the compilation system, specifying that GNU makefile will clear all local_xxx variables except local_path for you,
For example, local_module, local_src_files, local_shared_libraries, and local_static_libraries.
Include $ (build_static_library) indicates compiling to a static library
Include $ (build_shared_library) indicates compiling to a dynamic library.
Include $ (build_executable) indicates compiling into executable programs

(3) Example (frameworks/base/libs/audioflinger/Android. mk ):
Local_path: = $ (call my-DIR)
Include $ (clear_vars) Module 1
Ifeq ($ (audio_policy_test), true)
Enable_audio_dump: = true
Endif
Local_src_files: = \
Audiohardwaregeneric. cpp \
Audiohardwarestub. cpp \
Audiohardwareinterface. cpp
Ifeq ($ (enable_audio_dump), true)
Local_src_files + = audiodumpinterface. cpp
Local_cflags + =-denable_audio_dump
Endif
Local_shared_libraries: = \
Libcutils \
Libutils \
Libbinder \
Libmedia \
Libhardware_legacy
Ifeq ($ (Strip $ (board_uses_generic_audio), true)
Local_cflags + =-dgeneric_audio
Endif
Local_module: = libaudiointerface
Ifeq ($ (board_have_bluetooth), true)
Local_src_files + = a2dpaudiointerface. cpp
Local_shared_libraries + = liba2dp
Local_cflags + =-dwith_bluetooth-dwith_a2dp
Local_c_includes + = $ (call include-path-for, bluez)
Endif
Include $ (build_static_library) Module 1 is compiled into a static library
Include $ (clear_vars) Module 2
Local_src_files: = \
Audiopolicymanagerbase. cpp
Local_shared_libraries: = \
Libcutils \
Libutils \
Libmedia
Ifeq ($ (target_simulator), true)
Local_ldlibs + =-LDL
Else
Local_shared_libraries + = libdl
Endif
Local_module: = libaudiopolicybase
Ifeq ($ (board_have_bluetooth), true)
Local_cflags + =-dwith_a2dp
Endif
Ifeq ($ (audio_policy_test), true)
Local_cflags + =-daudio_policy_test
Endif
Include $ (build_static_library) Module 2 is compiled into a static library
Include $ (clear_vars) Module 3
Local_src_files: = \
Audioflinger. cpp \
Audiomixer. cpp. Arm \
Audioresampler. cpp. Arm \
Audioresamplersinc. cpp. Arm \
Audioresamplercubic. cpp. Arm \
Audiopolicyservice. cpp
Local_shared_libraries: = \
Libcutils \
Libutils \
Libbinder \
Libmedia \
Libhardware_legacy
Ifeq ($ (Strip $ (board_uses_generic_audio), true)
Local_static_libraries + = libaudiointerface libaudiopolicybase
Local_cflags + =-dgeneric_audio
Else
Local_shared_libraries + = libaudio libaudiopolicy
Endif
Ifeq ($ (target_simulator), true)
Local_ldlibs + =-LDL
Else
Local_shared_libraries + = libdl
Endif
Local_module: = libaudioflinger
Ifeq ($ (board_have_bluetooth), true)
Local_cflags + =-dwith_bluetooth-dwith_a2dp
Local_shared_libraries + = liba2dp
Endif
Ifeq ($ (audio_policy_test), true)
Local_cflags + =-daudio_policy_test
Endif
Ifeq ($ (target_simulator), true)
Ifeq ($ (host_ OS), Linux)
Local_ldlibs + =-LRT-lpthread
Endif
Endif
Ifeq ($ (board_use_lvmx), true)
Local_cflags + =-dlvmx
Local_c_includes + = vendor/NXP
Local_static_libraries + = liblifevibes
Local_shared_libraries + = liblvmxservice
# Local_shared_libraries + = liblvmxipc
Endif
Include $ (build_shared_library) Module 3 is compiled into a dynamic library

(4) Compile an application (APK)
Local_path: = $ (call my-DIR)
Include $ (clear_vars)

# Build all java files in the Java subdirectory --> literal translation (all java files created in the Java subdirectory)
Local_src_files: = $ (call all-subdir-Java-files)

# Name of the apk to build --> literal translation (name of the created APK)
Local_package_name: = localpackage

# Tell it to build an APK --> (tell it to build an APK)
Include $ (build_package)

(5) Compile an application dependent on the static Java Library (static. Jar)
Local_path: = $ (call my-DIR)
Include $ (clear_vars)

# List of static libraries to include in the package
Local_static_java_libraries: = static-Library

# Build all java files in the Java subdirectory
Local_src_files: = $ (call all-subdir-Java-files)

# Name of the apk to build
Local_package_name: = localpackage

# Tell it to build an APK
Include $ (build_package)

(6) Compile an application that requires the signature of the platform key.
Local_path: = $ (call my-DIR)
Include $ (clear_vars)

# Build all java files in the Java subdirectory
Local_src_files: = $ (call all-subdir-Java-files)

# Name of the apk to build
Local_package_name: = localpackage

Local_certificate: = Platform

# Tell it to build an APK
Include $ (build_package)

(7) Compile an application that requires a specific key.
Local_path: = $ (call my-DIR)
Include $ (clear_vars)

# Build all java files in the Java subdirectory
Local_src_files: = $ (call all-subdir-Java-files)

# Name of the apk to build
Local_package_name: = localpackage

Local_certificate: = vendor/example/certs/APP

# Tell it to build an APK
Include $ (build_package)

(8) Add a pre-compiled Application
Local_path: = $ (call my-DIR)
Include $ (clear_vars)

# Module name shoshould match APK name to be installed.
Local_module: = localmodulename
Local_src_files: = ((local_moduleapps.apk
Local_module_class: = apps
Local_module_suffix: = $ (common_android_package_suffix)

Include $ (build_prebuilt)

(9) Add a static Java Library
Local_path: = $ (call my-DIR)
Include $ (clear_vars)

# Build all java files in the Java subdirectory
Local_src_files: = $ (call all-subdir-Java-files)

# Any libraries that this library depends on
Local_java_libraries: = Android. Test. Runner

# The Name Of The JAR file to create
Local_module: = sample

# Build a static JAR file.
Include $ (build_static_java_library)

(10) The compilation module of Android. mk can define the relevant compilation content, that is, specify the relevant variables as follows:
Local_aapt_flags

Local_acp_unavailable

Local_additional_java_dir
 
Local_aidl_regiondes

Local_allow_undefined_symbols

Local_arm_mode

Local_asflags

Local_asset_dir

Local_asset_files sets this variable when compiling the application (build_package) in the Android. mk file, which indicates the resource file,
It is usually defined as local_asset_files + = $ (call find-subdir-assets)
 
Local_built_module_stem
Local_c_includes: the path of the header file for compiling C/C ++. Use local_path to indicate the directory of the file.
Example:
Local_c_includes + = extlibs/zlib-1.2.3
Local_c_includes + = $ (local_path)/src
 
Local_cc specifies the C Compiler

Local_certificate signature authentication

Local_cflags defines additional flag (such as macro definition) for the C/C ++ compiler. For example: local_cflags + =-dlibutils_native = 1
 
Local_classpath

Local_compress_module_symbols

The header file to be copied when local_copy_headers install the application. local_copy_headers_to must be defined at the same time.
 
Local_copy_headers_to the destination path for copying the header file when installing the application

Local_cpp_extension if your c ++ file is not suffixed with CPP, you can use local_cpp_extension to specify the suffix of the C ++ file.
For example: local_cpp_extension: =. CC
Note that the C ++ file suffixes must be consistent in the Unified Module.

Local_cppflags transmits additional flags to the C ++ compiler, such as: local_cppflags + =-ffriend-Injection

Local_cxx specifies the C ++ Compiler
 
Local_dx_flags

Local_export_package_resources

Local_force_static_executable if the compiled executable program requires a static Link (the execution does not depend on any dynamic library), set local_force_static_executable: = true

Currently, only libc has a static library, which is only used by applications in the/sbin directory of the file system.
Other parts of the file system have not been loaded, so static links must be made.
 
Local_generated_sources
 
Local_instrumentation_for

Local_instrumentation_for_package_name

Local_intermediate_sources

Local_intermediate_targets

Local_is_host_module

Local_jar_manifest

Local_jarjar_rules

Local_java_libraries specifies the included Java class library when compiling Java applications and libraries. Currently, there are two types: core and framework.
In most cases, it is defined as: local_java_libraries: = core framework
Note: local_java_libraries is not mandatory and cannot be defined during APK compilation (automatically added)
 
Local_java_resource_dirs

Local_java_resource_files

Local_jni_shared_libraries

Local_ldflags pass additional parameters to the connector (note the order of parameters)
 
Local_ldlibs specifies an additional library for compilation of executable programs or libraries. The specified library is in the "-LXXX" format. For example:
Local_ldlibs + =-lcurses-lpthread
Local_ldlibs + =-wl,-Z, Origin
 
Name of the module generated by local_module (note that the Application name is local_package_name instead of local_module)

Path of the module generated by local_module_path
 
Local_module_stem
 
Mark of the module generated by local_module_tags
 
Local_no_default_compiler_flags

Local_no_emma_compile

Local_no_emma_instrument

Local_no_standard_libraries

Local_overrides_packages

Local_package_name The Name Of The APK Application

Local_post_process_command
 
Local_prebuilt_executables used to pre-compile including $ (build_prebuilt) or $ (build_host_prebuilt), specify the executable file to be copied

Local_prebuilt_java_libraries

Local_prebuilt_libs used to pre-compile including $ (build_prebuilt) or $ (build_host_prebuilt), specifying the database to be copied.

Local_prebuilt_obj_files

Local_prebuilt_static_java_libraries
 
Local_prelink_module requires pre-connection processing (required by default for dynamic library optimization)

Local_required_modules specifies the module on which the module depends to run (the module will be installed synchronously during module installation)
 
Local_resource_dir

Local_sdk_version

Local_shared_libraries can be used to link dynamic libraries.
 
Local_src_files

 
Local_static_java_libraries

Local_static_libraries can be linked to a static library
 
Local_uninstallable_module

Local_unstripped_path
 
Local_whole_static_libraries specifies the complete static library to be loaded by the module (these master libraries do not allow the linker to delete useless code in the Link)
 
Local_yaccflags
 
Override_built_module_path

Next, let's take a closer look at the MAKEFILE file in Android.

The top-level directory structure of Android is as follows:
.
| -- Makefile (Global makefile)
| -- Bionic)
| -- Bootloader)
| -- Build (the content in the build directory is not the code used by the target, but the scripts and tools required for compilation and configuration)
| -- Dalvik (Java Virtual Machine)
| -- Development (templates and tools required for Program Development)
| -- External (some libraries used by the target machine)
| -- Frameworks (framework layer of the Application)
| -- Hardware (hardware-related Library)
| -- Kernel (source code of linux2.6)
| -- Packages (various Android applications)
| -- Prebuilt (preset scripts compiled by android on various platforms)
| -- Recovery (related to the recovery function of the target)
'-- System (some underlying libraries of Android)
This article will analyze makefile and shell files under the build directory. The Android code is version 1.5.
The main directory structure is as follows:
1. Get started with makefile
1.1 makefile helloworld
1.2 use makefile to build a cross-compiling environment
1.3 techniques in makefile
2. Android makefile Analysis
2.1 Android Shell Analysis
2.2 makefile Analysis in Android build
3. Android. mk Analysis in other android Directories

First, let's take a look at the makefile of andoroid through some articles on the Internet.
1.
Android build system
2.
Android Building System Analysis
3.
Android build system (Introduction)
1.1 makefile helloworld

MakefileThe rules are as follows:

Target...: prerequisites...

Command ......

Target can be a target file, an object file (such as helloworld. OBJ), an execution file, and a tag.

Prerequisites is the file or target required to generate the target.

Command
That is, the command to be executed to reach the target. Here we do not say "Use the command to generate the target" because the target may be a tag. Note that
The command must be preceded by a tab key rather than a space. Therefore, those who prefer to replace the tab key with spaces in the editor must be especially careful.

We generally write programs like helloworld. After we write a C helloworld, how should we write helloworld to compile helloworld. C?

The following is the makefile for compiling helloworld.

Helloworld: helloworld. o

CC-O helloworld. o

Helloworld. O: helloworld. c

CC-C main. c

Clean:

Rm helloworld helloworl. o

Then we can execute make to compile helloworld. C, and execute make clean to clear the compilation result (in fact, it is to delete helloworld helloworl. O ).

Some people may ask why helloworld is generated when make is executed? Starting from the default processing of make: Make regards the first target of makefile as the final

Target. All rules on which the rule depends will be executed. Otherwise, the rule will not be executed. Therefore, when executing make, the clean rule is not executed.

Upper
The following is the simplest makefile. For example, you can use variables and implicit rules to execute shell commands (common words are
String processing and File Processing). These rules are not introduced here. Later, we will analyze the makefile of Android based on the specific code.
I will write makefile together to learn more.

The general structure of makefile is the program tree, as shown below:



In this way, it is easy to write makefile. We will use the target as the first rule, break down the target into sub-targets, write rules one by one, and so on, until the bottom rule is easily implemented. In fact, this is very similar to the divide and conquer method in the algorithm, and divide and conquer a complicated problem.

Description
To the tree, I think of the syntax analysis in the compilation principle. There are top-bottom analysis methods and bottom-bottom analysis methods in the syntax analysis. Of course, makefile is not used for syntax analysis, but for syntax analysis.
Method to Analyze and analyze the opposite. (Syntax analysis is about whether a sentence can be introduced according to the syntax, while makefile is about to generate a command according to the rule.
Execution queue .) However, makefile rules are similar to lexical analysis. The following is an example of the compilation principle. You can understand the differences between makefile and lexical analysis.
Similarities:

->
-> | ε
->
-> | ε
-> +
->-
->
-> =


Finally, we will introduce autoconfautomake, which can be used to automatically generate makefile.



Slave
As shown in the figure above, we can use autoscan to generate a file called Configure. Scan Based on the code, and then edit the file with the parameter
Configure. In file. Then we write a makefile. Am file, and then we can use automake to generate makefile. In. Finally, according
Makefile. In and configure can generate makefile. In many open-source projects, we can see that
Makefile. am, configure. In, makefine. In, and configure files. You may also see a very complex makefile
When learning makefile, many people want to learn it by reading this file, but they finally find it too complicated. If we know that this file is automatically generated, we will understand this MAKEFILE file.
Why is this complicated.

For more details about tools such as automake, refer
Http://www.ibm.com/developerworks/cn/linux/l-makefile/
.
1.2 use makefile to build a cross-compiling environment

For more information, see
Http://blog.csdn.net/absurd/category/228434.aspx
In the cross-compilation scenario analysis, I just want to explain the steps I have taken:

1. Download the cross-compilation environment (
Http://www.codesourcery.com/downloads/public/gnu_toolchain/arm-none-linux-gnueabi
) And install it. Generally, unzip the package and add the bin directory to the path of the environment variable ~ /. Add a row at the bottom of bashrc: Export Path = $ path :~ /Arm-2009q1/bin.

2. In the user's home directory (Cd ~) Create a directory named cross-compile

3. Create a file cross. env in cross-compile. The content is as follows:

Export work_dir = ~ /Cross-compile
Export rootfs_dir = $ work_dir/rootfs
Export arch = arm
Export pkg_config_path = $ rootfs_dir/usr/local/lib/pkgconfig: $ rootfs_dir/usr/x11r6/lib/pkgconfig
If [! -E "$ rootfs_dir/usr/local/include"]; then mkdir-p $ rootfs_dir/usr/local/include; FI;
If [! -E "$ rootfs_dir/usr/local/lib"]; then mkdir-p $ rootfs_dir/usr/local/LIB; FI;
If [! -E "$ rootfs_dir/usr/local/etc"]; then mkdir-p $ rootfs_dir/usr/local/etc; FI;
If [! -E "$ rootfs_dir/usr/local/bin"]; then mkdir-p $ rootfs_dir/usr/local/bin; FI;
If [! -E "$ rootfs_dir/usr/local/share"]; then mkdir-p $ rootfs_dir/usr/local/share; FI;
If [! -E "$ rootfs_dir/usr/local/Man"]; then mkdir-p $ rootfs_dir/usr/local/man; FI;
If [! -E "$ rootfs_dir/usr/include"]; then mkdir-p $ rootfs_dir/usr/include; FI;
If [! -E "$ rootfs_dir/usr/lib"]; then mkdir-p $ rootfs_dir/usr/lib; FI;
If [! -E "$ rootfs_dir/usr/etc"]; then mkdir-p $ rootfs_dir/usr/etc; FI;
If [! -E "$ rootfs_dir/usr/bin"]; then mkdir-p $ rootfs_dir/usr/bin; FI;
If [! -E "$ rootfs_dir/usr/share"]; then mkdir-p $ rootfs_dir/usr/share; FI;
If [! -E "$ rootfs_dir/usr/Man"]; then mkdir-p $ rootfs_dir/usr/man; FI;

4. Enable the command line, go to the Cross-compile directory, and run. Cross. Env.

5. copy the header files and so files generated during Linux compilation to the directory corresponding to rootfs/usr under the Cross-compile directory (the header files can be copied to PCs, so files must be copied to the arm version ).

5. Download the source code to be compiled and put it in the Cross-compile directory.

6. Follow
Http://blog.csdn.net/absurd/category/228434.aspx
And put the MAKEFILE file in the Cross-compile directory.

7. Execute make-F libxml2.mk on the command line (libxml2.mk is the makefile written in the previous step)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.