Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

android - What is art.go? And why is it considered a way to write conditionals in bp files?

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

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

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.

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", ...],
}

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".

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

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"]
}

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"]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...