blob: cbcd4a1a347991ef1a817fdffe3ce368cfd3b4f7 [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 Kangadd2bb22024-11-05 22:29:34 +0000103 if _, ok := seen[dest]; !ok {
104 if optionalPath := android.ExistentPathForSource(ctx, src); optionalPath.Valid() {
105 seen[dest] = true
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000106 filtered[src] = append(filtered[src], dest)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000107 }
108 }
109 }
110
111 return filtered
112}
113
114type partitionToInstallPath struct {
115 name string
116 installPath string
117}
118
119func 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 Kang3a8759c2024-11-08 19:35:09 +0000134 destFiles := productCopyFileMap[src]
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000135 srcFileDir := filepath.Dir(src)
136 if _, ok := groupedSources[srcFileDir]; !ok {
137 groupedSources[srcFileDir] = newPrebuiltSrcGroupByInstallPartition()
138 }
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000139 for _, dest := range destFiles {
140 appendIfCorrectInstallPartition(partitionToInstallPathList, dest, filepath.Base(src), groupedSources[srcFileDir])
141 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000142 }
143
144 return groupedSources
145}
146
147type 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]
166type 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
172var (
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 Kangdca2f2b2024-11-06 18:43:19 +0000191 "optee": etc.PrebuiltOpteeFactory,
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000192 "overlay": etc.PrebuiltOverlayFactory,
193 "priv-app": etc.PrebuiltPrivAppFactory,
194 "res": etc.PrebuiltResFactory,
195 "rfs": etc.PrebuiltRfsFactory,
196 "tts": etc.PrebuiltVoicepackFactory,
Jihoon Kangdca2f2b2024-11-06 18:43:19 +0000197 "tvservice": etc.PrebuiltTvServiceFactory,
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000198 "usr/share": etc.PrebuiltUserShareFactory,
199 "usr/hyphen-data": etc.PrebuiltUserHyphenDataFactory,
200 "usr/keylayout": etc.PrebuiltUserKeyLayoutFactory,
201 "usr/keychars": etc.PrebuiltUserKeyCharsFactory,
202 "usr/srec": etc.PrebuiltUserSrecFactory,
203 "usr/idc": etc.PrebuiltUserIdcFactory,
204 "vendor_dlkm": etc.PrebuiltVendorDlkmFactory,
205 "wallpaper": etc.PrebuiltWallpaperFactory,
206 "wlc_upt": etc.PrebuiltWlcUptFactory,
207 }
208)
209
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000210func generatedPrebuiltEtcModuleName(partition, srcDir, destDir string, count int) string {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000211 // generated module name follows the pattern:
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000212 // <install partition>-<src file path>-<relative install path from partition root>-<number>
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000213 // Note that all path separators are replaced with "_" in the name
214 moduleName := partition
215 if !android.InList(srcDir, []string{"", "."}) {
216 moduleName += fmt.Sprintf("-%s", strings.ReplaceAll(srcDir, string(filepath.Separator), "_"))
217 }
218 if !android.InList(destDir, []string{"", "."}) {
219 moduleName += fmt.Sprintf("-%s", strings.ReplaceAll(destDir, string(filepath.Separator), "_"))
220 }
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000221 moduleName += fmt.Sprintf("-%d", count)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000222
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000223 return moduleName
224}
225
226func groupDestFilesBySrc(destFiles []srcBaseFileInstallBaseFileTuple) (ret map[string][]srcBaseFileInstallBaseFileTuple, maxLen int) {
227 ret = map[string][]srcBaseFileInstallBaseFileTuple{}
228 maxLen = 0
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000229 for _, tuple := range destFiles {
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000230 if _, ok := ret[tuple.srcBaseFile]; !ok {
231 ret[tuple.srcBaseFile] = []srcBaseFileInstallBaseFileTuple{}
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000232 }
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000233 ret[tuple.srcBaseFile] = append(ret[tuple.srcBaseFile], tuple)
234 maxLen = max(maxLen, len(ret[tuple.srcBaseFile]))
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000235 }
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000236 return ret, maxLen
237}
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000238
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000239func prebuiltEtcModuleProps(moduleName, partition string) prebuiltModuleProperties {
240 moduleProps := prebuiltModuleProperties{}
241 moduleProps.Name = proptools.StringPtr(moduleName)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000242
243 // Set partition specific properties
244 switch partition {
245 case "system_ext":
246 moduleProps.System_ext_specific = proptools.BoolPtr(true)
247 case "product":
248 moduleProps.Product_specific = proptools.BoolPtr(true)
249 case "vendor":
250 moduleProps.Soc_specific = proptools.BoolPtr(true)
251 }
252
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000253 moduleProps.No_full_install = proptools.BoolPtr(true)
254 moduleProps.NamespaceExportedToMake = true
255 moduleProps.Visibility = []string{"//visibility:public"}
256
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000257 return moduleProps
258}
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000259
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000260func createPrebuiltEtcModulesInDirectory(ctx android.LoadHookContext, partition, srcDir, destDir string, destFiles []srcBaseFileInstallBaseFileTuple) (moduleNames []string) {
261 groupedDestFiles, maxLen := groupDestFilesBySrc(destFiles)
262
263 // Find out the most appropriate module type to generate
264 var etcInstallPathKey string
265 for _, etcInstallPath := range android.SortedKeys(etcInstallPathToFactoryList) {
266 // Do not break when found but iterate until the end to find a module with more
267 // specific install path
268 if strings.HasPrefix(destDir, etcInstallPath) {
269 etcInstallPathKey = etcInstallPath
270 }
271 }
272 relDestDirFromInstallDirBase, _ := filepath.Rel(etcInstallPathKey, destDir)
273
274 for fileIndex := range maxLen {
275 srcTuple := []srcBaseFileInstallBaseFileTuple{}
276 for _, groupedDestFile := range groupedDestFiles {
277 if len(groupedDestFile) > fileIndex {
278 srcTuple = append(srcTuple, groupedDestFile[fileIndex])
279 }
280 }
281
282 moduleName := generatedPrebuiltEtcModuleName(partition, srcDir, destDir, fileIndex)
283 moduleProps := prebuiltEtcModuleProps(moduleName, partition)
284 modulePropsPtr := &moduleProps
285 propsList := []interface{}{modulePropsPtr}
286
287 allCopyFileNamesUnchanged := true
288 var srcBaseFiles, installBaseFiles []string
289 for _, tuple := range srcTuple {
290 if tuple.srcBaseFile != tuple.installBaseFile {
291 allCopyFileNamesUnchanged = false
292 }
293 srcBaseFiles = append(srcBaseFiles, tuple.srcBaseFile)
294 installBaseFiles = append(installBaseFiles, tuple.installBaseFile)
295 }
296
297 // Set appropriate srcs, dsts, and releative_install_path based on
298 // the source and install file names
299 if allCopyFileNamesUnchanged {
300 modulePropsPtr.Srcs = srcBaseFiles
301
302 // Specify relative_install_path if it is not installed in the root directory of the
303 // partition
304 if !android.InList(relDestDirFromInstallDirBase, []string{"", "."}) {
305 propsList = append(propsList, &prebuiltSubdirProperties{
306 Relative_install_path: proptools.StringPtr(relDestDirFromInstallDirBase),
307 })
308 }
309 } else {
310 modulePropsPtr.Srcs = srcBaseFiles
311 dsts := []string{}
312 for _, installBaseFile := range installBaseFiles {
313 dsts = append(dsts, filepath.Join(relDestDirFromInstallDirBase, installBaseFile))
314 }
315 modulePropsPtr.Dsts = dsts
316 }
317
318 ctx.CreateModuleInDirectory(etcInstallPathToFactoryList[etcInstallPathKey], srcDir, propsList...)
319 moduleNames = append(moduleNames, moduleName)
320 }
321
322 return moduleNames
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000323}
324
325func createPrebuiltEtcModulesForPartition(ctx android.LoadHookContext, partition, srcDir string, destDirFilesMap map[string][]srcBaseFileInstallBaseFileTuple) (ret []string) {
326 for _, destDir := range android.SortedKeys(destDirFilesMap) {
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000327 ret = append(ret, createPrebuiltEtcModulesInDirectory(ctx, partition, srcDir, destDir, destDirFilesMap[destDir])...)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000328 }
329 return ret
330}
331
332// Creates prebuilt_* modules based on the install paths and returns the list of generated
333// module names
334func createPrebuiltEtcModules(ctx android.LoadHookContext) (ret []string) {
335 groupedSources := processProductCopyFiles(ctx)
336 for _, srcDir := range android.SortedKeys(groupedSources) {
337 groupedSource := groupedSources[srcDir]
338 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "system", srcDir, groupedSource.system)...)
339 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "system_ext", srcDir, groupedSource.system_ext)...)
340 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "product", srcDir, groupedSource.product)...)
341 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "vendor", srcDir, groupedSource.vendor)...)
342 }
343
344 return ret
345}