Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 1 | package aconfig |
| 2 | |
| 3 | import ( |
| 4 | "android/soong/android" |
| 5 | "android/soong/rust" |
| 6 | "fmt" |
| 7 | |
| 8 | "github.com/google/blueprint" |
Vinh Tran | 0ea4ffa | 2023-08-11 17:29:33 -0400 | [diff] [blame] | 9 | "github.com/google/blueprint/proptools" |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | type rustDeclarationsTagType struct { |
| 13 | blueprint.BaseDependencyTag |
| 14 | } |
| 15 | |
| 16 | var rustDeclarationsTag = rustDeclarationsTagType{} |
| 17 | |
| 18 | type RustAconfigLibraryProperties struct { |
| 19 | // name of the aconfig_declarations module to generate a library for |
| 20 | Aconfig_declarations string |
Vinh Tran | 0ea4ffa | 2023-08-11 17:29:33 -0400 | [diff] [blame] | 21 | Test *bool |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | type aconfigDecorator struct { |
| 25 | *rust.BaseSourceProvider |
| 26 | |
| 27 | Properties RustAconfigLibraryProperties |
| 28 | } |
| 29 | |
| 30 | func 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. |
| 43 | func RustAconfigLibraryFactory() android.Module { |
| 44 | module, _ := NewRustAconfigLibrary(android.HostAndDeviceSupported) |
| 45 | return module.Init() |
| 46 | } |
| 47 | |
| 48 | func (a *aconfigDecorator) SourceProviderProps() []interface{} { |
| 49 | return append(a.BaseSourceProvider.SourceProviderProps(), &a.Properties) |
| 50 | } |
| 51 | |
| 52 | func (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 Tran | 0ea4ffa | 2023-08-11 17:29:33 -0400 | [diff] [blame] | 63 | mode := "production" |
| 64 | if proptools.Bool(a.Properties.Test) { |
| 65 | mode = "test" |
| 66 | } |
| 67 | |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 68 | 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 Tran | 0ea4ffa | 2023-08-11 17:29:33 -0400 | [diff] [blame] | 77 | "mode": mode, |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 78 | }, |
| 79 | }) |
| 80 | a.BaseSourceProvider.OutputFiles = android.Paths{generatedSource} |
| 81 | return generatedSource |
| 82 | } |
| 83 | |
| 84 | func (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 Shen | ba6ed2d | 2023-11-08 21:02:53 +0000 | [diff] [blame^] | 87 | deps.Rustlibs = append(deps.Rustlibs, "liblazy_static") |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 88 | ctx.AddDependency(ctx.Module(), rustDeclarationsTag, a.Properties.Aconfig_declarations) |
| 89 | return deps |
| 90 | } |