Android源码编译系列博客:
Android.bp你真的了解吗
Android.bp入门指南之Android.mk转换成Android.bp
Android.bp入门指南之浅析Android.bp语法
Android.bp正确姿势添加宏控制编译指南
Android高版本P/Q/R源码编译指南
如何在线查看Android源码自带学习文档docs
普法Android.mk中的一些宏和Android.bp对应关系
博客断更很久了,是时候动动我勤劳的双手,继续耕耘了!陆陆续续有不少小伙伴在后台咨询关于Android.mk怎么转换成Androdi.bp的问题,虽然也解答了不少,但是也有一些当时我没有能够给出及时的答复(不是我不想答复,而是确实有些我当时也没有一个具体的答案)。其中小伙们询问的最最多多的就是Android.mk中的一些预先定义的宏怎么在Android.bp中找到相对应的。这不我就抽空,来给大伙福利一波,来捯饬捯饬这个问题。
在开始本博客前,有两个非常重要的知识点我必须提前给出来,就是:
1.Android.mk可以引用Android.bp中的相关模块,但是不能反过来!这点非常重要。
2.Android.bp不直接支持条件宏控制语法编译,如果一定要条件宏控制可以借助go,参见博客Android.bp正确姿势添加宏控制编译指南
我们知道google为Android.mk到Android.bp的转换提供了一个工具androidmk(如果读者对该工具不是很熟悉,可以参见博客Android.bp入门指南之Android.mk转换成Android.bp)但是该工具非常鸡肋,转换效率不是非常好,对一些Android.mk中的宏基本不能正常转换,我这里简单举出一个例子,测试Android.mk如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
a.c
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
LOCAL_CFLAGS += -DXXX
LOCAL_SRC_FILES += \
b.c
else ifeq ($(TARGET_ARCH_ABI),arm64-v8a)
LOCAL_CFLAGS += -DXXX
LOCAL_SRC_FILES += \
c.c
endif
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_MODULE := libyuv_static
LOCAL_MODULE_TAGS := optional
include $(BUILD_STATIC_LIBRARY)
有一定Android.mk语法的小伙们一定对上述的,一看就一目了然了,可是当我们需要使用androidmk转换成Android.bp的时候,却会有如下的尴尬一幕:
cc_library_static {
name: "libyuv_static",
srcs: ["a.c"] + ["b.c"] + ["c.c"], // ANDROIDMK TRANSLATION ERROR: unsupported conditional
// ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
// ANDROIDMK TRANSLATION ERROR: else from unsupported conditional
// else
// ANDROIDMK TRANSLATION ERROR: unsupported conditional
// ifeq ($(TARGET_ARCH_ABI),arm64-v8a)
cflags: ["-DXXX"] + ["-DXXX"],
// ANDROIDMK TRANSLATION ERROR: endif from unsupported conditional
// endif
// ANDROIDMK TRANSLATION ERROR: unsupported assignment to LOCAL_EXPORT_C_INCLUDES
// LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
local_include_dirs: ["include"],
可以看到,我们的androidmk转换工具,无能为力了。那么要怎么解决上述出现的问题呢,这就需要我们了解Android.mk的一些宏和Android.bp的对应关系了。这也是我本篇博客的重点要说的了。
如果读者又对Android.mk中各种宏不是很熟悉的,可以参见博客Android.mk详解。有比较详细的分析!
对于上述Android.mk中的宏,我们要怎么正确的在Android.bp中找到替代关系呢。我这里个人提供两个解决思路:
这个方法虽然比较笨,但是也是最行之有效的,多看几个Android Aosp源码中的Android.bp,看多了自然就会了。
也许读者朋友会说,上面的方法说了等于白说,看来我不亮出点真本事,是不行了啊。好吗,上真家伙,我们可以到Android aosp的build/soong/androidmk/androidmk目录下面,查看android.go文件,里面有详细的Android.mk相关宏和Android.bp的对应关系,如下:
//android.go
"LOCAL_32_BIT_ONLY": local32BitOnly,
"LOCAL_AIDL_INCLUDES": localAidlIncludes,
"LOCAL_ASSET_DIR": localizePathList("asset_dirs"),
"LOCAL_C_INCLUDES": localIncludeDirs,
"LOCAL_EXPORT_C_INCLUDE_DIRS": exportIncludeDirs,
"LOCAL_JARJAR_RULES": localizePath("jarjar_rules"),
"LOCAL_LDFLAGS": ldflags,
"LOCAL_MODULE_CLASS": prebuiltClass,
"LOCAL_MODULE_STEM": stem,
"LOCAL_MODULE_HOST_OS": hostOs,
"LOCAL_RESOURCE_DIR": localizePathList("resource_dirs"),
"LOCAL_SANITIZE": sanitize(""),
"LOCAL_SANITIZE_DIAG": sanitize("diag."),
"LOCAL_STRIP_MODULE": strip(),
"LOCAL_CFLAGS": cflags,
"LOCAL_UNINSTALLABLE_MODULE": invert("installable"),
"LOCAL_PROGUARD_ENABLED": proguardEnabled,
"LOCAL_MODULE_PATH": prebuiltModulePath,
上述我只截取了一部分,更多的请使用时候具体参阅文档。
同时在同一个目录下面的androidmk_test.go,为我们提供了许多Android.mk转换为Android.bp的实例,这个非常值得我们借鉴。如下:
# Module Comment
include $(CLEAR_VARS)
# Name Comment
LOCAL_MODULE := test
# Source comment
LOCAL_SRC_FILES_EXCLUDE := a.c
# Second source comment
LOCAL_SRC_FILES_EXCLUDE += b.c
include $(BUILD_SHARED_LIBRARY)`,
expected: `
//
// Copyright
//
// Module Comment
cc_library_shared {
// Name Comment
name: "test",
// Source comment
exclude_srcs: ["a.c"] + ["b.c"], // Second source comment
}`,
},
{
desc: "split local/global include_dirs (1)",
in: `
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)
include $(BUILD_SHARED_LIBRARY)`,
expected: `
cc_library_shared {
local_include_dirs: ["."],
}`,
},
{
desc: "split local/global include_dirs (2)",
in: `
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
include $(BUILD_SHARED_LIBRARY)`,
expected: `
cc_library_shared {
local_include_dirs: ["include"],
}`,
},
{
desc: "split local/global include_dirs (3)",
in: `
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := system/core/include
include $(BUILD_SHARED_LIBRARY)`,
expected: `
cc_library_shared {
include_dirs: ["system/core/include"],
}`,
},
{
desc: "split local/global include_dirs (4)",
in: `
input := testing/include
include $(CLEAR_VARS)
# Comment 1
LOCAL_C_INCLUDES := $(LOCAL_PATH) $(LOCAL_PATH)/include system/core/include $(input)
# Comment 2
LOCAL_C_INCLUDES += $(TOP)/system/core/include $(LOCAL_PATH)/test/include
# Comment 3
include $(BUILD_SHARED_LIBRARY)`,
expected: `
input = ["testing/include"]
cc_library_shared {
// Comment 1
include_dirs: ["system/core/include"] + input + ["system/core/include"], // Comment 2
local_include_dirs: ["."] + ["include"] + ["test/include"],
// Comment 3
}`,
},
{
desc: "Convert to local path",
in: `
include $(CLEAR_VARS)
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res $(LOCAL_PATH)/res2
LOCAL_ASSET_DIR := $(LOCAL_PATH)/asset
LOCAL_JARJAR_RULES := $(LOCAL_PATH)/jarjar-rules.txt
include $(BUILD_PACKAGE)
好了,打卡收工!今天的博客普法Android.mk中的一些宏和Android.bp对应关系就到这里了。总之,青山不改绿水长流先到这里了。如果本博客对你有所帮助,麻烦关注或者点个赞,如果觉得很烂也可以踩一脚!谢谢各位了!!这里最后留下一个小彩蛋,对于上述的Android.mk读者朋友看看能否正确的转换为Android.bp,当然也希望读者能我一起探讨是最好的了。