Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package fsgen |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/filesystem" |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 20 | "crypto/sha256" |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 21 | "fmt" |
| 22 | "strconv" |
| 23 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" |
| 26 | ) |
| 27 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 28 | var pctx = android.NewPackageContext("android/soong/fsgen") |
| 29 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 30 | func init() { |
| 31 | registerBuildComponents(android.InitRegistrationContext) |
| 32 | } |
| 33 | |
| 34 | func registerBuildComponents(ctx android.RegistrationContext) { |
| 35 | ctx.RegisterModuleType("soong_filesystem_creator", filesystemCreatorFactory) |
| 36 | } |
| 37 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 38 | type filesystemCreatorProps struct { |
| 39 | Generated_partition_types []string `blueprint:"mutated"` |
| 40 | Unsupported_partition_types []string `blueprint:"mutated"` |
| 41 | } |
| 42 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 43 | type filesystemCreator struct { |
| 44 | android.ModuleBase |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 45 | |
| 46 | properties filesystemCreatorProps |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | func filesystemCreatorFactory() android.Module { |
| 50 | module := &filesystemCreator{} |
| 51 | |
| 52 | android.InitAndroidModule(module) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 53 | module.AddProperties(&module.properties) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 54 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
| 55 | module.createInternalModules(ctx) |
| 56 | }) |
| 57 | |
| 58 | return module |
| 59 | } |
| 60 | |
| 61 | func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 62 | 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 Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 69 | f.createDeviceModule(ctx) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 72 | func (f *filesystemCreator) generatedModuleName(cfg android.Config, suffix string) string { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 73 | prefix := "soong" |
| 74 | if cfg.HasDeviceProduct() { |
| 75 | prefix = cfg.DeviceProduct() |
| 76 | } |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 77 | return fmt.Sprintf("%s_generated_%s", prefix, suffix) |
| 78 | } |
| 79 | |
| 80 | func (f *filesystemCreator) generatedModuleNameForPartition(cfg android.Config, partitionType string) string { |
| 81 | return f.generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType)) |
| 82 | } |
| 83 | |
| 84 | func (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 Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Creates a soong module to build the given partition. Returns false if we can't support building |
| 101 | // it. |
| 102 | func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool { |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 103 | baseProps := &struct { |
| 104 | Name *string |
| 105 | }{ |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 106 | Name: proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), partitionType)), |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 109 | 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 Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 115 | partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 116 | specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType] |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 117 | |
| 118 | // BOARD_AVB_ENABLE |
| 119 | fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable) |
| 120 | // BOARD_AVB_KEY_PATH |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 121 | fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 122 | // BOARD_AVB_ALGORITHM |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 123 | fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 124 | // BOARD_AVB_SYSTEM_ROLLBACK_INDEX |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 125 | if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil { |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 126 | fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex) |
| 127 | } |
| 128 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 129 | fsProps.Partition_name = proptools.StringPtr(partitionType) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 130 | // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 131 | fsProps.Type = proptools.StringPtr(specificPartitionVars.BoardFileSystemType) |
| 132 | if *fsProps.Type != "ext4" { |
Spandan Das | 9466882 | 2024-10-09 20:51:33 +0000 | [diff] [blame] | 133 | // TODO(b/372522486): Support other FS types. |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 134 | // 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 Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 138 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 139 | fsProps.Base_dir = proptools.StringPtr(partitionType) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 140 | |
| 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 Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 166 | 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 | |
| 176 | func (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 Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 186 | emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName)) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 187 | android.WriteFileRule(ctx, emptyAllowlistFile, "") |
Jihoon Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 188 | diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName)) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 189 | |
| 190 | builder := android.NewRuleBuilder(pctx, ctx) |
| 191 | builder.Command().BuiltTool("file_list_diff"). |
| 192 | Input(makeFileList). |
| 193 | Input(filesystemInfo.FileListFile). |
Jihoon Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 194 | Text(partitionModuleName). |
| 195 | FlagWithInput("--allowlists ", emptyAllowlistFile) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 196 | builder.Command().Text("touch").Output(diffTestResultFile) |
| 197 | builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test") |
| 198 | return diffTestResultFile |
| 199 | } |
| 200 | |
| 201 | func 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 | |
| 213 | type systemImageDepTagType struct { |
| 214 | blueprint.BaseDependencyTag |
| 215 | } |
| 216 | |
| 217 | var generatedFilesystemDepTag systemImageDepTagType |
| 218 | |
| 219 | func (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 Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 226 | 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 Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 230 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 231 | 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 Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 239 | } |