Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 1 | package aconfig |
| 2 | |
| 3 | import ( |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame^] | 4 | "fmt" |
| 5 | |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 6 | "android/soong/android" |
| 7 | "android/soong/rust" |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 8 | |
| 9 | "github.com/google/blueprint" |
Vinh Tran | 0ea4ffa | 2023-08-11 17:29:33 -0400 | [diff] [blame] | 10 | "github.com/google/blueprint/proptools" |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | type rustDeclarationsTagType struct { |
| 14 | blueprint.BaseDependencyTag |
| 15 | } |
| 16 | |
| 17 | var rustDeclarationsTag = rustDeclarationsTagType{} |
| 18 | |
| 19 | type RustAconfigLibraryProperties struct { |
| 20 | // name of the aconfig_declarations module to generate a library for |
| 21 | Aconfig_declarations string |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame^] | 22 | |
| 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 Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type aconfigDecorator struct { |
| 35 | *rust.BaseSourceProvider |
| 36 | |
| 37 | Properties RustAconfigLibraryProperties |
| 38 | } |
| 39 | |
| 40 | func 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. |
| 53 | func RustAconfigLibraryFactory() android.Module { |
| 54 | module, _ := NewRustAconfigLibrary(android.HostAndDeviceSupported) |
| 55 | return module.Init() |
| 56 | } |
| 57 | |
| 58 | func (a *aconfigDecorator) SourceProviderProps() []interface{} { |
| 59 | return append(a.BaseSourceProvider.SourceProviderProps(), &a.Properties) |
| 60 | } |
| 61 | |
| 62 | func (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 Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame^] | 73 | 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 Tran | 0ea4ffa | 2023-08-11 17:29:33 -0400 | [diff] [blame] | 82 | if proptools.Bool(a.Properties.Test) { |
| 83 | mode = "test" |
| 84 | } |
| 85 | |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 86 | 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 Tran | 0ea4ffa | 2023-08-11 17:29:33 -0400 | [diff] [blame] | 95 | "mode": mode, |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 96 | }, |
| 97 | }) |
| 98 | a.BaseSourceProvider.OutputFiles = android.Paths{generatedSource} |
| 99 | return generatedSource |
| 100 | } |
| 101 | |
| 102 | func (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 Shen | ba6ed2d | 2023-11-08 21:02:53 +0000 | [diff] [blame] | 105 | deps.Rustlibs = append(deps.Rustlibs, "liblazy_static") |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 106 | ctx.AddDependency(ctx.Module(), rustDeclarationsTag, a.Properties.Aconfig_declarations) |
| 107 | return deps |
| 108 | } |