blob: a0a2a546bd34f79ea9a7fb59957b264460d1ceba [file] [log] [blame]
Yu Liueae7b362023-11-16 17:05:47 -08001package codegen
Vinh Tran457ddef2023-08-02 13:50:26 -04002
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
Zi Wang275f6542023-11-09 14:59:31 -080023 // default mode is "production", the other accepted modes are:
24 // "test": to generate test mode version of the library
25 // "exported": to generate exported mode version of the library
Zhi Dou70e21242023-12-20 23:14:34 +000026 // "force-read-only": to generate force-read-only mode version of the library
Zi Wang275f6542023-11-09 14:59:31 -080027 // an error will be thrown if the mode is not supported
28 Mode *string
Vinh Tran457ddef2023-08-02 13:50:26 -040029}
30
31type aconfigDecorator struct {
32 *rust.BaseSourceProvider
33
34 Properties RustAconfigLibraryProperties
35}
36
37func NewRustAconfigLibrary(hod android.HostOrDeviceSupported) (*rust.Module, *aconfigDecorator) {
38 aconfig := &aconfigDecorator{
39 BaseSourceProvider: rust.NewSourceProvider(),
40 Properties: RustAconfigLibraryProperties{},
41 }
42
43 module := rust.NewSourceProviderModule(android.HostAndDeviceSupported, aconfig, false, false)
44 return module, aconfig
45}
46
47// rust_aconfig_library generates aconfig rust code from the provided aconfig declaration. This module type will
48// create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs
49// properties of other modules.
50func RustAconfigLibraryFactory() android.Module {
51 module, _ := NewRustAconfigLibrary(android.HostAndDeviceSupported)
52 return module.Init()
53}
54
55func (a *aconfigDecorator) SourceProviderProps() []interface{} {
56 return append(a.BaseSourceProvider.SourceProviderProps(), &a.Properties)
57}
58
59func (a *aconfigDecorator) GenerateSource(ctx rust.ModuleContext, deps rust.PathDeps) android.Path {
60 generatedDir := android.PathForModuleGen(ctx)
61 generatedSource := android.PathForModuleGen(ctx, "src", "lib.rs")
62
63 declarationsModules := ctx.GetDirectDepsWithTag(rustDeclarationsTag)
64
65 if len(declarationsModules) != 1 {
66 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
67 }
LaMont Jonesaa005ae2023-12-19 19:01:57 +000068 declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey)
Vinh Tran457ddef2023-08-02 13:50:26 -040069
Zi Wang275f6542023-11-09 14:59:31 -080070 mode := proptools.StringDefault(a.Properties.Mode, "production")
71 if !isModeSupported(mode) {
72 ctx.PropertyErrorf("mode", "%q is not a supported mode", mode)
73 }
74
Vinh Tran457ddef2023-08-02 13:50:26 -040075 ctx.Build(pctx, android.BuildParams{
76 Rule: rustRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000077 Input: declarations.IntermediateCacheOutputPath,
Vinh Tran457ddef2023-08-02 13:50:26 -040078 Outputs: []android.WritablePath{
79 generatedSource,
80 },
81 Description: "rust_aconfig_library",
82 Args: map[string]string{
83 "gendir": generatedDir.String(),
Vinh Tran0ea4ffa2023-08-11 17:29:33 -040084 "mode": mode,
Vinh Tran457ddef2023-08-02 13:50:26 -040085 },
86 })
87 a.BaseSourceProvider.OutputFiles = android.Paths{generatedSource}
Yu Liu67a28422024-03-05 00:36:31 +000088
89 android.SetProvider(ctx, android.CodegenInfoProvider, android.CodegenInfo{
90 ModeInfos: map[string]android.ModeInfo{
91 ctx.ModuleName(): {
92 Container: declarations.Container,
93 Mode: mode,
94 }},
95 })
96
Vinh Tran457ddef2023-08-02 13:50:26 -040097 return generatedSource
98}
99
100func (a *aconfigDecorator) SourceProviderDeps(ctx rust.DepsContext, deps rust.Deps) rust.Deps {
101 deps = a.BaseSourceProvider.SourceProviderDeps(ctx, deps)
102 deps.Rustlibs = append(deps.Rustlibs, "libflags_rust")
Dennis Shenba6ed2d2023-11-08 21:02:53 +0000103 deps.Rustlibs = append(deps.Rustlibs, "liblazy_static")
Ted Bauer02d475c2024-03-27 20:56:26 +0000104 deps.Rustlibs = append(deps.Rustlibs, "libaconfig_storage_read_api")
Vinh Tran457ddef2023-08-02 13:50:26 -0400105 ctx.AddDependency(ctx.Module(), rustDeclarationsTag, a.Properties.Aconfig_declarations)
106 return deps
107}