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 ( |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 18 | "crypto/sha256" |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 19 | "fmt" |
Spandan Das | 312cc41 | 2024-10-29 18:20:11 +0000 | [diff] [blame] | 20 | "path/filepath" |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 21 | "strconv" |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 22 | "strings" |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 23 | |
| 24 | "android/soong/android" |
| 25 | "android/soong/filesystem" |
Spandan Das | 5e33642 | 2024-11-01 22:31:20 +0000 | [diff] [blame] | 26 | "android/soong/kernel" |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 27 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint" |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 29 | "github.com/google/blueprint/parser" |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 30 | "github.com/google/blueprint/proptools" |
| 31 | ) |
| 32 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 33 | var pctx = android.NewPackageContext("android/soong/fsgen") |
| 34 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 35 | func init() { |
| 36 | registerBuildComponents(android.InitRegistrationContext) |
| 37 | } |
| 38 | |
| 39 | func registerBuildComponents(ctx android.RegistrationContext) { |
| 40 | ctx.RegisterModuleType("soong_filesystem_creator", filesystemCreatorFactory) |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 41 | ctx.PreDepsMutators(RegisterCollectFileSystemDepsMutators) |
| 42 | } |
| 43 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 44 | type filesystemCreatorProps struct { |
| 45 | Generated_partition_types []string `blueprint:"mutated"` |
| 46 | Unsupported_partition_types []string `blueprint:"mutated"` |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 47 | |
| 48 | Vbmeta_module_names []string `blueprint:"mutated"` |
| 49 | Vbmeta_partition_names []string `blueprint:"mutated"` |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 52 | type filesystemCreator struct { |
| 53 | android.ModuleBase |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 54 | |
| 55 | properties filesystemCreatorProps |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func filesystemCreatorFactory() android.Module { |
| 59 | module := &filesystemCreator{} |
| 60 | |
Cole Faust | 6978879 | 2024-10-10 11:00:36 -0700 | [diff] [blame] | 61 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 62 | module.AddProperties(&module.properties) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 63 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
Jihoon Kang | 675d468 | 2024-10-24 23:45:11 +0000 | [diff] [blame] | 64 | generatedPrebuiltEtcModuleNames := createPrebuiltEtcModules(ctx) |
| 65 | createFsGenState(ctx, generatedPrebuiltEtcModuleNames) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 66 | module.createInternalModules(ctx) |
| 67 | }) |
| 68 | |
| 69 | return module |
| 70 | } |
| 71 | |
| 72 | func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) { |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 73 | soongGeneratedPartitions := generatedPartitions(ctx) |
| 74 | finalSoongGeneratedPartitions := make([]string, 0, len(soongGeneratedPartitions)) |
| 75 | for _, partitionType := range soongGeneratedPartitions { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 76 | if f.createPartition(ctx, partitionType) { |
| 77 | f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType) |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 78 | finalSoongGeneratedPartitions = append(finalSoongGeneratedPartitions, partitionType) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 79 | } else { |
| 80 | f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType) |
| 81 | } |
| 82 | } |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 83 | |
| 84 | for _, x := range createVbmetaPartitions(ctx, finalSoongGeneratedPartitions) { |
| 85 | f.properties.Vbmeta_module_names = append(f.properties.Vbmeta_module_names, x.moduleName) |
| 86 | f.properties.Vbmeta_partition_names = append(f.properties.Vbmeta_partition_names, x.partitionName) |
| 87 | } |
| 88 | |
| 89 | ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions = finalSoongGeneratedPartitions |
| 90 | f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 93 | func generatedModuleName(cfg android.Config, suffix string) string { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 94 | prefix := "soong" |
| 95 | if cfg.HasDeviceProduct() { |
| 96 | prefix = cfg.DeviceProduct() |
| 97 | } |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 98 | return fmt.Sprintf("%s_generated_%s", prefix, suffix) |
| 99 | } |
| 100 | |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 101 | func generatedModuleNameForPartition(cfg android.Config, partitionType string) string { |
| 102 | return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType)) |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 105 | func (f *filesystemCreator) createDeviceModule( |
| 106 | ctx android.LoadHookContext, |
| 107 | generatedPartitionTypes []string, |
| 108 | vbmetaPartitions []string, |
| 109 | ) { |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 110 | baseProps := &struct { |
| 111 | Name *string |
| 112 | }{ |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 113 | Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")), |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Priyanka Advani (xWF) | dafaa7f | 2024-10-21 22:55:13 +0000 | [diff] [blame] | 116 | // Currently, only the system and system_ext partition module is created. |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 117 | partitionProps := &filesystem.PartitionNameProperties{} |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 118 | if android.InList("system", generatedPartitionTypes) { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 119 | partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system")) |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 120 | } |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 121 | if android.InList("system_ext", generatedPartitionTypes) { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 122 | partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext")) |
Spandan Das | 7a46f6c | 2024-10-14 18:41:18 +0000 | [diff] [blame] | 123 | } |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 124 | if android.InList("vendor", generatedPartitionTypes) { |
Spandan Das | e3b6531 | 2024-10-22 00:27:27 +0000 | [diff] [blame] | 125 | partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor")) |
| 126 | } |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 127 | if android.InList("product", generatedPartitionTypes) { |
Jihoon Kang | 6dd13b6 | 2024-10-22 23:21:02 +0000 | [diff] [blame] | 128 | partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product")) |
| 129 | } |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 130 | if android.InList("odm", generatedPartitionTypes) { |
Spandan Das | c571716 | 2024-11-01 18:33:57 +0000 | [diff] [blame] | 131 | partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm")) |
| 132 | } |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 133 | partitionProps.Vbmeta_partitions = vbmetaPartitions |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 134 | |
| 135 | ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Jihoon Kang | 6850d8f | 2024-10-17 20:45:58 +0000 | [diff] [blame] | 138 | func partitionSpecificFsProps(fsProps *filesystem.FilesystemProperties, partitionType string) { |
| 139 | switch partitionType { |
| 140 | case "system": |
| 141 | fsProps.Build_logtags = proptools.BoolPtr(true) |
| 142 | // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0 |
| 143 | fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true) |
Spandan Das | a8fa6b4 | 2024-10-23 00:45:29 +0000 | [diff] [blame] | 144 | // Identical to that of the generic_system_image |
| 145 | fsProps.Fsverity.Inputs = []string{ |
| 146 | "etc/boot-image.prof", |
| 147 | "etc/dirty-image-objects", |
| 148 | "etc/preloaded-classes", |
| 149 | "etc/classpaths/*.pb", |
| 150 | "framework/*", |
| 151 | "framework/*/*", // framework/{arch} |
| 152 | "framework/oat/*/*", // framework/oat/{arch} |
| 153 | } |
| 154 | fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"} |
| 155 | case "system_ext": |
| 156 | fsProps.Fsverity.Inputs = []string{ |
| 157 | "framework/*", |
| 158 | "framework/*/*", // framework/{arch} |
| 159 | "framework/oat/*/*", // framework/oat/{arch} |
| 160 | } |
| 161 | fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"} |
Jihoon Kang | 6850d8f | 2024-10-17 20:45:58 +0000 | [diff] [blame] | 162 | case "product": |
| 163 | fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true) |
| 164 | case "vendor": |
| 165 | fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true) |
Spandan Das | 69464c3 | 2024-10-25 20:08:06 +0000 | [diff] [blame] | 166 | fsProps.Symlinks = []filesystem.SymlinkDefinition{ |
| 167 | filesystem.SymlinkDefinition{ |
| 168 | Target: proptools.StringPtr("/odm"), |
| 169 | Name: proptools.StringPtr("vendor/odm"), |
| 170 | }, |
| 171 | filesystem.SymlinkDefinition{ |
| 172 | Target: proptools.StringPtr("/vendor_dlkm/lib/modules"), |
| 173 | Name: proptools.StringPtr("vendor/lib/modules"), |
| 174 | }, |
| 175 | } |
| 176 | fsProps.Base_dir = proptools.StringPtr("vendor") |
Spandan Das | c571716 | 2024-11-01 18:33:57 +0000 | [diff] [blame] | 177 | case "odm": |
| 178 | fsProps.Symlinks = []filesystem.SymlinkDefinition{ |
| 179 | filesystem.SymlinkDefinition{ |
| 180 | Target: proptools.StringPtr("/odm_dlkm/lib/modules"), |
| 181 | Name: proptools.StringPtr("odm/lib/modules"), |
| 182 | }, |
| 183 | } |
| 184 | fsProps.Base_dir = proptools.StringPtr("odm") |
| 185 | |
Jihoon Kang | 6850d8f | 2024-10-17 20:45:58 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
Spandan Das | cbe641a | 2024-10-14 21:07:34 +0000 | [diff] [blame] | 188 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 189 | // Creates a soong module to build the given partition. Returns false if we can't support building |
| 190 | // it. |
| 191 | func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool { |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 192 | baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType))) |
| 193 | |
| 194 | fsProps, supported := generateFsProps(ctx, partitionType) |
| 195 | if !supported { |
| 196 | return false |
mrziwang | a077b94 | 2024-10-16 16:00:06 -0700 | [diff] [blame] | 197 | } |
mrziwang | a077b94 | 2024-10-16 16:00:06 -0700 | [diff] [blame] | 198 | |
Spandan Das | 8fe68dc | 2024-10-29 18:20:11 +0000 | [diff] [blame] | 199 | if partitionType == "vendor" || partitionType == "product" { |
Spandan Das | 173256b | 2024-10-31 19:59:30 +0000 | [diff] [blame] | 200 | fsProps.Linkerconfig.Gen_linker_config = proptools.BoolPtr(true) |
| 201 | fsProps.Linkerconfig.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType) |
Spandan Das | 312cc41 | 2024-10-29 18:20:11 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Spandan Das | 5e33642 | 2024-11-01 22:31:20 +0000 | [diff] [blame] | 204 | if partitionType == "system_dlkm" { |
| 205 | kernelModules := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules |
| 206 | f.createPrebuiltKernelModules(ctx, partitionType, kernelModules) |
| 207 | } |
| 208 | |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 209 | var module android.Module |
| 210 | if partitionType == "system" { |
| 211 | module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps) |
Spandan Das | 5e33642 | 2024-11-01 22:31:20 +0000 | [diff] [blame] | 212 | } else if partitionType == "system_dlkm" { |
| 213 | // Do not set partition_type. build/soong/android/paths#modulePartition currently does not support dlkm |
| 214 | // partitions. Since `android_filesystem` uses a partition based filter, setting the partition here |
| 215 | // would result in missing in entries. |
| 216 | module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps) |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 217 | } else { |
| 218 | // Explicitly set the partition. |
| 219 | fsProps.Partition_type = proptools.StringPtr(partitionType) |
| 220 | module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps) |
| 221 | } |
| 222 | module.HideFromMake() |
Spandan Das | 168098c | 2024-10-28 19:44:34 +0000 | [diff] [blame] | 223 | if partitionType == "vendor" { |
Spandan Das | 4cd93b5 | 2024-11-05 23:27:03 +0000 | [diff] [blame] | 224 | f.createVendorBuildProp(ctx) |
Spandan Das | 168098c | 2024-10-28 19:44:34 +0000 | [diff] [blame] | 225 | } |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 226 | return true |
| 227 | } |
| 228 | |
Spandan Das | 5e33642 | 2024-11-01 22:31:20 +0000 | [diff] [blame] | 229 | // createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the |
| 230 | // autogenerated *_dlkm filsystem modules. |
| 231 | // The input `kernelModules` is a space separated list of .ko files in the workspace. This will be partitioned per directory |
| 232 | // and a `prebuilt_kernel_modules` will be created per partition. |
| 233 | // These autogenerated modules will be subsequently added to the deps of the top level *_dlkm android_filesystem |
| 234 | func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string, kernelModules []string) { |
| 235 | // Partition the files per directory |
| 236 | dirToFiles := map[string][]string{} |
| 237 | for _, kernelModule := range kernelModules { |
| 238 | dir := filepath.Dir(kernelModule) |
| 239 | base := filepath.Base(kernelModule) |
| 240 | dirToFiles[dir] = append(dirToFiles[dir], base) |
| 241 | } |
| 242 | // Create a prebuilt_kernel_modules module per partition |
| 243 | fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState) |
| 244 | for index, dir := range android.SortedKeys(dirToFiles) { |
| 245 | name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules-%s", partitionType, strconv.Itoa(index))) |
| 246 | props := &struct { |
| 247 | Name *string |
| 248 | Srcs []string |
| 249 | }{ |
| 250 | Name: proptools.StringPtr(name), |
| 251 | Srcs: dirToFiles[dir], |
| 252 | } |
| 253 | kernelModule := ctx.CreateModuleInDirectory( |
| 254 | kernel.PrebuiltKernelModulesFactory, |
| 255 | dir, |
| 256 | props, |
| 257 | ) |
| 258 | kernelModule.HideFromMake() |
| 259 | // Add to deps |
| 260 | (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config()) |
| 261 | } |
| 262 | } |
| 263 | |
Spandan Das | 4cd93b5 | 2024-11-05 23:27:03 +0000 | [diff] [blame] | 264 | // Create a build_prop and android_info module. This will be used to create /vendor/build.prop |
| 265 | func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) { |
| 266 | // Create a android_info for vendor |
| 267 | // The board info files might be in a directory outside the root soong namespace, so create |
| 268 | // the module in "." |
| 269 | partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 270 | androidInfoProps := &struct { |
| 271 | Name *string |
| 272 | Board_info_files []string |
| 273 | Bootloader_board_name *string |
| 274 | }{ |
| 275 | Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")), |
| 276 | Board_info_files: partitionVars.BoardInfoFiles, |
| 277 | } |
| 278 | if len(androidInfoProps.Board_info_files) == 0 { |
| 279 | androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName) |
| 280 | } |
| 281 | androidInfoProp := ctx.CreateModuleInDirectory( |
| 282 | android.AndroidInfoFactory, |
| 283 | ".", |
| 284 | androidInfoProps, |
| 285 | ) |
| 286 | androidInfoProp.HideFromMake() |
| 287 | // Create a build prop for vendor |
| 288 | vendorBuildProps := &struct { |
| 289 | Name *string |
| 290 | Vendor *bool |
| 291 | Stem *string |
| 292 | Product_config *string |
| 293 | Android_info *string |
| 294 | }{ |
| 295 | Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")), |
| 296 | Vendor: proptools.BoolPtr(true), |
| 297 | Stem: proptools.StringPtr("build.prop"), |
| 298 | Product_config: proptools.StringPtr(":product_config"), |
| 299 | Android_info: proptools.StringPtr(":" + androidInfoProp.Name()), |
| 300 | } |
| 301 | vendorBuildProp := ctx.CreateModule( |
| 302 | android.BuildPropFactory, |
| 303 | vendorBuildProps, |
| 304 | ) |
| 305 | vendorBuildProp.HideFromMake() |
| 306 | } |
| 307 | |
Spandan Das | 8fe68dc | 2024-10-29 18:20:11 +0000 | [diff] [blame] | 308 | // createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions |
| 309 | // 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list) |
| 310 | // 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list) |
| 311 | // It creates a filegroup for each file in the fragment list |
Spandan Das | 312cc41 | 2024-10-29 18:20:11 +0000 | [diff] [blame] | 312 | // The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`. |
Spandan Das | 8fe68dc | 2024-10-29 18:20:11 +0000 | [diff] [blame] | 313 | func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string { |
Spandan Das | 312cc41 | 2024-10-29 18:20:11 +0000 | [diff] [blame] | 314 | ret := []string{} |
| 315 | partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
Spandan Das | 8fe68dc | 2024-10-29 18:20:11 +0000 | [diff] [blame] | 316 | var linkerConfigSrcs []string |
| 317 | if partitionType == "vendor" { |
| 318 | linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs) |
| 319 | } else if partitionType == "product" { |
| 320 | linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs) |
| 321 | } else { |
| 322 | ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`") |
| 323 | } |
| 324 | |
| 325 | if len(linkerConfigSrcs) > 0 { |
Spandan Das | 312cc41 | 2024-10-29 18:20:11 +0000 | [diff] [blame] | 326 | // Create a filegroup, and add `:<filegroup_name>` to ret. |
| 327 | for index, linkerConfigSrc := range linkerConfigSrcs { |
| 328 | dir := filepath.Dir(linkerConfigSrc) |
| 329 | base := filepath.Base(linkerConfigSrc) |
Spandan Das | 8fe68dc | 2024-10-29 18:20:11 +0000 | [diff] [blame] | 330 | fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index))) |
Spandan Das | 312cc41 | 2024-10-29 18:20:11 +0000 | [diff] [blame] | 331 | srcs := []string{base} |
| 332 | fgProps := &struct { |
| 333 | Name *string |
| 334 | Srcs proptools.Configurable[[]string] |
| 335 | }{ |
| 336 | Name: proptools.StringPtr(fgName), |
| 337 | Srcs: proptools.NewSimpleConfigurable(srcs), |
| 338 | } |
| 339 | ctx.CreateModuleInDirectory( |
| 340 | android.FileGroupFactory, |
| 341 | dir, |
| 342 | fgProps, |
| 343 | ) |
| 344 | ret = append(ret, ":"+fgName) |
| 345 | } |
| 346 | } |
| 347 | return ret |
| 348 | } |
| 349 | |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 350 | type filesystemBaseProperty struct { |
| 351 | Name *string |
| 352 | Compile_multilib *string |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 353 | Visibility []string |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | func generateBaseProps(namePtr *string) *filesystemBaseProperty { |
| 357 | return &filesystemBaseProperty{ |
| 358 | Name: namePtr, |
| 359 | Compile_multilib: proptools.StringPtr("both"), |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 360 | // The vbmeta modules are currently in the root directory and depend on the partitions |
| 361 | Visibility: []string{"//.", "//build/soong:__subpackages__"}, |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 366 | fsProps := &filesystem.FilesystemProperties{} |
| 367 | |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 368 | partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 369 | specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType] |
| 370 | |
| 371 | // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE |
| 372 | fsType := specificPartitionVars.BoardFileSystemType |
| 373 | if fsType == "" { |
| 374 | fsType = "ext4" //default |
| 375 | } |
| 376 | fsProps.Type = proptools.StringPtr(fsType) |
| 377 | if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() { |
| 378 | // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs |
| 379 | return nil, false |
| 380 | } |
| 381 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 382 | // Don't build this module on checkbuilds, the soong-built partitions are still in-progress |
| 383 | // and sometimes don't build. |
| 384 | fsProps.Unchecked_module = proptools.BoolPtr(true) |
| 385 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 386 | // BOARD_AVB_ENABLE |
| 387 | fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable) |
| 388 | // BOARD_AVB_KEY_PATH |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 389 | fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 390 | // BOARD_AVB_ALGORITHM |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 391 | fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 392 | // BOARD_AVB_SYSTEM_ROLLBACK_INDEX |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 393 | if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil { |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 394 | fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex) |
| 395 | } |
| 396 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 397 | fsProps.Partition_name = proptools.StringPtr(partitionType) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 398 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 399 | fsProps.Base_dir = proptools.StringPtr(partitionType) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 400 | |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 401 | fsProps.Is_auto_generated = proptools.BoolPtr(true) |
| 402 | |
Jihoon Kang | 6850d8f | 2024-10-17 20:45:58 +0000 | [diff] [blame] | 403 | partitionSpecificFsProps(fsProps, partitionType) |
| 404 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 405 | // system_image properties that are not set: |
| 406 | // - filesystemProperties.Avb_hash_algorithm |
| 407 | // - filesystemProperties.File_contexts |
| 408 | // - filesystemProperties.Dirs |
| 409 | // - filesystemProperties.Symlinks |
| 410 | // - filesystemProperties.Fake_timestamp |
| 411 | // - filesystemProperties.Uuid |
| 412 | // - filesystemProperties.Mount_point |
| 413 | // - filesystemProperties.Include_make_built_files |
| 414 | // - filesystemProperties.Build_logtags |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 415 | // - systemImageProperties.Linker_config_src |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 416 | |
| 417 | return fsProps, true |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 421 | partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 422 | systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag) |
| 423 | filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider) |
| 424 | if !ok { |
| 425 | ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName) |
| 426 | } |
| 427 | makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType)) |
| 428 | // For now, don't allowlist anything. The test will fail, but that's fine in the current |
| 429 | // early stages where we're just figuring out what we need |
Jihoon Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 430 | emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName)) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 431 | android.WriteFileRule(ctx, emptyAllowlistFile, "") |
Jihoon Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 432 | diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName)) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 433 | |
| 434 | builder := android.NewRuleBuilder(pctx, ctx) |
| 435 | builder.Command().BuiltTool("file_list_diff"). |
| 436 | Input(makeFileList). |
| 437 | Input(filesystemInfo.FileListFile). |
Jihoon Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 438 | Text(partitionModuleName). |
| 439 | FlagWithInput("--allowlists ", emptyAllowlistFile) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 440 | builder.Command().Text("touch").Output(diffTestResultFile) |
| 441 | builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test") |
| 442 | return diffTestResultFile |
| 443 | } |
| 444 | |
| 445 | func createFailingCommand(ctx android.ModuleContext, message string) android.Path { |
| 446 | hasher := sha256.New() |
| 447 | hasher.Write([]byte(message)) |
| 448 | filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil)) |
| 449 | file := android.PathForModuleOut(ctx, filename) |
| 450 | builder := android.NewRuleBuilder(pctx, ctx) |
| 451 | builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message)) |
| 452 | builder.Command().Text("exit 1 #").Output(file) |
| 453 | builder.Build("failing command "+filename, "failing command "+filename) |
| 454 | return file |
| 455 | } |
| 456 | |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 457 | func createVbmetaDiff(ctx android.ModuleContext, vbmetaModuleName string, vbmetaPartitionName string) android.Path { |
| 458 | vbmetaModule := ctx.GetDirectDepWithTag(vbmetaModuleName, generatedVbmetaPartitionDepTag) |
| 459 | outputFilesProvider, ok := android.OtherModuleProvider(ctx, vbmetaModule, android.OutputFilesProvider) |
| 460 | if !ok { |
| 461 | ctx.ModuleErrorf("Expected module %s to provide OutputFiles", vbmetaModule) |
| 462 | } |
| 463 | if len(outputFilesProvider.DefaultOutputFiles) != 1 { |
| 464 | ctx.ModuleErrorf("Expected 1 output file from module %s", vbmetaModule) |
| 465 | } |
| 466 | soongVbMetaFile := outputFilesProvider.DefaultOutputFiles[0] |
| 467 | makeVbmetaFile := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/%s.img", ctx.Config().DeviceName(), vbmetaPartitionName)) |
| 468 | |
| 469 | diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", vbmetaModuleName)) |
| 470 | builder := android.NewRuleBuilder(pctx, ctx) |
| 471 | builder.Command().Text("diff"). |
| 472 | Input(soongVbMetaFile). |
| 473 | Input(makeVbmetaFile) |
| 474 | builder.Command().Text("touch").Output(diffTestResultFile) |
| 475 | builder.Build(vbmetaModuleName+" diff test", vbmetaModuleName+" diff test") |
| 476 | return diffTestResultFile |
| 477 | } |
| 478 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 479 | type systemImageDepTagType struct { |
| 480 | blueprint.BaseDependencyTag |
| 481 | } |
| 482 | |
| 483 | var generatedFilesystemDepTag systemImageDepTagType |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 484 | var generatedVbmetaPartitionDepTag systemImageDepTagType |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 485 | |
| 486 | func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 487 | for _, partitionType := range f.properties.Generated_partition_types { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 488 | ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType)) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 489 | } |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 490 | for _, vbmetaModule := range f.properties.Vbmeta_module_names { |
| 491 | ctx.AddDependency(ctx.Module(), generatedVbmetaPartitionDepTag, vbmetaModule) |
| 492 | } |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 496 | if ctx.ModuleDir() != "build/soong/fsgen" { |
| 497 | ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen") |
| 498 | } |
| 499 | f.HideFromMake() |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 500 | |
Jihoon Kang | 4e5d8de | 2024-10-19 01:59:58 +0000 | [diff] [blame] | 501 | var content strings.Builder |
| 502 | generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp") |
| 503 | for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions { |
| 504 | content.WriteString(generateBpContent(ctx, partition)) |
| 505 | content.WriteString("\n") |
| 506 | } |
| 507 | android.WriteFileRule(ctx, generatedBp, content.String()) |
| 508 | |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 509 | ctx.Phony("product_config_to_bp", generatedBp) |
| 510 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 511 | var diffTestFiles []android.Path |
| 512 | for _, partitionType := range f.properties.Generated_partition_types { |
Jihoon Kang | 72f812f | 2024-10-17 18:46:24 +0000 | [diff] [blame] | 513 | diffTestFile := f.createDiffTest(ctx, partitionType) |
| 514 | diffTestFiles = append(diffTestFiles, diffTestFile) |
| 515 | ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 516 | } |
| 517 | for _, partitionType := range f.properties.Unsupported_partition_types { |
Jihoon Kang | 72f812f | 2024-10-17 18:46:24 +0000 | [diff] [blame] | 518 | diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType)) |
| 519 | diffTestFiles = append(diffTestFiles, diffTestFile) |
| 520 | ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 521 | } |
Cole Faust | 1fa1c6d | 2024-11-06 18:07:26 -0800 | [diff] [blame^] | 522 | for i, vbmetaModule := range f.properties.Vbmeta_module_names { |
| 523 | diffTestFile := createVbmetaDiff(ctx, vbmetaModule, f.properties.Vbmeta_partition_names[i]) |
| 524 | diffTestFiles = append(diffTestFiles, diffTestFile) |
| 525 | ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", f.properties.Vbmeta_partition_names[i]), diffTestFile) |
| 526 | } |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 527 | ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 528 | } |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 529 | |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 530 | func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string { |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 531 | fsProps, fsTypeSupported := generateFsProps(ctx, partitionType) |
| 532 | if !fsTypeSupported { |
| 533 | return "" |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 534 | } |
| 535 | |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 536 | baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType))) |
mrziwang | 2a506cf | 2024-10-17 15:38:37 -0700 | [diff] [blame] | 537 | deps := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).fsDeps[partitionType] |
| 538 | depProps := generateDepStruct(*deps) |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 539 | |
mrziwang | 4b0ca97 | 2024-10-17 14:56:19 -0700 | [diff] [blame] | 540 | result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps}) |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 541 | if err != nil { |
Cole Faust | ae3e1d3 | 2024-11-05 13:22:50 -0800 | [diff] [blame] | 542 | ctx.ModuleErrorf("%s", err.Error()) |
| 543 | return "" |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Jihoon Kang | 4e5d8de | 2024-10-19 01:59:58 +0000 | [diff] [blame] | 546 | moduleType := "android_filesystem" |
| 547 | if partitionType == "system" { |
| 548 | moduleType = "android_system_image" |
| 549 | } |
| 550 | |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 551 | file := &parser.File{ |
| 552 | Defs: []parser.Definition{ |
| 553 | &parser.Module{ |
Jihoon Kang | 4e5d8de | 2024-10-19 01:59:58 +0000 | [diff] [blame] | 554 | Type: moduleType, |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 555 | Map: *result, |
| 556 | }, |
| 557 | }, |
| 558 | } |
| 559 | bytes, err := parser.Print(file) |
| 560 | if err != nil { |
| 561 | ctx.ModuleErrorf(err.Error()) |
| 562 | } |
| 563 | return strings.TrimSpace(string(bytes)) |
| 564 | } |