blob: f81589f6ded88f6bbc8edeaa9af915c433872c4e [file] [log] [blame]
Jihoon Kang98047cf2024-10-02 17:13:54 +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 (
Cole Faust92ccbe22024-10-03 14:38:37 -070018 "crypto/sha256"
Jihoon Kang98047cf2024-10-02 17:13:54 +000019 "fmt"
mrziwang8f86c882024-10-03 12:34:33 -070020 "slices"
Jihoon Kang98047cf2024-10-02 17:13:54 +000021 "strconv"
mrziwang8f86c882024-10-03 12:34:33 -070022 "strings"
23 "sync"
24
25 "android/soong/android"
26 "android/soong/filesystem"
Jihoon Kang98047cf2024-10-02 17:13:54 +000027
Cole Faust92ccbe22024-10-03 14:38:37 -070028 "github.com/google/blueprint"
mrziwang8f86c882024-10-03 12:34:33 -070029 "github.com/google/blueprint/parser"
Jihoon Kang98047cf2024-10-02 17:13:54 +000030 "github.com/google/blueprint/proptools"
31)
32
Cole Faust92ccbe22024-10-03 14:38:37 -070033var pctx = android.NewPackageContext("android/soong/fsgen")
34
Jihoon Kang98047cf2024-10-02 17:13:54 +000035func init() {
36 registerBuildComponents(android.InitRegistrationContext)
37}
38
39func registerBuildComponents(ctx android.RegistrationContext) {
40 ctx.RegisterModuleType("soong_filesystem_creator", filesystemCreatorFactory)
mrziwang8f86c882024-10-03 12:34:33 -070041 ctx.PreDepsMutators(RegisterCollectFileSystemDepsMutators)
42}
43
44func RegisterCollectFileSystemDepsMutators(ctx android.RegisterMutatorsContext) {
45 ctx.BottomUp("fs_collect_deps", collectDepsMutator).MutatesGlobalState()
Jihoon Kang0d545b82024-10-11 00:21:57 +000046 ctx.BottomUp("fs_set_deps", setDepsMutator)
mrziwang8f86c882024-10-03 12:34:33 -070047}
48
Jihoon Kang0d545b82024-10-11 00:21:57 +000049var fsGenStateOnceKey = android.NewOnceKey("FsGenState")
50
51// Map of partition module name to its partition that may be generated by Soong.
52// Note that it is not guaranteed that all modules returned by this function are successfully
53// created.
54func getAllSoongGeneratedPartitionNames(config android.Config, partitions []string) map[string]string {
55 ret := map[string]string{}
56 for _, partition := range partitions {
57 ret[generatedModuleNameForPartition(config, partition)] = partition
58 }
59 return ret
60}
61
62type depCandidateProps struct {
63 Namespace string
64 Multilib string
65 Arch []android.ArchType
66}
67
68// Map of module name to depCandidateProps
69type multilibDeps *map[string]*depCandidateProps
70
71// Information necessary to generate the filesystem modules, including details about their
72// dependencies
73type FsGenState struct {
74 // List of modules in `PRODUCT_PACKAGES` and `PRODUCT_PACKAGES_DEBUG`
75 depCandidates []string
76 // Map of names of partition to the information of modules to be added as deps
77 fsDeps map[string]multilibDeps
78 // List of name of partitions to be generated by the filesystem_creator module
79 soongGeneratedPartitions []string
80 // Mutex to protect the fsDeps
81 fsDepsMutex sync.Mutex
82}
83
84func newMultilibDeps() multilibDeps {
85 return &map[string]*depCandidateProps{}
86}
87
88func defaultDepCandidateProps(config android.Config) *depCandidateProps {
89 return &depCandidateProps{
90 Namespace: ".",
91 Arch: []android.ArchType{config.BuildArch},
92 }
93}
94
95func createFsGenState(ctx android.LoadHookContext) *FsGenState {
96 return ctx.Config().Once(fsGenStateOnceKey, func() interface{} {
97 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
98 candidates := android.FirstUniqueStrings(android.Concat(partitionVars.ProductPackages, partitionVars.ProductPackagesDebug))
99
100 generatedPartitions := []string{"system"}
101 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
102 generatedPartitions = append(generatedPartitions, "system_ext")
103 }
104
105 return &FsGenState{
106 depCandidates: candidates,
107 fsDeps: map[string]multilibDeps{
108 // These additional deps are added according to the cuttlefish system image bp.
109 "system": &map[string]*depCandidateProps{
110 "com.android.apex.cts.shim.v1_prebuilt": defaultDepCandidateProps(ctx.Config()),
111 "dex_bootjars": defaultDepCandidateProps(ctx.Config()),
112 "framework_compatibility_matrix.device.xml": defaultDepCandidateProps(ctx.Config()),
113 "idc_data": defaultDepCandidateProps(ctx.Config()),
114 "init.environ.rc-soong": defaultDepCandidateProps(ctx.Config()),
115 "keychars_data": defaultDepCandidateProps(ctx.Config()),
116 "keylayout_data": defaultDepCandidateProps(ctx.Config()),
117 "libclang_rt.asan": defaultDepCandidateProps(ctx.Config()),
118 "libcompiler_rt": defaultDepCandidateProps(ctx.Config()),
119 "libdmabufheap": defaultDepCandidateProps(ctx.Config()),
120 "libgsi": defaultDepCandidateProps(ctx.Config()),
121 "llndk.libraries.txt": defaultDepCandidateProps(ctx.Config()),
122 "logpersist.start": defaultDepCandidateProps(ctx.Config()),
123 "preloaded-classes": defaultDepCandidateProps(ctx.Config()),
124 "public.libraries.android.txt": defaultDepCandidateProps(ctx.Config()),
125 "update_engine_sideload": defaultDepCandidateProps(ctx.Config()),
126 },
Spandan Dasd9875bc2024-10-17 21:36:17 +0000127 "vendor": newMultilibDeps(),
128 "odm": newMultilibDeps(),
129 "product": newMultilibDeps(),
130 "system_ext": &map[string]*depCandidateProps{
131 // VNDK apexes are automatically included.
132 // This hardcoded list will need to be updated if `PRODUCT_EXTRA_VNDK_VERSIONS` is updated.
133 // https://cs.android.com/android/_/android/platform/build/+/adba533072b00c53ac0f198c550a3cbd7a00e4cd:core/main.mk;l=984;bpv=1;bpt=0;drc=174db7b179592cf07cbfd2adb0119486fda911e7
134 "com.android.vndk.v30": defaultDepCandidateProps(ctx.Config()),
135 "com.android.vndk.v31": defaultDepCandidateProps(ctx.Config()),
136 "com.android.vndk.v32": defaultDepCandidateProps(ctx.Config()),
137 "com.android.vndk.v33": defaultDepCandidateProps(ctx.Config()),
138 "com.android.vndk.v34": defaultDepCandidateProps(ctx.Config()),
139 },
Jihoon Kang0d545b82024-10-11 00:21:57 +0000140 },
141 soongGeneratedPartitions: generatedPartitions,
142 fsDepsMutex: sync.Mutex{},
143 }
144 }).(*FsGenState)
145}
146
147func checkDepModuleInMultipleNamespaces(mctx android.BottomUpMutatorContext, foundDeps map[string]*depCandidateProps, module string, partitionName string) {
148 otherNamespace := mctx.Namespace().Path
149 if val, found := foundDeps[module]; found && otherNamespace != "." && !android.InList(val.Namespace, []string{".", otherNamespace}) {
150 mctx.ModuleErrorf("found in multiple namespaces(%s and %s) when including in %s partition", val.Namespace, otherNamespace, partitionName)
151 }
152}
153
154func appendDepIfAppropriate(mctx android.BottomUpMutatorContext, deps *map[string]*depCandidateProps, installPartition string) {
155 checkDepModuleInMultipleNamespaces(mctx, *deps, mctx.Module().Name(), installPartition)
156 if _, ok := (*deps)[mctx.Module().Name()]; ok {
157 // Prefer the namespace-specific module over the platform module
158 if mctx.Namespace().Path != "." {
159 (*deps)[mctx.Module().Name()].Namespace = mctx.Namespace().Path
160 }
161 (*deps)[mctx.Module().Name()].Arch = append((*deps)[mctx.Module().Name()].Arch, mctx.Module().Target().Arch.ArchType)
162 } else {
163 multilib, _ := mctx.Module().DecodeMultilib(mctx)
164 (*deps)[mctx.Module().Name()] = &depCandidateProps{
165 Namespace: mctx.Namespace().Path,
166 Multilib: multilib,
167 Arch: []android.ArchType{mctx.Module().Target().Arch.ArchType},
168 }
169 }
170}
mrziwang8f86c882024-10-03 12:34:33 -0700171
172func collectDepsMutator(mctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000173 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
mrziwang8f86c882024-10-03 12:34:33 -0700174
175 m := mctx.Module()
Jihoon Kang0d545b82024-10-11 00:21:57 +0000176 if slices.Contains(fsGenState.depCandidates, m.Name()) {
177 installPartition := m.PartitionTag(mctx.DeviceConfig())
178 fsGenState.fsDepsMutex.Lock()
179 // Only add the module as dependency when:
180 // - its enabled
181 // - its namespace is included in PRODUCT_SOONG_NAMESPACES
182 if m.Enabled(mctx) && m.ExportedToMake() {
183 appendDepIfAppropriate(mctx, fsGenState.fsDeps[installPartition], installPartition)
184 }
185 fsGenState.fsDepsMutex.Unlock()
186 }
187}
188
189type depsStruct struct {
190 Deps []string
191}
192
193type multilibDepsStruct struct {
194 Common depsStruct
195 Lib32 depsStruct
196 Lib64 depsStruct
197 Both depsStruct
198 Prefer32 depsStruct
199}
200
201type packagingPropsStruct struct {
202 Deps []string
203 Multilib multilibDepsStruct
204}
205
206func fullyQualifiedModuleName(moduleName, namespace string) string {
207 if namespace == "." {
208 return moduleName
209 }
210 return fmt.Sprintf("//%s:%s", namespace, moduleName)
211}
212
213// Returns the sorted unique list of module names with namespace, if the module specifies one.
214func fullyQualifiedModuleNames(modules multilibDeps) (ret []string) {
215 for moduleName, moduleProp := range *modules {
216 ret = append(ret, fullyQualifiedModuleName(moduleName, moduleProp.Namespace))
217 }
218 return android.SortedUniqueStrings(ret)
219}
220
221func getBitness(archTypes []android.ArchType) (ret []string) {
222 for _, archType := range archTypes {
223 if archType.Multilib == "" {
224 ret = append(ret, android.COMMON_VARIANT)
225 } else {
226 ret = append(ret, archType.Bitness())
227 }
228 }
229 return ret
230}
231
232func setDepsMutator(mctx android.BottomUpMutatorContext) {
233 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
234 fsDeps := fsGenState.fsDeps
235 soongGeneratedPartitionMap := getAllSoongGeneratedPartitionNames(mctx.Config(), fsGenState.soongGeneratedPartitions)
236 m := mctx.Module()
237 if partition, ok := soongGeneratedPartitionMap[m.Name()]; ok {
238 depsStruct := packagingPropsStruct{}
239 for depName, depProps := range *fsDeps[partition] {
240 bitness := getBitness(depProps.Arch)
241 fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace)
242 if android.InList("32", bitness) && android.InList("64", bitness) {
243 // If both 32 and 64 bit variants are enabled for this module
244 switch depProps.Multilib {
245 case string(android.MultilibBoth):
246 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
247 case string(android.MultilibCommon), string(android.MultilibFirst):
248 depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName)
249 case "32":
250 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
251 case "64", "darwin_universal":
252 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
253 case "prefer32", "first_prefer32":
254 depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName)
255 default:
256 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
257 }
258 } else if android.InList("64", bitness) {
259 // If only 64 bit variant is enabled
260 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
261 } else if android.InList("32", bitness) {
262 // If only 32 bit variant is enabled
263 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
264 } else {
265 // If only common variant is enabled
266 depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName)
267 }
268 }
269 if err := proptools.AppendMatchingProperties(m.GetProperties(), &depsStruct, nil); err != nil {
270 mctx.ModuleErrorf(err.Error())
mrziwang8f86c882024-10-03 12:34:33 -0700271 }
272 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000273}
274
Cole Faust92ccbe22024-10-03 14:38:37 -0700275type filesystemCreatorProps struct {
276 Generated_partition_types []string `blueprint:"mutated"`
277 Unsupported_partition_types []string `blueprint:"mutated"`
278}
279
Jihoon Kang98047cf2024-10-02 17:13:54 +0000280type filesystemCreator struct {
281 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -0700282
283 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +0000284}
285
286func filesystemCreatorFactory() android.Module {
287 module := &filesystemCreator{}
288
Cole Faust69788792024-10-10 11:00:36 -0700289 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -0700290 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000291 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000292 createFsGenState(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000293 module.createInternalModules(ctx)
294 })
295
296 return module
297}
298
299func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000300 soongGeneratedPartitions := &ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions
301 for _, partitionType := range *soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700302 if f.createPartition(ctx, partitionType) {
303 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
304 } else {
305 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
Jihoon Kang0d545b82024-10-11 00:21:57 +0000306 _, *soongGeneratedPartitions = android.RemoveFromList(partitionType, *soongGeneratedPartitions)
Cole Faust92ccbe22024-10-03 14:38:37 -0700307 }
308 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000309 f.createDeviceModule(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000310}
311
Jihoon Kang0d545b82024-10-11 00:21:57 +0000312func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700313 prefix := "soong"
314 if cfg.HasDeviceProduct() {
315 prefix = cfg.DeviceProduct()
316 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000317 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
318}
319
Jihoon Kang0d545b82024-10-11 00:21:57 +0000320func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
321 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000322}
323
324func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
325 baseProps := &struct {
326 Name *string
327 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000328 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000329 }
330
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000331 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000332 partitionProps := &filesystem.PartitionNameProperties{}
333 if android.InList("system", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000334 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000335 }
Spandan Das7a46f6c2024-10-14 18:41:18 +0000336 if android.InList("system_ext", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000337 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000338 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000339
340 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700341}
342
Spandan Dascbe641a2024-10-14 21:07:34 +0000343var (
344 // https://source.corp.google.com/h/googleplex-android/platform/build/+/639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
345 partitionsWithAconfig = []string{"system", "product", "vendor"}
346)
347
Cole Faust92ccbe22024-10-03 14:38:37 -0700348// Creates a soong module to build the given partition. Returns false if we can't support building
349// it.
350func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000351 baseProps := &struct {
352 Name *string
353 Compile_multilib *string
354 }{
355 Name: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)),
mrziwanga077b942024-10-16 16:00:06 -0700356 Compile_multilib: proptools.StringPtr("both"),
357 }
mrziwanga077b942024-10-16 16:00:06 -0700358
Cole Faust92ccbe22024-10-03 14:38:37 -0700359 fsProps := &filesystem.FilesystemProperties{}
360
361 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
362 // and sometimes don't build.
363 fsProps.Unchecked_module = proptools.BoolPtr(true)
364
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000365 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
366 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
367
Jihoon Kang98047cf2024-10-02 17:13:54 +0000368 // BOARD_AVB_ENABLE
369 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
370 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700371 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000372 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700373 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000374 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700375 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000376 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
377 }
378
Cole Faust92ccbe22024-10-03 14:38:37 -0700379 fsProps.Partition_name = proptools.StringPtr(partitionType)
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000380 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
381 fsType := specificPartitionVars.BoardFileSystemType
382 if fsType == "" {
383 fsType = "ext4" //default
384 }
385 fsProps.Type = proptools.StringPtr(fsType)
386 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
387 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
388 return false
389 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000390
Cole Faust92ccbe22024-10-03 14:38:37 -0700391 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000392
Spandan Dascbe641a2024-10-14 21:07:34 +0000393 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(android.InList(partitionType, partitionsWithAconfig))
Jihoon Kang98047cf2024-10-02 17:13:54 +0000394
Jihoon Kang0d545b82024-10-11 00:21:57 +0000395 fsProps.Is_auto_generated = proptools.BoolPtr(true)
396
Jihoon Kang98047cf2024-10-02 17:13:54 +0000397 // Identical to that of the generic_system_image
398 fsProps.Fsverity.Inputs = []string{
399 "etc/boot-image.prof",
400 "etc/dirty-image-objects",
401 "etc/preloaded-classes",
402 "etc/classpaths/*.pb",
403 "framework/*",
404 "framework/*/*", // framework/{arch}
405 "framework/oat/*/*", // framework/oat/{arch}
406 }
407
408 // system_image properties that are not set:
409 // - filesystemProperties.Avb_hash_algorithm
410 // - filesystemProperties.File_contexts
411 // - filesystemProperties.Dirs
412 // - filesystemProperties.Symlinks
413 // - filesystemProperties.Fake_timestamp
414 // - filesystemProperties.Uuid
415 // - filesystemProperties.Mount_point
416 // - filesystemProperties.Include_make_built_files
417 // - filesystemProperties.Build_logtags
418 // - filesystemProperties.Fsverity.Libs
419 // - systemImageProperties.Linker_config_src
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000420 var module android.Module
421 if partitionType == "system" {
422 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
423 } else {
424 // Explicitly set the partition.
425 fsProps.Partition_type = proptools.StringPtr(partitionType)
426 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
427 }
428 module.HideFromMake()
429 return true
Cole Faust92ccbe22024-10-03 14:38:37 -0700430}
431
432func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000433 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700434 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
435 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
436 if !ok {
437 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
438 }
439 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
440 // For now, don't allowlist anything. The test will fail, but that's fine in the current
441 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000442 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700443 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000444 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700445
446 builder := android.NewRuleBuilder(pctx, ctx)
447 builder.Command().BuiltTool("file_list_diff").
448 Input(makeFileList).
449 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000450 Text(partitionModuleName).
451 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700452 builder.Command().Text("touch").Output(diffTestResultFile)
453 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
454 return diffTestResultFile
455}
456
457func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
458 hasher := sha256.New()
459 hasher.Write([]byte(message))
460 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
461 file := android.PathForModuleOut(ctx, filename)
462 builder := android.NewRuleBuilder(pctx, ctx)
463 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
464 builder.Command().Text("exit 1 #").Output(file)
465 builder.Build("failing command "+filename, "failing command "+filename)
466 return file
467}
468
469type systemImageDepTagType struct {
470 blueprint.BaseDependencyTag
471}
472
473var generatedFilesystemDepTag systemImageDepTagType
474
475func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
476 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000477 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700478 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000479}
480
481func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700482 if ctx.ModuleDir() != "build/soong/fsgen" {
483 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
484 }
485 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000486
mrziwang8f86c882024-10-03 12:34:33 -0700487 content := generateBpContent(ctx, "system")
488 generatedBp := android.PathForOutput(ctx, "soong_generated_product_config.bp")
489 android.WriteFileRule(ctx, generatedBp, content)
490 ctx.Phony("product_config_to_bp", generatedBp)
491
Cole Faust92ccbe22024-10-03 14:38:37 -0700492 var diffTestFiles []android.Path
493 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000494 diffTestFile := f.createDiffTest(ctx, partitionType)
495 diffTestFiles = append(diffTestFiles, diffTestFile)
496 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700497 }
498 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000499 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
500 diffTestFiles = append(diffTestFiles, diffTestFile)
501 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700502 }
503 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000504}
mrziwang8f86c882024-10-03 12:34:33 -0700505
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000506// TODO: assemble baseProps and fsProps here
mrziwang8f86c882024-10-03 12:34:33 -0700507func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
508 // Currently only system partition is supported
509 if partitionType != "system" {
510 return ""
511 }
512
Jihoon Kang0d545b82024-10-11 00:21:57 +0000513 deps := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).fsDeps
mrziwang8f86c882024-10-03 12:34:33 -0700514 depProps := &android.PackagingProperties{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000515 Deps: android.NewSimpleConfigurable(fullyQualifiedModuleNames(deps[partitionType])),
mrziwang8f86c882024-10-03 12:34:33 -0700516 }
517
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000518 result, err := proptools.RepackProperties([]interface{}{depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700519 if err != nil {
520 ctx.ModuleErrorf(err.Error())
521 }
522
523 file := &parser.File{
524 Defs: []parser.Definition{
525 &parser.Module{
526 Type: "module",
527 Map: *result,
528 },
529 },
530 }
531 bytes, err := parser.Print(file)
532 if err != nil {
533 ctx.ModuleErrorf(err.Error())
534 }
535 return strings.TrimSpace(string(bytes))
536}