android.bp 如何在cc_library_static的cflags中添加一个宏变量


cc_library_static {
    name: "AAAi",
    defaults: [],
    include_dirs: ,
    srcs: [

    ],
    shared_libs: [
 
    ],
    whole_static_libs: [

    ],
    header_libs: [

    ],
    cflags: [

    ],

}

如何使用go语言,对cflags进行添加宏变量
我写的go,但是编译不过:

package btif

import (
        "android/soong/android"
        "android/soong/cc"
        "fmt"
        "sync"
        "strings"
        "github.com/google/blueprint/proptools"
        "android/soong/apex"
        "android/soong/cc/config"
)

func init() {
        android.RegisterModuleType("cc_library_static", AdvAduioFeature_Cflag)
}

func AdvAduioFeature_Cflag() (android.Module) {
        module := cc.LibraryStaticFactory()
        android.AddLoadHook(module, AdvAduioFeature_Defaults)
        return module
}

func AdvAduioFeature_Defaults(ctx android.LoadHookContext) {
        type props struct {
                Cflags []string
        }
        p := &props{}

        p.Cflags = append(p.Cflags, "-DAAAA")
        ctx.AppendProperties(p)
}

能帮忙看一下是什么问题吗?感谢!

看看你的 makefile 一般是怎么设置的。

该回答引用ChatGPT

如有疑问,可以回复我!

根据你提供的代码,可以看到你尝试为 cc_library_static 添加一个 cflags 属性,其中包含一个宏定义 -DAAAA。但是在你的代码中,AdvAduioFeature_Cflag 函数被注册为 cc_library_static 的工厂函数,这可能会引起问题,因为它将 cc_library_static 重新定义为一个函数。为避免这个问题,你可以尝试将函数重命名,例如:


func AdvAduioFeatureFactory() android.Module {
    module := cc.LibraryStaticFactory()
    android.AddLoadHook(module, AdvAduioFeatureDefaults)
    return module
}

func AdvAduioFeatureDefaults(ctx android.LoadHookContext) {
    type props struct {
        Cflags []string
    }
    p := &props{}

    p.Cflags = append(p.Cflags, "-DAAAA")
    ctx.AppendProperties(p)
}

func init() {
    android.RegisterModuleType("cc_library_static", AdvAduioFeatureFactory)
}

这将确保你的工厂函数与 cc_library_static 不会冲突。现在,当你在 Android.bp 文件中定义一个 cc_library_static 模块时,你可以通过添加 adv_audio_feature 属性来启用 AdvAduioFeatureDefaults 函数中的 Cflags 选项:

cc_library_static {
    name: "AAAi",
    defaults: ["adv_audio_feature"],
    include_dirs: [],
    srcs: [],
    shared_libs: [],
    whole_static_libs: [],
    header_libs: [],
    cflags: [],
}

请注意,defaults 属性中的 adv_audio_feature 值与你在 Go 代码中注册的模块名称 AdvAduioFeatureFactory 相关联。这将确保在加载 AAAi 模块时,将自动添加 Cflags 属性中指定的 -DAAAA 宏定义。

参考GPT和自己的思路,根据你的代码,你已经在 AdvAduioFeature_Defaults 函数中向 cc_library_static 的 Cflags 属性添加了一个宏变量 -DAAAA。但是,你的 cc_library_static 模块中的属性值都为空。

为了使 cc_library_static 模块能够使用添加的宏变量,你需要在 cc_library_static 模块中将 Cflags 属性设置为 $$(cflags),这将允许你的 cc_library_static 模块将 Cflags 属性与默认值(在 AdvAduioFeature_Defaults 函数中添加的 -DAAAA 宏变量)合并起来。

下面是更新后的代码:

cc_library_static {
    name: "AAAi",
    defaults: [],
    include_dirs: [],
    srcs: [],
    shared_libs: [],
    whole_static_libs: [],
    header_libs: [],
    cflags: ["$$(cflags)"],
}

请注意,您还需要更新 defaults 属性,以使 cc_library_static 模块可以正确继承默认属性。

参考GPT和自己的思路:在 cc_library_static 的 cflags 中添加宏变量可以使用以下代码:

cc_library_static {
    name: "AAAi",
    defaults: [],
    include_dirs: ,
    srcs: [

    ],
    shared_libs: [
 
    ],
    whole_static_libs: [

    ],
    header_libs: [

    ],
    cflags: [
        "-DAAA",
    ],
}


在 Go 代码中使用 cc.LibraryStaticFactory() 来创建 cc_library_static 模块类型,并且需要在 init() 函数中注册该模块类型,代码如下:

package btif

import (
    "android/soong/android"
    "android/soong/cc"
)

func init() {
    // 注册模块类型
    android.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
}


接着,你可以使用 android.AddLoadHook() 函数在模块加载时添加默认属性,其中 AdvAduioFeature_Defaults() 函数可以在 Cflags 中添加 -DAAAA 宏定义:

func AdvAduioFeature_Defaults(ctx android.LoadHookContext) {
    type props struct {
        Cflags []string
    }
    p := &props{}

    p.Cflags = append(p.Cflags, "-DAAAA")
    ctx.AppendProperties(p)
}

func AdvAduioFeature_Cflag() android.Module {
    // 创建 cc_library_static 模块
    module := cc.LibraryStaticFactory()

    // 添加默认属性
    android.AddLoadHook(module, AdvAduioFeature_Defaults)

    return module
}


至于你的代码编译不过,可能的原因是你导入了不必要的包,或者 ctx.AppendProperties() 函数调用的方式有误。你可以参考上面的代码修改你的实现。

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
根据您提供的信息,您将 AdvAduioFeature_Cflag 函数注册为新的 cc_library_static 模块类型,并在其中添加了一个 AdvAduioFeature_Defaults 加载钩子,用于向 cflags 添加一个值为 -DAAAA 的宏变量。

然而,在您的代码中,只有调用 append 函数并不足以为属性添加值。您需要使用 ctx.AppendProperties 函数将您的属性添加到模块属性列表中。因此,您的 AdvAduioFeature_Defaults 函数应该像这样:

func AdvAduioFeature_Defaults(ctx android.LoadHookContext) {
    type props struct {
        Cflags []string
    }
    p := &props{}

    p.Cflags = append(p.Cflags, "-DAAAA")
    ctx.AppendProperties(p)
}

如果编译仍然失败,请查看错误消息以获取更多信息。
如果我的回答解决了您的问题,请采纳!