android.bp

引用原文
早期的Android系统都是采用Android.mk的配置来编译源码,从Android 7.0开始引入Android.bp。

Android 7.0引入ninja和kati
Android 8.0使用Android.bp来替换Android.mk,引入Soong
Android 9.0强制使用Android.bp

image.png
cc_library_shared {             //编译成动态库,类似于Android.mk中的BUILD_SHARED_LIBRARY
    name: "libbluetooth_jni",   //编译出的模块的名称,类似于Android.mk中的LOCAL_MODULE
    srcs: [                     //源文件,类似于Android.mk中的LOCAL_SRC_FILES
        "com_android_bluetooth_btservice_AdapterService.cpp",
        "com_android_bluetooth_hfp.cpp",
        "com_android_bluetooth_hfpclient.cpp",
        "com_android_bluetooth_a2dp.cpp",
        "com_android_bluetooth_a2dp_sink.cpp",
        "com_android_bluetooth_avrcp.cpp",
        "com_android_bluetooth_avrcp_controller.cpp",
        "com_android_bluetooth_hid.cpp",
        "com_android_bluetooth_hidd.cpp",
        "com_android_bluetooth_hdp.cpp",
        "com_android_bluetooth_pan.cpp",
        "com_android_bluetooth_gatt.cpp",
        "com_android_bluetooth_sdp.cpp",
    ],
    include_dirs: [             //用户指定的头文件查找路径,类似于Android.mk中的LOCAL_C_INCLUDES
        "libnativehelper/include/nativehelper",
        "system/bt/types",
    ],
    shared_libs: [              //编译所依赖的动态库,类似于Android.mk中的LOCAL_SHARED_LIBRARIES
        "libandroid_runtime",
        "libchrome",
        "libnativehelper",
        "libcutils",
        "libutils",
        "liblog",
        "libhardware",
    ],
    static_libs: [              //编译所依赖的静态库,类似于Android.mk中的LOCAL_STATIC_LIBRARIES
        "libbluetooth-types",
    ],
    cflags: [                   ///编译flag,类似于Android.mk中的LOCAL_CFLAGS
        "-Wall",
        "-Wextra",
        "-Wno-unused-parameter",
    ],
}

1.1 模块
定义一个模块从模块的类型开始,模块有不同的类型,如前面例子中的cc_library_shared,当然类型还有很多种,譬如cc_binary android_app cc_library_static等等。模块包含一些属性格式为“property-name:property-value”,其中name属性必须指定,其属性值必须是全局唯一的。

cc_defaults {//     //默认模块名称
    name: "default_module",
    shared_libs: ["libz"],
    stl: "none",
}
cc_binary {
    name: "test1",
    defaults: ["default_module"],   //引用默认模块名称
    srcs: ["src/test/test.c"],
}

srcs 属性以字符串列表的形式指定用于编译模块的源文件。您可以使用模块引用语法 “:” 来引用生成源文件的其他模块的输出,如 genrule 或 filegroup。

filegroup {
    name: "IKeyAttestationApplicationIdProvider.aidl",
    srcs: ["android/security/keymaster/IKeyAttestationApplicationIdProvider.aidl"],
}

filegroup {
    name: "IDropBoxManagerService.aidl",
    srcs: ["com/android/internal/os/IDropBoxManagerService.aidl"],

~/ssd/qcom_64/msm8953-9/frameworks/base/core$ cd ..
~/ssd/qcom_64/msm8953-9/frameworks/base$ vi libs/services/Android.bp

cc_library_shared {
    name: "libservices",
    srcs: [
        ":IDropBoxManagerService.aidl",
        "src/os/DropBoxManager.cpp",
        "src/os/StatsDimensionsValue.cpp",
        "src/os/StatsLogEventWrapper.cpp",
    ],  

    shared_libs: [
        "libbinder",
        "liblog",
        "libcutils",
        "libutils",
    ],

1.2 变量
变量范围限定为声明它们的文件的其余部分,可以使用 “=” 号赋值, 但是不能使用 “:=” 赋值。变量是不可变的,但有一个例外它们可以附上+= 赋值,但仅在变量被引用之前。

gzip_srcs = ["src/minigzip.c"],
cc_binary {
  name: "gzip",
  srcs: gzip_srcs,
  shared_libs: ["libz"],
  stl: "none",
}

1.3 注释
Android.bp使用单行注释//和多行注释/* */两种方式。
1.4类型
具体支持以下几种类型:

Bool(true or false)
Integers(int)
Strings("string")
Listsof strings (["string1", "string2"])
Maps({key1: "value1", key2: ["value2"]})

1.5 操作符
String类型、字符串列表类型和Map类型支持操作符“+”。
1.6 支持模块类型
Android.bp可以支持android_app、cc_binary、cc_binary_host等多种类型,具体定义在Android源码的build/soong/androidmk/cmd/androidmk/android.go

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",
    "BUILD_STATIC_JAVA_LIBRARY":      "java_library_static",
    "BUILD_HOST_JAVA_LIBRARY":        "java_library_host",
    "BUILD_HOST_DALVIK_JAVA_LIBRARY": "java_library_host_dalvik",
    "BUILD_PACKAGE":                  "android_app",
}    "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",
    "BUILD_STATIC_JAVA_LIBRARY":      "java_library_static",
    "BUILD_HOST_JAVA_LIBRARY":        "java_library_host",
    "BUILD_HOST_DALVIK_JAVA_LIBRARY": "java_library_host_dalvik",
    "BUILD_PACKAGE":                  "android_app",
}

1.7 支持预编译类型

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

1.8 条件式编译

system/core/libusbhost$ cat Android.bp 

cc_library {
    name: "libusbhost",
    vendor_available: true,
    vndk: {
        enabled: true,
    },
    host_supported: true,
    srcs: ["usbhost.c"],
    cflags: ["-Werror"],
    export_include_dirs: ["include"],
    target: {
        android: {    //编译Android上运行的程序
            cflags: [
                "-g",
                "-DUSE_LIBLOG",
            ],
            shared_libs: ["liblog"],
        },
        darwin: {    //编译darwin上运行的程序
            enabled: false,
        },
    },
}

你可能感兴趣的:(android.bp)