blob: c9033389705d3f74e79712d2ea088008b4856f08 [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 Dase3b65312024-10-22 00:27:27 +0000104 if ctx.DeviceConfig().BuildingVendorImage() && ctx.DeviceConfig().VendorPath() == "vendor" {
105 generatedPartitions = append(generatedPartitions, "vendor")
106 }
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000107 if ctx.DeviceConfig().BuildingProductImage() && ctx.DeviceConfig().ProductPath() == "product" {
108 generatedPartitions = append(generatedPartitions, "product")
109 }
Jihoon Kang0d545b82024-10-11 00:21:57 +0000110
111 return &FsGenState{
112 depCandidates: candidates,
113 fsDeps: map[string]multilibDeps{
114 // These additional deps are added according to the cuttlefish system image bp.
115 "system": &map[string]*depCandidateProps{
116 "com.android.apex.cts.shim.v1_prebuilt": defaultDepCandidateProps(ctx.Config()),
117 "dex_bootjars": defaultDepCandidateProps(ctx.Config()),
118 "framework_compatibility_matrix.device.xml": defaultDepCandidateProps(ctx.Config()),
119 "idc_data": defaultDepCandidateProps(ctx.Config()),
120 "init.environ.rc-soong": defaultDepCandidateProps(ctx.Config()),
121 "keychars_data": defaultDepCandidateProps(ctx.Config()),
122 "keylayout_data": defaultDepCandidateProps(ctx.Config()),
123 "libclang_rt.asan": defaultDepCandidateProps(ctx.Config()),
124 "libcompiler_rt": defaultDepCandidateProps(ctx.Config()),
125 "libdmabufheap": defaultDepCandidateProps(ctx.Config()),
126 "libgsi": defaultDepCandidateProps(ctx.Config()),
127 "llndk.libraries.txt": defaultDepCandidateProps(ctx.Config()),
128 "logpersist.start": defaultDepCandidateProps(ctx.Config()),
129 "preloaded-classes": defaultDepCandidateProps(ctx.Config()),
130 "public.libraries.android.txt": defaultDepCandidateProps(ctx.Config()),
131 "update_engine_sideload": defaultDepCandidateProps(ctx.Config()),
132 },
Spandan Das49cc3e82024-10-23 20:54:01 +0000133 "vendor": &map[string]*depCandidateProps{
134 "fs_config_files_vendor": defaultDepCandidateProps(ctx.Config()),
135 "fs_config_dirs_vendor": defaultDepCandidateProps(ctx.Config()),
136 },
Spandan Dasd9875bc2024-10-17 21:36:17 +0000137 "odm": newMultilibDeps(),
138 "product": newMultilibDeps(),
139 "system_ext": &map[string]*depCandidateProps{
140 // VNDK apexes are automatically included.
141 // This hardcoded list will need to be updated if `PRODUCT_EXTRA_VNDK_VERSIONS` is updated.
142 // https://cs.android.com/android/_/android/platform/build/+/adba533072b00c53ac0f198c550a3cbd7a00e4cd:core/main.mk;l=984;bpv=1;bpt=0;drc=174db7b179592cf07cbfd2adb0119486fda911e7
143 "com.android.vndk.v30": defaultDepCandidateProps(ctx.Config()),
144 "com.android.vndk.v31": defaultDepCandidateProps(ctx.Config()),
145 "com.android.vndk.v32": defaultDepCandidateProps(ctx.Config()),
146 "com.android.vndk.v33": defaultDepCandidateProps(ctx.Config()),
147 "com.android.vndk.v34": defaultDepCandidateProps(ctx.Config()),
148 },
Jihoon Kang0d545b82024-10-11 00:21:57 +0000149 },
150 soongGeneratedPartitions: generatedPartitions,
151 fsDepsMutex: sync.Mutex{},
152 }
153 }).(*FsGenState)
154}
155
156func checkDepModuleInMultipleNamespaces(mctx android.BottomUpMutatorContext, foundDeps map[string]*depCandidateProps, module string, partitionName string) {
157 otherNamespace := mctx.Namespace().Path
158 if val, found := foundDeps[module]; found && otherNamespace != "." && !android.InList(val.Namespace, []string{".", otherNamespace}) {
159 mctx.ModuleErrorf("found in multiple namespaces(%s and %s) when including in %s partition", val.Namespace, otherNamespace, partitionName)
160 }
161}
162
163func appendDepIfAppropriate(mctx android.BottomUpMutatorContext, deps *map[string]*depCandidateProps, installPartition string) {
164 checkDepModuleInMultipleNamespaces(mctx, *deps, mctx.Module().Name(), installPartition)
165 if _, ok := (*deps)[mctx.Module().Name()]; ok {
166 // Prefer the namespace-specific module over the platform module
167 if mctx.Namespace().Path != "." {
168 (*deps)[mctx.Module().Name()].Namespace = mctx.Namespace().Path
169 }
170 (*deps)[mctx.Module().Name()].Arch = append((*deps)[mctx.Module().Name()].Arch, mctx.Module().Target().Arch.ArchType)
171 } else {
172 multilib, _ := mctx.Module().DecodeMultilib(mctx)
173 (*deps)[mctx.Module().Name()] = &depCandidateProps{
174 Namespace: mctx.Namespace().Path,
175 Multilib: multilib,
176 Arch: []android.ArchType{mctx.Module().Target().Arch.ArchType},
177 }
178 }
179}
mrziwang8f86c882024-10-03 12:34:33 -0700180
181func collectDepsMutator(mctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000182 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
mrziwang8f86c882024-10-03 12:34:33 -0700183
184 m := mctx.Module()
Spandan Dasfcc07c02024-10-21 23:33:43 +0000185 if m.Target().Os.Class == android.Device && slices.Contains(fsGenState.depCandidates, m.Name()) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000186 installPartition := m.PartitionTag(mctx.DeviceConfig())
187 fsGenState.fsDepsMutex.Lock()
188 // Only add the module as dependency when:
189 // - its enabled
190 // - its namespace is included in PRODUCT_SOONG_NAMESPACES
191 if m.Enabled(mctx) && m.ExportedToMake() {
192 appendDepIfAppropriate(mctx, fsGenState.fsDeps[installPartition], installPartition)
193 }
194 fsGenState.fsDepsMutex.Unlock()
195 }
196}
197
198type depsStruct struct {
199 Deps []string
200}
201
202type multilibDepsStruct struct {
203 Common depsStruct
204 Lib32 depsStruct
205 Lib64 depsStruct
206 Both depsStruct
207 Prefer32 depsStruct
208}
209
210type packagingPropsStruct struct {
211 Deps []string
212 Multilib multilibDepsStruct
213}
214
215func fullyQualifiedModuleName(moduleName, namespace string) string {
216 if namespace == "." {
217 return moduleName
218 }
219 return fmt.Sprintf("//%s:%s", namespace, moduleName)
220}
221
Jihoon Kang0d545b82024-10-11 00:21:57 +0000222func getBitness(archTypes []android.ArchType) (ret []string) {
223 for _, archType := range archTypes {
224 if archType.Multilib == "" {
225 ret = append(ret, android.COMMON_VARIANT)
226 } else {
227 ret = append(ret, archType.Bitness())
228 }
229 }
230 return ret
231}
232
233func setDepsMutator(mctx android.BottomUpMutatorContext) {
234 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
235 fsDeps := fsGenState.fsDeps
236 soongGeneratedPartitionMap := getAllSoongGeneratedPartitionNames(mctx.Config(), fsGenState.soongGeneratedPartitions)
237 m := mctx.Module()
238 if partition, ok := soongGeneratedPartitionMap[m.Name()]; ok {
mrziwang2a506cf2024-10-17 15:38:37 -0700239 depsStruct := generateDepStruct(*fsDeps[partition])
240 if err := proptools.AppendMatchingProperties(m.GetProperties(), depsStruct, nil); err != nil {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000241 mctx.ModuleErrorf(err.Error())
mrziwang8f86c882024-10-03 12:34:33 -0700242 }
243 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000244}
245
mrziwang2a506cf2024-10-17 15:38:37 -0700246func generateDepStruct(deps map[string]*depCandidateProps) *packagingPropsStruct {
247 depsStruct := packagingPropsStruct{}
248 for depName, depProps := range deps {
249 bitness := getBitness(depProps.Arch)
250 fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace)
251 if android.InList("32", bitness) && android.InList("64", bitness) {
252 // If both 32 and 64 bit variants are enabled for this module
253 switch depProps.Multilib {
254 case string(android.MultilibBoth):
255 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
256 case string(android.MultilibCommon), string(android.MultilibFirst):
257 depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName)
258 case "32":
259 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
260 case "64", "darwin_universal":
261 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
262 case "prefer32", "first_prefer32":
263 depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName)
264 default:
265 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
266 }
267 } else if android.InList("64", bitness) {
268 // If only 64 bit variant is enabled
269 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
270 } else if android.InList("32", bitness) {
271 // If only 32 bit variant is enabled
272 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
273 } else {
274 // If only common variant is enabled
275 depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName)
276 }
277 }
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000278 depsStruct.Deps = android.SortedUniqueStrings(depsStruct.Deps)
279 depsStruct.Multilib.Lib32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib32.Deps)
280 depsStruct.Multilib.Lib64.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib64.Deps)
281 depsStruct.Multilib.Prefer32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Prefer32.Deps)
282 depsStruct.Multilib.Both.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Both.Deps)
283 depsStruct.Multilib.Common.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Common.Deps)
284
mrziwang2a506cf2024-10-17 15:38:37 -0700285 return &depsStruct
286}
287
Cole Faust92ccbe22024-10-03 14:38:37 -0700288type filesystemCreatorProps struct {
289 Generated_partition_types []string `blueprint:"mutated"`
290 Unsupported_partition_types []string `blueprint:"mutated"`
291}
292
Jihoon Kang98047cf2024-10-02 17:13:54 +0000293type filesystemCreator struct {
294 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -0700295
296 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +0000297}
298
299func filesystemCreatorFactory() android.Module {
300 module := &filesystemCreator{}
301
Cole Faust69788792024-10-10 11:00:36 -0700302 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -0700303 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000304 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000305 createFsGenState(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000306 module.createInternalModules(ctx)
307 })
308
309 return module
310}
311
312func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000313 soongGeneratedPartitions := &ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions
314 for _, partitionType := range *soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700315 if f.createPartition(ctx, partitionType) {
316 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
317 } else {
318 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
Jihoon Kang0d545b82024-10-11 00:21:57 +0000319 _, *soongGeneratedPartitions = android.RemoveFromList(partitionType, *soongGeneratedPartitions)
Cole Faust92ccbe22024-10-03 14:38:37 -0700320 }
321 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000322 f.createDeviceModule(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000323}
324
Jihoon Kang0d545b82024-10-11 00:21:57 +0000325func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700326 prefix := "soong"
327 if cfg.HasDeviceProduct() {
328 prefix = cfg.DeviceProduct()
329 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000330 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
331}
332
Jihoon Kang0d545b82024-10-11 00:21:57 +0000333func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
334 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000335}
336
337func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
338 baseProps := &struct {
339 Name *string
340 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000341 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000342 }
343
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000344 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000345 partitionProps := &filesystem.PartitionNameProperties{}
346 if android.InList("system", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000347 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000348 }
Spandan Das7a46f6c2024-10-14 18:41:18 +0000349 if android.InList("system_ext", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000350 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000351 }
Spandan Dase3b65312024-10-22 00:27:27 +0000352 if android.InList("vendor", f.properties.Generated_partition_types) {
353 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
354 }
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000355 if android.InList("product", f.properties.Generated_partition_types) {
356 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
357 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000358
359 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700360}
361
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000362func partitionSpecificFsProps(fsProps *filesystem.FilesystemProperties, partitionType string) {
363 switch partitionType {
364 case "system":
365 fsProps.Build_logtags = proptools.BoolPtr(true)
366 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
367 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000368 // Identical to that of the generic_system_image
369 fsProps.Fsverity.Inputs = []string{
370 "etc/boot-image.prof",
371 "etc/dirty-image-objects",
372 "etc/preloaded-classes",
373 "etc/classpaths/*.pb",
374 "framework/*",
375 "framework/*/*", // framework/{arch}
376 "framework/oat/*/*", // framework/oat/{arch}
377 }
378 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
379 case "system_ext":
380 fsProps.Fsverity.Inputs = []string{
381 "framework/*",
382 "framework/*/*", // framework/{arch}
383 "framework/oat/*/*", // framework/oat/{arch}
384 }
385 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000386 case "product":
387 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
388 case "vendor":
389 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
390 }
391}
Spandan Dascbe641a2024-10-14 21:07:34 +0000392
Cole Faust92ccbe22024-10-03 14:38:37 -0700393// Creates a soong module to build the given partition. Returns false if we can't support building
394// it.
395func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700396 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
397
398 fsProps, supported := generateFsProps(ctx, partitionType)
399 if !supported {
400 return false
mrziwanga077b942024-10-16 16:00:06 -0700401 }
mrziwanga077b942024-10-16 16:00:06 -0700402
mrziwang4b0ca972024-10-17 14:56:19 -0700403 var module android.Module
404 if partitionType == "system" {
405 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
406 } else {
407 // Explicitly set the partition.
408 fsProps.Partition_type = proptools.StringPtr(partitionType)
409 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
410 }
411 module.HideFromMake()
412 return true
413}
414
415type filesystemBaseProperty struct {
416 Name *string
417 Compile_multilib *string
418}
419
420func generateBaseProps(namePtr *string) *filesystemBaseProperty {
421 return &filesystemBaseProperty{
422 Name: namePtr,
423 Compile_multilib: proptools.StringPtr("both"),
424 }
425}
426
427func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700428 fsProps := &filesystem.FilesystemProperties{}
429
mrziwang4b0ca972024-10-17 14:56:19 -0700430 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
431 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
432
433 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
434 fsType := specificPartitionVars.BoardFileSystemType
435 if fsType == "" {
436 fsType = "ext4" //default
437 }
438 fsProps.Type = proptools.StringPtr(fsType)
439 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
440 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
441 return nil, false
442 }
443
Cole Faust92ccbe22024-10-03 14:38:37 -0700444 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
445 // and sometimes don't build.
446 fsProps.Unchecked_module = proptools.BoolPtr(true)
447
Jihoon Kang98047cf2024-10-02 17:13:54 +0000448 // BOARD_AVB_ENABLE
449 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
450 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700451 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000452 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700453 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000454 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700455 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000456 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
457 }
458
Cole Faust92ccbe22024-10-03 14:38:37 -0700459 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000460
Cole Faust92ccbe22024-10-03 14:38:37 -0700461 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000462
Jihoon Kang0d545b82024-10-11 00:21:57 +0000463 fsProps.Is_auto_generated = proptools.BoolPtr(true)
464
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000465 partitionSpecificFsProps(fsProps, partitionType)
466
Jihoon Kang98047cf2024-10-02 17:13:54 +0000467 // system_image properties that are not set:
468 // - filesystemProperties.Avb_hash_algorithm
469 // - filesystemProperties.File_contexts
470 // - filesystemProperties.Dirs
471 // - filesystemProperties.Symlinks
472 // - filesystemProperties.Fake_timestamp
473 // - filesystemProperties.Uuid
474 // - filesystemProperties.Mount_point
475 // - filesystemProperties.Include_make_built_files
476 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000477 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700478
479 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700480}
481
482func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000483 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700484 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
485 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
486 if !ok {
487 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
488 }
489 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
490 // For now, don't allowlist anything. The test will fail, but that's fine in the current
491 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000492 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700493 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000494 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700495
496 builder := android.NewRuleBuilder(pctx, ctx)
497 builder.Command().BuiltTool("file_list_diff").
498 Input(makeFileList).
499 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000500 Text(partitionModuleName).
501 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700502 builder.Command().Text("touch").Output(diffTestResultFile)
503 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
504 return diffTestResultFile
505}
506
507func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
508 hasher := sha256.New()
509 hasher.Write([]byte(message))
510 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
511 file := android.PathForModuleOut(ctx, filename)
512 builder := android.NewRuleBuilder(pctx, ctx)
513 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
514 builder.Command().Text("exit 1 #").Output(file)
515 builder.Build("failing command "+filename, "failing command "+filename)
516 return file
517}
518
519type systemImageDepTagType struct {
520 blueprint.BaseDependencyTag
521}
522
523var generatedFilesystemDepTag systemImageDepTagType
524
525func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
526 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000527 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700528 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000529}
530
531func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700532 if ctx.ModuleDir() != "build/soong/fsgen" {
533 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
534 }
535 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000536
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000537 var content strings.Builder
538 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
539 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
540 content.WriteString(generateBpContent(ctx, partition))
541 content.WriteString("\n")
542 }
543 android.WriteFileRule(ctx, generatedBp, content.String())
544
mrziwang8f86c882024-10-03 12:34:33 -0700545 ctx.Phony("product_config_to_bp", generatedBp)
546
Cole Faust92ccbe22024-10-03 14:38:37 -0700547 var diffTestFiles []android.Path
548 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000549 diffTestFile := f.createDiffTest(ctx, partitionType)
550 diffTestFiles = append(diffTestFiles, diffTestFile)
551 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700552 }
553 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000554 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
555 diffTestFiles = append(diffTestFiles, diffTestFile)
556 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700557 }
558 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000559}
mrziwang8f86c882024-10-03 12:34:33 -0700560
mrziwang8f86c882024-10-03 12:34:33 -0700561func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -0700562 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
563 if !fsTypeSupported {
564 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700565 }
566
mrziwang4b0ca972024-10-17 14:56:19 -0700567 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
mrziwang2a506cf2024-10-17 15:38:37 -0700568 deps := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).fsDeps[partitionType]
569 depProps := generateDepStruct(*deps)
mrziwang8f86c882024-10-03 12:34:33 -0700570
mrziwang4b0ca972024-10-17 14:56:19 -0700571 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700572 if err != nil {
573 ctx.ModuleErrorf(err.Error())
574 }
575
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000576 moduleType := "android_filesystem"
577 if partitionType == "system" {
578 moduleType = "android_system_image"
579 }
580
mrziwang8f86c882024-10-03 12:34:33 -0700581 file := &parser.File{
582 Defs: []parser.Definition{
583 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000584 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -0700585 Map: *result,
586 },
587 },
588 }
589 bytes, err := parser.Print(file)
590 if err != nil {
591 ctx.ModuleErrorf(err.Error())
592 }
593 return strings.TrimSpace(string(bytes))
594}