1// Copyright 2021 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14package etc 15 16// This file implements snapshot module of 'prebuilt_etc' type 17// 'snapshot_etc' module defines android.PrebuiltInterface so it can be handled 18// as prebuilt of 'prebuilt_etc' type. 19// Properties of 'snapshot_etc' follows properties from snapshotJsonFlags type 20 21import ( 22 "android/soong/android" 23 "fmt" 24 "strings" 25 26 "github.com/google/blueprint" 27 "github.com/google/blueprint/proptools" 28) 29 30func RegisterSnapshotEtcModule(ctx android.RegistrationContext) { 31 ctx.RegisterModuleType("snapshot_etc", SnapshotEtcFactory) 32} 33 34func init() { 35 RegisterSnapshotEtcModule(android.InitRegistrationContext) 36} 37 38// snapshot_etc is a prebuilt module type to be installed under etc which is auto-generated by 39// development/vendor_snapshot/update.py. This module will override prebuilt_etc module with same 40// name when 'prefer' property is true. 41func SnapshotEtcFactory() android.Module { 42 module := &SnapshotEtc{} 43 module.AddProperties(&module.properties) 44 45 var srcsSupplier = func(_ android.BaseModuleContext, prebuilt android.Module) []string { 46 s, ok := prebuilt.(*SnapshotEtc) 47 if !ok || s.properties.Src == nil { 48 return []string{} 49 } 50 51 return []string{*s.properties.Src} 52 } 53 54 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) 55 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "src") 56 return module 57} 58 59type snapshotEtcProperties struct { 60 Src *string `android:"path,arch_variant"` // Source of snapshot_etc file 61 Filename *string `android:"arch_variant"` // Target file name when it differs from module name 62 Relative_install_path *string `android:"arch_variant"` // Relative install path when it should be installed subdirectory of etc 63} 64 65type SnapshotEtc struct { 66 android.ModuleBase 67 prebuilt android.Prebuilt 68 properties snapshotEtcProperties 69 70 outputFilePath android.OutputPath 71 installDirPath android.InstallPath 72} 73 74func (s *SnapshotEtc) Prebuilt() *android.Prebuilt { 75 return &s.prebuilt 76} 77 78func (s *SnapshotEtc) Name() string { 79 return s.prebuilt.Name(s.BaseModuleName()) 80} 81 82func (s *SnapshotEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) { 83 if s.properties.Src == nil { 84 ctx.PropertyErrorf("src", "missing prebuilt source file") 85 return 86 } 87 88 sourceFilePath := s.prebuilt.SingleSourcePath(ctx) 89 90 // Determine the output file basename. 91 // If Filename is set, use the name specified by the property. 92 // Otherwise use the module name. 93 filename := proptools.String(s.properties.Filename) 94 if filename == "" { 95 filename = ctx.ModuleName() 96 } 97 98 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath 99 100 if strings.Contains(filename, "/") { 101 ctx.PropertyErrorf("filename", "filename cannot contain separator '/'") 102 return 103 } 104 105 subDir := "" 106 if s.properties.Relative_install_path != nil { 107 subDir = *s.properties.Relative_install_path 108 } 109 110 s.installDirPath = android.PathForModuleInstall(ctx, "etc", subDir) 111 112 // This ensures that outputFilePath has the correct name for others to 113 // use, as the source file may have a different name. 114 ctx.Build(pctx, android.BuildParams{ 115 Rule: android.Cp, 116 Input: sourceFilePath, 117 Output: s.outputFilePath, 118 Description: "Install snapshot etc module " + s.BaseModuleName(), 119 }) 120 121 ctx.InstallFile(s.installDirPath, s.outputFilePath.Base(), sourceFilePath) 122} 123 124func (p *SnapshotEtc) AndroidMkEntries() []android.AndroidMkEntries { 125 return []android.AndroidMkEntries{{ 126 Class: "ETC", 127 OutputFile: android.OptionalPathForPath(p.outputFilePath), 128 ExtraEntries: []android.AndroidMkExtraEntriesFunc{ 129 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { 130 entries.SetString("LOCAL_MODULE_TAGS", "optional") 131 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.String()) 132 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base()) 133 }, 134 }, 135 }} 136} 137 138type snapshotEtcDependencyTag struct { 139 blueprint.DependencyTag 140} 141 142var tag = snapshotEtcDependencyTag{} 143 144func (s *SnapshotEtc) CoreVariantNeeded(ctx android.BaseModuleContext) bool { 145 return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk() && 146 !s.ModuleBase.InstallInVendorRamdisk() && !s.ModuleBase.InstallInDebugRamdisk() 147} 148 149func (p *SnapshotEtc) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { 150 return p.ModuleBase.InstallInRamdisk() 151} 152 153func (p *SnapshotEtc) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { 154 return p.ModuleBase.InstallInVendorRamdisk() 155} 156 157func (p *SnapshotEtc) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { 158 return p.ModuleBase.InstallInDebugRamdisk() 159} 160 161func (p *SnapshotEtc) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { 162 return p.ModuleBase.InstallInRecovery() 163} 164 165func (p *SnapshotEtc) ExtraImageVariations(ctx android.BaseModuleContext) []string { 166 return nil 167} 168 169func (p *SnapshotEtc) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { 170} 171 172func (p *SnapshotEtc) ImageMutatorBegin(ctx android.BaseModuleContext) {} 173 174func (p *SnapshotEtc) OutputFiles(tag string) (android.Paths, error) { 175 switch tag { 176 case "": 177 return android.Paths{p.outputFilePath}, nil 178 default: 179 return nil, fmt.Errorf("unsupported module reference tag %q", tag) 180 } 181 182} 183 184var _ android.PrebuiltInterface = (*SnapshotEtc)(nil) 185var _ android.ImageInterface = (*SnapshotEtc)(nil) 186var _ android.OutputFileProducer = (*SnapshotEtc)(nil) 187