Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 1 | // Copyright (C) 2024 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. |
| 14 | |
| 15 | package fsgen |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/etc" |
| 20 | "fmt" |
| 21 | "path/filepath" |
| 22 | "strings" |
| 23 | |
| 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
| 27 | type srcBaseFileInstallBaseFileTuple struct { |
| 28 | srcBaseFile string |
| 29 | installBaseFile string |
| 30 | } |
| 31 | |
| 32 | // prebuilt src files grouped by the install partitions. |
| 33 | // Each groups are a mapping of the relative install path to the name of the files |
| 34 | type prebuiltSrcGroupByInstallPartition struct { |
| 35 | system map[string][]srcBaseFileInstallBaseFileTuple |
| 36 | system_ext map[string][]srcBaseFileInstallBaseFileTuple |
| 37 | product map[string][]srcBaseFileInstallBaseFileTuple |
| 38 | vendor map[string][]srcBaseFileInstallBaseFileTuple |
| 39 | } |
| 40 | |
| 41 | func newPrebuiltSrcGroupByInstallPartition() *prebuiltSrcGroupByInstallPartition { |
| 42 | return &prebuiltSrcGroupByInstallPartition{ |
| 43 | system: map[string][]srcBaseFileInstallBaseFileTuple{}, |
| 44 | system_ext: map[string][]srcBaseFileInstallBaseFileTuple{}, |
| 45 | product: map[string][]srcBaseFileInstallBaseFileTuple{}, |
| 46 | vendor: map[string][]srcBaseFileInstallBaseFileTuple{}, |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func isSubdirectory(parent, child string) bool { |
| 51 | rel, err := filepath.Rel(parent, child) |
| 52 | if err != nil { |
| 53 | return false |
| 54 | } |
| 55 | return !strings.HasPrefix(rel, "..") |
| 56 | } |
| 57 | |
| 58 | func appendIfCorrectInstallPartition(partitionToInstallPathList []partitionToInstallPath, destPath, srcPath string, srcGroup *prebuiltSrcGroupByInstallPartition) { |
| 59 | for _, part := range partitionToInstallPathList { |
| 60 | partition := part.name |
| 61 | installPath := part.installPath |
| 62 | |
| 63 | if isSubdirectory(installPath, destPath) { |
| 64 | relativeInstallPath, _ := filepath.Rel(installPath, destPath) |
| 65 | relativeInstallDir := filepath.Dir(relativeInstallPath) |
| 66 | var srcMap map[string][]srcBaseFileInstallBaseFileTuple |
| 67 | switch partition { |
| 68 | case "system": |
| 69 | srcMap = srcGroup.system |
| 70 | case "system_ext": |
| 71 | srcMap = srcGroup.system_ext |
| 72 | case "product": |
| 73 | srcMap = srcGroup.product |
| 74 | case "vendor": |
| 75 | srcMap = srcGroup.vendor |
| 76 | } |
| 77 | if srcMap != nil { |
| 78 | srcMap[relativeInstallDir] = append(srcMap[relativeInstallDir], srcBaseFileInstallBaseFileTuple{ |
| 79 | srcBaseFile: filepath.Base(srcPath), |
| 80 | installBaseFile: filepath.Base(destPath), |
| 81 | }) |
| 82 | } |
| 83 | return |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 88 | // Create a map of source files to the list of destination files from PRODUCT_COPY_FILES entries. |
| 89 | // Note that the value of the map is a list of string, given that a single source file can be |
| 90 | // copied to multiple files. |
| 91 | // This function also checks the existence of the source files, and validates that there is no |
| 92 | // multiple source files copying to the same dest file. |
| 93 | func uniqueExistingProductCopyFileMap(ctx android.LoadHookContext) map[string][]string { |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 94 | seen := make(map[string]bool) |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 95 | filtered := make(map[string][]string) |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 96 | |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 97 | for _, copyFilePair := range ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.ProductCopyFiles { |
| 98 | srcDestList := strings.Split(copyFilePair, ":") |
| 99 | if len(srcDestList) < 2 { |
| 100 | ctx.ModuleErrorf("PRODUCT_COPY_FILES must follow the format \"src:dest\", got: %s", copyFilePair) |
| 101 | } |
| 102 | src, dest := srcDestList[0], srcDestList[1] |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 103 | if _, ok := seen[dest]; !ok { |
| 104 | if optionalPath := android.ExistentPathForSource(ctx, src); optionalPath.Valid() { |
| 105 | seen[dest] = true |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 106 | filtered[src] = append(filtered[src], dest) |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return filtered |
| 112 | } |
| 113 | |
| 114 | type partitionToInstallPath struct { |
| 115 | name string |
| 116 | installPath string |
| 117 | } |
| 118 | |
| 119 | func processProductCopyFiles(ctx android.LoadHookContext) map[string]*prebuiltSrcGroupByInstallPartition { |
| 120 | // Filter out duplicate dest entries and non existing src entries |
| 121 | productCopyFileMap := uniqueExistingProductCopyFileMap(ctx) |
| 122 | |
| 123 | // System is intentionally added at the last to consider the scenarios where |
| 124 | // non-system partitions are installed as part of the system partition |
| 125 | partitionToInstallPathList := []partitionToInstallPath{ |
| 126 | {name: "vendor", installPath: ctx.DeviceConfig().VendorPath()}, |
| 127 | {name: "product", installPath: ctx.DeviceConfig().ProductPath()}, |
| 128 | {name: "system_ext", installPath: ctx.DeviceConfig().SystemExtPath()}, |
| 129 | {name: "system", installPath: "system"}, |
| 130 | } |
| 131 | |
| 132 | groupedSources := map[string]*prebuiltSrcGroupByInstallPartition{} |
| 133 | for _, src := range android.SortedKeys(productCopyFileMap) { |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 134 | destFiles := productCopyFileMap[src] |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 135 | srcFileDir := filepath.Dir(src) |
| 136 | if _, ok := groupedSources[srcFileDir]; !ok { |
| 137 | groupedSources[srcFileDir] = newPrebuiltSrcGroupByInstallPartition() |
| 138 | } |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 139 | for _, dest := range destFiles { |
| 140 | appendIfCorrectInstallPartition(partitionToInstallPathList, dest, filepath.Base(src), groupedSources[srcFileDir]) |
| 141 | } |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | return groupedSources |
| 145 | } |
| 146 | |
| 147 | type prebuiltModuleProperties struct { |
| 148 | Name *string |
| 149 | |
| 150 | Soc_specific *bool |
| 151 | Product_specific *bool |
| 152 | System_ext_specific *bool |
| 153 | |
| 154 | Srcs []string |
| 155 | Dsts []string |
| 156 | |
| 157 | No_full_install *bool |
| 158 | |
| 159 | NamespaceExportedToMake bool |
| 160 | |
| 161 | Visibility []string |
| 162 | } |
| 163 | |
| 164 | // Split relative_install_path to a separate struct, because it is not supported for every |
| 165 | // modules listed in [etcInstallPathToFactoryMap] |
| 166 | type prebuiltSubdirProperties struct { |
| 167 | // If the base file name of the src and dst all match, dsts property does not need to be |
| 168 | // set, and only relative_install_path can be set. |
| 169 | Relative_install_path *string |
| 170 | } |
| 171 | |
| 172 | var ( |
| 173 | etcInstallPathToFactoryList = map[string]android.ModuleFactory{ |
| 174 | "": etc.PrebuiltRootFactory, |
| 175 | "avb": etc.PrebuiltAvbFactory, |
| 176 | "bin": etc.PrebuiltBinaryFactory, |
| 177 | "bt_firmware": etc.PrebuiltBtFirmwareFactory, |
| 178 | "cacerts": etc.PrebuiltEtcCaCertsFactory, |
| 179 | "dsp": etc.PrebuiltDSPFactory, |
| 180 | "etc": etc.PrebuiltEtcFactory, |
| 181 | "etc/dsp": etc.PrebuiltDSPFactory, |
| 182 | "etc/firmware": etc.PrebuiltFirmwareFactory, |
| 183 | "firmware": etc.PrebuiltFirmwareFactory, |
| 184 | "fonts": etc.PrebuiltFontFactory, |
| 185 | "framework": etc.PrebuiltFrameworkFactory, |
| 186 | "lib": etc.PrebuiltRenderScriptBitcodeFactory, |
| 187 | "lib64": etc.PrebuiltRenderScriptBitcodeFactory, |
| 188 | "lib/rfsa": etc.PrebuiltRFSAFactory, |
| 189 | "media": etc.PrebuiltMediaFactory, |
| 190 | "odm": etc.PrebuiltOdmFactory, |
Jihoon Kang | dca2f2b | 2024-11-06 18:43:19 +0000 | [diff] [blame] | 191 | "optee": etc.PrebuiltOpteeFactory, |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 192 | "overlay": etc.PrebuiltOverlayFactory, |
| 193 | "priv-app": etc.PrebuiltPrivAppFactory, |
| 194 | "res": etc.PrebuiltResFactory, |
| 195 | "rfs": etc.PrebuiltRfsFactory, |
| 196 | "tts": etc.PrebuiltVoicepackFactory, |
Jihoon Kang | ecf76dd | 2024-11-12 05:24:46 +0000 | [diff] [blame] | 197 | "tvconfig": etc.PrebuiltTvConfigFactory, |
Jihoon Kang | dca2f2b | 2024-11-06 18:43:19 +0000 | [diff] [blame] | 198 | "tvservice": etc.PrebuiltTvServiceFactory, |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 199 | "usr/share": etc.PrebuiltUserShareFactory, |
| 200 | "usr/hyphen-data": etc.PrebuiltUserHyphenDataFactory, |
| 201 | "usr/keylayout": etc.PrebuiltUserKeyLayoutFactory, |
| 202 | "usr/keychars": etc.PrebuiltUserKeyCharsFactory, |
| 203 | "usr/srec": etc.PrebuiltUserSrecFactory, |
| 204 | "usr/idc": etc.PrebuiltUserIdcFactory, |
| 205 | "vendor_dlkm": etc.PrebuiltVendorDlkmFactory, |
| 206 | "wallpaper": etc.PrebuiltWallpaperFactory, |
| 207 | "wlc_upt": etc.PrebuiltWlcUptFactory, |
| 208 | } |
| 209 | ) |
| 210 | |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 211 | func generatedPrebuiltEtcModuleName(partition, srcDir, destDir string, count int) string { |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 212 | // generated module name follows the pattern: |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 213 | // <install partition>-<src file path>-<relative install path from partition root>-<number> |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 214 | // Note that all path separators are replaced with "_" in the name |
| 215 | moduleName := partition |
| 216 | if !android.InList(srcDir, []string{"", "."}) { |
| 217 | moduleName += fmt.Sprintf("-%s", strings.ReplaceAll(srcDir, string(filepath.Separator), "_")) |
| 218 | } |
| 219 | if !android.InList(destDir, []string{"", "."}) { |
| 220 | moduleName += fmt.Sprintf("-%s", strings.ReplaceAll(destDir, string(filepath.Separator), "_")) |
| 221 | } |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 222 | moduleName += fmt.Sprintf("-%d", count) |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 223 | |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 224 | return moduleName |
| 225 | } |
| 226 | |
| 227 | func groupDestFilesBySrc(destFiles []srcBaseFileInstallBaseFileTuple) (ret map[string][]srcBaseFileInstallBaseFileTuple, maxLen int) { |
| 228 | ret = map[string][]srcBaseFileInstallBaseFileTuple{} |
| 229 | maxLen = 0 |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 230 | for _, tuple := range destFiles { |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 231 | if _, ok := ret[tuple.srcBaseFile]; !ok { |
| 232 | ret[tuple.srcBaseFile] = []srcBaseFileInstallBaseFileTuple{} |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 233 | } |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 234 | ret[tuple.srcBaseFile] = append(ret[tuple.srcBaseFile], tuple) |
| 235 | maxLen = max(maxLen, len(ret[tuple.srcBaseFile])) |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 236 | } |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 237 | return ret, maxLen |
| 238 | } |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 239 | |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 240 | func prebuiltEtcModuleProps(moduleName, partition string) prebuiltModuleProperties { |
| 241 | moduleProps := prebuiltModuleProperties{} |
| 242 | moduleProps.Name = proptools.StringPtr(moduleName) |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 243 | |
| 244 | // Set partition specific properties |
| 245 | switch partition { |
| 246 | case "system_ext": |
| 247 | moduleProps.System_ext_specific = proptools.BoolPtr(true) |
| 248 | case "product": |
| 249 | moduleProps.Product_specific = proptools.BoolPtr(true) |
| 250 | case "vendor": |
| 251 | moduleProps.Soc_specific = proptools.BoolPtr(true) |
| 252 | } |
| 253 | |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 254 | moduleProps.No_full_install = proptools.BoolPtr(true) |
| 255 | moduleProps.NamespaceExportedToMake = true |
| 256 | moduleProps.Visibility = []string{"//visibility:public"} |
| 257 | |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 258 | return moduleProps |
| 259 | } |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 260 | |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 261 | func createPrebuiltEtcModulesInDirectory(ctx android.LoadHookContext, partition, srcDir, destDir string, destFiles []srcBaseFileInstallBaseFileTuple) (moduleNames []string) { |
| 262 | groupedDestFiles, maxLen := groupDestFilesBySrc(destFiles) |
| 263 | |
| 264 | // Find out the most appropriate module type to generate |
| 265 | var etcInstallPathKey string |
| 266 | for _, etcInstallPath := range android.SortedKeys(etcInstallPathToFactoryList) { |
| 267 | // Do not break when found but iterate until the end to find a module with more |
| 268 | // specific install path |
| 269 | if strings.HasPrefix(destDir, etcInstallPath) { |
| 270 | etcInstallPathKey = etcInstallPath |
| 271 | } |
| 272 | } |
| 273 | relDestDirFromInstallDirBase, _ := filepath.Rel(etcInstallPathKey, destDir) |
| 274 | |
| 275 | for fileIndex := range maxLen { |
| 276 | srcTuple := []srcBaseFileInstallBaseFileTuple{} |
| 277 | for _, groupedDestFile := range groupedDestFiles { |
| 278 | if len(groupedDestFile) > fileIndex { |
| 279 | srcTuple = append(srcTuple, groupedDestFile[fileIndex]) |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | moduleName := generatedPrebuiltEtcModuleName(partition, srcDir, destDir, fileIndex) |
| 284 | moduleProps := prebuiltEtcModuleProps(moduleName, partition) |
| 285 | modulePropsPtr := &moduleProps |
| 286 | propsList := []interface{}{modulePropsPtr} |
| 287 | |
| 288 | allCopyFileNamesUnchanged := true |
| 289 | var srcBaseFiles, installBaseFiles []string |
| 290 | for _, tuple := range srcTuple { |
| 291 | if tuple.srcBaseFile != tuple.installBaseFile { |
| 292 | allCopyFileNamesUnchanged = false |
| 293 | } |
| 294 | srcBaseFiles = append(srcBaseFiles, tuple.srcBaseFile) |
| 295 | installBaseFiles = append(installBaseFiles, tuple.installBaseFile) |
| 296 | } |
| 297 | |
| 298 | // Set appropriate srcs, dsts, and releative_install_path based on |
| 299 | // the source and install file names |
| 300 | if allCopyFileNamesUnchanged { |
| 301 | modulePropsPtr.Srcs = srcBaseFiles |
| 302 | |
| 303 | // Specify relative_install_path if it is not installed in the root directory of the |
| 304 | // partition |
| 305 | if !android.InList(relDestDirFromInstallDirBase, []string{"", "."}) { |
| 306 | propsList = append(propsList, &prebuiltSubdirProperties{ |
| 307 | Relative_install_path: proptools.StringPtr(relDestDirFromInstallDirBase), |
| 308 | }) |
| 309 | } |
| 310 | } else { |
| 311 | modulePropsPtr.Srcs = srcBaseFiles |
| 312 | dsts := []string{} |
| 313 | for _, installBaseFile := range installBaseFiles { |
| 314 | dsts = append(dsts, filepath.Join(relDestDirFromInstallDirBase, installBaseFile)) |
| 315 | } |
| 316 | modulePropsPtr.Dsts = dsts |
| 317 | } |
| 318 | |
| 319 | ctx.CreateModuleInDirectory(etcInstallPathToFactoryList[etcInstallPathKey], srcDir, propsList...) |
| 320 | moduleNames = append(moduleNames, moduleName) |
| 321 | } |
| 322 | |
| 323 | return moduleNames |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | func createPrebuiltEtcModulesForPartition(ctx android.LoadHookContext, partition, srcDir string, destDirFilesMap map[string][]srcBaseFileInstallBaseFileTuple) (ret []string) { |
| 327 | for _, destDir := range android.SortedKeys(destDirFilesMap) { |
Jihoon Kang | 3a8759c | 2024-11-08 19:35:09 +0000 | [diff] [blame] | 328 | ret = append(ret, createPrebuiltEtcModulesInDirectory(ctx, partition, srcDir, destDir, destDirFilesMap[destDir])...) |
Jihoon Kang | add2bb2 | 2024-11-05 22:29:34 +0000 | [diff] [blame] | 329 | } |
| 330 | return ret |
| 331 | } |
| 332 | |
| 333 | // Creates prebuilt_* modules based on the install paths and returns the list of generated |
| 334 | // module names |
| 335 | func createPrebuiltEtcModules(ctx android.LoadHookContext) (ret []string) { |
| 336 | groupedSources := processProductCopyFiles(ctx) |
| 337 | for _, srcDir := range android.SortedKeys(groupedSources) { |
| 338 | groupedSource := groupedSources[srcDir] |
| 339 | ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "system", srcDir, groupedSource.system)...) |
| 340 | ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "system_ext", srcDir, groupedSource.system_ext)...) |
| 341 | ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "product", srcDir, groupedSource.product)...) |
| 342 | ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "vendor", srcDir, groupedSource.vendor)...) |
| 343 | } |
| 344 | |
| 345 | return ret |
| 346 | } |