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" |
Dennis Shen | 58378c9 | 2024-04-03 19:26:36 +0000 | [diff] [blame] | 5 | "strconv" |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 6 | |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 7 | "android/soong/android" |
| 8 | "android/soong/rust" |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 9 | |
| 10 | "github.com/google/blueprint" |
Vinh Tran | 0ea4ffa | 2023-08-11 17:29:33 -0400 | [diff] [blame] | 11 | "github.com/google/blueprint/proptools" |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | type rustDeclarationsTagType struct { |
| 15 | blueprint.BaseDependencyTag |
| 16 | } |
| 17 | |
| 18 | var rustDeclarationsTag = rustDeclarationsTagType{} |
| 19 | |
| 20 | type RustAconfigLibraryProperties struct { |
| 21 | // name of the aconfig_declarations module to generate a library for |
| 22 | Aconfig_declarations string |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 23 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 24 | // default mode is "production", the other accepted modes are: |
| 25 | // "test": to generate test mode version of the library |
| 26 | // "exported": to generate exported mode version of the library |
Zhi Dou | 70e2124 | 2023-12-20 23:14:34 +0000 | [diff] [blame] | 27 | // "force-read-only": to generate force-read-only mode version of the library |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 28 | // an error will be thrown if the mode is not supported |
| 29 | Mode *string |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | type aconfigDecorator struct { |
| 33 | *rust.BaseSourceProvider |
| 34 | |
| 35 | Properties RustAconfigLibraryProperties |
| 36 | } |
| 37 | |
| 38 | func NewRustAconfigLibrary(hod android.HostOrDeviceSupported) (*rust.Module, *aconfigDecorator) { |
| 39 | aconfig := &aconfigDecorator{ |
| 40 | BaseSourceProvider: rust.NewSourceProvider(), |
| 41 | Properties: RustAconfigLibraryProperties{}, |
| 42 | } |
| 43 | |
| 44 | module := rust.NewSourceProviderModule(android.HostAndDeviceSupported, aconfig, false, false) |
| 45 | return module, aconfig |
| 46 | } |
| 47 | |
| 48 | // rust_aconfig_library generates aconfig rust code from the provided aconfig declaration. This module type will |
| 49 | // create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs |
| 50 | // properties of other modules. |
| 51 | func RustAconfigLibraryFactory() android.Module { |
| 52 | module, _ := NewRustAconfigLibrary(android.HostAndDeviceSupported) |
| 53 | return module.Init() |
| 54 | } |
| 55 | |
| 56 | func (a *aconfigDecorator) SourceProviderProps() []interface{} { |
| 57 | return append(a.BaseSourceProvider.SourceProviderProps(), &a.Properties) |
| 58 | } |
| 59 | |
| 60 | func (a *aconfigDecorator) GenerateSource(ctx rust.ModuleContext, deps rust.PathDeps) android.Path { |
| 61 | generatedDir := android.PathForModuleGen(ctx) |
| 62 | generatedSource := android.PathForModuleGen(ctx, "src", "lib.rs") |
| 63 | |
| 64 | declarationsModules := ctx.GetDirectDepsWithTag(rustDeclarationsTag) |
| 65 | |
| 66 | if len(declarationsModules) != 1 { |
| 67 | panic(fmt.Errorf("Exactly one aconfig_declarations property required")) |
| 68 | } |
LaMont Jones | aa005ae | 2023-12-19 19:01:57 +0000 | [diff] [blame] | 69 | declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey) |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 70 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 71 | mode := proptools.StringDefault(a.Properties.Mode, "production") |
| 72 | if !isModeSupported(mode) { |
| 73 | ctx.PropertyErrorf("mode", "%q is not a supported mode", mode) |
| 74 | } |
| 75 | |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 76 | ctx.Build(pctx, android.BuildParams{ |
| 77 | Rule: rustRule, |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 78 | Input: declarations.IntermediateCacheOutputPath, |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 79 | Outputs: []android.WritablePath{ |
| 80 | generatedSource, |
| 81 | }, |
| 82 | Description: "rust_aconfig_library", |
| 83 | Args: map[string]string{ |
| 84 | "gendir": generatedDir.String(), |
Vinh Tran | 0ea4ffa | 2023-08-11 17:29:33 -0400 | [diff] [blame] | 85 | "mode": mode, |
Dennis Shen | 58378c9 | 2024-04-03 19:26:36 +0000 | [diff] [blame] | 86 | "debug": strconv.FormatBool(ctx.Config().ReleaseReadFromNewStorage()), |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 87 | }, |
| 88 | }) |
| 89 | a.BaseSourceProvider.OutputFiles = android.Paths{generatedSource} |
Yu Liu | 67a2842 | 2024-03-05 00:36:31 +0000 | [diff] [blame] | 90 | |
| 91 | android.SetProvider(ctx, android.CodegenInfoProvider, android.CodegenInfo{ |
| 92 | ModeInfos: map[string]android.ModeInfo{ |
| 93 | ctx.ModuleName(): { |
| 94 | Container: declarations.Container, |
| 95 | Mode: mode, |
| 96 | }}, |
| 97 | }) |
| 98 | |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 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) |
Ted Bauer | 6ef40db | 2024-03-29 14:04:10 +0000 | [diff] [blame] | 104 | deps.Rustlibs = append(deps.Rustlibs, "libaconfig_storage_read_api") |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 105 | deps.Rustlibs = append(deps.Rustlibs, "libflags_rust") |
Dennis Shen | ba6ed2d | 2023-11-08 21:02:53 +0000 | [diff] [blame] | 106 | deps.Rustlibs = append(deps.Rustlibs, "liblazy_static") |
Ted Bauer | 6ef40db | 2024-03-29 14:04:10 +0000 | [diff] [blame] | 107 | deps.Rustlibs = append(deps.Rustlibs, "liblogger") |
| 108 | deps.Rustlibs = append(deps.Rustlibs, "liblog_rust") |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 109 | ctx.AddDependency(ctx.Module(), rustDeclarationsTag, a.Properties.Aconfig_declarations) |
| 110 | return deps |
| 111 | } |