blob: d75a4a206dd0eb3924f8ace6bdb5991fc4025de4 [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 Kangf1c79ca2024-10-09 20:18:38 +000069 f.createDeviceModule(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +000070}
71
Jihoon Kangf1c79ca2024-10-09 20:18:38 +000072func (f *filesystemCreator) generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -070073 prefix := "soong"
74 if cfg.HasDeviceProduct() {
75 prefix = cfg.DeviceProduct()
76 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +000077 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
78}
79
80func (f *filesystemCreator) generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
81 return f.generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
82}
83
84func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
85 baseProps := &struct {
86 Name *string
87 }{
88 Name: proptools.StringPtr(f.generatedModuleName(ctx.Config(), "device")),
89 }
90
91 // Currently, only the system partition module is created.
92 partitionProps := &filesystem.PartitionNameProperties{}
93 if android.InList("system", f.properties.Generated_partition_types) {
94 partitionProps.System_partition_name = proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), "system"))
95 }
96
97 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -070098}
99
100// Creates a soong module to build the given partition. Returns false if we can't support building
101// it.
102func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000103 baseProps := &struct {
104 Name *string
105 }{
Cole Faust92ccbe22024-10-03 14:38:37 -0700106 Name: proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), partitionType)),
Jihoon Kang98047cf2024-10-02 17:13:54 +0000107 }
108
Cole Faust92ccbe22024-10-03 14:38:37 -0700109 fsProps := &filesystem.FilesystemProperties{}
110
111 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
112 // and sometimes don't build.
113 fsProps.Unchecked_module = proptools.BoolPtr(true)
114
Jihoon Kang98047cf2024-10-02 17:13:54 +0000115 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust92ccbe22024-10-03 14:38:37 -0700116 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Jihoon Kang98047cf2024-10-02 17:13:54 +0000117
118 // BOARD_AVB_ENABLE
119 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
120 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700121 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000122 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700123 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000124 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700125 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000126 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
127 }
128
Cole Faust92ccbe22024-10-03 14:38:37 -0700129 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000130 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
Cole Faust92ccbe22024-10-03 14:38:37 -0700131 fsProps.Type = proptools.StringPtr(specificPartitionVars.BoardFileSystemType)
132 if *fsProps.Type != "ext4" {
Spandan Das94668822024-10-09 20:51:33 +0000133 // TODO(b/372522486): Support other FS types.
Cole Faust92ccbe22024-10-03 14:38:37 -0700134 // Currently the android_filesystem module type only supports ext4:
135 // https://cs.android.com/android/platform/superproject/main/+/main:build/soong/filesystem/filesystem.go;l=416;drc=98047cfd07944b297a12d173453bc984806760d2
136 return false
137 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000138
Cole Faust92ccbe22024-10-03 14:38:37 -0700139 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000140
141 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
142
143 // Identical to that of the generic_system_image
144 fsProps.Fsverity.Inputs = []string{
145 "etc/boot-image.prof",
146 "etc/dirty-image-objects",
147 "etc/preloaded-classes",
148 "etc/classpaths/*.pb",
149 "framework/*",
150 "framework/*/*", // framework/{arch}
151 "framework/oat/*/*", // framework/oat/{arch}
152 }
153
154 // system_image properties that are not set:
155 // - filesystemProperties.Avb_hash_algorithm
156 // - filesystemProperties.File_contexts
157 // - filesystemProperties.Dirs
158 // - filesystemProperties.Symlinks
159 // - filesystemProperties.Fake_timestamp
160 // - filesystemProperties.Uuid
161 // - filesystemProperties.Mount_point
162 // - filesystemProperties.Include_make_built_files
163 // - filesystemProperties.Build_logtags
164 // - filesystemProperties.Fsverity.Libs
165 // - systemImageProperties.Linker_config_src
Cole Faust92ccbe22024-10-03 14:38:37 -0700166 var module android.Module
167 if partitionType == "system" {
168 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
169 } else {
170 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
171 }
172 module.HideFromMake()
173 return true
174}
175
176func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
177 partitionModuleName := f.generatedModuleNameForPartition(ctx.Config(), partitionType)
178 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
179 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
180 if !ok {
181 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
182 }
183 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
184 // For now, don't allowlist anything. The test will fail, but that's fine in the current
185 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000186 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700187 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000188 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700189
190 builder := android.NewRuleBuilder(pctx, ctx)
191 builder.Command().BuiltTool("file_list_diff").
192 Input(makeFileList).
193 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000194 Text(partitionModuleName).
195 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700196 builder.Command().Text("touch").Output(diffTestResultFile)
197 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
198 return diffTestResultFile
199}
200
201func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
202 hasher := sha256.New()
203 hasher.Write([]byte(message))
204 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
205 file := android.PathForModuleOut(ctx, filename)
206 builder := android.NewRuleBuilder(pctx, ctx)
207 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
208 builder.Command().Text("exit 1 #").Output(file)
209 builder.Build("failing command "+filename, "failing command "+filename)
210 return file
211}
212
213type systemImageDepTagType struct {
214 blueprint.BaseDependencyTag
215}
216
217var generatedFilesystemDepTag systemImageDepTagType
218
219func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
220 for _, partitionType := range f.properties.Generated_partition_types {
221 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, f.generatedModuleNameForPartition(ctx.Config(), partitionType))
222 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000223}
224
225func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700226 if ctx.ModuleDir() != "build/soong/fsgen" {
227 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
228 }
229 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000230
Cole Faust92ccbe22024-10-03 14:38:37 -0700231 var diffTestFiles []android.Path
232 for _, partitionType := range f.properties.Generated_partition_types {
233 diffTestFiles = append(diffTestFiles, f.createDiffTest(ctx, partitionType))
234 }
235 for _, partitionType := range f.properties.Unsupported_partition_types {
236 diffTestFiles = append(diffTestFiles, createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType)))
237 }
238 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000239}