blob: 3f2208d5074966f9b896abfc56d0a167708f66d7 [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 (
18 "android/soong/android"
19 "android/soong/filesystem"
Cole Faust92ccbe22024-10-03 14:38:37 -070020 "crypto/sha256"
Jihoon Kang98047cf2024-10-02 17:13:54 +000021 "fmt"
22 "strconv"
23
Cole Faust92ccbe22024-10-03 14:38:37 -070024 "github.com/google/blueprint"
Jihoon Kang98047cf2024-10-02 17:13:54 +000025 "github.com/google/blueprint/proptools"
26)
27
Cole Faust92ccbe22024-10-03 14:38:37 -070028var pctx = android.NewPackageContext("android/soong/fsgen")
29
Jihoon Kang98047cf2024-10-02 17:13:54 +000030func init() {
31 registerBuildComponents(android.InitRegistrationContext)
32}
33
34func registerBuildComponents(ctx android.RegistrationContext) {
35 ctx.RegisterModuleType("soong_filesystem_creator", filesystemCreatorFactory)
36}
37
Cole Faust92ccbe22024-10-03 14:38:37 -070038type filesystemCreatorProps struct {
39 Generated_partition_types []string `blueprint:"mutated"`
40 Unsupported_partition_types []string `blueprint:"mutated"`
41}
42
Jihoon Kang98047cf2024-10-02 17:13:54 +000043type filesystemCreator struct {
44 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -070045
46 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +000047}
48
49func filesystemCreatorFactory() android.Module {
50 module := &filesystemCreator{}
51
52 android.InitAndroidModule(module)
Cole Faust92ccbe22024-10-03 14:38:37 -070053 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +000054 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
55 module.createInternalModules(ctx)
56 })
57
58 return module
59}
60
61func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -070062 for _, partitionType := range []string{"system"} {
63 if f.createPartition(ctx, partitionType) {
64 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
65 } else {
66 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
67 }
68 }
Jihoon Kang98047cf2024-10-02 17:13:54 +000069}
70
Cole Faust92ccbe22024-10-03 14:38:37 -070071func (f *filesystemCreator) generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
72 prefix := "soong"
73 if cfg.HasDeviceProduct() {
74 prefix = cfg.DeviceProduct()
75 }
76 return fmt.Sprintf("%s_generated_%s_image", prefix, partitionType)
77}
78
79// Creates a soong module to build the given partition. Returns false if we can't support building
80// it.
81func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
Jihoon Kang98047cf2024-10-02 17:13:54 +000082 baseProps := &struct {
83 Name *string
84 }{
Cole Faust92ccbe22024-10-03 14:38:37 -070085 Name: proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), partitionType)),
Jihoon Kang98047cf2024-10-02 17:13:54 +000086 }
87
Cole Faust92ccbe22024-10-03 14:38:37 -070088 fsProps := &filesystem.FilesystemProperties{}
89
90 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
91 // and sometimes don't build.
92 fsProps.Unchecked_module = proptools.BoolPtr(true)
93
Jihoon Kang98047cf2024-10-02 17:13:54 +000094 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust92ccbe22024-10-03 14:38:37 -070095 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Jihoon Kang98047cf2024-10-02 17:13:54 +000096
97 // BOARD_AVB_ENABLE
98 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
99 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700100 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000101 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700102 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000103 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700104 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000105 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
106 }
107
Cole Faust92ccbe22024-10-03 14:38:37 -0700108 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000109 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
Cole Faust92ccbe22024-10-03 14:38:37 -0700110 fsProps.Type = proptools.StringPtr(specificPartitionVars.BoardFileSystemType)
111 if *fsProps.Type != "ext4" {
Spandan Das94668822024-10-09 20:51:33 +0000112 // TODO(b/372522486): Support other FS types.
Cole Faust92ccbe22024-10-03 14:38:37 -0700113 // Currently the android_filesystem module type only supports ext4:
114 // https://cs.android.com/android/platform/superproject/main/+/main:build/soong/filesystem/filesystem.go;l=416;drc=98047cfd07944b297a12d173453bc984806760d2
115 return false
116 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000117
Cole Faust92ccbe22024-10-03 14:38:37 -0700118 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000119
120 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
121
122 // Identical to that of the generic_system_image
123 fsProps.Fsverity.Inputs = []string{
124 "etc/boot-image.prof",
125 "etc/dirty-image-objects",
126 "etc/preloaded-classes",
127 "etc/classpaths/*.pb",
128 "framework/*",
129 "framework/*/*", // framework/{arch}
130 "framework/oat/*/*", // framework/oat/{arch}
131 }
132
133 // system_image properties that are not set:
134 // - filesystemProperties.Avb_hash_algorithm
135 // - filesystemProperties.File_contexts
136 // - filesystemProperties.Dirs
137 // - filesystemProperties.Symlinks
138 // - filesystemProperties.Fake_timestamp
139 // - filesystemProperties.Uuid
140 // - filesystemProperties.Mount_point
141 // - filesystemProperties.Include_make_built_files
142 // - filesystemProperties.Build_logtags
143 // - filesystemProperties.Fsverity.Libs
144 // - systemImageProperties.Linker_config_src
Cole Faust92ccbe22024-10-03 14:38:37 -0700145 var module android.Module
146 if partitionType == "system" {
147 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
148 } else {
149 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
150 }
151 module.HideFromMake()
152 return true
153}
154
155func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
156 partitionModuleName := f.generatedModuleNameForPartition(ctx.Config(), partitionType)
157 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
158 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
159 if !ok {
160 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
161 }
162 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
163 // For now, don't allowlist anything. The test will fail, but that's fine in the current
164 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000165 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700166 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000167 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700168
169 builder := android.NewRuleBuilder(pctx, ctx)
170 builder.Command().BuiltTool("file_list_diff").
171 Input(makeFileList).
172 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000173 Text(partitionModuleName).
174 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700175 builder.Command().Text("touch").Output(diffTestResultFile)
176 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
177 return diffTestResultFile
178}
179
180func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
181 hasher := sha256.New()
182 hasher.Write([]byte(message))
183 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
184 file := android.PathForModuleOut(ctx, filename)
185 builder := android.NewRuleBuilder(pctx, ctx)
186 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
187 builder.Command().Text("exit 1 #").Output(file)
188 builder.Build("failing command "+filename, "failing command "+filename)
189 return file
190}
191
192type systemImageDepTagType struct {
193 blueprint.BaseDependencyTag
194}
195
196var generatedFilesystemDepTag systemImageDepTagType
197
198func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
199 for _, partitionType := range f.properties.Generated_partition_types {
200 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, f.generatedModuleNameForPartition(ctx.Config(), partitionType))
201 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000202}
203
204func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700205 if ctx.ModuleDir() != "build/soong/fsgen" {
206 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
207 }
208 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000209
Cole Faust92ccbe22024-10-03 14:38:37 -0700210 var diffTestFiles []android.Path
211 for _, partitionType := range f.properties.Generated_partition_types {
212 diffTestFiles = append(diffTestFiles, f.createDiffTest(ctx, partitionType))
213 }
214 for _, partitionType := range f.properties.Unsupported_partition_types {
215 diffTestFiles = append(diffTestFiles, createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType)))
216 }
217 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000218}