blob: 83990a638e75035c207240678dc2537a01e393b1 [file] [log] [blame]
Jihoon Kangadd2bb22024-11-05 22:29:34 +00001// 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
15package fsgen
16
17import (
18 "android/soong/android"
19 "android/soong/etc"
20 "fmt"
21 "path/filepath"
22 "strings"
23
24 "github.com/google/blueprint/proptools"
25)
26
27type 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
34type 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
41func 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
50func 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
58func 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 Kang3a8759c2024-11-08 19:35:09 +000088// 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.
93func uniqueExistingProductCopyFileMap(ctx android.LoadHookContext) map[string][]string {
Jihoon Kangadd2bb22024-11-05 22:29:34 +000094 seen := make(map[string]bool)
Jihoon Kang3a8759c2024-11-08 19:35:09 +000095 filtered := make(map[string][]string)
Jihoon Kangadd2bb22024-11-05 22:29:34 +000096
Jihoon Kang3a8759c2024-11-08 19:35:09 +000097 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 Kangf27c3a52024-11-12 21:27:09 +0000103
104 // Some downstream branches use absolute path as entries in PRODUCT_COPY_FILES.
105 // Convert them to relative path from top and check if they do not escape the tree root.
106 relSrc := android.ToRelativeSourcePath(ctx, src)
107
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000108 if _, ok := seen[dest]; !ok {
Jihoon Kangf27c3a52024-11-12 21:27:09 +0000109 if optionalPath := android.ExistentPathForSource(ctx, relSrc); optionalPath.Valid() {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000110 seen[dest] = true
Jihoon Kangf27c3a52024-11-12 21:27:09 +0000111 filtered[relSrc] = append(filtered[relSrc], dest)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000112 }
113 }
114 }
115
116 return filtered
117}
118
119type partitionToInstallPath struct {
120 name string
121 installPath string
122}
123
124func processProductCopyFiles(ctx android.LoadHookContext) map[string]*prebuiltSrcGroupByInstallPartition {
125 // Filter out duplicate dest entries and non existing src entries
126 productCopyFileMap := uniqueExistingProductCopyFileMap(ctx)
127
128 // System is intentionally added at the last to consider the scenarios where
129 // non-system partitions are installed as part of the system partition
130 partitionToInstallPathList := []partitionToInstallPath{
131 {name: "vendor", installPath: ctx.DeviceConfig().VendorPath()},
132 {name: "product", installPath: ctx.DeviceConfig().ProductPath()},
133 {name: "system_ext", installPath: ctx.DeviceConfig().SystemExtPath()},
134 {name: "system", installPath: "system"},
135 }
136
137 groupedSources := map[string]*prebuiltSrcGroupByInstallPartition{}
138 for _, src := range android.SortedKeys(productCopyFileMap) {
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000139 destFiles := productCopyFileMap[src]
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000140 srcFileDir := filepath.Dir(src)
141 if _, ok := groupedSources[srcFileDir]; !ok {
142 groupedSources[srcFileDir] = newPrebuiltSrcGroupByInstallPartition()
143 }
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000144 for _, dest := range destFiles {
145 appendIfCorrectInstallPartition(partitionToInstallPathList, dest, filepath.Base(src), groupedSources[srcFileDir])
146 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000147 }
148
149 return groupedSources
150}
151
152type prebuiltModuleProperties struct {
153 Name *string
154
155 Soc_specific *bool
156 Product_specific *bool
157 System_ext_specific *bool
158
159 Srcs []string
160 Dsts []string
161
162 No_full_install *bool
163
164 NamespaceExportedToMake bool
165
166 Visibility []string
167}
168
169// Split relative_install_path to a separate struct, because it is not supported for every
170// modules listed in [etcInstallPathToFactoryMap]
171type prebuiltSubdirProperties struct {
172 // If the base file name of the src and dst all match, dsts property does not need to be
173 // set, and only relative_install_path can be set.
174 Relative_install_path *string
175}
176
177var (
178 etcInstallPathToFactoryList = map[string]android.ModuleFactory{
Jihoon Kang320ca7c2024-12-03 18:14:50 +0000179 "": etc.PrebuiltRootFactory,
180 "avb": etc.PrebuiltAvbFactory,
181 "bin": etc.PrebuiltBinaryFactory,
182 "bt_firmware": etc.PrebuiltBtFirmwareFactory,
183 "cacerts": etc.PrebuiltEtcCaCertsFactory,
184 "dsp": etc.PrebuiltDSPFactory,
185 "etc": etc.PrebuiltEtcFactory,
186 "etc/dsp": etc.PrebuiltDSPFactory,
187 "etc/firmware": etc.PrebuiltFirmwareFactory,
188 "firmware": etc.PrebuiltFirmwareFactory,
189 "first_stage_ramdisk": etc.PrebuiltFirstStageRamdiskFactory,
190 "fonts": etc.PrebuiltFontFactory,
191 "framework": etc.PrebuiltFrameworkFactory,
192 "lib": etc.PrebuiltRenderScriptBitcodeFactory,
193 "lib64": etc.PrebuiltRenderScriptBitcodeFactory,
194 "lib/rfsa": etc.PrebuiltRFSAFactory,
195 "media": etc.PrebuiltMediaFactory,
196 "odm": etc.PrebuiltOdmFactory,
197 "optee": etc.PrebuiltOpteeFactory,
198 "overlay": etc.PrebuiltOverlayFactory,
199 "priv-app": etc.PrebuiltPrivAppFactory,
200 "sbin": etc.PrebuiltSbinFactory,
201 "system": etc.PrebuiltSystemFactory,
202 "res": etc.PrebuiltResFactory,
203 "rfs": etc.PrebuiltRfsFactory,
204 "tts": etc.PrebuiltVoicepackFactory,
205 "tvconfig": etc.PrebuiltTvConfigFactory,
206 "tvservice": etc.PrebuiltTvServiceFactory,
207 "usr/share": etc.PrebuiltUserShareFactory,
208 "usr/hyphen-data": etc.PrebuiltUserHyphenDataFactory,
209 "usr/keylayout": etc.PrebuiltUserKeyLayoutFactory,
210 "usr/keychars": etc.PrebuiltUserKeyCharsFactory,
211 "usr/srec": etc.PrebuiltUserSrecFactory,
212 "usr/idc": etc.PrebuiltUserIdcFactory,
213 "vendor": etc.PrebuiltVendorFactory,
214 "vendor_dlkm": etc.PrebuiltVendorDlkmFactory,
215 "wallpaper": etc.PrebuiltWallpaperFactory,
216 "wlc_upt": etc.PrebuiltWlcUptFactory,
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000217 }
218)
219
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000220func generatedPrebuiltEtcModuleName(partition, srcDir, destDir string, count int) string {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000221 // generated module name follows the pattern:
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000222 // <install partition>-<src file path>-<relative install path from partition root>-<number>
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000223 // Note that all path separators are replaced with "_" in the name
224 moduleName := partition
225 if !android.InList(srcDir, []string{"", "."}) {
226 moduleName += fmt.Sprintf("-%s", strings.ReplaceAll(srcDir, string(filepath.Separator), "_"))
227 }
228 if !android.InList(destDir, []string{"", "."}) {
229 moduleName += fmt.Sprintf("-%s", strings.ReplaceAll(destDir, string(filepath.Separator), "_"))
230 }
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000231 moduleName += fmt.Sprintf("-%d", count)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000232
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000233 return moduleName
234}
235
236func groupDestFilesBySrc(destFiles []srcBaseFileInstallBaseFileTuple) (ret map[string][]srcBaseFileInstallBaseFileTuple, maxLen int) {
237 ret = map[string][]srcBaseFileInstallBaseFileTuple{}
238 maxLen = 0
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000239 for _, tuple := range destFiles {
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000240 if _, ok := ret[tuple.srcBaseFile]; !ok {
241 ret[tuple.srcBaseFile] = []srcBaseFileInstallBaseFileTuple{}
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000242 }
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000243 ret[tuple.srcBaseFile] = append(ret[tuple.srcBaseFile], tuple)
244 maxLen = max(maxLen, len(ret[tuple.srcBaseFile]))
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000245 }
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000246 return ret, maxLen
247}
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000248
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000249func prebuiltEtcModuleProps(moduleName, partition string) prebuiltModuleProperties {
250 moduleProps := prebuiltModuleProperties{}
251 moduleProps.Name = proptools.StringPtr(moduleName)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000252
253 // Set partition specific properties
254 switch partition {
255 case "system_ext":
256 moduleProps.System_ext_specific = proptools.BoolPtr(true)
257 case "product":
258 moduleProps.Product_specific = proptools.BoolPtr(true)
259 case "vendor":
260 moduleProps.Soc_specific = proptools.BoolPtr(true)
261 }
262
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000263 moduleProps.No_full_install = proptools.BoolPtr(true)
264 moduleProps.NamespaceExportedToMake = true
265 moduleProps.Visibility = []string{"//visibility:public"}
266
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000267 return moduleProps
268}
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000269
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000270func createPrebuiltEtcModulesInDirectory(ctx android.LoadHookContext, partition, srcDir, destDir string, destFiles []srcBaseFileInstallBaseFileTuple) (moduleNames []string) {
271 groupedDestFiles, maxLen := groupDestFilesBySrc(destFiles)
272
273 // Find out the most appropriate module type to generate
274 var etcInstallPathKey string
275 for _, etcInstallPath := range android.SortedKeys(etcInstallPathToFactoryList) {
276 // Do not break when found but iterate until the end to find a module with more
277 // specific install path
278 if strings.HasPrefix(destDir, etcInstallPath) {
279 etcInstallPathKey = etcInstallPath
280 }
281 }
282 relDestDirFromInstallDirBase, _ := filepath.Rel(etcInstallPathKey, destDir)
283
284 for fileIndex := range maxLen {
285 srcTuple := []srcBaseFileInstallBaseFileTuple{}
Jihoon Kang47dadd92024-11-13 01:00:57 +0000286 for _, srcFile := range android.SortedKeys(groupedDestFiles) {
287 groupedDestFile := groupedDestFiles[srcFile]
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000288 if len(groupedDestFile) > fileIndex {
289 srcTuple = append(srcTuple, groupedDestFile[fileIndex])
290 }
291 }
292
293 moduleName := generatedPrebuiltEtcModuleName(partition, srcDir, destDir, fileIndex)
294 moduleProps := prebuiltEtcModuleProps(moduleName, partition)
295 modulePropsPtr := &moduleProps
296 propsList := []interface{}{modulePropsPtr}
297
298 allCopyFileNamesUnchanged := true
299 var srcBaseFiles, installBaseFiles []string
300 for _, tuple := range srcTuple {
301 if tuple.srcBaseFile != tuple.installBaseFile {
302 allCopyFileNamesUnchanged = false
303 }
304 srcBaseFiles = append(srcBaseFiles, tuple.srcBaseFile)
305 installBaseFiles = append(installBaseFiles, tuple.installBaseFile)
306 }
307
308 // Set appropriate srcs, dsts, and releative_install_path based on
309 // the source and install file names
310 if allCopyFileNamesUnchanged {
311 modulePropsPtr.Srcs = srcBaseFiles
312
313 // Specify relative_install_path if it is not installed in the root directory of the
314 // partition
315 if !android.InList(relDestDirFromInstallDirBase, []string{"", "."}) {
316 propsList = append(propsList, &prebuiltSubdirProperties{
317 Relative_install_path: proptools.StringPtr(relDestDirFromInstallDirBase),
318 })
319 }
320 } else {
321 modulePropsPtr.Srcs = srcBaseFiles
322 dsts := []string{}
323 for _, installBaseFile := range installBaseFiles {
324 dsts = append(dsts, filepath.Join(relDestDirFromInstallDirBase, installBaseFile))
325 }
326 modulePropsPtr.Dsts = dsts
327 }
328
329 ctx.CreateModuleInDirectory(etcInstallPathToFactoryList[etcInstallPathKey], srcDir, propsList...)
330 moduleNames = append(moduleNames, moduleName)
331 }
332
333 return moduleNames
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000334}
335
336func createPrebuiltEtcModulesForPartition(ctx android.LoadHookContext, partition, srcDir string, destDirFilesMap map[string][]srcBaseFileInstallBaseFileTuple) (ret []string) {
337 for _, destDir := range android.SortedKeys(destDirFilesMap) {
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000338 ret = append(ret, createPrebuiltEtcModulesInDirectory(ctx, partition, srcDir, destDir, destDirFilesMap[destDir])...)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000339 }
340 return ret
341}
342
343// Creates prebuilt_* modules based on the install paths and returns the list of generated
344// module names
345func createPrebuiltEtcModules(ctx android.LoadHookContext) (ret []string) {
346 groupedSources := processProductCopyFiles(ctx)
347 for _, srcDir := range android.SortedKeys(groupedSources) {
348 groupedSource := groupedSources[srcDir]
349 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "system", srcDir, groupedSource.system)...)
350 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "system_ext", srcDir, groupedSource.system_ext)...)
351 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "product", srcDir, groupedSource.product)...)
352 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "vendor", srcDir, groupedSource.vendor)...)
353 }
354
355 return ret
356}
Jihoon Kang04f12c92024-11-12 23:03:08 +0000357
358func createAvbpubkeyModule(ctx android.LoadHookContext) bool {
359 avbKeyPath := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.BoardAvbKeyPath
360 if avbKeyPath == "" {
361 return false
362 }
363 ctx.CreateModuleInDirectory(
364 etc.AvbpubkeyModuleFactory,
365 ".",
366 &struct {
367 Name *string
368 Product_specific *bool
369 Private_key *string
370 No_full_install *bool
371 Visibility []string
372 }{
373 Name: proptools.StringPtr("system_other_avbpubkey"),
374 Product_specific: proptools.BoolPtr(true),
375 Private_key: proptools.StringPtr(avbKeyPath),
376 No_full_install: proptools.BoolPtr(true),
377 Visibility: []string{"//visibility:public"},
378 },
379 )
380 return true
381}