blob: 71918dd21f41b7d08f739cf226f60c3e83202f8b [file] [log] [blame]
Vinh Tran457ddef2023-08-02 13:50:26 -04001package aconfig
2
3import (
4 "android/soong/android"
5 "android/soong/rust"
6 "fmt"
7
8 "github.com/google/blueprint"
Vinh Tran0ea4ffa2023-08-11 17:29:33 -04009 "github.com/google/blueprint/proptools"
Vinh Tran457ddef2023-08-02 13:50:26 -040010)
11
12type rustDeclarationsTagType struct {
13 blueprint.BaseDependencyTag
14}
15
16var rustDeclarationsTag = rustDeclarationsTagType{}
17
18type RustAconfigLibraryProperties struct {
19 // name of the aconfig_declarations module to generate a library for
20 Aconfig_declarations string
Vinh Tran0ea4ffa2023-08-11 17:29:33 -040021 Test *bool
Vinh Tran457ddef2023-08-02 13:50:26 -040022}
23
24type aconfigDecorator struct {
25 *rust.BaseSourceProvider
26
27 Properties RustAconfigLibraryProperties
28}
29
30func NewRustAconfigLibrary(hod android.HostOrDeviceSupported) (*rust.Module, *aconfigDecorator) {
31 aconfig := &aconfigDecorator{
32 BaseSourceProvider: rust.NewSourceProvider(),
33 Properties: RustAconfigLibraryProperties{},
34 }
35
36 module := rust.NewSourceProviderModule(android.HostAndDeviceSupported, aconfig, false, false)
37 return module, aconfig
38}
39
40// rust_aconfig_library generates aconfig rust code from the provided aconfig declaration. This module type will
41// create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs
42// properties of other modules.
43func RustAconfigLibraryFactory() android.Module {
44 module, _ := NewRustAconfigLibrary(android.HostAndDeviceSupported)
45 return module.Init()
46}
47
48func (a *aconfigDecorator) SourceProviderProps() []interface{} {
49 return append(a.BaseSourceProvider.SourceProviderProps(), &a.Properties)
50}
51
52func (a *aconfigDecorator) GenerateSource(ctx rust.ModuleContext, deps rust.PathDeps) android.Path {
53 generatedDir := android.PathForModuleGen(ctx)
54 generatedSource := android.PathForModuleGen(ctx, "src", "lib.rs")
55
56 declarationsModules := ctx.GetDirectDepsWithTag(rustDeclarationsTag)
57
58 if len(declarationsModules) != 1 {
59 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
60 }
61 declarations := ctx.OtherModuleProvider(declarationsModules[0], declarationsProviderKey).(declarationsProviderData)
62
Vinh Tran0ea4ffa2023-08-11 17:29:33 -040063 mode := "production"
64 if proptools.Bool(a.Properties.Test) {
65 mode = "test"
66 }
67
Vinh Tran457ddef2023-08-02 13:50:26 -040068 ctx.Build(pctx, android.BuildParams{
69 Rule: rustRule,
70 Input: declarations.IntermediatePath,
71 Outputs: []android.WritablePath{
72 generatedSource,
73 },
74 Description: "rust_aconfig_library",
75 Args: map[string]string{
76 "gendir": generatedDir.String(),
Vinh Tran0ea4ffa2023-08-11 17:29:33 -040077 "mode": mode,
Vinh Tran457ddef2023-08-02 13:50:26 -040078 },
79 })
80 a.BaseSourceProvider.OutputFiles = android.Paths{generatedSource}
81 return generatedSource
82}
83
84func (a *aconfigDecorator) SourceProviderDeps(ctx rust.DepsContext, deps rust.Deps) rust.Deps {
85 deps = a.BaseSourceProvider.SourceProviderDeps(ctx, deps)
86 deps.Rustlibs = append(deps.Rustlibs, "libflags_rust")
Dennis Shenba6ed2d2023-11-08 21:02:53 +000087 deps.Rustlibs = append(deps.Rustlibs, "liblazy_static")
Vinh Tran457ddef2023-08-02 13:50:26 -040088 ctx.AddDependency(ctx.Module(), rustDeclarationsTag, a.Properties.Aconfig_declarations)
89 return deps
90}