Android. mk File Parsing in Android, androidandroid. mk
1 Overview
Everyone knows that a Makefile is often written for editing in Linux. You can understand this Makefile as a compilation configuration file, which stores the compilation configuration information, that is, it guides the compiler how to compile the program and determines the compilation result. Android. mk files are also type functions. As the name suggests, we can guess from the name, Android. the mk file is a Makefile for Android. specifically, this file is a small part of GNU Makefile and will be parsed once or multiple times by the compilation system. You can define one or more modules in each Android. mk file. You can also use the same source code file in several modules. The compilation system handles many details for you.
2. Overall structure of Android. mk
For example, the content of Android. mk is as follows:
LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES:= \ android_media_MediaPlayer.cpp \ android_media_MediaRecorder.cpp \ android_media_MediaScanner.cpp \ android_media_MediaMetadataRetriever.cpp \ android_media_ResampleInputStream.cpp \ android_media_MediaProfiles.cpp \ android_media_AmrInputStream.cpp \ android_media_Utils.cpp \ android_mtp_MtpDatabase.cpp \ android_mtp_MtpDevice.cpp \ android_mtp_MtpServer.cpp \LOCAL_SHARED_LIBRARIES := \ libandroid_runtime \ libnativehelper \ libutils \ libbinder \ libmedia \ libskia \ libui \ libcutils \ libgui \ libstagefright \ libcamera_client \ libsqlite \ libmtp \ libusbhost \ libexifLOCAL_C_INCLUDES += \ external/jhead \ external/tremor/Tremor \ frameworks/base/core/jni \ frameworks/base/media/libmedia \ frameworks/base/media/libstagefright/codecs/amrnb/enc/src \ frameworks/base/media/libstagefright/codecs/amrnb/common \ frameworks/base/media/libstagefright/codecs/amrnb/common/include \ frameworks/base/media/mtp \ $(PV_INCLUDES) \ $(JNI_H_INCLUDE) \ $(call include-path-for, corecg graphics)LOCAL_CFLAGS +=LOCAL_LDLIBS := -lpthreadLOCAL_MODULE:= libmedia_jniinclude $(BUILD_SHARED_LIBRARY)# build libsoundpool.so# build libaudioeffect_jni.soinclude $(call all-makefiles-under,$(LOCAL_PATH))
The LOCAL_PATH variable must be defined first. Then clear the values of all LOCAL_XX variables, except for the values of LOCAL_PATH, and then define the source file, header file, and then the compilation option parameter, followed by the compiled file name, and finally the generated file type.
3. The variable definition in Android. mk is 3.1 LOCAL_PATH.
An Android. mk file must first define the LOCAL_PATH variable. It is used to search for source files in the Development tree. For example:
LOCAL_PATH: = $ (call my-dir)
The Macro function 'my-dir' is provided by the compilation system and used to return the current path (that is, the directory containing the Android. mk file)
3.2 include $ (CLEAR_VARS)
The macro CLEAR_VARS is provided by the compilation system, specifying that gnu makefile can clear many LOCAL_XXX variables for you (such as LOCAL_MODULE, LOCAL_SRC_FILES, LOCAL_STATIC_LIBRARIES, and so on...), except LOCAL_PATH. This is necessary because all the compilation control files are in the same gnu make execution environment, and all the variables are global.
3.3 LOCAL_SRC_FILES
Source files to be compiled this time
3.4 LOCAL_SHARED_LIBRARIES
The dynamic link library file to be linked in this compilation, that is, the. so file.
3.5 LOCAL_STATIC_LIBRARIES
Static Link Library.
3.6 LOCAL_C_INCLUDES
The header file to be included in this compilation, a list of optional paths relative to the current directory. When compiling all source files (C, C ++, and assembly, it will be added to the include search path. For example:
LOCAL_C_INCLUDES: = sources/foo
Or even:
LOCAL_C_INCLUDES: = $ (LOCAL_PATH)/../foo
3.6 LOCAL_LDLIBS
This compilation link option is used to specify the additional library files to be referenced when compiling a specific dynamic library or executable file. Here, capital L is used to specify the path of a specific library file, lowercase l is used to specify a specific library file (under system/lib), and capital I is used to specify a specific header file search path, lowercase I specifies a specific standard library file.
3.7 LOCAL_CFLAGS
It is also the compilation option, passing additional parameters (such as macro definition) for the C compiler, for example: LOCAL_CFLAGS + =-DLIBUTILS_NATIVE = 1.
3.8 LOCAL_MODULE
The name of the generated module. This variable must be defined to indicate the name of the file to be generated after make.
3.9 LOCAL_PACKAGE_NAME
Apk file name
4 include
Include can appear in Android in this form, such as: include $ (CLEAR_VARS), include $ (BUILD_SHARED_LIBRARY). In fact, this include can be understood as "execution", so what is the execution? Of course, it's the macro behind.
The macro CLEAR_VARS has already been introduced in section 3.2, indicating to clear some variables.
The macro BUILD_SHARED_LIBRARY indicates that the shared library is generated, that is, the. so file is generated.
Therefore, include $ (BUILD_SHARED_LIBRARY) is to generate a lib $ (LOCAL_MOUDULE). so file in the/system/lib/directory. The same type of macro is shown below:
- CLEAR_VARS clear the LOCAL_xxx variable
- BUILD_SHARED_LIBRARY generates the lib $ (LOCAL_MOUDULE). so file in the/system/lib/directory.
- BUILD_STATIC_LIBRARY generate lib $ (LOCAL_MOUDULE). a file
- BUILD_EXECUTABLE generate an executable file in the/system/bin/directory
- BUILD_PACKAGE is compiled into an apk file.
Appendix:
Common gcc commands
Option description
-Ansi only supports the ANSI standard C syntax. This option will disable some features of gnu c,
For example, asm or typeof keywords.
-C only compiles and generates the target file.
-DMACRO defines the MACRO with the string "1.
-DMACRO = DEFN: Define the MACRO with the string "DEFN.
-E only runs the C pre-compiler.
-G generates debugging information. The GNU Debugger can use this information.
-IDIRECTORY: specify an additional header file to search for the path DIRECTORY.
-LDIRECTORY: specify an additional function library to search for the path DIRECTORY.
-Search for the specified LIBRARY when connecting to lLIBRARY.
-Msung optimizes code for 486.
-O FILE: generate the specified output FILE. Used to generate executable files.
-O0 is not optimized.
-O or-O1 optimized code generation.
-O2 is further optimized.
-O3 is further optimized than-O2, including the inline function.
-Shared object generation. It is usually used to create a shared library.
-Static prohibit the use of shared connections.
-UMACRO undefines MACRO macros.
-W does not generate any warning information.
-Wall generates all warning information.