blob: 43078b651db08961a76966602af37ba762cbee4b [file] [log] [blame]
Vinh Tran457ddef2023-08-02 13:50:26 -04001package aconfig
2
3import (
Zi Wang275f6542023-11-09 14:59:31 -08004 "fmt"
5
Vinh Tran457ddef2023-08-02 13:50:26 -04006 "android/soong/android"
7 "android/soong/rust"
Vinh Tran457ddef2023-08-02 13:50:26 -04008
9 "github.com/google/blueprint"
Vinh Tran0ea4ffa2023-08-11 17:29:33 -040010 "github.com/google/blueprint/proptools"
Vinh Tran457ddef2023-08-02 13:50:26 -040011)
12
13type rustDeclarationsTagType struct {
14 blueprint.BaseDependencyTag
15}
16
17var rustDeclarationsTag = rustDeclarationsTagType{}
18
19type RustAconfigLibraryProperties struct {
20 // name of the aconfig_declarations module to generate a library for
21 Aconfig_declarations string
Zi Wang275f6542023-11-09 14:59:31 -080022
23 // whether to generate test mode version of the library
24 // TODO: remove "Test" property when "Mode" can be used in all the branches
25 Test *bool
26
27 // default mode is "production", the other accepted modes are:
28 // "test": to generate test mode version of the library
29 // "exported": to generate exported mode version of the library
30 // an error will be thrown if the mode is not supported
31 Mode *string
Vinh Tran457ddef2023-08-02 13:50:26 -040032}
33
34type aconfigDecorator struct {
35 *rust.BaseSourceProvider
36
37 Properties RustAconfigLibraryProperties
38}
39
40func NewRustAconfigLibrary(hod android.HostOrDeviceSupported) (*rust.Module, *aconfigDecorator) {
41 aconfig := &aconfigDecorator{
42 BaseSourceProvider: rust.NewSourceProvider(),
43 Properties: RustAconfigLibraryProperties{},
44 }
45
46 module := rust.NewSourceProviderModule(android.HostAndDeviceSupported, aconfig, false, false)
47 return module, aconfig
48}
49
50// rust_aconfig_library generates aconfig rust code from the provided aconfig declaration. This module type will
51// create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs
52// properties of other modules.
53func RustAconfigLibraryFactory() android.Module {
54 module, _ := NewRustAconfigLibrary(android.HostAndDeviceSupported)
55 return module.Init()
56}
57
58func (a *aconfigDecorator) SourceProviderProps() []interface{} {
59 return append(a.BaseSourceProvider.SourceProviderProps(), &a.Properties)
60}
61
62func (a *aconfigDecorator) GenerateSource(ctx rust.ModuleContext, deps rust.PathDeps) android.Path {
63 generatedDir := android.PathForModuleGen(ctx)
64 generatedSource := android.PathForModuleGen(ctx, "src", "lib.rs")
65
66 declarationsModules := ctx.GetDirectDepsWithTag(rustDeclarationsTag)
67
68 if len(declarationsModules) != 1 {
69 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
70 }
71 declarations := ctx.OtherModuleProvider(declarationsModules[0], declarationsProviderKey).(declarationsProviderData)
72
Zi Wang275f6542023-11-09 14:59:31 -080073 if a.Properties.Mode != nil && a.Properties.Test != nil {
74 ctx.PropertyErrorf("test", "test prop should not be specified when mode prop is set")
75 }
76 mode := proptools.StringDefault(a.Properties.Mode, "production")
77 if !isModeSupported(mode) {
78 ctx.PropertyErrorf("mode", "%q is not a supported mode", mode)
79 }
80
81 // TODO: remove "Test" property
Vinh Tran0ea4ffa2023-08-11 17:29:33 -040082 if proptools.Bool(a.Properties.Test) {
83 mode = "test"
84 }
85
Vinh Tran457ddef2023-08-02 13:50:26 -040086 ctx.Build(pctx, android.BuildParams{
87 Rule: rustRule,
88 Input: declarations.IntermediatePath,
89 Outputs: []android.WritablePath{
90 generatedSource,
91 },
92 Description: "rust_aconfig_library",
93 Args: map[string]string{
94 "gendir": generatedDir.String(),
Vinh Tran0ea4ffa2023-08-11 17:29:33 -040095 "mode": mode,
Vinh Tran457ddef2023-08-02 13:50:26 -040096 },
97 })
98 a.BaseSourceProvider.OutputFiles = android.Paths{generatedSource}
99 return generatedSource
100}
101
102func (a *aconfigDecorator) SourceProviderDeps(ctx rust.DepsContext, deps rust.Deps) rust.Deps {
103 deps = a.BaseSourceProvider.SourceProviderDeps(ctx, deps)
104 deps.Rustlibs = append(deps.Rustlibs, "libflags_rust")
Dennis Shenba6ed2d2023-11-08 21:02:53 +0000105 deps.Rustlibs = append(deps.Rustlibs, "liblazy_static")
Vinh Tran457ddef2023-08-02 13:50:26 -0400106 ctx.AddDependency(ctx.Module(), rustDeclarationsTag, a.Properties.Aconfig_declarations)
107 return deps
108}