blob: 73b6fece2ee621f337c37d71fa7bf6a232755bf5 [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
26 // an error will be thrown if the mode is not supported
27 Mode *string
Vinh Tran457ddef2023-08-02 13:50:26 -040028}
29
30type aconfigDecorator struct {
31 *rust.BaseSourceProvider
32
33 Properties RustAconfigLibraryProperties
34}
35
36func NewRustAconfigLibrary(hod android.HostOrDeviceSupported) (*rust.Module, *aconfigDecorator) {
37 aconfig := &aconfigDecorator{
38 BaseSourceProvider: rust.NewSourceProvider(),
39 Properties: RustAconfigLibraryProperties{},
40 }
41
42 module := rust.NewSourceProviderModule(android.HostAndDeviceSupported, aconfig, false, false)
43 return module, aconfig
44}
45
46// rust_aconfig_library generates aconfig rust code from the provided aconfig declaration. This module type will
47// create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs
48// properties of other modules.
49func RustAconfigLibraryFactory() android.Module {
50 module, _ := NewRustAconfigLibrary(android.HostAndDeviceSupported)
51 return module.Init()
52}
53
54func (a *aconfigDecorator) SourceProviderProps() []interface{} {
55 return append(a.BaseSourceProvider.SourceProviderProps(), &a.Properties)
56}
57
58func (a *aconfigDecorator) GenerateSource(ctx rust.ModuleContext, deps rust.PathDeps) android.Path {
59 generatedDir := android.PathForModuleGen(ctx)
60 generatedSource := android.PathForModuleGen(ctx, "src", "lib.rs")
61
62 declarationsModules := ctx.GetDirectDepsWithTag(rustDeclarationsTag)
63
64 if len(declarationsModules) != 1 {
65 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
66 }
LaMont Jonesaa005ae2023-12-19 19:01:57 +000067 declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey)
Vinh Tran457ddef2023-08-02 13:50:26 -040068
Zi Wang275f6542023-11-09 14:59:31 -080069 mode := proptools.StringDefault(a.Properties.Mode, "production")
70 if !isModeSupported(mode) {
71 ctx.PropertyErrorf("mode", "%q is not a supported mode", mode)
72 }
73
Vinh Tran457ddef2023-08-02 13:50:26 -040074 ctx.Build(pctx, android.BuildParams{
75 Rule: rustRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000076 Input: declarations.IntermediateCacheOutputPath,
Vinh Tran457ddef2023-08-02 13:50:26 -040077 Outputs: []android.WritablePath{
78 generatedSource,
79 },
80 Description: "rust_aconfig_library",
81 Args: map[string]string{
82 "gendir": generatedDir.String(),
Vinh Tran0ea4ffa2023-08-11 17:29:33 -040083 "mode": mode,
Vinh Tran457ddef2023-08-02 13:50:26 -040084 },
85 })
86 a.BaseSourceProvider.OutputFiles = android.Paths{generatedSource}
87 return generatedSource
88}
89
90func (a *aconfigDecorator) SourceProviderDeps(ctx rust.DepsContext, deps rust.Deps) rust.Deps {
91 deps = a.BaseSourceProvider.SourceProviderDeps(ctx, deps)
92 deps.Rustlibs = append(deps.Rustlibs, "libflags_rust")
Dennis Shenba6ed2d2023-11-08 21:02:53 +000093 deps.Rustlibs = append(deps.Rustlibs, "liblazy_static")
Vinh Tran457ddef2023-08-02 13:50:26 -040094 ctx.AddDependency(ctx.Module(), rustDeclarationsTag, a.Properties.Aconfig_declarations)
95 return deps
96}