Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 1 | package codegen |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 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 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 23 | // 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 Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | type aconfigDecorator struct { |
| 31 | *rust.BaseSourceProvider |
| 32 | |
| 33 | Properties RustAconfigLibraryProperties |
| 34 | } |
| 35 | |
| 36 | func 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. |
| 49 | func RustAconfigLibraryFactory() android.Module { |
| 50 | module, _ := NewRustAconfigLibrary(android.HostAndDeviceSupported) |
| 51 | return module.Init() |
| 52 | } |
| 53 | |
| 54 | func (a *aconfigDecorator) SourceProviderProps() []interface{} { |
| 55 | return append(a.BaseSourceProvider.SourceProviderProps(), &a.Properties) |
| 56 | } |
| 57 | |
| 58 | func (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 Jones | aa005ae | 2023-12-19 19:01:57 +0000 | [diff] [blame] | 67 | declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey) |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 68 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 69 | 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 Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 74 | ctx.Build(pctx, android.BuildParams{ |
| 75 | Rule: rustRule, |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 76 | Input: declarations.IntermediateCacheOutputPath, |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 77 | Outputs: []android.WritablePath{ |
| 78 | generatedSource, |
| 79 | }, |
| 80 | Description: "rust_aconfig_library", |
| 81 | Args: map[string]string{ |
| 82 | "gendir": generatedDir.String(), |
Vinh Tran | 0ea4ffa | 2023-08-11 17:29:33 -0400 | [diff] [blame] | 83 | "mode": mode, |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 84 | }, |
| 85 | }) |
| 86 | a.BaseSourceProvider.OutputFiles = android.Paths{generatedSource} |
| 87 | return generatedSource |
| 88 | } |
| 89 | |
| 90 | func (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 Shen | ba6ed2d | 2023-11-08 21:02:53 +0000 | [diff] [blame] | 93 | deps.Rustlibs = append(deps.Rustlibs, "liblazy_static") |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 94 | ctx.AddDependency(ctx.Module(), rustDeclarationsTag, a.Properties.Aconfig_declarations) |
| 95 | return deps |
| 96 | } |