解决Android bp build error: dependency "..." of "..." missing variant
Android O上开始谷歌加大力度推广使用新的soong编译,随之而来的影响就是原先在Android.mk文件需要转换为Android.bp文件做编译配置。
使用soong提供的androidmk命令可以把Android.mk文件转换为Android.bp文件,但是对于一些复杂的mk语法,转换的结果往往不能像原先的Android.mk那样可以正常编译出想要的结果。这就需要将转换出的Android.bp做进一步的修改,以符合原来的编译需求。
androidmk Android.mk > Android.bp
下面来说说我所遇到的dependency "..." of "..." missing variant 错误的解决过程:
1.Android.mk转换成Android.bp:
原先的Android.mk中有如下配置,目的是需要将编译出两个版本的tinyalsa, Android版和Host版,且名称相同。以下是Android.mk中tinyalsa的两个版本的配置:
include $(CLEAR_VARS)
LOCAL_C_INCLUDES:= external/tinyalsa/include
LOCAL_SRC_FILES:= mixer.c pcm.c
LOCAL_MODULE := libtinyalsa
LOCAL_SHARED_LIBRARIES:= libcutils libutils liblog
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS += -Werror
ifeq ($(strip $(NXP_SMARTPA_SUPPORT)),tfa9895)
LOCAL_CFLAGS += -DNXP_TFA9895_SUPPORT
LOCAL_C_INCLUDES+= device/vendor/msm8937/TFA9895/app/exTfa98xx/inc
LOCAL_SHARED_LIBRARIES+= libtfa9895
endif
include $(BUILD_SHARED_LIBRARY)
ifeq ($(HOST_OS), linux)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES:= external/tinyalsa/include
LOCAL_SRC_FILES:= mixer.c pcm.c
LOCAL_MODULE := libtinyalsa
LOCAL_STATIC_LIBRARIES:= libcutils libutils liblog
LOCAL_CFLAGS += -Werror
include $(BUILD_HOST_STATIC_LIBRARY)
endif
原android版tinyalsa是shared_library, host版tinyalsa是static_library, 用androidmk命令转换成bp文件后内容如下:
cc_library_shared {
include_dirs: ["external/tinyalsa/include"] + ["device/vendor/msm8937/TFA9895/app/exTfa98xx/inc"] + ["device/vendor/msm8937/TFA9891/app/exTfa98xx/inc"],
// ANDROIDMK TRANSLATION ERROR: unsupported conditional
// ifeq ($(strip $(NXP_SMARTPA_SUPPORT)),tfa9895)
// ANDROIDMK TRANSLATION ERROR: endif from unsupported contitional
// endif
// ANDROIDMK TRANSLATION ERROR: endif from unsupported contitional
// endif
// ANDROIDMK TRANSLATION ERROR: unsupported conditional
// ifeq ($(strip $(NXP_SMARTPA_SUPPORT)),tfa9895)
srcs: [
"mixer.c",
"pcm.c",
],
name: "libtinyalsa",
shared_libs: [
"libcutils",
"libutils",
],
cflags: ["-Werror"] + ["-DNXP_TFA9895_SUPPORT"] + ["-DNXP_TFA9895_SUPPORT"],
shared_libs: ["libtfa9895"],
// ANDROIDMK TRANSLATION ERROR: endif from unsupported contitional
// endif
// ANDROIDMK TRANSLATION ERROR: endif from unsupported contitional
// endif
}
cc_library_host_static {
include_dirs: ["external/tinyalsa/include"],
srcs: [
"mixer.c",
"pcm.c",
],
name: "libtinyalsa",
static_libs: [
"libcutils",
"libutils",
],
cflags:["-Werror"],
target:{
not_linux:{
enabled:false,
},
},
}
转换后有两个名字相同的libtinyalsa module。
因为项目需求,在libtinyalsa Android版中添加了共享库依赖:libtfa9895:
所以现在的库依赖关系变为:
Android: libtinyalsa -> libcutils, libutils,libtfa9895
Host: libtinyalsa -> libcutils, libutils
2. Android.mk转为Android.bp之后,手动修改,删除名字相同的多余module.
bp文件定义的一个规则是,可以定义很多种很多个module,每个module必须有一个名字,且名字必须唯一不重复。这是因为bp文件没有判断逻辑,不能像mk中那样根据不同的编译选项选择不同的版本。
在原Android.mk中定义了两个名字相同的tinyalsa, 一个是Android版,一个是Host版。转成bp后,出现了两个名字相同的tinyalsa module,一个是cc_library_shared, 一个是cc_library_host_static。而根据bp的module名字不重复规则,这种情况是不允许的,所以我们做的第一个修改是将两个module合并为一个,然后通过**host_supported: true**使其支持host;
又因为原来的两个module,一个为shared,一个为static,所以第二个修改是合并后的module类型为cc_library, 这样才能同时包含shared与static两种不同的属性类型。
修改后的Android.bp:
cc_library {
include_dirs: ["device/vendor/msm8937/TFA9895/app/exTfa98xx/inc"],
name: "libtinyalsa",
host_supported: true,
vendor_available: true,
srcs: [
"mixer.c",
"pcm.c",
],
shared_libs: [
"libcutils",
"libutils",
"libtfa9895",
],
cflags: ["-Werror", "-Wno-macro-redefined"]+ ["-DNXP_TFA9895_SUPPORT"],
export_include_dirs: ["include"],
local_include_dirs: ["include"],
target: {
darwin: {
enabled: false,
},
},
}
下面要说的编译错误就是因为在tinyplay和tinyalsa中添加了host_supported引起的:
error: external/tinyalsa/Android.bp:1:1: dependency "libtfa9895" of "libtinyalsa" missing variant:
arch:linux_x86_64, link:shared
available variants:
arch:android_arm64_armv8-a, link:shared, image:core
arch:android_arm_armv7-a-neon_cortex-a53, link:shared, image:core
从出错信息来看,是由于libtfa9895中不支持linux_x86_64平台的编译。
以下是libtfa9895转换为Android.bp后的配置:
cc_library_shared {
local_include_dirs: [
"srv/inc",
"tfa/inc",
"utl/inc",
"hal/inc",
"hal/src",
"hal/src/lxScribo",
"srv/src/iniFile",
"app/exTfa98xx/inc",
],
export_include_dirs: [
"srv/inc",
"tfa/inc",
"utl/inc",
"hal/inc",
"hal/src",
"hal/src/lxScribo",
"srv/src/iniFile",
"app/exTfa98xx/inc",
],
srcs: ["app/exTfa98xx/src/main_container.c"],
name: "libtfa9895",
shared_libs: [
"libcutils",
],
static_libs: [
"libsrv",
"libtfa",
"libhal",
],
}
3. 除错过程:
libtinyalsa依赖库有三个,libcutils, libutils, libtfa9895,
而log中只提供libtfa9895有错,查看libcutils和libutils的配置发现,这两个库里的配置信息都有添加host_supported,而libtfa9895没有,所以尝试在libtfa9895中添加host_supported配置后重新编译,
然后出现了相同的错误提示,只不过这次提示libtfa9895依赖的三个库(libsrv/libtfa/libhal)不支持host。看来在有host_supported配置的库中,其依赖库也必须是host_supported的。
在libsrv/libtfa/libhal中分别添加host_supported配置后,再次提示这三个库依赖的libutils不支持host.
查看libutils的配置发现libutils有添加host_supported,这里怎么回事呢?
cc_library {
name: "libutils",
vendor_available: true,
host_supported: true,
srcs: [
....
],
cflags: ["-Werror"],
include_dirs: ["external/safe-iop/include"],
header_libs: [
"libutils_headers",
],
export_header_lib_headers: [
"libutils_headers",
],
arch: {
mips: {
cflags: ["-DALIGN_DOUBLE"],
},
},
target: {
android: {
srcs: [
"Looper.cpp",
"ProcessCallStack.cpp",
"Trace.cpp",
],
cflags: ["-fvisibility=protected"],
shared_libs: [
"libbacktrace",
"libcutils",
"libdl",
"liblog",
"libvndksupport",
],
sanitize: {
misc_undefined: ["integer"],
},
},
host: {
cflags: ["-DLIBUTILS_NATIVE=1"],
shared: {
enabled: false,
},
},
linux: {
srcs: [
"Looper.cpp",
"ProcessCallStack.cpp",
],
},
linux_bionic: {
enabled: true,
srcs: [
"Looper.cpp",
"ProcessCallStack.cpp",
],
},
darwin: {
cflags: ["-Wno-unused-parameter"],
},
// Under MinGW, ctype.h doesn't need multi-byte support
windows: {
cflags: ["-DMB_CUR_MAX=1"],
enabled: true,
},
},
clang: true,
}
仔细分析libutils中的配置,发现原来在target:下的host配置,将shared属性设置成了false,就是说在编译Android版时,libutils可以作为shared lib引用,但是在host版中,只能做为static lib引用。
host: {
cflags: ["-DLIBUTILS_NATIVE=1"],
shared: {
enabled: false,
},
},
所以对引用libutils的module中,做了如下修改,首先删除libutils的引用,然后添加如下配置:
target: {
android: {
cflags: ["-DAndroid"],
shared_libs: [
"libutils",
],
},
host: {
static_libs: ["libutils"],
},
},
android版以shared lib引用libutils, host版以static lib引用。
修改后的完整的libtfa9895的配置信息如下:
cc_library_shared {
local_include_dirs: [
"srv/inc",
"tfa/inc",
"utl/inc",
"hal/inc",
"hal/src",
"hal/src/lxScribo",
"srv/src/iniFile",
"app/exTfa98xx/inc",
],
export_include_dirs: [
"srv/inc",
"tfa/inc",
"utl/inc",
"hal/inc",
"hal/src",
"hal/src/lxScribo",
"srv/src/iniFile",
"app/exTfa98xx/inc",
],
srcs: ["app/exTfa98xx/src/main_container.c"],
name: "libtfa9895",
host_supported: true,
shared_libs: [
"libcutils",
],
static_libs: [
"libsrv",
"libtfa",
"libhal",
],
target: {
android: {
cflags: ["-DAndroid"],
shared_libs: [
"libutils",
],
},
host: {
static_libs: ["libutils"],
},
},
}
所以修改完成后,编译通过。
4. 进一步完善
回到前面我们libtinyalsa的定义,在原mk中android版 libtinyalsa是shared library, host版是static library,而转成bp后,libtinyalsa合并成了一个cc_library,但是并未对android版与host版区分其shared/static属性。
参考libutils中的方法,可以对libtinyalsa target为android与host两个版本的shared属性设置为不同的值。
target: {
android:{
shared:{
enabled: true,
},
},
host:{
cflags: ["-DLIBUTILS_NATIVE=1"],
shared: {
enabled: false,
},},
},