什么是art.go?为什么要考虑在bp文件中写条件语句的方法呢? [英] What is art.go? And why is it considered a way to write conditionals in bp files?

查看:109
本文介绍了什么是art.go?为什么要考虑在bp文件中写条件语句的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种在.bp文件中写入条件代码的方法.

I am trying to find a way to write a conditional inside a .bp file.

我在这里找到了一个文档: https://android .googlesource.com/platform/build/soong/+/HEAD/README.md

I found a documentation here: https://android.googlesource.com/platform/build/soong/+/HEAD/README.md

它有一个我该如何写条件语句?"部分,指向art.go: https://android.googlesource.com/platform/art/+/master/build/art.go

It has a "How do I write conditionals?" part, which points to the art.go: https://android.googlesource.com/platform/art/+/master/build/art.go

这几乎不是上述问题的答案.比简单的链接需要更多的澄清.

Which is hardly an answer to the aforementioned question. It needs a lot more clarification than a simple link.

我在Android.bp中有我开发的模块(HAL)的cc_binary.

I have cc_binary in my Android.bp for the module (HAL) I develop.

cc_binary {
  name: "name",
  init_rc: ["script.rc"],
  vintf_fragments: ["fragments.xml"],
  relative_install_path: "path",
  srcs: ["src1.cpp", "src2.cpp", ...],
  shared_libs: ["sh_lib1", "sh_lib2", ...],
  tstic_libs: ["lib1", "lib2", ...],
}

我想添加一个条件cflag(-DCONDITIONAL),如果环境变量SOME_ENV_VAR等于"some_value",则将其设置为1.

I want to add a conditional cflag (-DCONDITIONAL), which will be set to 1 if environmental variable SOME_ENV_VAR is equal to "some_value".

我在AOSP中发现了许多类似* .go文件的示例,但事实证明它们对我没有用,因为我不能简单地将此处描述的做法应用于我的案例. 我也找不到有关* .go文件的任何文档,该文档描述了如何使用它们进行操作.我什至找不到使用的最简单示例"之类的东西.

I found a lot of examples of similar *.go files inside AOSP, but they turned out to be of no use for me, because I cannot simply apply practices described there to my case. I also failed to find any documentation about *.go files, which describes how to do stuff using them. I cannot even find something like "simplest example of usage".

有人知道我在这里尝试做什么吗?

Does anyone know is it even possible what I am trying to do here?

推荐答案

在项目文件夹中,创建一个"my_defaults.go"文件,您可以在其中定义根据环境变量或其他条件添加自定义构建标记的逻辑(这基本上就是在art.go中所发生的情况(以示例为例).

In your project folder, create a "my_defaults.go" file, where you can define the logic that adds custom build flags based on environment variables or other conditions (which is basically what happens in the art.go that was given as an example).

package my_defaults

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

func globalFlags(ctx android.BaseContext) []string {
    var cflags []string

    if ctx.AConfig().Getenv("SOME_ENV_VAR") == "some_value" {
        cflags = append(cflags, "-DCONDITIONAL")
    }

    return cflags
}

func deviceFlags(ctx android.BaseContext) []string {
    var cflags []string

    return cflags
}

func hostFlags(ctx android.BaseContext) []string {
    var cflags []string

    return cflags
}

func myDefaults(ctx android.LoadHookContext) {
    type props struct {
        Target struct {
            Android struct {
                Cflags []string
                Enabled *bool
            }
            Host struct {
                Enabled *bool
            }
            Linux struct {
                Cflags []string
            }
            Darwin struct {
                Cflags []string
            }
        }
        Cflags []string
    }

    p := &props{}
    p.Cflags = globalFlags(ctx)
    p.Target.Android.Cflags = deviceFlags(ctx)
    h := hostFlags(ctx)
    p.Target.Linux.Cflags = h
    p.Target.Darwin.Cflags = h

    ctx.AppendProperties(p)
}

func init() {
    android.RegisterModuleType("my_defaults", myDefaultsFactory)
}

func myDefaultsFactory() android.Module {
    module := cc.DefaultsFactory()
    android.AddLoadHook(module, myDefaults)

    return module
}

在您的Android.bp中,声明以下模块:

In your Android.bp, declare the following module:

bootstrap_go_package {
    name: "soong-my_defaults",
    pkgPath: "android/soong/my/defaults",
    deps: [
        "soong",
        "soong-android",
        "soong-cc"
    ],
    srcs: [
        "my_defaults.go"
    ],
    pluginFor: ["soong_build"]
}

然后,您可以在Android.bp中使用"my_defaults"代替通常的"cc_defaults",例如

You can then use "my_defaults" instead of the usual "cc_defaults" in your Android.bp, e.g.

my_defaults {
  name: "my-defaults-instance"
  // Set some additional non-conditional cflags ...
}

cc_binary {
  name: "my_binary",
  defaults: ["my-defaults-instance"]
}

这篇关于什么是art.go?为什么要考虑在bp文件中写条件语句的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆