blob: 96dda04a8a3d109dd15a7b1c1081b328406bc1bf [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 },
127 "vendor": newMultilibDeps(),
128 "odm": newMultilibDeps(),
129 "product": newMultilibDeps(),
130 "system_ext": newMultilibDeps(),
131 },
132 soongGeneratedPartitions: generatedPartitions,
133 fsDepsMutex: sync.Mutex{},
134 }
135 }).(*FsGenState)
136}
137
138func checkDepModuleInMultipleNamespaces(mctx android.BottomUpMutatorContext, foundDeps map[string]*depCandidateProps, module string, partitionName string) {
139 otherNamespace := mctx.Namespace().Path
140 if val, found := foundDeps[module]; found && otherNamespace != "." && !android.InList(val.Namespace, []string{".", otherNamespace}) {
141 mctx.ModuleErrorf("found in multiple namespaces(%s and %s) when including in %s partition", val.Namespace, otherNamespace, partitionName)
142 }
143}
144
145func appendDepIfAppropriate(mctx android.BottomUpMutatorContext, deps *map[string]*depCandidateProps, installPartition string) {
146 checkDepModuleInMultipleNamespaces(mctx, *deps, mctx.Module().Name(), installPartition)
147 if _, ok := (*deps)[mctx.Module().Name()]; ok {
148 // Prefer the namespace-specific module over the platform module
149 if mctx.Namespace().Path != "." {
150 (*deps)[mctx.Module().Name()].Namespace = mctx.Namespace().Path
151 }
152 (*deps)[mctx.Module().Name()].Arch = append((*deps)[mctx.Module().Name()].Arch, mctx.Module().Target().Arch.ArchType)
153 } else {
154 multilib, _ := mctx.Module().DecodeMultilib(mctx)
155 (*deps)[mctx.Module().Name()] = &depCandidateProps{
156 Namespace: mctx.Namespace().Path,
157 Multilib: multilib,
158 Arch: []android.ArchType{mctx.Module().Target().Arch.ArchType},
159 }
160 }
161}
mrziwang8f86c882024-10-03 12:34:33 -0700162
163func collectDepsMutator(mctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000164 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
mrziwang8f86c882024-10-03 12:34:33 -0700165
166 m := mctx.Module()
Jihoon Kang0d545b82024-10-11 00:21:57 +0000167 if slices.Contains(fsGenState.depCandidates, m.Name()) {
168 installPartition := m.PartitionTag(mctx.DeviceConfig())
169 fsGenState.fsDepsMutex.Lock()
170 // Only add the module as dependency when:
171 // - its enabled
172 // - its namespace is included in PRODUCT_SOONG_NAMESPACES
173 if m.Enabled(mctx) && m.ExportedToMake() {
174 appendDepIfAppropriate(mctx, fsGenState.fsDeps[installPartition], installPartition)
175 }
176 fsGenState.fsDepsMutex.Unlock()
177 }
178}
179
180type depsStruct struct {
181 Deps []string
182}
183
184type multilibDepsStruct struct {
185 Common depsStruct
186 Lib32 depsStruct
187 Lib64 depsStruct
188 Both depsStruct
189 Prefer32 depsStruct
190}
191
192type packagingPropsStruct struct {
193 Deps []string
194 Multilib multilibDepsStruct
195}
196
197func fullyQualifiedModuleName(moduleName, namespace string) string {
198 if namespace == "." {
199 return moduleName
200 }
201 return fmt.Sprintf("//%s:%s", namespace, moduleName)
202}
203
204// Returns the sorted unique list of module names with namespace, if the module specifies one.
205func fullyQualifiedModuleNames(modules multilibDeps) (ret []string) {
206 for moduleName, moduleProp := range *modules {
207 ret = append(ret, fullyQualifiedModuleName(moduleName, moduleProp.Namespace))
208 }
209 return android.SortedUniqueStrings(ret)
210}
211
212func getBitness(archTypes []android.ArchType) (ret []string) {
213 for _, archType := range archTypes {
214 if archType.Multilib == "" {
215 ret = append(ret, android.COMMON_VARIANT)
216 } else {
217 ret = append(ret, archType.Bitness())
218 }
219 }
220 return ret
221}
222
223func setDepsMutator(mctx android.BottomUpMutatorContext) {
224 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
225 fsDeps := fsGenState.fsDeps
226 soongGeneratedPartitionMap := getAllSoongGeneratedPartitionNames(mctx.Config(), fsGenState.soongGeneratedPartitions)
227 m := mctx.Module()
228 if partition, ok := soongGeneratedPartitionMap[m.Name()]; ok {
229 depsStruct := packagingPropsStruct{}
230 for depName, depProps := range *fsDeps[partition] {
231 bitness := getBitness(depProps.Arch)
232 fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace)
233 if android.InList("32", bitness) && android.InList("64", bitness) {
234 // If both 32 and 64 bit variants are enabled for this module
235 switch depProps.Multilib {
236 case string(android.MultilibBoth):
237 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
238 case string(android.MultilibCommon), string(android.MultilibFirst):
239 depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName)
240 case "32":
241 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
242 case "64", "darwin_universal":
243 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
244 case "prefer32", "first_prefer32":
245 depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName)
246 default:
247 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
248 }
249 } else if android.InList("64", bitness) {
250 // If only 64 bit variant is enabled
251 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
252 } else if android.InList("32", bitness) {
253 // If only 32 bit variant is enabled
254 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
255 } else {
256 // If only common variant is enabled
257 depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName)
258 }
259 }
260 if err := proptools.AppendMatchingProperties(m.GetProperties(), &depsStruct, nil); err != nil {
261 mctx.ModuleErrorf(err.Error())
mrziwang8f86c882024-10-03 12:34:33 -0700262 }
263 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000264}
265
Cole Faust92ccbe22024-10-03 14:38:37 -0700266type filesystemCreatorProps struct {
267 Generated_partition_types []string `blueprint:"mutated"`
268 Unsupported_partition_types []string `blueprint:"mutated"`
269}
270
Jihoon Kang98047cf2024-10-02 17:13:54 +0000271type filesystemCreator struct {
272 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -0700273
274 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +0000275}
276
277func filesystemCreatorFactory() android.Module {
278 module := &filesystemCreator{}
279
Cole Faust69788792024-10-10 11:00:36 -0700280 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -0700281 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000282 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000283 createFsGenState(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000284 module.createInternalModules(ctx)
285 })
286
287 return module
288}
289
290func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000291 soongGeneratedPartitions := &ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions
292 for _, partitionType := range *soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700293 if f.createPartition(ctx, partitionType) {
294 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
295 } else {
296 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
Jihoon Kang0d545b82024-10-11 00:21:57 +0000297 _, *soongGeneratedPartitions = android.RemoveFromList(partitionType, *soongGeneratedPartitions)
Cole Faust92ccbe22024-10-03 14:38:37 -0700298 }
299 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000300 f.createDeviceModule(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000301}
302
Jihoon Kang0d545b82024-10-11 00:21:57 +0000303func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700304 prefix := "soong"
305 if cfg.HasDeviceProduct() {
306 prefix = cfg.DeviceProduct()
307 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000308 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
309}
310
Jihoon Kang0d545b82024-10-11 00:21:57 +0000311func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
312 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000313}
314
315func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
316 baseProps := &struct {
317 Name *string
318 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000319 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000320 }
321
Spandan Das7a46f6c2024-10-14 18:41:18 +0000322 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000323 partitionProps := &filesystem.PartitionNameProperties{}
324 if android.InList("system", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000325 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000326 }
Spandan Das7a46f6c2024-10-14 18:41:18 +0000327 if android.InList("system_ext", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000328 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000329 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000330
331 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700332}
333
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000334func partitionSpecificFsProps(fsProps *filesystem.FilesystemProperties, partitionType string) {
335 switch partitionType {
336 case "system":
337 fsProps.Build_logtags = proptools.BoolPtr(true)
338 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
339 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
340 case "product":
341 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
342 case "vendor":
343 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
344 }
345}
Spandan Dascbe641a2024-10-14 21:07:34 +0000346
Cole Faust92ccbe22024-10-03 14:38:37 -0700347// Creates a soong module to build the given partition. Returns false if we can't support building
348// it.
349func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000350 baseProps := &struct {
351 Name *string
352 Compile_multilib *string
353 }{
354 Name: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)),
mrziwanga077b942024-10-16 16:00:06 -0700355 Compile_multilib: proptools.StringPtr("both"),
356 }
mrziwanga077b942024-10-16 16:00:06 -0700357
Cole Faust92ccbe22024-10-03 14:38:37 -0700358 fsProps := &filesystem.FilesystemProperties{}
359
360 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
361 // and sometimes don't build.
362 fsProps.Unchecked_module = proptools.BoolPtr(true)
363
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000364 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
365 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
366
Jihoon Kang98047cf2024-10-02 17:13:54 +0000367 // BOARD_AVB_ENABLE
368 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
369 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700370 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000371 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700372 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000373 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700374 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000375 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
376 }
377
Cole Faust92ccbe22024-10-03 14:38:37 -0700378 fsProps.Partition_name = proptools.StringPtr(partitionType)
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000379 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
380 fsType := specificPartitionVars.BoardFileSystemType
381 if fsType == "" {
382 fsType = "ext4" //default
383 }
384 fsProps.Type = proptools.StringPtr(fsType)
385 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
386 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
387 return false
388 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000389
Cole Faust92ccbe22024-10-03 14:38:37 -0700390 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000391
Jihoon Kang0d545b82024-10-11 00:21:57 +0000392 fsProps.Is_auto_generated = proptools.BoolPtr(true)
393
Jihoon Kang98047cf2024-10-02 17:13:54 +0000394 // Identical to that of the generic_system_image
395 fsProps.Fsverity.Inputs = []string{
396 "etc/boot-image.prof",
397 "etc/dirty-image-objects",
398 "etc/preloaded-classes",
399 "etc/classpaths/*.pb",
400 "framework/*",
401 "framework/*/*", // framework/{arch}
402 "framework/oat/*/*", // framework/oat/{arch}
403 }
404
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000405 partitionSpecificFsProps(fsProps, partitionType)
406
Jihoon Kang98047cf2024-10-02 17:13:54 +0000407 // system_image properties that are not set:
408 // - filesystemProperties.Avb_hash_algorithm
409 // - filesystemProperties.File_contexts
410 // - filesystemProperties.Dirs
411 // - filesystemProperties.Symlinks
412 // - filesystemProperties.Fake_timestamp
413 // - filesystemProperties.Uuid
414 // - filesystemProperties.Mount_point
415 // - filesystemProperties.Include_make_built_files
416 // - filesystemProperties.Build_logtags
417 // - filesystemProperties.Fsverity.Libs
418 // - systemImageProperties.Linker_config_src
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000419 var module android.Module
420 if partitionType == "system" {
421 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
422 } else {
423 // Explicitly set the partition.
424 fsProps.Partition_type = proptools.StringPtr(partitionType)
425 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
426 }
427 module.HideFromMake()
428 return true
Cole Faust92ccbe22024-10-03 14:38:37 -0700429}
430
431func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000432 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700433 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
434 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
435 if !ok {
436 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
437 }
438 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
439 // For now, don't allowlist anything. The test will fail, but that's fine in the current
440 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000441 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700442 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000443 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700444
445 builder := android.NewRuleBuilder(pctx, ctx)
446 builder.Command().BuiltTool("file_list_diff").
447 Input(makeFileList).
448 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000449 Text(partitionModuleName).
450 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700451 builder.Command().Text("touch").Output(diffTestResultFile)
452 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
453 return diffTestResultFile
454}
455
456func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
457 hasher := sha256.New()
458 hasher.Write([]byte(message))
459 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
460 file := android.PathForModuleOut(ctx, filename)
461 builder := android.NewRuleBuilder(pctx, ctx)
462 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
463 builder.Command().Text("exit 1 #").Output(file)
464 builder.Build("failing command "+filename, "failing command "+filename)
465 return file
466}
467
468type systemImageDepTagType struct {
469 blueprint.BaseDependencyTag
470}
471
472var generatedFilesystemDepTag systemImageDepTagType
473
474func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
475 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000476 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700477 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000478}
479
480func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700481 if ctx.ModuleDir() != "build/soong/fsgen" {
482 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
483 }
484 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000485
mrziwang8f86c882024-10-03 12:34:33 -0700486 content := generateBpContent(ctx, "system")
487 generatedBp := android.PathForOutput(ctx, "soong_generated_product_config.bp")
488 android.WriteFileRule(ctx, generatedBp, content)
489 ctx.Phony("product_config_to_bp", generatedBp)
490
Cole Faust92ccbe22024-10-03 14:38:37 -0700491 var diffTestFiles []android.Path
492 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000493 diffTestFile := f.createDiffTest(ctx, partitionType)
494 diffTestFiles = append(diffTestFiles, diffTestFile)
495 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700496 }
497 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000498 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
499 diffTestFiles = append(diffTestFiles, diffTestFile)
500 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700501 }
502 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000503}
mrziwang8f86c882024-10-03 12:34:33 -0700504
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000505// TODO: assemble baseProps and fsProps here
mrziwang8f86c882024-10-03 12:34:33 -0700506func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
507 // Currently only system partition is supported
508 if partitionType != "system" {
509 return ""
510 }
511
Jihoon Kang0d545b82024-10-11 00:21:57 +0000512 deps := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).fsDeps
mrziwang8f86c882024-10-03 12:34:33 -0700513 depProps := &android.PackagingProperties{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000514 Deps: android.NewSimpleConfigurable(fullyQualifiedModuleNames(deps[partitionType])),
mrziwang8f86c882024-10-03 12:34:33 -0700515 }
516
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000517 result, err := proptools.RepackProperties([]interface{}{depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700518 if err != nil {
519 ctx.ModuleErrorf(err.Error())
520 }
521
522 file := &parser.File{
523 Defs: []parser.Definition{
524 &parser.Module{
525 Type: "module",
526 Map: *result,
527 },
528 },
529 }
530 bytes, err := parser.Print(file)
531 if err != nil {
532 ctx.ModuleErrorf(err.Error())
533 }
534 return strings.TrimSpace(string(bytes))
535}