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 | |
Cole Faust | 6978879 | 2024-10-10 11:00:36 -0700 | [diff] [blame] | 52 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
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) { |
Spandan Das | efc456a | 2024-10-10 21:24:34 +0000 | [diff] [blame^] | 62 | for _, partitionType := range []string{"system", "system_ext"} { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 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 | |
Spandan Das | efc456a | 2024-10-10 21:24:34 +0000 | [diff] [blame^] | 91 | // Currently, only the system and system_ext partition module is created. |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 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 | } |
Spandan Das | efc456a | 2024-10-10 21:24:34 +0000 | [diff] [blame^] | 96 | if android.InList("system_ext", f.properties.Generated_partition_types) { |
| 97 | partitionProps.System_ext_partition_name = proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), "system_ext")) |
| 98 | } |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 99 | |
| 100 | ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Creates a soong module to build the given partition. Returns false if we can't support building |
| 104 | // it. |
| 105 | func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool { |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 106 | baseProps := &struct { |
| 107 | Name *string |
| 108 | }{ |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 109 | Name: proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), partitionType)), |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 112 | fsProps := &filesystem.FilesystemProperties{} |
| 113 | |
| 114 | // Don't build this module on checkbuilds, the soong-built partitions are still in-progress |
| 115 | // and sometimes don't build. |
| 116 | fsProps.Unchecked_module = proptools.BoolPtr(true) |
| 117 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 118 | partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 119 | specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType] |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 120 | |
| 121 | // BOARD_AVB_ENABLE |
| 122 | fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable) |
| 123 | // BOARD_AVB_KEY_PATH |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 124 | fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 125 | // BOARD_AVB_ALGORITHM |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 126 | fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 127 | // BOARD_AVB_SYSTEM_ROLLBACK_INDEX |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 128 | if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil { |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 129 | fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex) |
| 130 | } |
| 131 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 132 | fsProps.Partition_name = proptools.StringPtr(partitionType) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 133 | // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE |
Spandan Das | efc456a | 2024-10-10 21:24:34 +0000 | [diff] [blame^] | 134 | fsType := specificPartitionVars.BoardFileSystemType |
| 135 | if fsType == "" { |
| 136 | fsType = "ext4" //default |
| 137 | } |
| 138 | fsProps.Type = proptools.StringPtr(fsType) |
| 139 | if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() { |
| 140 | // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 141 | return false |
| 142 | } |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 143 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 144 | fsProps.Base_dir = proptools.StringPtr(partitionType) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 145 | |
| 146 | fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true) |
| 147 | |
| 148 | // Identical to that of the generic_system_image |
| 149 | fsProps.Fsverity.Inputs = []string{ |
| 150 | "etc/boot-image.prof", |
| 151 | "etc/dirty-image-objects", |
| 152 | "etc/preloaded-classes", |
| 153 | "etc/classpaths/*.pb", |
| 154 | "framework/*", |
| 155 | "framework/*/*", // framework/{arch} |
| 156 | "framework/oat/*/*", // framework/oat/{arch} |
| 157 | } |
| 158 | |
| 159 | // system_image properties that are not set: |
| 160 | // - filesystemProperties.Avb_hash_algorithm |
| 161 | // - filesystemProperties.File_contexts |
| 162 | // - filesystemProperties.Dirs |
| 163 | // - filesystemProperties.Symlinks |
| 164 | // - filesystemProperties.Fake_timestamp |
| 165 | // - filesystemProperties.Uuid |
| 166 | // - filesystemProperties.Mount_point |
| 167 | // - filesystemProperties.Include_make_built_files |
| 168 | // - filesystemProperties.Build_logtags |
| 169 | // - filesystemProperties.Fsverity.Libs |
| 170 | // - systemImageProperties.Linker_config_src |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 171 | var module android.Module |
| 172 | if partitionType == "system" { |
| 173 | module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps) |
| 174 | } else { |
| 175 | module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps) |
| 176 | } |
| 177 | module.HideFromMake() |
| 178 | return true |
| 179 | } |
| 180 | |
| 181 | func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path { |
| 182 | partitionModuleName := f.generatedModuleNameForPartition(ctx.Config(), partitionType) |
| 183 | systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag) |
| 184 | filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider) |
| 185 | if !ok { |
| 186 | ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName) |
| 187 | } |
| 188 | makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType)) |
| 189 | // For now, don't allowlist anything. The test will fail, but that's fine in the current |
| 190 | // early stages where we're just figuring out what we need |
Jihoon Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 191 | emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName)) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 192 | android.WriteFileRule(ctx, emptyAllowlistFile, "") |
Jihoon Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 193 | diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName)) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 194 | |
| 195 | builder := android.NewRuleBuilder(pctx, ctx) |
| 196 | builder.Command().BuiltTool("file_list_diff"). |
| 197 | Input(makeFileList). |
| 198 | Input(filesystemInfo.FileListFile). |
Jihoon Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 199 | Text(partitionModuleName). |
| 200 | FlagWithInput("--allowlists ", emptyAllowlistFile) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 201 | builder.Command().Text("touch").Output(diffTestResultFile) |
| 202 | builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test") |
| 203 | return diffTestResultFile |
| 204 | } |
| 205 | |
| 206 | func createFailingCommand(ctx android.ModuleContext, message string) android.Path { |
| 207 | hasher := sha256.New() |
| 208 | hasher.Write([]byte(message)) |
| 209 | filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil)) |
| 210 | file := android.PathForModuleOut(ctx, filename) |
| 211 | builder := android.NewRuleBuilder(pctx, ctx) |
| 212 | builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message)) |
| 213 | builder.Command().Text("exit 1 #").Output(file) |
| 214 | builder.Build("failing command "+filename, "failing command "+filename) |
| 215 | return file |
| 216 | } |
| 217 | |
| 218 | type systemImageDepTagType struct { |
| 219 | blueprint.BaseDependencyTag |
| 220 | } |
| 221 | |
| 222 | var generatedFilesystemDepTag systemImageDepTagType |
| 223 | |
| 224 | func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 225 | for _, partitionType := range f.properties.Generated_partition_types { |
| 226 | ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, f.generatedModuleNameForPartition(ctx.Config(), partitionType)) |
| 227 | } |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 231 | if ctx.ModuleDir() != "build/soong/fsgen" { |
| 232 | ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen") |
| 233 | } |
| 234 | f.HideFromMake() |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 235 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 236 | var diffTestFiles []android.Path |
| 237 | for _, partitionType := range f.properties.Generated_partition_types { |
| 238 | diffTestFiles = append(diffTestFiles, f.createDiffTest(ctx, partitionType)) |
| 239 | } |
| 240 | for _, partitionType := range f.properties.Unsupported_partition_types { |
| 241 | diffTestFiles = append(diffTestFiles, createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))) |
| 242 | } |
| 243 | ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 244 | } |