1*333d2b36SAndroid Build Coastguard Workerpackage codegen 2*333d2b36SAndroid Build Coastguard Worker 3*333d2b36SAndroid Build Coastguard Workerimport ( 4*333d2b36SAndroid Build Coastguard Worker "fmt" 5*333d2b36SAndroid Build Coastguard Worker "strconv" 6*333d2b36SAndroid Build Coastguard Worker 7*333d2b36SAndroid Build Coastguard Worker "android/soong/android" 8*333d2b36SAndroid Build Coastguard Worker "android/soong/rust" 9*333d2b36SAndroid Build Coastguard Worker 10*333d2b36SAndroid Build Coastguard Worker "github.com/google/blueprint" 11*333d2b36SAndroid Build Coastguard Worker "github.com/google/blueprint/proptools" 12*333d2b36SAndroid Build Coastguard Worker) 13*333d2b36SAndroid Build Coastguard Worker 14*333d2b36SAndroid Build Coastguard Workertype rustDeclarationsTagType struct { 15*333d2b36SAndroid Build Coastguard Worker blueprint.BaseDependencyTag 16*333d2b36SAndroid Build Coastguard Worker} 17*333d2b36SAndroid Build Coastguard Worker 18*333d2b36SAndroid Build Coastguard Workervar rustDeclarationsTag = rustDeclarationsTagType{} 19*333d2b36SAndroid Build Coastguard Worker 20*333d2b36SAndroid Build Coastguard Workertype RustAconfigLibraryProperties struct { 21*333d2b36SAndroid Build Coastguard Worker // name of the aconfig_declarations module to generate a library for 22*333d2b36SAndroid Build Coastguard Worker Aconfig_declarations string 23*333d2b36SAndroid Build Coastguard Worker 24*333d2b36SAndroid Build Coastguard Worker // default mode is "production", the other accepted modes are: 25*333d2b36SAndroid Build Coastguard Worker // "test": to generate test mode version of the library 26*333d2b36SAndroid Build Coastguard Worker // "exported": to generate exported mode version of the library 27*333d2b36SAndroid Build Coastguard Worker // "force-read-only": to generate force-read-only mode version of the library 28*333d2b36SAndroid Build Coastguard Worker // an error will be thrown if the mode is not supported 29*333d2b36SAndroid Build Coastguard Worker Mode *string 30*333d2b36SAndroid Build Coastguard Worker} 31*333d2b36SAndroid Build Coastguard Worker 32*333d2b36SAndroid Build Coastguard Workertype aconfigDecorator struct { 33*333d2b36SAndroid Build Coastguard Worker *rust.BaseSourceProvider 34*333d2b36SAndroid Build Coastguard Worker 35*333d2b36SAndroid Build Coastguard Worker Properties RustAconfigLibraryProperties 36*333d2b36SAndroid Build Coastguard Worker} 37*333d2b36SAndroid Build Coastguard Worker 38*333d2b36SAndroid Build Coastguard Workerfunc NewRustAconfigLibrary(hod android.HostOrDeviceSupported) (*rust.Module, *aconfigDecorator) { 39*333d2b36SAndroid Build Coastguard Worker aconfig := &aconfigDecorator{ 40*333d2b36SAndroid Build Coastguard Worker BaseSourceProvider: rust.NewSourceProvider(), 41*333d2b36SAndroid Build Coastguard Worker Properties: RustAconfigLibraryProperties{}, 42*333d2b36SAndroid Build Coastguard Worker } 43*333d2b36SAndroid Build Coastguard Worker 44*333d2b36SAndroid Build Coastguard Worker module := rust.NewSourceProviderModule(android.HostAndDeviceSupported, aconfig, false, false) 45*333d2b36SAndroid Build Coastguard Worker return module, aconfig 46*333d2b36SAndroid Build Coastguard Worker} 47*333d2b36SAndroid Build Coastguard Worker 48*333d2b36SAndroid Build Coastguard Worker// rust_aconfig_library generates aconfig rust code from the provided aconfig declaration. This module type will 49*333d2b36SAndroid Build Coastguard Worker// create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs 50*333d2b36SAndroid Build Coastguard Worker// properties of other modules. 51*333d2b36SAndroid Build Coastguard Workerfunc RustAconfigLibraryFactory() android.Module { 52*333d2b36SAndroid Build Coastguard Worker module, _ := NewRustAconfigLibrary(android.HostAndDeviceSupported) 53*333d2b36SAndroid Build Coastguard Worker return module.Init() 54*333d2b36SAndroid Build Coastguard Worker} 55*333d2b36SAndroid Build Coastguard Worker 56*333d2b36SAndroid Build Coastguard Workerfunc (a *aconfigDecorator) SourceProviderProps() []interface{} { 57*333d2b36SAndroid Build Coastguard Worker return append(a.BaseSourceProvider.SourceProviderProps(), &a.Properties) 58*333d2b36SAndroid Build Coastguard Worker} 59*333d2b36SAndroid Build Coastguard Worker 60*333d2b36SAndroid Build Coastguard Workerfunc (a *aconfigDecorator) GenerateSource(ctx rust.ModuleContext, deps rust.PathDeps) android.Path { 61*333d2b36SAndroid Build Coastguard Worker generatedDir := android.PathForModuleGen(ctx) 62*333d2b36SAndroid Build Coastguard Worker generatedSource := android.PathForModuleGen(ctx, "src", "lib.rs") 63*333d2b36SAndroid Build Coastguard Worker 64*333d2b36SAndroid Build Coastguard Worker declarationsModules := ctx.GetDirectDepsWithTag(rustDeclarationsTag) 65*333d2b36SAndroid Build Coastguard Worker 66*333d2b36SAndroid Build Coastguard Worker if len(declarationsModules) != 1 { 67*333d2b36SAndroid Build Coastguard Worker panic(fmt.Errorf("Exactly one aconfig_declarations property required")) 68*333d2b36SAndroid Build Coastguard Worker } 69*333d2b36SAndroid Build Coastguard Worker declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey) 70*333d2b36SAndroid Build Coastguard Worker 71*333d2b36SAndroid Build Coastguard Worker mode := proptools.StringDefault(a.Properties.Mode, "production") 72*333d2b36SAndroid Build Coastguard Worker if !isModeSupported(mode) { 73*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("mode", "%q is not a supported mode", mode) 74*333d2b36SAndroid Build Coastguard Worker } 75*333d2b36SAndroid Build Coastguard Worker 76*333d2b36SAndroid Build Coastguard Worker ctx.Build(pctx, android.BuildParams{ 77*333d2b36SAndroid Build Coastguard Worker Rule: rustRule, 78*333d2b36SAndroid Build Coastguard Worker Input: declarations.IntermediateCacheOutputPath, 79*333d2b36SAndroid Build Coastguard Worker Outputs: []android.WritablePath{ 80*333d2b36SAndroid Build Coastguard Worker generatedSource, 81*333d2b36SAndroid Build Coastguard Worker }, 82*333d2b36SAndroid Build Coastguard Worker Description: "rust_aconfig_library", 83*333d2b36SAndroid Build Coastguard Worker Args: map[string]string{ 84*333d2b36SAndroid Build Coastguard Worker "gendir": generatedDir.String(), 85*333d2b36SAndroid Build Coastguard Worker "mode": mode, 86*333d2b36SAndroid Build Coastguard Worker "debug": strconv.FormatBool(ctx.Config().ReleaseReadFromNewStorage()), 87*333d2b36SAndroid Build Coastguard Worker }, 88*333d2b36SAndroid Build Coastguard Worker }) 89*333d2b36SAndroid Build Coastguard Worker a.BaseSourceProvider.OutputFiles = android.Paths{generatedSource} 90*333d2b36SAndroid Build Coastguard Worker 91*333d2b36SAndroid Build Coastguard Worker android.SetProvider(ctx, android.CodegenInfoProvider, android.CodegenInfo{ 92*333d2b36SAndroid Build Coastguard Worker ModeInfos: map[string]android.ModeInfo{ 93*333d2b36SAndroid Build Coastguard Worker ctx.ModuleName(): { 94*333d2b36SAndroid Build Coastguard Worker Container: declarations.Container, 95*333d2b36SAndroid Build Coastguard Worker Mode: mode, 96*333d2b36SAndroid Build Coastguard Worker }}, 97*333d2b36SAndroid Build Coastguard Worker }) 98*333d2b36SAndroid Build Coastguard Worker 99*333d2b36SAndroid Build Coastguard Worker return generatedSource 100*333d2b36SAndroid Build Coastguard Worker} 101*333d2b36SAndroid Build Coastguard Worker 102*333d2b36SAndroid Build Coastguard Workerfunc (a *aconfigDecorator) SourceProviderDeps(ctx rust.DepsContext, deps rust.Deps) rust.Deps { 103*333d2b36SAndroid Build Coastguard Worker deps = a.BaseSourceProvider.SourceProviderDeps(ctx, deps) 104*333d2b36SAndroid Build Coastguard Worker deps.Rustlibs = append(deps.Rustlibs, "libaconfig_storage_read_api") 105*333d2b36SAndroid Build Coastguard Worker deps.Rustlibs = append(deps.Rustlibs, "libflags_rust") 106*333d2b36SAndroid Build Coastguard Worker deps.Rustlibs = append(deps.Rustlibs, "liblazy_static") 107*333d2b36SAndroid Build Coastguard Worker deps.Rustlibs = append(deps.Rustlibs, "liblogger") 108*333d2b36SAndroid Build Coastguard Worker deps.Rustlibs = append(deps.Rustlibs, "liblog_rust") 109*333d2b36SAndroid Build Coastguard Worker ctx.AddDependency(ctx.Module(), rustDeclarationsTag, a.Properties.Aconfig_declarations) 110*333d2b36SAndroid Build Coastguard Worker return deps 111*333d2b36SAndroid Build Coastguard Worker} 112