Android.bp syntax and usage

1. What is the Android.bp file?

        The Android.bp file is firstly a compilation configuration file of the Android system, which is used to replace the original Android.mk
file. Before Android7.0, Android used make to organize the compilation of each module, and the corresponding compilation
configuration file was Android.mk.

        Starting from Android7.0, Google introduced ninja and kati to compile, why introduce ninja? Because as Android is getting bigger and bigger, there are more and more modules, and the compilation time is getting longer and longer, and using ninja has a great improvement in the concurrent processing of compilation compared with make. The configuration file of Ninja is Android.bp, and the Android system uses Blueprint and Soong tools to analyze Android.bp and convert it into a ninja file. In order to be compatible with the old mk configuration files, Android also developed the Kati tool to convert mk files to generate ninja. Currently, Android Q still supports the Android.mk method. I believe that in future versions, the mk file will be completely discarded, and Kati will be eliminated at the same time, only the bp configuration method will be retained, so we have to learn
bp in advance.

        This involves the concepts of Ninja, kati, Soong, and bp, and they will be briefly introduced next.

1.1 Ninja

Ninja is a compilation framework, which will be compiled according to the corresponding configuration file in ninja format, but ninja files are generally not manually modified, but compiled by converting the Android.bp file into a ninja format file.

1.2 Android.bp

The emergence of Android.bp is to replace the Android.mk file. Unlike mk files, bp is a pure configuration, without flow control such as branching and looping, and cannot perform arithmetic and logic operations. If control logic is required, it can only be written in Go language.

1.3 Soong

Soong is similar to the core of the previous Makefile compilation system, responsible for providing Android.bp semantic analysis and converting it into a Ninja file. Soong will also compile and generate an androidmk command to convert the Android.mk file into an Android.bp file, but this conversion function is only valid for Android.mk without branching, looping and other flow control.

1.4 Blueprint

Blueprint is a tool for generating and parsing Android.bp, which is a part of Soong. Soong is responsible for the tool designed for Android compilation, while Blueprint only parses the file format, and Soong parses the specific meaning of the content. Both Blueprint and Soong are projects written by Golang. From Android 7.0, the runtime environment required by Golang is added under the prebuilts/go/ directory, which is used during compilation.

1.5 Medium

kati is a Golang and C++-based tool specially developed for Android. Its main function is to convert the Android.mk file in Android into a Ninja file. The code path is build/kati/, and the compiled product is ckati.

       

2. Grammatical correspondence rules

        We may have gotten used to the grammar in Android.mk, and now we need to change it to Android.bp. For easy understanding, you can find the source code and check the grammar correspondence rules between Android.mk and Android.bp:

Source code location: /build/soong/androidmk/cmd/androidmk/android.go, here I only paste a part, please check the source file for the complete code.

var moduleTypes = map[string]string{
	"BUILD_SHARED_LIBRARY":        "cc_library_shared",
	"BUILD_STATIC_LIBRARY":        "cc_library_static",
	"BUILD_HOST_SHARED_LIBRARY":   "cc_library_host_shared",
	"BUILD_HOST_STATIC_LIBRARY":   "cc_library_host_static",
	"BUILD_HEADER_LIBRARY":        "cc_library_headers",
	"BUILD_EXECUTABLE":            "cc_binary",
	"BUILD_HOST_EXECUTABLE":       "cc_binary_host",
	"BUILD_NATIVE_TEST":           "cc_test",
	"BUILD_HOST_NATIVE_TEST":      "cc_test_host",
	"BUILD_NATIVE_BENCHMARK":      "cc_benchmark",
	"BUILD_HOST_NATIVE_BENCHMARK": "cc_benchmark_host",

	"BUILD_JAVA_LIBRARY":             "java_library_installable", // will be rewritten to java_library by bpfix
	"BUILD_STATIC_JAVA_LIBRARY":      "java_library",
	"BUILD_HOST_JAVA_LIBRARY":        "java_library_host",
	"BUILD_HOST_DALVIK_JAVA_LIBRARY": "java_library_host_dalvik",
	"BUILD_PACKAGE":                  "android_app",

	"BUILD_CTS_EXECUTABLE":          "cc_binary",               // will be further massaged by bpfix depending on the output path
	"BUILD_CTS_SUPPORT_PACKAGE":     "cts_support_package",     // will be rewritten to android_test by bpfix
	"BUILD_CTS_PACKAGE":             "cts_package",             // will be rewritten to android_test by bpfix
	"BUILD_CTS_TARGET_JAVA_LIBRARY": "cts_target_java_library", // will be rewritten to java_library by bpfix
	"BUILD_CTS_HOST_JAVA_LIBRARY":   "cts_host_java_library",   // will be rewritten to java_library_host by bpfix
}

var prebuiltTypes = map[string]string{
	"SHARED_LIBRARIES": "cc_prebuilt_library_shared",
	"STATIC_LIBRARIES": "cc_prebuilt_library_static",
	"EXECUTABLES":      "cc_prebuilt_binary",
	"JAVA_LIBRARIES":   "java_import",
	"ETC":              "prebuilt_etc",
}

3. How to convert Android.mk file to Android.bp

         1. In the project source code:

         1.   source build/envsetup.sh

         2.   lunch  xxx 

         3.   make  androidmk 

         Generate androidmk conversion tool, the path is: /out/soong/host/linux-x86/bin/androidmk
 

        2. Place the Android.mk file you want to convert directly into this directory, and then execute the command:

              androidmk   Android.mk  >  Android.bp

4. Grammar explanation

        For ease of understanding, it is easier to understand the syntax of Android.mk and Android.bp together:

4.1 Compiling different types of modules

        4.1.1 Compile into Java library

Android.mk
include $(BUILD_JAVA_LIBRARY)


Android.bp
java_library {
......
}

        4.1.2 Compile into Java static library

Android.mk
include $(BUILD_STATIC_JAVA_LIBRARY)


Android.bp
java_library_static {
......
}

        4.1.3 Compile into App application

Android.mk
include $(BUILD_PACKAGE)


Android.bp
android_app {
......
}

        4.1.4 Compile into  Native  dynamic library

Android.mk
include $(BUILD_SHARED_LIBRARY)



Android.bp
cc_library_shared {
......
}

        4.1.5 Compile into Native static library

Android.mk
include $(BUILD_STATIC_LIBRARY)


Android.bp
cc_library_static {
......
}

          4.1.6 Compile into Native execution program

Android.mk
include $(BUILD_EXECUTABLE)



Android.bp
cc_binary {
......
}

        4.1.7 Compile into a header file library

Android.mk
include $(BUILD_HEADER_LIBRARY)


Android.bp
cc_library_headers {
......
}

  4.2 File path

        4.2.1 Local header file path

Android.mk
LOCAL_C_INCLUDES := 


Android.bp
local_include_dirs: ["xxx", ...]

        4.2.2 Exported header file path

Android.mk
LOCAL_EXPORT_C_INCLUDE_DIRS := 


Android.bp
export_include_dirs: ["xxx", ...]

        4.2.3 Resource file path

Android.mk
LOCAL_RESOURCE_DIR := 


Android.bp
resource_dirs: ["xxx", ...]

4.3 Library dependencies

        4.3.1 Dependent static libraries

Android.mk
LOCAL_STATIC_LIBRARIES := 



Android.bp
static_libs: ["xxx", "xxx", ...]

        4.3.2 Dependent dynamic libraries

Android.mk
LOCAL_SHARED_LIBRARIES := 


Android.bp
shared_libs: ["xxx", "xxx", ...]

        4.3.3 Dependent header file libraries

Android.mk
LOCAL_JAVA_LIBRARIES := 


Android.bp
header_libs: ["xxx", "xxx", ...]

        4.3.4 Dependent Java libraries

Android.mk
LOCAL_STATIC_JAVA_LIBRARIES := 



Android.bp
static_libs: ["xxx", "xxx", ...]

        

4.4 Install into different partitions

        4.4.1 Install into vendor

Android.mk
LOCAL_VENDOR_MODULE := true
        or
LOCAL_PROPRIETARY_MODULE := true



Android.bp
proprietary: true
    or
vendor: true

        4.4.2 Install into product

Android.mk
LOCAL_PRODUCT_MODULE := true


Android.bp
product_specific: true

        4.4.3 Install into odm

Android.mk
LOCAL_ODM_MODULE := true


Android.bp
device_specific: true

4.5 Compilation parameters

        4.5.1 C flags

Android.mk
LOCAL_CFLAGS := 


Android.bp
cflags: ["xxx", "xxx", ...]

        4.5.2 Cpp flags

Android.mk
LOCAL_CPPFLAGS := 


Android.bp
cppflags: ["xxx", "xxx", ...]

        4.5.3 Java flags

Android.mk
LOCAL_JAVACFLAGS := 


Android.bp
javacflags: ["xxx", "xxx", ...]

        

4.6 Follow-up and then summarize and continue to update

Guess you like

Origin blog.csdn.net/u012514113/article/details/125443131