blob: 8e7bb5727114c6659f774c5d2aa9c67d32bfe1e9 [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,
191 "overlay": etc.PrebuiltOverlayFactory,
192 "priv-app": etc.PrebuiltPrivAppFactory,
193 "res": etc.PrebuiltResFactory,
194 "rfs": etc.PrebuiltRfsFactory,
195 "tts": etc.PrebuiltVoicepackFactory,
196 "usr/share": etc.PrebuiltUserShareFactory,
197 "usr/hyphen-data": etc.PrebuiltUserHyphenDataFactory,
198 "usr/keylayout": etc.PrebuiltUserKeyLayoutFactory,
199 "usr/keychars": etc.PrebuiltUserKeyCharsFactory,
200 "usr/srec": etc.PrebuiltUserSrecFactory,
201 "usr/idc": etc.PrebuiltUserIdcFactory,
202 "vendor_dlkm": etc.PrebuiltVendorDlkmFactory,
203 "wallpaper": etc.PrebuiltWallpaperFactory,
204 "wlc_upt": etc.PrebuiltWlcUptFactory,
205 }
206)
207
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000208func generatedPrebuiltEtcModuleName(partition, srcDir, destDir string, count int) string {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000209 // generated module name follows the pattern:
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000210 // <install partition>-<src file path>-<relative install path from partition root>-<number>
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000211 // Note that all path separators are replaced with "_" in the name
212 moduleName := partition
213 if !android.InList(srcDir, []string{"", "."}) {
214 moduleName += fmt.Sprintf("-%s", strings.ReplaceAll(srcDir, string(filepath.Separator), "_"))
215 }
216 if !android.InList(destDir, []string{"", "."}) {
217 moduleName += fmt.Sprintf("-%s", strings.ReplaceAll(destDir, string(filepath.Separator), "_"))
218 }
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000219 moduleName += fmt.Sprintf("-%d", count)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000220
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000221 return moduleName
222}
223
224func groupDestFilesBySrc(destFiles []srcBaseFileInstallBaseFileTuple) (ret map[string][]srcBaseFileInstallBaseFileTuple, maxLen int) {
225 ret = map[string][]srcBaseFileInstallBaseFileTuple{}
226 maxLen = 0
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000227 for _, tuple := range destFiles {
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000228 if _, ok := ret[tuple.srcBaseFile]; !ok {
229 ret[tuple.srcBaseFile] = []srcBaseFileInstallBaseFileTuple{}
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000230 }
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000231 ret[tuple.srcBaseFile] = append(ret[tuple.srcBaseFile], tuple)
232 maxLen = max(maxLen, len(ret[tuple.srcBaseFile]))
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000233 }
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000234 return ret, maxLen
235}
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000236
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000237func prebuiltEtcModuleProps(moduleName, partition string) prebuiltModuleProperties {
238 moduleProps := prebuiltModuleProperties{}
239 moduleProps.Name = proptools.StringPtr(moduleName)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000240
241 // Set partition specific properties
242 switch partition {
243 case "system_ext":
244 moduleProps.System_ext_specific = proptools.BoolPtr(true)
245 case "product":
246 moduleProps.Product_specific = proptools.BoolPtr(true)
247 case "vendor":
248 moduleProps.Soc_specific = proptools.BoolPtr(true)
249 }
250
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000251 moduleProps.No_full_install = proptools.BoolPtr(true)
252 moduleProps.NamespaceExportedToMake = true
253 moduleProps.Visibility = []string{"//visibility:public"}
254
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000255 return moduleProps
256}
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000257
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000258func createPrebuiltEtcModulesInDirectory(ctx android.LoadHookContext, partition, srcDir, destDir string, destFiles []srcBaseFileInstallBaseFileTuple) (moduleNames []string) {
259 groupedDestFiles, maxLen := groupDestFilesBySrc(destFiles)
260
261 // Find out the most appropriate module type to generate
262 var etcInstallPathKey string
263 for _, etcInstallPath := range android.SortedKeys(etcInstallPathToFactoryList) {
264 // Do not break when found but iterate until the end to find a module with more
265 // specific install path
266 if strings.HasPrefix(destDir, etcInstallPath) {
267 etcInstallPathKey = etcInstallPath
268 }
269 }
270 relDestDirFromInstallDirBase, _ := filepath.Rel(etcInstallPathKey, destDir)
271
272 for fileIndex := range maxLen {
273 srcTuple := []srcBaseFileInstallBaseFileTuple{}
274 for _, groupedDestFile := range groupedDestFiles {
275 if len(groupedDestFile) > fileIndex {
276 srcTuple = append(srcTuple, groupedDestFile[fileIndex])
277 }
278 }
279
280 moduleName := generatedPrebuiltEtcModuleName(partition, srcDir, destDir, fileIndex)
281 moduleProps := prebuiltEtcModuleProps(moduleName, partition)
282 modulePropsPtr := &moduleProps
283 propsList := []interface{}{modulePropsPtr}
284
285 allCopyFileNamesUnchanged := true
286 var srcBaseFiles, installBaseFiles []string
287 for _, tuple := range srcTuple {
288 if tuple.srcBaseFile != tuple.installBaseFile {
289 allCopyFileNamesUnchanged = false
290 }
291 srcBaseFiles = append(srcBaseFiles, tuple.srcBaseFile)
292 installBaseFiles = append(installBaseFiles, tuple.installBaseFile)
293 }
294
295 // Set appropriate srcs, dsts, and releative_install_path based on
296 // the source and install file names
297 if allCopyFileNamesUnchanged {
298 modulePropsPtr.Srcs = srcBaseFiles
299
300 // Specify relative_install_path if it is not installed in the root directory of the
301 // partition
302 if !android.InList(relDestDirFromInstallDirBase, []string{"", "."}) {
303 propsList = append(propsList, &prebuiltSubdirProperties{
304 Relative_install_path: proptools.StringPtr(relDestDirFromInstallDirBase),
305 })
306 }
307 } else {
308 modulePropsPtr.Srcs = srcBaseFiles
309 dsts := []string{}
310 for _, installBaseFile := range installBaseFiles {
311 dsts = append(dsts, filepath.Join(relDestDirFromInstallDirBase, installBaseFile))
312 }
313 modulePropsPtr.Dsts = dsts
314 }
315
316 ctx.CreateModuleInDirectory(etcInstallPathToFactoryList[etcInstallPathKey], srcDir, propsList...)
317 moduleNames = append(moduleNames, moduleName)
318 }
319
320 return moduleNames
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000321}
322
323func createPrebuiltEtcModulesForPartition(ctx android.LoadHookContext, partition, srcDir string, destDirFilesMap map[string][]srcBaseFileInstallBaseFileTuple) (ret []string) {
324 for _, destDir := range android.SortedKeys(destDirFilesMap) {
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000325 ret = append(ret, createPrebuiltEtcModulesInDirectory(ctx, partition, srcDir, destDir, destDirFilesMap[destDir])...)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000326 }
327 return ret
328}
329
330// Creates prebuilt_* modules based on the install paths and returns the list of generated
331// module names
332func createPrebuiltEtcModules(ctx android.LoadHookContext) (ret []string) {
333 groupedSources := processProductCopyFiles(ctx)
334 for _, srcDir := range android.SortedKeys(groupedSources) {
335 groupedSource := groupedSources[srcDir]
336 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "system", srcDir, groupedSource.system)...)
337 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "system_ext", srcDir, groupedSource.system_ext)...)
338 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "product", srcDir, groupedSource.product)...)
339 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "vendor", srcDir, groupedSource.vendor)...)
340 }
341
342 return ret
343}