blob: a9f4256ec14059707c7260e2c71f4f60d09b200a [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" {
112 // Currently the android_filesystem module type only supports ext4:
113 // https://cs.android.com/android/platform/superproject/main/+/main:build/soong/filesystem/filesystem.go;l=416;drc=98047cfd07944b297a12d173453bc984806760d2
114 return false
115 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000116
Cole Faust92ccbe22024-10-03 14:38:37 -0700117 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000118
119 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
120
121 // Identical to that of the generic_system_image
122 fsProps.Fsverity.Inputs = []string{
123 "etc/boot-image.prof",
124 "etc/dirty-image-objects",
125 "etc/preloaded-classes",
126 "etc/classpaths/*.pb",
127 "framework/*",
128 "framework/*/*", // framework/{arch}
129 "framework/oat/*/*", // framework/oat/{arch}
130 }
131
132 // system_image properties that are not set:
133 // - filesystemProperties.Avb_hash_algorithm
134 // - filesystemProperties.File_contexts
135 // - filesystemProperties.Dirs
136 // - filesystemProperties.Symlinks
137 // - filesystemProperties.Fake_timestamp
138 // - filesystemProperties.Uuid
139 // - filesystemProperties.Mount_point
140 // - filesystemProperties.Include_make_built_files
141 // - filesystemProperties.Build_logtags
142 // - filesystemProperties.Fsverity.Libs
143 // - systemImageProperties.Linker_config_src
Cole Faust92ccbe22024-10-03 14:38:37 -0700144 var module android.Module
145 if partitionType == "system" {
146 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
147 } else {
148 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
149 }
150 module.HideFromMake()
151 return true
152}
153
154func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
155 partitionModuleName := f.generatedModuleNameForPartition(ctx.Config(), partitionType)
156 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
157 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
158 if !ok {
159 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
160 }
161 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
162 // For now, don't allowlist anything. The test will fail, but that's fine in the current
163 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000164 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700165 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000166 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700167
168 builder := android.NewRuleBuilder(pctx, ctx)
169 builder.Command().BuiltTool("file_list_diff").
170 Input(makeFileList).
171 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000172 Text(partitionModuleName).
173 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700174 builder.Command().Text("touch").Output(diffTestResultFile)
175 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
176 return diffTestResultFile
177}
178
179func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
180 hasher := sha256.New()
181 hasher.Write([]byte(message))
182 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
183 file := android.PathForModuleOut(ctx, filename)
184 builder := android.NewRuleBuilder(pctx, ctx)
185 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
186 builder.Command().Text("exit 1 #").Output(file)
187 builder.Build("failing command "+filename, "failing command "+filename)
188 return file
189}
190
191type systemImageDepTagType struct {
192 blueprint.BaseDependencyTag
193}
194
195var generatedFilesystemDepTag systemImageDepTagType
196
197func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
198 for _, partitionType := range f.properties.Generated_partition_types {
199 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, f.generatedModuleNameForPartition(ctx.Config(), partitionType))
200 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000201}
202
203func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700204 if ctx.ModuleDir() != "build/soong/fsgen" {
205 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
206 }
207 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000208
Cole Faust92ccbe22024-10-03 14:38:37 -0700209 var diffTestFiles []android.Path
210 for _, partitionType := range f.properties.Generated_partition_types {
211 diffTestFiles = append(diffTestFiles, f.createDiffTest(ctx, partitionType))
212 }
213 for _, partitionType := range f.properties.Unsupported_partition_types {
214 diffTestFiles = append(diffTestFiles, createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType)))
215 }
216 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000217}