blob: 3cee7e4bff5cb35587c63808c03065964407eea6 [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
Spandan Dascbe641a2024-10-14 21:07:34 +0000334var (
335 // https://source.corp.google.com/h/googleplex-android/platform/build/+/639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
336 partitionsWithAconfig = []string{"system", "product", "vendor"}
337)
338
Cole Faust92ccbe22024-10-03 14:38:37 -0700339// Creates a soong module to build the given partition. Returns false if we can't support building
340// it.
341func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwanga077b942024-10-16 16:00:06 -0700342 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
343
344 fsProps, supported := generateFsProps(ctx, partitionType)
345 if !supported {
346 return false
Jihoon Kang98047cf2024-10-02 17:13:54 +0000347 }
348
mrziwanga077b942024-10-16 16:00:06 -0700349 var module android.Module
350 if partitionType == "system" {
351 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
352 } else {
353 // Explicitly set the partition.
354 fsProps.Partition_type = proptools.StringPtr(partitionType)
355 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
356 }
357 module.HideFromMake()
358 return true
359}
360
361type filesystemBaseProperty struct {
362 Name *string
363 Compile_multilib *string
364}
365
366func generateBaseProps(namePtr *string) *filesystemBaseProperty {
367 return &filesystemBaseProperty{
368 Name: namePtr,
369 Compile_multilib: proptools.StringPtr("both"),
370 }
371}
372
373func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700374 fsProps := &filesystem.FilesystemProperties{}
375
mrziwanga077b942024-10-16 16:00:06 -0700376 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
377 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
378
379 // 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 nil, false
388 }
389
Cole Faust92ccbe22024-10-03 14:38:37 -0700390 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
391 // and sometimes don't build.
392 fsProps.Unchecked_module = proptools.BoolPtr(true)
393
Jihoon Kang98047cf2024-10-02 17:13:54 +0000394 // BOARD_AVB_ENABLE
395 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
396 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700397 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000398 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700399 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000400 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700401 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000402 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
403 }
404
Cole Faust92ccbe22024-10-03 14:38:37 -0700405 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000406
Cole Faust92ccbe22024-10-03 14:38:37 -0700407 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000408
Spandan Dascbe641a2024-10-14 21:07:34 +0000409 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(android.InList(partitionType, partitionsWithAconfig))
Jihoon Kang98047cf2024-10-02 17:13:54 +0000410
Jihoon Kang0d545b82024-10-11 00:21:57 +0000411 fsProps.Is_auto_generated = proptools.BoolPtr(true)
412
Jihoon Kang98047cf2024-10-02 17:13:54 +0000413 // Identical to that of the generic_system_image
414 fsProps.Fsverity.Inputs = []string{
415 "etc/boot-image.prof",
416 "etc/dirty-image-objects",
417 "etc/preloaded-classes",
418 "etc/classpaths/*.pb",
419 "framework/*",
420 "framework/*/*", // framework/{arch}
421 "framework/oat/*/*", // framework/oat/{arch}
422 }
423
424 // system_image properties that are not set:
425 // - filesystemProperties.Avb_hash_algorithm
426 // - filesystemProperties.File_contexts
427 // - filesystemProperties.Dirs
428 // - filesystemProperties.Symlinks
429 // - filesystemProperties.Fake_timestamp
430 // - filesystemProperties.Uuid
431 // - filesystemProperties.Mount_point
432 // - filesystemProperties.Include_make_built_files
433 // - filesystemProperties.Build_logtags
434 // - filesystemProperties.Fsverity.Libs
435 // - systemImageProperties.Linker_config_src
mrziwanga077b942024-10-16 16:00:06 -0700436
437 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700438}
439
440func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000441 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700442 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
443 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
444 if !ok {
445 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
446 }
447 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
448 // For now, don't allowlist anything. The test will fail, but that's fine in the current
449 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000450 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700451 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000452 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700453
454 builder := android.NewRuleBuilder(pctx, ctx)
455 builder.Command().BuiltTool("file_list_diff").
456 Input(makeFileList).
457 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000458 Text(partitionModuleName).
459 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700460 builder.Command().Text("touch").Output(diffTestResultFile)
461 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
462 return diffTestResultFile
463}
464
465func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
466 hasher := sha256.New()
467 hasher.Write([]byte(message))
468 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
469 file := android.PathForModuleOut(ctx, filename)
470 builder := android.NewRuleBuilder(pctx, ctx)
471 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
472 builder.Command().Text("exit 1 #").Output(file)
473 builder.Build("failing command "+filename, "failing command "+filename)
474 return file
475}
476
477type systemImageDepTagType struct {
478 blueprint.BaseDependencyTag
479}
480
481var generatedFilesystemDepTag systemImageDepTagType
482
483func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
484 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000485 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700486 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000487}
488
489func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700490 if ctx.ModuleDir() != "build/soong/fsgen" {
491 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
492 }
493 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000494
mrziwang8f86c882024-10-03 12:34:33 -0700495 content := generateBpContent(ctx, "system")
496 generatedBp := android.PathForOutput(ctx, "soong_generated_product_config.bp")
497 android.WriteFileRule(ctx, generatedBp, content)
498 ctx.Phony("product_config_to_bp", generatedBp)
499
Cole Faust92ccbe22024-10-03 14:38:37 -0700500 var diffTestFiles []android.Path
501 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000502 diffTestFile := f.createDiffTest(ctx, partitionType)
503 diffTestFiles = append(diffTestFiles, diffTestFile)
504 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700505 }
506 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000507 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
508 diffTestFiles = append(diffTestFiles, diffTestFile)
509 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700510 }
511 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000512}
mrziwang8f86c882024-10-03 12:34:33 -0700513
mrziwang8f86c882024-10-03 12:34:33 -0700514func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
515 // Currently only system partition is supported
516 if partitionType != "system" {
517 return ""
518 }
519
mrziwanga077b942024-10-16 16:00:06 -0700520 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
521 fsProps, _ := generateFsProps(ctx, partitionType)
522
Jihoon Kang0d545b82024-10-11 00:21:57 +0000523 deps := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).fsDeps
mrziwang8f86c882024-10-03 12:34:33 -0700524 depProps := &android.PackagingProperties{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000525 Deps: android.NewSimpleConfigurable(fullyQualifiedModuleNames(deps[partitionType])),
mrziwang8f86c882024-10-03 12:34:33 -0700526 }
527
mrziwanga077b942024-10-16 16:00:06 -0700528 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700529 if err != nil {
530 ctx.ModuleErrorf(err.Error())
531 }
532
533 file := &parser.File{
534 Defs: []parser.Definition{
535 &parser.Module{
536 Type: "module",
537 Map: *result,
538 },
539 },
540 }
541 bytes, err := parser.Print(file)
542 if err != nil {
543 ctx.ModuleErrorf(err.Error())
544 }
545 return strings.TrimSpace(string(bytes))
546}