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" |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 20 | "slices" |
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" |
| 23 | "sync" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | "android/soong/filesystem" |
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 | |
| 44 | func RegisterCollectFileSystemDepsMutators(ctx android.RegisterMutatorsContext) { |
| 45 | ctx.BottomUp("fs_collect_deps", collectDepsMutator).MutatesGlobalState() |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 46 | ctx.BottomUp("fs_set_deps", setDepsMutator) |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 49 | var fsGenStateOnceKey = android.NewOnceKey("FsGenState") |
| 50 | |
| 51 | // Map of partition module name to its partition that may be generated by Soong. |
| 52 | // Note that it is not guaranteed that all modules returned by this function are successfully |
| 53 | // created. |
| 54 | func getAllSoongGeneratedPartitionNames(config android.Config, partitions []string) map[string]string { |
| 55 | ret := map[string]string{} |
| 56 | for _, partition := range partitions { |
| 57 | ret[generatedModuleNameForPartition(config, partition)] = partition |
| 58 | } |
| 59 | return ret |
| 60 | } |
| 61 | |
| 62 | type depCandidateProps struct { |
| 63 | Namespace string |
| 64 | Multilib string |
| 65 | Arch []android.ArchType |
| 66 | } |
| 67 | |
| 68 | // Map of module name to depCandidateProps |
| 69 | type multilibDeps *map[string]*depCandidateProps |
| 70 | |
| 71 | // Information necessary to generate the filesystem modules, including details about their |
| 72 | // dependencies |
| 73 | type FsGenState struct { |
| 74 | // List of modules in `PRODUCT_PACKAGES` and `PRODUCT_PACKAGES_DEBUG` |
| 75 | depCandidates []string |
| 76 | // Map of names of partition to the information of modules to be added as deps |
| 77 | fsDeps map[string]multilibDeps |
| 78 | // List of name of partitions to be generated by the filesystem_creator module |
| 79 | soongGeneratedPartitions []string |
| 80 | // Mutex to protect the fsDeps |
| 81 | fsDepsMutex sync.Mutex |
| 82 | } |
| 83 | |
| 84 | func newMultilibDeps() multilibDeps { |
| 85 | return &map[string]*depCandidateProps{} |
| 86 | } |
| 87 | |
| 88 | func defaultDepCandidateProps(config android.Config) *depCandidateProps { |
| 89 | return &depCandidateProps{ |
| 90 | Namespace: ".", |
| 91 | Arch: []android.ArchType{config.BuildArch}, |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func createFsGenState(ctx android.LoadHookContext) *FsGenState { |
| 96 | return ctx.Config().Once(fsGenStateOnceKey, func() interface{} { |
| 97 | partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 98 | candidates := android.FirstUniqueStrings(android.Concat(partitionVars.ProductPackages, partitionVars.ProductPackagesDebug)) |
| 99 | |
| 100 | generatedPartitions := []string{"system"} |
| 101 | if ctx.DeviceConfig().SystemExtPath() == "system_ext" { |
| 102 | generatedPartitions = append(generatedPartitions, "system_ext") |
| 103 | } |
| 104 | |
| 105 | return &FsGenState{ |
| 106 | depCandidates: candidates, |
| 107 | fsDeps: map[string]multilibDeps{ |
| 108 | // These additional deps are added according to the cuttlefish system image bp. |
| 109 | "system": &map[string]*depCandidateProps{ |
| 110 | "com.android.apex.cts.shim.v1_prebuilt": defaultDepCandidateProps(ctx.Config()), |
| 111 | "dex_bootjars": defaultDepCandidateProps(ctx.Config()), |
| 112 | "framework_compatibility_matrix.device.xml": defaultDepCandidateProps(ctx.Config()), |
| 113 | "idc_data": defaultDepCandidateProps(ctx.Config()), |
| 114 | "init.environ.rc-soong": defaultDepCandidateProps(ctx.Config()), |
| 115 | "keychars_data": defaultDepCandidateProps(ctx.Config()), |
| 116 | "keylayout_data": defaultDepCandidateProps(ctx.Config()), |
| 117 | "libclang_rt.asan": defaultDepCandidateProps(ctx.Config()), |
| 118 | "libcompiler_rt": defaultDepCandidateProps(ctx.Config()), |
| 119 | "libdmabufheap": defaultDepCandidateProps(ctx.Config()), |
| 120 | "libgsi": defaultDepCandidateProps(ctx.Config()), |
| 121 | "llndk.libraries.txt": defaultDepCandidateProps(ctx.Config()), |
| 122 | "logpersist.start": defaultDepCandidateProps(ctx.Config()), |
| 123 | "preloaded-classes": defaultDepCandidateProps(ctx.Config()), |
| 124 | "public.libraries.android.txt": defaultDepCandidateProps(ctx.Config()), |
| 125 | "update_engine_sideload": defaultDepCandidateProps(ctx.Config()), |
| 126 | }, |
| 127 | "vendor": newMultilibDeps(), |
| 128 | "odm": newMultilibDeps(), |
| 129 | "product": newMultilibDeps(), |
| 130 | "system_ext": newMultilibDeps(), |
| 131 | }, |
| 132 | soongGeneratedPartitions: generatedPartitions, |
| 133 | fsDepsMutex: sync.Mutex{}, |
| 134 | } |
| 135 | }).(*FsGenState) |
| 136 | } |
| 137 | |
| 138 | func checkDepModuleInMultipleNamespaces(mctx android.BottomUpMutatorContext, foundDeps map[string]*depCandidateProps, module string, partitionName string) { |
| 139 | otherNamespace := mctx.Namespace().Path |
| 140 | if val, found := foundDeps[module]; found && otherNamespace != "." && !android.InList(val.Namespace, []string{".", otherNamespace}) { |
| 141 | mctx.ModuleErrorf("found in multiple namespaces(%s and %s) when including in %s partition", val.Namespace, otherNamespace, partitionName) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func appendDepIfAppropriate(mctx android.BottomUpMutatorContext, deps *map[string]*depCandidateProps, installPartition string) { |
| 146 | checkDepModuleInMultipleNamespaces(mctx, *deps, mctx.Module().Name(), installPartition) |
| 147 | if _, ok := (*deps)[mctx.Module().Name()]; ok { |
| 148 | // Prefer the namespace-specific module over the platform module |
| 149 | if mctx.Namespace().Path != "." { |
| 150 | (*deps)[mctx.Module().Name()].Namespace = mctx.Namespace().Path |
| 151 | } |
| 152 | (*deps)[mctx.Module().Name()].Arch = append((*deps)[mctx.Module().Name()].Arch, mctx.Module().Target().Arch.ArchType) |
| 153 | } else { |
| 154 | multilib, _ := mctx.Module().DecodeMultilib(mctx) |
| 155 | (*deps)[mctx.Module().Name()] = &depCandidateProps{ |
| 156 | Namespace: mctx.Namespace().Path, |
| 157 | Multilib: multilib, |
| 158 | Arch: []android.ArchType{mctx.Module().Target().Arch.ArchType}, |
| 159 | } |
| 160 | } |
| 161 | } |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 162 | |
| 163 | func collectDepsMutator(mctx android.BottomUpMutatorContext) { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 164 | fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState) |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 165 | |
| 166 | m := mctx.Module() |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 167 | if slices.Contains(fsGenState.depCandidates, m.Name()) { |
| 168 | installPartition := m.PartitionTag(mctx.DeviceConfig()) |
| 169 | fsGenState.fsDepsMutex.Lock() |
| 170 | // Only add the module as dependency when: |
| 171 | // - its enabled |
| 172 | // - its namespace is included in PRODUCT_SOONG_NAMESPACES |
| 173 | if m.Enabled(mctx) && m.ExportedToMake() { |
| 174 | appendDepIfAppropriate(mctx, fsGenState.fsDeps[installPartition], installPartition) |
| 175 | } |
| 176 | fsGenState.fsDepsMutex.Unlock() |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | type depsStruct struct { |
| 181 | Deps []string |
| 182 | } |
| 183 | |
| 184 | type multilibDepsStruct struct { |
| 185 | Common depsStruct |
| 186 | Lib32 depsStruct |
| 187 | Lib64 depsStruct |
| 188 | Both depsStruct |
| 189 | Prefer32 depsStruct |
| 190 | } |
| 191 | |
| 192 | type packagingPropsStruct struct { |
| 193 | Deps []string |
| 194 | Multilib multilibDepsStruct |
| 195 | } |
| 196 | |
| 197 | func fullyQualifiedModuleName(moduleName, namespace string) string { |
| 198 | if namespace == "." { |
| 199 | return moduleName |
| 200 | } |
| 201 | return fmt.Sprintf("//%s:%s", namespace, moduleName) |
| 202 | } |
| 203 | |
| 204 | // Returns the sorted unique list of module names with namespace, if the module specifies one. |
| 205 | func fullyQualifiedModuleNames(modules multilibDeps) (ret []string) { |
| 206 | for moduleName, moduleProp := range *modules { |
| 207 | ret = append(ret, fullyQualifiedModuleName(moduleName, moduleProp.Namespace)) |
| 208 | } |
| 209 | return android.SortedUniqueStrings(ret) |
| 210 | } |
| 211 | |
| 212 | func getBitness(archTypes []android.ArchType) (ret []string) { |
| 213 | for _, archType := range archTypes { |
| 214 | if archType.Multilib == "" { |
| 215 | ret = append(ret, android.COMMON_VARIANT) |
| 216 | } else { |
| 217 | ret = append(ret, archType.Bitness()) |
| 218 | } |
| 219 | } |
| 220 | return ret |
| 221 | } |
| 222 | |
| 223 | func setDepsMutator(mctx android.BottomUpMutatorContext) { |
| 224 | fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState) |
| 225 | fsDeps := fsGenState.fsDeps |
| 226 | soongGeneratedPartitionMap := getAllSoongGeneratedPartitionNames(mctx.Config(), fsGenState.soongGeneratedPartitions) |
| 227 | m := mctx.Module() |
| 228 | if partition, ok := soongGeneratedPartitionMap[m.Name()]; ok { |
| 229 | depsStruct := packagingPropsStruct{} |
| 230 | for depName, depProps := range *fsDeps[partition] { |
| 231 | bitness := getBitness(depProps.Arch) |
| 232 | fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace) |
| 233 | if android.InList("32", bitness) && android.InList("64", bitness) { |
| 234 | // If both 32 and 64 bit variants are enabled for this module |
| 235 | switch depProps.Multilib { |
| 236 | case string(android.MultilibBoth): |
| 237 | depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName) |
| 238 | case string(android.MultilibCommon), string(android.MultilibFirst): |
| 239 | depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName) |
| 240 | case "32": |
| 241 | depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName) |
| 242 | case "64", "darwin_universal": |
| 243 | depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName) |
| 244 | case "prefer32", "first_prefer32": |
| 245 | depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName) |
| 246 | default: |
| 247 | depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName) |
| 248 | } |
| 249 | } else if android.InList("64", bitness) { |
| 250 | // If only 64 bit variant is enabled |
| 251 | depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName) |
| 252 | } else if android.InList("32", bitness) { |
| 253 | // If only 32 bit variant is enabled |
| 254 | depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName) |
| 255 | } else { |
| 256 | // If only common variant is enabled |
| 257 | depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName) |
| 258 | } |
| 259 | } |
| 260 | if err := proptools.AppendMatchingProperties(m.GetProperties(), &depsStruct, nil); err != nil { |
| 261 | mctx.ModuleErrorf(err.Error()) |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 266 | type filesystemCreatorProps struct { |
| 267 | Generated_partition_types []string `blueprint:"mutated"` |
| 268 | Unsupported_partition_types []string `blueprint:"mutated"` |
| 269 | } |
| 270 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 271 | type filesystemCreator struct { |
| 272 | android.ModuleBase |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 273 | |
| 274 | properties filesystemCreatorProps |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | func filesystemCreatorFactory() android.Module { |
| 278 | module := &filesystemCreator{} |
| 279 | |
Cole Faust | 6978879 | 2024-10-10 11:00:36 -0700 | [diff] [blame] | 280 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 281 | module.AddProperties(&module.properties) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 282 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 283 | createFsGenState(ctx) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 284 | module.createInternalModules(ctx) |
| 285 | }) |
| 286 | |
| 287 | return module |
| 288 | } |
| 289 | |
| 290 | func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 291 | soongGeneratedPartitions := &ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions |
| 292 | for _, partitionType := range *soongGeneratedPartitions { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 293 | if f.createPartition(ctx, partitionType) { |
| 294 | f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType) |
| 295 | } else { |
| 296 | f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType) |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 297 | _, *soongGeneratedPartitions = android.RemoveFromList(partitionType, *soongGeneratedPartitions) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 300 | f.createDeviceModule(ctx) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 303 | func generatedModuleName(cfg android.Config, suffix string) string { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 304 | prefix := "soong" |
| 305 | if cfg.HasDeviceProduct() { |
| 306 | prefix = cfg.DeviceProduct() |
| 307 | } |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 308 | return fmt.Sprintf("%s_generated_%s", prefix, suffix) |
| 309 | } |
| 310 | |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 311 | func generatedModuleNameForPartition(cfg android.Config, partitionType string) string { |
| 312 | return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType)) |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) { |
| 316 | baseProps := &struct { |
| 317 | Name *string |
| 318 | }{ |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 319 | Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")), |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Spandan Das | 7a46f6c | 2024-10-14 18:41:18 +0000 | [diff] [blame] | 322 | // Currently, only the system and system_ext partition module is created. |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 323 | partitionProps := &filesystem.PartitionNameProperties{} |
| 324 | if android.InList("system", f.properties.Generated_partition_types) { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 325 | partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system")) |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 326 | } |
Spandan Das | 7a46f6c | 2024-10-14 18:41:18 +0000 | [diff] [blame] | 327 | if android.InList("system_ext", f.properties.Generated_partition_types) { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 328 | partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext")) |
Spandan Das | 7a46f6c | 2024-10-14 18:41:18 +0000 | [diff] [blame] | 329 | } |
Jihoon Kang | f1c79ca | 2024-10-09 20:18:38 +0000 | [diff] [blame] | 330 | |
| 331 | ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Spandan Das | cbe641a | 2024-10-14 21:07:34 +0000 | [diff] [blame] | 334 | var ( |
| 335 | // https://source.corp.google.com/h/googleplex-android/platform/build/+/639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0 |
| 336 | partitionsWithAconfig = []string{"system", "product", "vendor"} |
| 337 | ) |
| 338 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 339 | // Creates a soong module to build the given partition. Returns false if we can't support building |
| 340 | // it. |
| 341 | func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool { |
mrziwang | a077b94 | 2024-10-16 16:00:06 -0700 | [diff] [blame^] | 342 | baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType))) |
| 343 | |
| 344 | fsProps, supported := generateFsProps(ctx, partitionType) |
| 345 | if !supported { |
| 346 | return false |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 347 | } |
| 348 | |
mrziwang | a077b94 | 2024-10-16 16:00:06 -0700 | [diff] [blame^] | 349 | var module android.Module |
| 350 | if partitionType == "system" { |
| 351 | module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps) |
| 352 | } else { |
| 353 | // Explicitly set the partition. |
| 354 | fsProps.Partition_type = proptools.StringPtr(partitionType) |
| 355 | module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps) |
| 356 | } |
| 357 | module.HideFromMake() |
| 358 | return true |
| 359 | } |
| 360 | |
| 361 | type filesystemBaseProperty struct { |
| 362 | Name *string |
| 363 | Compile_multilib *string |
| 364 | } |
| 365 | |
| 366 | func generateBaseProps(namePtr *string) *filesystemBaseProperty { |
| 367 | return &filesystemBaseProperty{ |
| 368 | Name: namePtr, |
| 369 | Compile_multilib: proptools.StringPtr("both"), |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 374 | fsProps := &filesystem.FilesystemProperties{} |
| 375 | |
mrziwang | a077b94 | 2024-10-16 16:00:06 -0700 | [diff] [blame^] | 376 | partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 377 | specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType] |
| 378 | |
| 379 | // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE |
| 380 | fsType := specificPartitionVars.BoardFileSystemType |
| 381 | if fsType == "" { |
| 382 | fsType = "ext4" //default |
| 383 | } |
| 384 | fsProps.Type = proptools.StringPtr(fsType) |
| 385 | if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() { |
| 386 | // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs |
| 387 | return nil, false |
| 388 | } |
| 389 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 390 | // Don't build this module on checkbuilds, the soong-built partitions are still in-progress |
| 391 | // and sometimes don't build. |
| 392 | fsProps.Unchecked_module = proptools.BoolPtr(true) |
| 393 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 394 | // BOARD_AVB_ENABLE |
| 395 | fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable) |
| 396 | // BOARD_AVB_KEY_PATH |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 397 | fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 398 | // BOARD_AVB_ALGORITHM |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 399 | fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 400 | // BOARD_AVB_SYSTEM_ROLLBACK_INDEX |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 401 | if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil { |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 402 | fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex) |
| 403 | } |
| 404 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 405 | fsProps.Partition_name = proptools.StringPtr(partitionType) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 406 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 407 | fsProps.Base_dir = proptools.StringPtr(partitionType) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 408 | |
Spandan Das | cbe641a | 2024-10-14 21:07:34 +0000 | [diff] [blame] | 409 | fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(android.InList(partitionType, partitionsWithAconfig)) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 410 | |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 411 | fsProps.Is_auto_generated = proptools.BoolPtr(true) |
| 412 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 413 | // Identical to that of the generic_system_image |
| 414 | fsProps.Fsverity.Inputs = []string{ |
| 415 | "etc/boot-image.prof", |
| 416 | "etc/dirty-image-objects", |
| 417 | "etc/preloaded-classes", |
| 418 | "etc/classpaths/*.pb", |
| 419 | "framework/*", |
| 420 | "framework/*/*", // framework/{arch} |
| 421 | "framework/oat/*/*", // framework/oat/{arch} |
| 422 | } |
| 423 | |
| 424 | // system_image properties that are not set: |
| 425 | // - filesystemProperties.Avb_hash_algorithm |
| 426 | // - filesystemProperties.File_contexts |
| 427 | // - filesystemProperties.Dirs |
| 428 | // - filesystemProperties.Symlinks |
| 429 | // - filesystemProperties.Fake_timestamp |
| 430 | // - filesystemProperties.Uuid |
| 431 | // - filesystemProperties.Mount_point |
| 432 | // - filesystemProperties.Include_make_built_files |
| 433 | // - filesystemProperties.Build_logtags |
| 434 | // - filesystemProperties.Fsverity.Libs |
| 435 | // - systemImageProperties.Linker_config_src |
mrziwang | a077b94 | 2024-10-16 16:00:06 -0700 | [diff] [blame^] | 436 | |
| 437 | return fsProps, true |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 441 | partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 442 | systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag) |
| 443 | filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider) |
| 444 | if !ok { |
| 445 | ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName) |
| 446 | } |
| 447 | makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType)) |
| 448 | // For now, don't allowlist anything. The test will fail, but that's fine in the current |
| 449 | // early stages where we're just figuring out what we need |
Jihoon Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 450 | emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName)) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 451 | android.WriteFileRule(ctx, emptyAllowlistFile, "") |
Jihoon Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 452 | diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName)) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 453 | |
| 454 | builder := android.NewRuleBuilder(pctx, ctx) |
| 455 | builder.Command().BuiltTool("file_list_diff"). |
| 456 | Input(makeFileList). |
| 457 | Input(filesystemInfo.FileListFile). |
Jihoon Kang | 9e866c8 | 2024-10-07 22:39:18 +0000 | [diff] [blame] | 458 | Text(partitionModuleName). |
| 459 | FlagWithInput("--allowlists ", emptyAllowlistFile) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 460 | builder.Command().Text("touch").Output(diffTestResultFile) |
| 461 | builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test") |
| 462 | return diffTestResultFile |
| 463 | } |
| 464 | |
| 465 | func createFailingCommand(ctx android.ModuleContext, message string) android.Path { |
| 466 | hasher := sha256.New() |
| 467 | hasher.Write([]byte(message)) |
| 468 | filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil)) |
| 469 | file := android.PathForModuleOut(ctx, filename) |
| 470 | builder := android.NewRuleBuilder(pctx, ctx) |
| 471 | builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message)) |
| 472 | builder.Command().Text("exit 1 #").Output(file) |
| 473 | builder.Build("failing command "+filename, "failing command "+filename) |
| 474 | return file |
| 475 | } |
| 476 | |
| 477 | type systemImageDepTagType struct { |
| 478 | blueprint.BaseDependencyTag |
| 479 | } |
| 480 | |
| 481 | var generatedFilesystemDepTag systemImageDepTagType |
| 482 | |
| 483 | func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 484 | for _, partitionType := range f.properties.Generated_partition_types { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 485 | ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType)) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 486 | } |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 490 | if ctx.ModuleDir() != "build/soong/fsgen" { |
| 491 | ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen") |
| 492 | } |
| 493 | f.HideFromMake() |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 494 | |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 495 | content := generateBpContent(ctx, "system") |
| 496 | generatedBp := android.PathForOutput(ctx, "soong_generated_product_config.bp") |
| 497 | android.WriteFileRule(ctx, generatedBp, content) |
| 498 | ctx.Phony("product_config_to_bp", generatedBp) |
| 499 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 500 | var diffTestFiles []android.Path |
| 501 | for _, partitionType := range f.properties.Generated_partition_types { |
| 502 | diffTestFiles = append(diffTestFiles, f.createDiffTest(ctx, partitionType)) |
| 503 | } |
| 504 | for _, partitionType := range f.properties.Unsupported_partition_types { |
| 505 | diffTestFiles = append(diffTestFiles, createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))) |
| 506 | } |
| 507 | ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 508 | } |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 509 | |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 510 | func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string { |
| 511 | // Currently only system partition is supported |
| 512 | if partitionType != "system" { |
| 513 | return "" |
| 514 | } |
| 515 | |
mrziwang | a077b94 | 2024-10-16 16:00:06 -0700 | [diff] [blame^] | 516 | baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType))) |
| 517 | fsProps, _ := generateFsProps(ctx, partitionType) |
| 518 | |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 519 | deps := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).fsDeps |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 520 | depProps := &android.PackagingProperties{ |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 521 | Deps: android.NewSimpleConfigurable(fullyQualifiedModuleNames(deps[partitionType])), |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 522 | } |
| 523 | |
mrziwang | a077b94 | 2024-10-16 16:00:06 -0700 | [diff] [blame^] | 524 | result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps}) |
mrziwang | 8f86c88 | 2024-10-03 12:34:33 -0700 | [diff] [blame] | 525 | if err != nil { |
| 526 | ctx.ModuleErrorf(err.Error()) |
| 527 | } |
| 528 | |
| 529 | file := &parser.File{ |
| 530 | Defs: []parser.Definition{ |
| 531 | &parser.Module{ |
| 532 | Type: "module", |
| 533 | Map: *result, |
| 534 | }, |
| 535 | }, |
| 536 | } |
| 537 | bytes, err := parser.Print(file) |
| 538 | if err != nil { |
| 539 | ctx.ModuleErrorf(err.Error()) |
| 540 | } |
| 541 | return strings.TrimSpace(string(bytes)) |
| 542 | } |