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