blob: ffc380298eacb220d082f4bc7aecdfba81cfed8e [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 }
Spandan Das46c98d92024-10-18 22:10:02 +0000104 if ctx.DeviceConfig().VendorPath() == "vendor" {
105 generatedPartitions = append(generatedPartitions, "vendor")
106 }
Jihoon Kang0d545b82024-10-11 00:21:57 +0000107
108 return &FsGenState{
109 depCandidates: candidates,
110 fsDeps: map[string]multilibDeps{
111 // These additional deps are added according to the cuttlefish system image bp.
112 "system": &map[string]*depCandidateProps{
113 "com.android.apex.cts.shim.v1_prebuilt": defaultDepCandidateProps(ctx.Config()),
114 "dex_bootjars": defaultDepCandidateProps(ctx.Config()),
115 "framework_compatibility_matrix.device.xml": defaultDepCandidateProps(ctx.Config()),
116 "idc_data": defaultDepCandidateProps(ctx.Config()),
117 "init.environ.rc-soong": defaultDepCandidateProps(ctx.Config()),
118 "keychars_data": defaultDepCandidateProps(ctx.Config()),
119 "keylayout_data": defaultDepCandidateProps(ctx.Config()),
120 "libclang_rt.asan": defaultDepCandidateProps(ctx.Config()),
121 "libcompiler_rt": defaultDepCandidateProps(ctx.Config()),
122 "libdmabufheap": defaultDepCandidateProps(ctx.Config()),
123 "libgsi": defaultDepCandidateProps(ctx.Config()),
124 "llndk.libraries.txt": defaultDepCandidateProps(ctx.Config()),
125 "logpersist.start": defaultDepCandidateProps(ctx.Config()),
126 "preloaded-classes": defaultDepCandidateProps(ctx.Config()),
127 "public.libraries.android.txt": defaultDepCandidateProps(ctx.Config()),
128 "update_engine_sideload": defaultDepCandidateProps(ctx.Config()),
129 },
Spandan Dasd9875bc2024-10-17 21:36:17 +0000130 "vendor": newMultilibDeps(),
131 "odm": newMultilibDeps(),
132 "product": newMultilibDeps(),
133 "system_ext": &map[string]*depCandidateProps{
134 // VNDK apexes are automatically included.
135 // This hardcoded list will need to be updated if `PRODUCT_EXTRA_VNDK_VERSIONS` is updated.
136 // https://cs.android.com/android/_/android/platform/build/+/adba533072b00c53ac0f198c550a3cbd7a00e4cd:core/main.mk;l=984;bpv=1;bpt=0;drc=174db7b179592cf07cbfd2adb0119486fda911e7
137 "com.android.vndk.v30": defaultDepCandidateProps(ctx.Config()),
138 "com.android.vndk.v31": defaultDepCandidateProps(ctx.Config()),
139 "com.android.vndk.v32": defaultDepCandidateProps(ctx.Config()),
140 "com.android.vndk.v33": defaultDepCandidateProps(ctx.Config()),
141 "com.android.vndk.v34": defaultDepCandidateProps(ctx.Config()),
142 },
Jihoon Kang0d545b82024-10-11 00:21:57 +0000143 },
144 soongGeneratedPartitions: generatedPartitions,
145 fsDepsMutex: sync.Mutex{},
146 }
147 }).(*FsGenState)
148}
149
150func checkDepModuleInMultipleNamespaces(mctx android.BottomUpMutatorContext, foundDeps map[string]*depCandidateProps, module string, partitionName string) {
151 otherNamespace := mctx.Namespace().Path
152 if val, found := foundDeps[module]; found && otherNamespace != "." && !android.InList(val.Namespace, []string{".", otherNamespace}) {
153 mctx.ModuleErrorf("found in multiple namespaces(%s and %s) when including in %s partition", val.Namespace, otherNamespace, partitionName)
154 }
155}
156
157func appendDepIfAppropriate(mctx android.BottomUpMutatorContext, deps *map[string]*depCandidateProps, installPartition string) {
158 checkDepModuleInMultipleNamespaces(mctx, *deps, mctx.Module().Name(), installPartition)
159 if _, ok := (*deps)[mctx.Module().Name()]; ok {
160 // Prefer the namespace-specific module over the platform module
161 if mctx.Namespace().Path != "." {
162 (*deps)[mctx.Module().Name()].Namespace = mctx.Namespace().Path
163 }
164 (*deps)[mctx.Module().Name()].Arch = append((*deps)[mctx.Module().Name()].Arch, mctx.Module().Target().Arch.ArchType)
165 } else {
166 multilib, _ := mctx.Module().DecodeMultilib(mctx)
167 (*deps)[mctx.Module().Name()] = &depCandidateProps{
168 Namespace: mctx.Namespace().Path,
169 Multilib: multilib,
170 Arch: []android.ArchType{mctx.Module().Target().Arch.ArchType},
171 }
172 }
173}
mrziwang8f86c882024-10-03 12:34:33 -0700174
175func collectDepsMutator(mctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000176 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
mrziwang8f86c882024-10-03 12:34:33 -0700177
178 m := mctx.Module()
Jihoon Kang0d545b82024-10-11 00:21:57 +0000179 if slices.Contains(fsGenState.depCandidates, m.Name()) {
180 installPartition := m.PartitionTag(mctx.DeviceConfig())
181 fsGenState.fsDepsMutex.Lock()
182 // Only add the module as dependency when:
183 // - its enabled
184 // - its namespace is included in PRODUCT_SOONG_NAMESPACES
185 if m.Enabled(mctx) && m.ExportedToMake() {
186 appendDepIfAppropriate(mctx, fsGenState.fsDeps[installPartition], installPartition)
187 }
188 fsGenState.fsDepsMutex.Unlock()
189 }
190}
191
192type depsStruct struct {
193 Deps []string
194}
195
196type multilibDepsStruct struct {
197 Common depsStruct
198 Lib32 depsStruct
199 Lib64 depsStruct
200 Both depsStruct
201 Prefer32 depsStruct
202}
203
204type packagingPropsStruct struct {
205 Deps []string
206 Multilib multilibDepsStruct
207}
208
209func fullyQualifiedModuleName(moduleName, namespace string) string {
210 if namespace == "." {
211 return moduleName
212 }
213 return fmt.Sprintf("//%s:%s", namespace, moduleName)
214}
215
216// Returns the sorted unique list of module names with namespace, if the module specifies one.
217func fullyQualifiedModuleNames(modules multilibDeps) (ret []string) {
218 for moduleName, moduleProp := range *modules {
219 ret = append(ret, fullyQualifiedModuleName(moduleName, moduleProp.Namespace))
220 }
221 return android.SortedUniqueStrings(ret)
222}
223
224func getBitness(archTypes []android.ArchType) (ret []string) {
225 for _, archType := range archTypes {
226 if archType.Multilib == "" {
227 ret = append(ret, android.COMMON_VARIANT)
228 } else {
229 ret = append(ret, archType.Bitness())
230 }
231 }
232 return ret
233}
234
235func setDepsMutator(mctx android.BottomUpMutatorContext) {
236 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
237 fsDeps := fsGenState.fsDeps
238 soongGeneratedPartitionMap := getAllSoongGeneratedPartitionNames(mctx.Config(), fsGenState.soongGeneratedPartitions)
239 m := mctx.Module()
240 if partition, ok := soongGeneratedPartitionMap[m.Name()]; ok {
241 depsStruct := packagingPropsStruct{}
242 for depName, depProps := range *fsDeps[partition] {
243 bitness := getBitness(depProps.Arch)
244 fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace)
245 if android.InList("32", bitness) && android.InList("64", bitness) {
246 // If both 32 and 64 bit variants are enabled for this module
247 switch depProps.Multilib {
248 case string(android.MultilibBoth):
249 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
250 case string(android.MultilibCommon), string(android.MultilibFirst):
251 depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName)
252 case "32":
253 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
254 case "64", "darwin_universal":
255 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
256 case "prefer32", "first_prefer32":
257 depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName)
258 default:
259 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
260 }
261 } else if android.InList("64", bitness) {
262 // If only 64 bit variant is enabled
263 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
264 } else if android.InList("32", bitness) {
265 // If only 32 bit variant is enabled
266 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
267 } else {
268 // If only common variant is enabled
269 depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName)
270 }
271 }
272 if err := proptools.AppendMatchingProperties(m.GetProperties(), &depsStruct, nil); err != nil {
273 mctx.ModuleErrorf(err.Error())
mrziwang8f86c882024-10-03 12:34:33 -0700274 }
275 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000276}
277
Cole Faust92ccbe22024-10-03 14:38:37 -0700278type filesystemCreatorProps struct {
279 Generated_partition_types []string `blueprint:"mutated"`
280 Unsupported_partition_types []string `blueprint:"mutated"`
281}
282
Jihoon Kang98047cf2024-10-02 17:13:54 +0000283type filesystemCreator struct {
284 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -0700285
286 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +0000287}
288
289func filesystemCreatorFactory() android.Module {
290 module := &filesystemCreator{}
291
Cole Faust69788792024-10-10 11:00:36 -0700292 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -0700293 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000294 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000295 createFsGenState(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000296 module.createInternalModules(ctx)
297 })
298
299 return module
300}
301
302func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000303 soongGeneratedPartitions := &ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions
304 for _, partitionType := range *soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700305 if f.createPartition(ctx, partitionType) {
306 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
307 } else {
308 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
Jihoon Kang0d545b82024-10-11 00:21:57 +0000309 _, *soongGeneratedPartitions = android.RemoveFromList(partitionType, *soongGeneratedPartitions)
Cole Faust92ccbe22024-10-03 14:38:37 -0700310 }
311 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000312 f.createDeviceModule(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000313}
314
Jihoon Kang0d545b82024-10-11 00:21:57 +0000315func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700316 prefix := "soong"
317 if cfg.HasDeviceProduct() {
318 prefix = cfg.DeviceProduct()
319 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000320 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
321}
322
Jihoon Kang0d545b82024-10-11 00:21:57 +0000323func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
324 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000325}
326
327func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
328 baseProps := &struct {
329 Name *string
330 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000331 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000332 }
333
Spandan Das46c98d92024-10-18 22:10:02 +0000334 // Currently, only a select set of partitions like system, system_ext is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000335 partitionProps := &filesystem.PartitionNameProperties{}
336 if android.InList("system", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000337 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000338 }
Spandan Das7a46f6c2024-10-14 18:41:18 +0000339 if android.InList("system_ext", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000340 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000341 }
Spandan Das46c98d92024-10-18 22:10:02 +0000342 if android.InList("vendor", f.properties.Generated_partition_types) {
343 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
344 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000345
346 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700347}
348
Spandan Dascbe641a2024-10-14 21:07:34 +0000349var (
350 // https://source.corp.google.com/h/googleplex-android/platform/build/+/639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
351 partitionsWithAconfig = []string{"system", "product", "vendor"}
352)
353
Cole Faust92ccbe22024-10-03 14:38:37 -0700354// Creates a soong module to build the given partition. Returns false if we can't support building
355// it.
356func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000357 baseProps := &struct {
358 Name *string
359 Compile_multilib *string
360 }{
361 Name: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)),
mrziwanga077b942024-10-16 16:00:06 -0700362 Compile_multilib: proptools.StringPtr("both"),
363 }
mrziwanga077b942024-10-16 16:00:06 -0700364
Cole Faust92ccbe22024-10-03 14:38:37 -0700365 fsProps := &filesystem.FilesystemProperties{}
366
367 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
368 // and sometimes don't build.
369 fsProps.Unchecked_module = proptools.BoolPtr(true)
370
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000371 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
372 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
373
Jihoon Kang98047cf2024-10-02 17:13:54 +0000374 // BOARD_AVB_ENABLE
375 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
376 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700377 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000378 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700379 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000380 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700381 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000382 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
383 }
384
Cole Faust92ccbe22024-10-03 14:38:37 -0700385 fsProps.Partition_name = proptools.StringPtr(partitionType)
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000386 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
387 fsType := specificPartitionVars.BoardFileSystemType
388 if fsType == "" {
389 fsType = "ext4" //default
390 }
391 fsProps.Type = proptools.StringPtr(fsType)
392 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
393 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
394 return false
395 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000396
Cole Faust92ccbe22024-10-03 14:38:37 -0700397 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000398
Spandan Dascbe641a2024-10-14 21:07:34 +0000399 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(android.InList(partitionType, partitionsWithAconfig))
Jihoon Kang98047cf2024-10-02 17:13:54 +0000400
Jihoon Kang0d545b82024-10-11 00:21:57 +0000401 fsProps.Is_auto_generated = proptools.BoolPtr(true)
402
Jihoon Kang98047cf2024-10-02 17:13:54 +0000403 // Identical to that of the generic_system_image
404 fsProps.Fsverity.Inputs = []string{
405 "etc/boot-image.prof",
406 "etc/dirty-image-objects",
407 "etc/preloaded-classes",
408 "etc/classpaths/*.pb",
409 "framework/*",
410 "framework/*/*", // framework/{arch}
411 "framework/oat/*/*", // framework/oat/{arch}
412 }
413
414 // system_image properties that are not set:
415 // - filesystemProperties.Avb_hash_algorithm
416 // - filesystemProperties.File_contexts
417 // - filesystemProperties.Dirs
418 // - filesystemProperties.Symlinks
419 // - filesystemProperties.Fake_timestamp
420 // - filesystemProperties.Uuid
421 // - filesystemProperties.Mount_point
422 // - filesystemProperties.Include_make_built_files
423 // - filesystemProperties.Build_logtags
424 // - filesystemProperties.Fsverity.Libs
425 // - systemImageProperties.Linker_config_src
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000426 var module android.Module
427 if partitionType == "system" {
428 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
429 } else {
430 // Explicitly set the partition.
431 fsProps.Partition_type = proptools.StringPtr(partitionType)
432 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
433 }
434 module.HideFromMake()
435 return true
Cole Faust92ccbe22024-10-03 14:38:37 -0700436}
437
438func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000439 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700440 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
441 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
442 if !ok {
443 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
444 }
445 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
446 // For now, don't allowlist anything. The test will fail, but that's fine in the current
447 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000448 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700449 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000450 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700451
452 builder := android.NewRuleBuilder(pctx, ctx)
453 builder.Command().BuiltTool("file_list_diff").
454 Input(makeFileList).
455 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000456 Text(partitionModuleName).
457 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700458 builder.Command().Text("touch").Output(diffTestResultFile)
459 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
460 return diffTestResultFile
461}
462
463func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
464 hasher := sha256.New()
465 hasher.Write([]byte(message))
466 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
467 file := android.PathForModuleOut(ctx, filename)
468 builder := android.NewRuleBuilder(pctx, ctx)
469 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
470 builder.Command().Text("exit 1 #").Output(file)
471 builder.Build("failing command "+filename, "failing command "+filename)
472 return file
473}
474
475type systemImageDepTagType struct {
476 blueprint.BaseDependencyTag
477}
478
479var generatedFilesystemDepTag systemImageDepTagType
480
481func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
482 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000483 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700484 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000485}
486
487func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700488 if ctx.ModuleDir() != "build/soong/fsgen" {
489 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
490 }
491 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000492
mrziwang8f86c882024-10-03 12:34:33 -0700493 content := generateBpContent(ctx, "system")
494 generatedBp := android.PathForOutput(ctx, "soong_generated_product_config.bp")
495 android.WriteFileRule(ctx, generatedBp, content)
496 ctx.Phony("product_config_to_bp", generatedBp)
497
Cole Faust92ccbe22024-10-03 14:38:37 -0700498 var diffTestFiles []android.Path
499 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000500 diffTestFile := f.createDiffTest(ctx, partitionType)
501 diffTestFiles = append(diffTestFiles, diffTestFile)
502 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700503 }
504 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000505 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
506 diffTestFiles = append(diffTestFiles, diffTestFile)
507 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700508 }
509 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000510}
mrziwang8f86c882024-10-03 12:34:33 -0700511
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000512// TODO: assemble baseProps and fsProps here
mrziwang8f86c882024-10-03 12:34:33 -0700513func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
514 // Currently only system partition is supported
515 if partitionType != "system" {
516 return ""
517 }
518
Jihoon Kang0d545b82024-10-11 00:21:57 +0000519 deps := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).fsDeps
mrziwang8f86c882024-10-03 12:34:33 -0700520 depProps := &android.PackagingProperties{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000521 Deps: android.NewSimpleConfigurable(fullyQualifiedModuleNames(deps[partitionType])),
mrziwang8f86c882024-10-03 12:34:33 -0700522 }
523
Priyanka Advani (xWF)dcca9db2024-10-17 20:26:38 +0000524 result, err := proptools.RepackProperties([]interface{}{depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700525 if err != nil {
526 ctx.ModuleErrorf(err.Error())
527 }
528
529 file := &parser.File{
530 Defs: []parser.Definition{
531 &parser.Module{
532 Type: "module",
533 Map: *result,
534 },
535 },
536 }
537 bytes, err := parser.Print(file)
538 if err != nil {
539 ctx.ModuleErrorf(err.Error())
540 }
541 return strings.TrimSpace(string(bytes))
542}