blob: dc7becbf0e9014a366c759dce52cb40b1f1befb9 [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 {
mrziwang2a506cf2024-10-17 15:38:37 -0700238 depsStruct := generateDepStruct(*fsDeps[partition])
239 if err := proptools.AppendMatchingProperties(m.GetProperties(), depsStruct, nil); err != nil {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000240 mctx.ModuleErrorf(err.Error())
mrziwang8f86c882024-10-03 12:34:33 -0700241 }
242 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000243}
244
mrziwang2a506cf2024-10-17 15:38:37 -0700245func generateDepStruct(deps map[string]*depCandidateProps) *packagingPropsStruct {
246 depsStruct := packagingPropsStruct{}
247 for depName, depProps := range deps {
248 bitness := getBitness(depProps.Arch)
249 fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace)
250 if android.InList("32", bitness) && android.InList("64", bitness) {
251 // If both 32 and 64 bit variants are enabled for this module
252 switch depProps.Multilib {
253 case string(android.MultilibBoth):
254 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
255 case string(android.MultilibCommon), string(android.MultilibFirst):
256 depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName)
257 case "32":
258 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
259 case "64", "darwin_universal":
260 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
261 case "prefer32", "first_prefer32":
262 depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName)
263 default:
264 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
265 }
266 } else if android.InList("64", bitness) {
267 // If only 64 bit variant is enabled
268 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
269 } else if android.InList("32", bitness) {
270 // If only 32 bit variant is enabled
271 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
272 } else {
273 // If only common variant is enabled
274 depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName)
275 }
276 }
277 return &depsStruct
278}
279
Cole Faust92ccbe22024-10-03 14:38:37 -0700280type filesystemCreatorProps struct {
281 Generated_partition_types []string `blueprint:"mutated"`
282 Unsupported_partition_types []string `blueprint:"mutated"`
283}
284
Jihoon Kang98047cf2024-10-02 17:13:54 +0000285type filesystemCreator struct {
286 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -0700287
288 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +0000289}
290
291func filesystemCreatorFactory() android.Module {
292 module := &filesystemCreator{}
293
Cole Faust69788792024-10-10 11:00:36 -0700294 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -0700295 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000296 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000297 createFsGenState(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000298 module.createInternalModules(ctx)
299 })
300
301 return module
302}
303
304func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000305 soongGeneratedPartitions := &ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions
306 for _, partitionType := range *soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700307 if f.createPartition(ctx, partitionType) {
308 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
309 } else {
310 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
Jihoon Kang0d545b82024-10-11 00:21:57 +0000311 _, *soongGeneratedPartitions = android.RemoveFromList(partitionType, *soongGeneratedPartitions)
Cole Faust92ccbe22024-10-03 14:38:37 -0700312 }
313 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000314 f.createDeviceModule(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000315}
316
Jihoon Kang0d545b82024-10-11 00:21:57 +0000317func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700318 prefix := "soong"
319 if cfg.HasDeviceProduct() {
320 prefix = cfg.DeviceProduct()
321 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000322 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
323}
324
Jihoon Kang0d545b82024-10-11 00:21:57 +0000325func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
326 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000327}
328
329func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
330 baseProps := &struct {
331 Name *string
332 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000333 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000334 }
335
Spandan Das7a46f6c2024-10-14 18:41:18 +0000336 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000337 partitionProps := &filesystem.PartitionNameProperties{}
338 if android.InList("system", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000339 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000340 }
Spandan Das7a46f6c2024-10-14 18:41:18 +0000341 if android.InList("system_ext", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000342 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000343 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000344
345 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700346}
347
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000348func partitionSpecificFsProps(fsProps *filesystem.FilesystemProperties, partitionType string) {
349 switch partitionType {
350 case "system":
351 fsProps.Build_logtags = proptools.BoolPtr(true)
352 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
353 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
354 case "product":
355 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
356 case "vendor":
357 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
358 }
359}
Spandan Dascbe641a2024-10-14 21:07:34 +0000360
Cole Faust92ccbe22024-10-03 14:38:37 -0700361// Creates a soong module to build the given partition. Returns false if we can't support building
362// it.
363func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700364 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
365
366 fsProps, supported := generateFsProps(ctx, partitionType)
367 if !supported {
368 return false
mrziwanga077b942024-10-16 16:00:06 -0700369 }
mrziwanga077b942024-10-16 16:00:06 -0700370
mrziwang4b0ca972024-10-17 14:56:19 -0700371 var module android.Module
372 if partitionType == "system" {
373 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
374 } else {
375 // Explicitly set the partition.
376 fsProps.Partition_type = proptools.StringPtr(partitionType)
377 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
378 }
379 module.HideFromMake()
380 return true
381}
382
383type filesystemBaseProperty struct {
384 Name *string
385 Compile_multilib *string
386}
387
388func generateBaseProps(namePtr *string) *filesystemBaseProperty {
389 return &filesystemBaseProperty{
390 Name: namePtr,
391 Compile_multilib: proptools.StringPtr("both"),
392 }
393}
394
395func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700396 fsProps := &filesystem.FilesystemProperties{}
397
mrziwang4b0ca972024-10-17 14:56:19 -0700398 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
399 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
400
401 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
402 fsType := specificPartitionVars.BoardFileSystemType
403 if fsType == "" {
404 fsType = "ext4" //default
405 }
406 fsProps.Type = proptools.StringPtr(fsType)
407 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
408 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
409 return nil, false
410 }
411
Cole Faust92ccbe22024-10-03 14:38:37 -0700412 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
413 // and sometimes don't build.
414 fsProps.Unchecked_module = proptools.BoolPtr(true)
415
Jihoon Kang98047cf2024-10-02 17:13:54 +0000416 // BOARD_AVB_ENABLE
417 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
418 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700419 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000420 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700421 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000422 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700423 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000424 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
425 }
426
Cole Faust92ccbe22024-10-03 14:38:37 -0700427 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000428
Cole Faust92ccbe22024-10-03 14:38:37 -0700429 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000430
Jihoon Kang0d545b82024-10-11 00:21:57 +0000431 fsProps.Is_auto_generated = proptools.BoolPtr(true)
432
Jihoon Kang98047cf2024-10-02 17:13:54 +0000433 // Identical to that of the generic_system_image
434 fsProps.Fsverity.Inputs = []string{
435 "etc/boot-image.prof",
436 "etc/dirty-image-objects",
437 "etc/preloaded-classes",
438 "etc/classpaths/*.pb",
439 "framework/*",
440 "framework/*/*", // framework/{arch}
441 "framework/oat/*/*", // framework/oat/{arch}
442 }
Jihoon Kang3c7be412024-10-10 22:14:22 +0000443 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Jihoon Kang98047cf2024-10-02 17:13:54 +0000444
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000445 partitionSpecificFsProps(fsProps, partitionType)
446
Jihoon Kang98047cf2024-10-02 17:13:54 +0000447 // system_image properties that are not set:
448 // - filesystemProperties.Avb_hash_algorithm
449 // - filesystemProperties.File_contexts
450 // - filesystemProperties.Dirs
451 // - filesystemProperties.Symlinks
452 // - filesystemProperties.Fake_timestamp
453 // - filesystemProperties.Uuid
454 // - filesystemProperties.Mount_point
455 // - filesystemProperties.Include_make_built_files
456 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000457 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700458
459 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700460}
461
462func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000463 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700464 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
465 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
466 if !ok {
467 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
468 }
469 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
470 // For now, don't allowlist anything. The test will fail, but that's fine in the current
471 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000472 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700473 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000474 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700475
476 builder := android.NewRuleBuilder(pctx, ctx)
477 builder.Command().BuiltTool("file_list_diff").
478 Input(makeFileList).
479 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000480 Text(partitionModuleName).
481 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700482 builder.Command().Text("touch").Output(diffTestResultFile)
483 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
484 return diffTestResultFile
485}
486
487func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
488 hasher := sha256.New()
489 hasher.Write([]byte(message))
490 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
491 file := android.PathForModuleOut(ctx, filename)
492 builder := android.NewRuleBuilder(pctx, ctx)
493 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
494 builder.Command().Text("exit 1 #").Output(file)
495 builder.Build("failing command "+filename, "failing command "+filename)
496 return file
497}
498
499type systemImageDepTagType struct {
500 blueprint.BaseDependencyTag
501}
502
503var generatedFilesystemDepTag systemImageDepTagType
504
505func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
506 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000507 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700508 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000509}
510
511func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700512 if ctx.ModuleDir() != "build/soong/fsgen" {
513 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
514 }
515 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000516
mrziwang8f86c882024-10-03 12:34:33 -0700517 content := generateBpContent(ctx, "system")
518 generatedBp := android.PathForOutput(ctx, "soong_generated_product_config.bp")
519 android.WriteFileRule(ctx, generatedBp, content)
520 ctx.Phony("product_config_to_bp", generatedBp)
521
Cole Faust92ccbe22024-10-03 14:38:37 -0700522 var diffTestFiles []android.Path
523 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000524 diffTestFile := f.createDiffTest(ctx, partitionType)
525 diffTestFiles = append(diffTestFiles, diffTestFile)
526 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700527 }
528 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000529 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
530 diffTestFiles = append(diffTestFiles, diffTestFile)
531 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700532 }
533 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000534}
mrziwang8f86c882024-10-03 12:34:33 -0700535
mrziwang8f86c882024-10-03 12:34:33 -0700536func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
537 // Currently only system partition is supported
538 if partitionType != "system" {
539 return ""
540 }
mrziwang4b0ca972024-10-17 14:56:19 -0700541 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
542 if !fsTypeSupported {
543 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700544 }
545
mrziwang4b0ca972024-10-17 14:56:19 -0700546 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
mrziwang2a506cf2024-10-17 15:38:37 -0700547 deps := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).fsDeps[partitionType]
548 depProps := generateDepStruct(*deps)
mrziwang8f86c882024-10-03 12:34:33 -0700549
mrziwang4b0ca972024-10-17 14:56:19 -0700550 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700551 if err != nil {
552 ctx.ModuleErrorf(err.Error())
553 }
554
555 file := &parser.File{
556 Defs: []parser.Definition{
557 &parser.Module{
558 Type: "module",
559 Map: *result,
560 },
561 },
562 }
563 bytes, err := parser.Print(file)
564 if err != nil {
565 ctx.ModuleErrorf(err.Error())
566 }
567 return strings.TrimSpace(string(bytes))
568}