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" |
| 9 | ) |
| 10 | |
| 11 | type rustDeclarationsTagType struct { |
| 12 | blueprint.BaseDependencyTag |
| 13 | } |
| 14 | |
| 15 | var rustDeclarationsTag = rustDeclarationsTagType{} |
| 16 | |
| 17 | type RustAconfigLibraryProperties struct { |
| 18 | // name of the aconfig_declarations module to generate a library for |
| 19 | Aconfig_declarations string |
| 20 | } |
| 21 | |
| 22 | type aconfigDecorator struct { |
| 23 | *rust.BaseSourceProvider |
| 24 | |
| 25 | Properties RustAconfigLibraryProperties |
| 26 | } |
| 27 | |
| 28 | func NewRustAconfigLibrary(hod android.HostOrDeviceSupported) (*rust.Module, *aconfigDecorator) { |
| 29 | aconfig := &aconfigDecorator{ |
| 30 | BaseSourceProvider: rust.NewSourceProvider(), |
| 31 | Properties: RustAconfigLibraryProperties{}, |
| 32 | } |
| 33 | |
| 34 | module := rust.NewSourceProviderModule(android.HostAndDeviceSupported, aconfig, false, false) |
| 35 | return module, aconfig |
| 36 | } |
| 37 | |
| 38 | // rust_aconfig_library generates aconfig rust code from the provided aconfig declaration. This module type will |
| 39 | // create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs |
| 40 | // properties of other modules. |
| 41 | func RustAconfigLibraryFactory() android.Module { |
| 42 | module, _ := NewRustAconfigLibrary(android.HostAndDeviceSupported) |
| 43 | return module.Init() |
| 44 | } |
| 45 | |
| 46 | func (a *aconfigDecorator) SourceProviderProps() []interface{} { |
| 47 | return append(a.BaseSourceProvider.SourceProviderProps(), &a.Properties) |
| 48 | } |
| 49 | |
| 50 | func (a *aconfigDecorator) GenerateSource(ctx rust.ModuleContext, deps rust.PathDeps) android.Path { |
| 51 | generatedDir := android.PathForModuleGen(ctx) |
| 52 | generatedSource := android.PathForModuleGen(ctx, "src", "lib.rs") |
| 53 | |
| 54 | declarationsModules := ctx.GetDirectDepsWithTag(rustDeclarationsTag) |
| 55 | |
| 56 | if len(declarationsModules) != 1 { |
| 57 | panic(fmt.Errorf("Exactly one aconfig_declarations property required")) |
| 58 | } |
| 59 | declarations := ctx.OtherModuleProvider(declarationsModules[0], declarationsProviderKey).(declarationsProviderData) |
| 60 | |
| 61 | ctx.Build(pctx, android.BuildParams{ |
| 62 | Rule: rustRule, |
| 63 | Input: declarations.IntermediatePath, |
| 64 | Outputs: []android.WritablePath{ |
| 65 | generatedSource, |
| 66 | }, |
| 67 | Description: "rust_aconfig_library", |
| 68 | Args: map[string]string{ |
| 69 | "gendir": generatedDir.String(), |
| 70 | // TODO: Add test mode |
| 71 | "mode": "production", |
| 72 | }, |
| 73 | }) |
| 74 | a.BaseSourceProvider.OutputFiles = android.Paths{generatedSource} |
| 75 | return generatedSource |
| 76 | } |
| 77 | |
| 78 | func (a *aconfigDecorator) SourceProviderDeps(ctx rust.DepsContext, deps rust.Deps) rust.Deps { |
| 79 | deps = a.BaseSourceProvider.SourceProviderDeps(ctx, deps) |
| 80 | deps.Rustlibs = append(deps.Rustlibs, "libflags_rust") |
| 81 | ctx.AddDependency(ctx.Module(), rustDeclarationsTag, a.Properties.Aconfig_declarations) |
| 82 | return deps |
| 83 | } |