blob: 1697220259b7a2594346885fc75dabd7687fae9f [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()
46}
47
mrziwangc7e58c92024-10-11 09:49:48 -070048var fsDepsMutex = sync.Mutex{}
mrziwang8f86c882024-10-03 12:34:33 -070049var collectFsDepsOnceKey = android.NewOnceKey("CollectFsDeps")
50var depCandidatesOnceKey = android.NewOnceKey("DepCandidates")
51
52func collectDepsMutator(mctx android.BottomUpMutatorContext) {
53 // These additional deps are added according to the cuttlefish system image bp.
54 fsDeps := mctx.Config().Once(collectFsDepsOnceKey, func() interface{} {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +000055 deps := []string{
56 "android_vintf_manifest",
57 "com.android.apex.cts.shim.v1_prebuilt",
58 "dex_bootjars",
59 "framework_compatibility_matrix.device.xml",
60 "idc_data",
61 "init.environ.rc-soong",
62 "keychars_data",
63 "keylayout_data",
64 "libclang_rt.asan",
65 "libcompiler_rt",
66 "libdmabufheap",
67 "libgsi",
68 "llndk.libraries.txt",
69 "logpersist.start",
70 "preloaded-classes",
71 "public.libraries.android.txt",
72 "update_engine_sideload",
mrziwang8f86c882024-10-03 12:34:33 -070073 }
74 return &deps
Jihoon Kangac2d1ba2024-10-12 01:44:47 +000075 }).(*[]string)
mrziwang8f86c882024-10-03 12:34:33 -070076
77 depCandidates := mctx.Config().Once(depCandidatesOnceKey, func() interface{} {
78 partitionVars := mctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
79 candidates := slices.Concat(partitionVars.ProductPackages, partitionVars.ProductPackagesDebug)
80 return &candidates
81 }).(*[]string)
82
83 m := mctx.Module()
mrziwang8f86c882024-10-03 12:34:33 -070084 if slices.Contains(*depCandidates, m.Name()) {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +000085 if installInSystem(mctx, m) {
86 fsDepsMutex.Lock()
87 *fsDeps = append(*fsDeps, m.Name())
88 fsDepsMutex.Unlock()
mrziwang8f86c882024-10-03 12:34:33 -070089 }
90 }
Jihoon Kang98047cf2024-10-02 17:13:54 +000091}
92
Cole Faust92ccbe22024-10-03 14:38:37 -070093type filesystemCreatorProps struct {
94 Generated_partition_types []string `blueprint:"mutated"`
95 Unsupported_partition_types []string `blueprint:"mutated"`
96}
97
Jihoon Kang98047cf2024-10-02 17:13:54 +000098type filesystemCreator struct {
99 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -0700100
101 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +0000102}
103
104func filesystemCreatorFactory() android.Module {
105 module := &filesystemCreator{}
106
Cole Faust69788792024-10-10 11:00:36 -0700107 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -0700108 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000109 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
110 module.createInternalModules(ctx)
111 })
112
113 return module
114}
115
116func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Spandan Das7a46f6c2024-10-14 18:41:18 +0000117 partitionTypes := []string{"system"}
118 if ctx.DeviceConfig().SystemExtPath() == "system_ext" { // system_ext exists
119 partitionTypes = append(partitionTypes, "system_ext")
120 }
121 for _, partitionType := range partitionTypes {
Cole Faust92ccbe22024-10-03 14:38:37 -0700122 if f.createPartition(ctx, partitionType) {
123 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
124 } else {
125 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
126 }
127 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000128 f.createDeviceModule(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000129}
130
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000131func (f *filesystemCreator) generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700132 prefix := "soong"
133 if cfg.HasDeviceProduct() {
134 prefix = cfg.DeviceProduct()
135 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000136 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
137}
138
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000139func (f *filesystemCreator) generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
140 return f.generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000141}
142
143func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
144 baseProps := &struct {
145 Name *string
146 }{
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000147 Name: proptools.StringPtr(f.generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000148 }
149
Spandan Das7a46f6c2024-10-14 18:41:18 +0000150 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000151 partitionProps := &filesystem.PartitionNameProperties{}
152 if android.InList("system", f.properties.Generated_partition_types) {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000153 partitionProps.System_partition_name = proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000154 }
Spandan Das7a46f6c2024-10-14 18:41:18 +0000155 if android.InList("system_ext", f.properties.Generated_partition_types) {
156 partitionProps.System_ext_partition_name = proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), "system_ext"))
157 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000158
159 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700160}
161
162// Creates a soong module to build the given partition. Returns false if we can't support building
163// it.
164func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000165 baseProps := &struct {
166 Name *string
167 }{
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000168 Name: proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), partitionType)),
Jihoon Kang98047cf2024-10-02 17:13:54 +0000169 }
170
Cole Faust92ccbe22024-10-03 14:38:37 -0700171 fsProps := &filesystem.FilesystemProperties{}
172
173 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
174 // and sometimes don't build.
175 fsProps.Unchecked_module = proptools.BoolPtr(true)
176
Jihoon Kang98047cf2024-10-02 17:13:54 +0000177 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust92ccbe22024-10-03 14:38:37 -0700178 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Jihoon Kang98047cf2024-10-02 17:13:54 +0000179
180 // BOARD_AVB_ENABLE
181 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
182 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700183 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000184 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700185 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000186 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700187 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000188 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
189 }
190
Cole Faust92ccbe22024-10-03 14:38:37 -0700191 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000192 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
Spandan Das7a46f6c2024-10-14 18:41:18 +0000193 fsType := specificPartitionVars.BoardFileSystemType
194 if fsType == "" {
195 fsType = "ext4" //default
196 }
197 fsProps.Type = proptools.StringPtr(fsType)
198 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
199 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
Cole Faust92ccbe22024-10-03 14:38:37 -0700200 return false
201 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000202
Cole Faust92ccbe22024-10-03 14:38:37 -0700203 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000204
205 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
206
207 // Identical to that of the generic_system_image
208 fsProps.Fsverity.Inputs = []string{
209 "etc/boot-image.prof",
210 "etc/dirty-image-objects",
211 "etc/preloaded-classes",
212 "etc/classpaths/*.pb",
213 "framework/*",
214 "framework/*/*", // framework/{arch}
215 "framework/oat/*/*", // framework/oat/{arch}
216 }
217
218 // system_image properties that are not set:
219 // - filesystemProperties.Avb_hash_algorithm
220 // - filesystemProperties.File_contexts
221 // - filesystemProperties.Dirs
222 // - filesystemProperties.Symlinks
223 // - filesystemProperties.Fake_timestamp
224 // - filesystemProperties.Uuid
225 // - filesystemProperties.Mount_point
226 // - filesystemProperties.Include_make_built_files
227 // - filesystemProperties.Build_logtags
228 // - filesystemProperties.Fsverity.Libs
229 // - systemImageProperties.Linker_config_src
Cole Faust92ccbe22024-10-03 14:38:37 -0700230 var module android.Module
231 if partitionType == "system" {
232 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
233 } else {
234 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
235 }
236 module.HideFromMake()
237 return true
238}
239
240func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000241 partitionModuleName := f.generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700242 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
243 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
244 if !ok {
245 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
246 }
247 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
248 // For now, don't allowlist anything. The test will fail, but that's fine in the current
249 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000250 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700251 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000252 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700253
254 builder := android.NewRuleBuilder(pctx, ctx)
255 builder.Command().BuiltTool("file_list_diff").
256 Input(makeFileList).
257 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000258 Text(partitionModuleName).
259 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700260 builder.Command().Text("touch").Output(diffTestResultFile)
261 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
262 return diffTestResultFile
263}
264
265func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
266 hasher := sha256.New()
267 hasher.Write([]byte(message))
268 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
269 file := android.PathForModuleOut(ctx, filename)
270 builder := android.NewRuleBuilder(pctx, ctx)
271 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
272 builder.Command().Text("exit 1 #").Output(file)
273 builder.Build("failing command "+filename, "failing command "+filename)
274 return file
275}
276
277type systemImageDepTagType struct {
278 blueprint.BaseDependencyTag
279}
280
281var generatedFilesystemDepTag systemImageDepTagType
282
283func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
284 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000285 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, f.generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700286 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000287}
288
289func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700290 if ctx.ModuleDir() != "build/soong/fsgen" {
291 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
292 }
293 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000294
mrziwang8f86c882024-10-03 12:34:33 -0700295 content := generateBpContent(ctx, "system")
296 generatedBp := android.PathForOutput(ctx, "soong_generated_product_config.bp")
297 android.WriteFileRule(ctx, generatedBp, content)
298 ctx.Phony("product_config_to_bp", generatedBp)
299
Cole Faust92ccbe22024-10-03 14:38:37 -0700300 var diffTestFiles []android.Path
301 for _, partitionType := range f.properties.Generated_partition_types {
302 diffTestFiles = append(diffTestFiles, f.createDiffTest(ctx, partitionType))
303 }
304 for _, partitionType := range f.properties.Unsupported_partition_types {
305 diffTestFiles = append(diffTestFiles, createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType)))
306 }
307 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000308}
mrziwang8f86c882024-10-03 12:34:33 -0700309
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000310func installInSystem(ctx android.BottomUpMutatorContext, m android.Module) bool {
311 return m.PartitionTag(ctx.DeviceConfig()) == "system" && !m.InstallInData() &&
312 !m.InstallInTestcases() && !m.InstallInSanitizerDir() && !m.InstallInVendorRamdisk() &&
313 !m.InstallInDebugRamdisk() && !m.InstallInRecovery() && !m.InstallInOdm() &&
314 !m.InstallInVendor()
mrziwang8f86c882024-10-03 12:34:33 -0700315}
316
317// TODO: assemble baseProps and fsProps here
318func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
319 // Currently only system partition is supported
320 if partitionType != "system" {
321 return ""
322 }
323
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000324 deps := ctx.Config().Get(collectFsDepsOnceKey).(*[]string)
mrziwang8f86c882024-10-03 12:34:33 -0700325 depProps := &android.PackagingProperties{
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000326 Deps: android.NewSimpleConfigurable(android.SortedUniqueStrings(*deps)),
mrziwang8f86c882024-10-03 12:34:33 -0700327 }
328
329 result, err := proptools.RepackProperties([]interface{}{depProps})
330 if err != nil {
331 ctx.ModuleErrorf(err.Error())
332 }
333
334 file := &parser.File{
335 Defs: []parser.Definition{
336 &parser.Module{
337 Type: "module",
338 Map: *result,
339 },
340 },
341 }
342 bytes, err := parser.Print(file)
343 if err != nil {
344 ctx.ModuleErrorf(err.Error())
345 }
346 return strings.TrimSpace(string(bytes))
347}