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