blob: 5eeb0a9f0e882cef1626303e76b5add9899a1c8a [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 Faust2cf1bf52024-10-03 14:38:37 -070020 "crypto/sha256"
Jihoon Kang98047cf2024-10-02 17:13:54 +000021 "fmt"
22 "strconv"
23
Cole Faust2cf1bf52024-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 Faust2cf1bf52024-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 Faust2cf1bf52024-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 Faust2cf1bf52024-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 Faust2cf1bf52024-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 Faust2cf1bf52024-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 Faust2cf1bf52024-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 Faust2cf1bf52024-10-03 14:38:37 -070085 Name: proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), partitionType)),
Jihoon Kang98047cf2024-10-02 17:13:54 +000086 }
87
88 fsProps := &(filesystem.FilesystemProperties{})
89 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust2cf1bf52024-10-03 14:38:37 -070090 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Jihoon Kang98047cf2024-10-02 17:13:54 +000091
92 // BOARD_AVB_ENABLE
93 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
94 // BOARD_AVB_KEY_PATH
Cole Faust2cf1bf52024-10-03 14:38:37 -070095 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +000096 // BOARD_AVB_ALGORITHM
Cole Faust2cf1bf52024-10-03 14:38:37 -070097 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +000098 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust2cf1bf52024-10-03 14:38:37 -070099 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000100 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
101 }
102
Cole Faust2cf1bf52024-10-03 14:38:37 -0700103 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000104 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
Cole Faust2cf1bf52024-10-03 14:38:37 -0700105 fsProps.Type = proptools.StringPtr(specificPartitionVars.BoardFileSystemType)
106 if *fsProps.Type != "ext4" {
107 // Currently the android_filesystem module type only supports ext4:
108 // https://cs.android.com/android/platform/superproject/main/+/main:build/soong/filesystem/filesystem.go;l=416;drc=98047cfd07944b297a12d173453bc984806760d2
109 return false
110 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000111
Cole Faust2cf1bf52024-10-03 14:38:37 -0700112 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000113
114 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
115
116 // Identical to that of the generic_system_image
117 fsProps.Fsverity.Inputs = []string{
118 "etc/boot-image.prof",
119 "etc/dirty-image-objects",
120 "etc/preloaded-classes",
121 "etc/classpaths/*.pb",
122 "framework/*",
123 "framework/*/*", // framework/{arch}
124 "framework/oat/*/*", // framework/oat/{arch}
125 }
126
127 // system_image properties that are not set:
128 // - filesystemProperties.Avb_hash_algorithm
129 // - filesystemProperties.File_contexts
130 // - filesystemProperties.Dirs
131 // - filesystemProperties.Symlinks
132 // - filesystemProperties.Fake_timestamp
133 // - filesystemProperties.Uuid
134 // - filesystemProperties.Mount_point
135 // - filesystemProperties.Include_make_built_files
136 // - filesystemProperties.Build_logtags
137 // - filesystemProperties.Fsverity.Libs
138 // - systemImageProperties.Linker_config_src
Cole Faust2cf1bf52024-10-03 14:38:37 -0700139 if partitionType == "system" {
140 ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
141 } else {
142 ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
143 }
144 return true
145}
146
147func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
148 partitionModuleName := f.generatedModuleNameForPartition(ctx.Config(), partitionType)
149 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
150 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
151 if !ok {
152 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
153 }
154 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
155 // For now, don't allowlist anything. The test will fail, but that's fine in the current
156 // early stages where we're just figuring out what we need
157 emptyAllowlistFile := android.PathForModuleOut(ctx, "allowlist_%s.txt", partitionModuleName)
158 android.WriteFileRule(ctx, emptyAllowlistFile, "")
159 diffTestResultFile := android.PathForModuleOut(ctx, "diff_test_%s.txt", partitionModuleName)
160
161 builder := android.NewRuleBuilder(pctx, ctx)
162 builder.Command().BuiltTool("file_list_diff").
163 Input(makeFileList).
164 Input(filesystemInfo.FileListFile).
165 Input(emptyAllowlistFile).
166 Text(partitionModuleName)
167 builder.Command().Text("touch").Output(diffTestResultFile)
168 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
169 return diffTestResultFile
170}
171
172func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
173 hasher := sha256.New()
174 hasher.Write([]byte(message))
175 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
176 file := android.PathForModuleOut(ctx, filename)
177 builder := android.NewRuleBuilder(pctx, ctx)
178 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
179 builder.Command().Text("exit 1 #").Output(file)
180 builder.Build("failing command "+filename, "failing command "+filename)
181 return file
182}
183
184type systemImageDepTagType struct {
185 blueprint.BaseDependencyTag
186}
187
188var generatedFilesystemDepTag systemImageDepTagType
189
190func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
191 for _, partitionType := range f.properties.Generated_partition_types {
192 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, f.generatedModuleNameForPartition(ctx.Config(), partitionType))
193 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000194}
195
196func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust2cf1bf52024-10-03 14:38:37 -0700197 if ctx.ModuleDir() != "build/soong/fsgen" {
198 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
199 }
200 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000201
Cole Faust2cf1bf52024-10-03 14:38:37 -0700202 var diffTestFiles []android.Path
203 for _, partitionType := range f.properties.Generated_partition_types {
204 diffTestFiles = append(diffTestFiles, f.createDiffTest(ctx, partitionType))
205 }
206 for _, partitionType := range f.properties.Unsupported_partition_types {
207 diffTestFiles = append(diffTestFiles, createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType)))
208 }
209 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000210}