blob: d5e0c581a6cb71909d89318cfd636e5c349780ee [file] [log] [blame]
Jihoon Kang98047cf2024-10-02 17:13:54 +00001// 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
15package fsgen
16
17import (
Cole Faust92ccbe22024-10-03 14:38:37 -070018 "crypto/sha256"
Jihoon Kang98047cf2024-10-02 17:13:54 +000019 "fmt"
mrziwang8f86c882024-10-03 12:34:33 -070020 "slices"
Jihoon Kang98047cf2024-10-02 17:13:54 +000021 "strconv"
mrziwang8f86c882024-10-03 12:34:33 -070022 "strings"
23 "sync"
24
25 "android/soong/android"
26 "android/soong/filesystem"
Jihoon Kang98047cf2024-10-02 17:13:54 +000027
Cole Faust92ccbe22024-10-03 14:38:37 -070028 "github.com/google/blueprint"
mrziwang8f86c882024-10-03 12:34:33 -070029 "github.com/google/blueprint/parser"
Jihoon Kang98047cf2024-10-02 17:13:54 +000030 "github.com/google/blueprint/proptools"
31)
32
Cole Faust92ccbe22024-10-03 14:38:37 -070033var pctx = android.NewPackageContext("android/soong/fsgen")
34
Jihoon Kang98047cf2024-10-02 17:13:54 +000035func init() {
36 registerBuildComponents(android.InitRegistrationContext)
37}
38
39func registerBuildComponents(ctx android.RegistrationContext) {
40 ctx.RegisterModuleType("soong_filesystem_creator", filesystemCreatorFactory)
mrziwang8f86c882024-10-03 12:34:33 -070041 ctx.PreDepsMutators(RegisterCollectFileSystemDepsMutators)
42}
43
44func RegisterCollectFileSystemDepsMutators(ctx android.RegisterMutatorsContext) {
45 ctx.BottomUp("fs_collect_deps", collectDepsMutator).MutatesGlobalState()
46}
47
mrziwangc7e58c92024-10-11 09:49:48 -070048var fsDepsMutex = sync.Mutex{}
mrziwang8f86c882024-10-03 12:34:33 -070049var collectFsDepsOnceKey = android.NewOnceKey("CollectFsDeps")
50var depCandidatesOnceKey = android.NewOnceKey("DepCandidates")
51
52func collectDepsMutator(mctx android.BottomUpMutatorContext) {
53 // These additional deps are added according to the cuttlefish system image bp.
54 fsDeps := mctx.Config().Once(collectFsDepsOnceKey, func() interface{} {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +000055 deps := []string{
56 "android_vintf_manifest",
57 "com.android.apex.cts.shim.v1_prebuilt",
58 "dex_bootjars",
59 "framework_compatibility_matrix.device.xml",
60 "idc_data",
61 "init.environ.rc-soong",
62 "keychars_data",
63 "keylayout_data",
64 "libclang_rt.asan",
65 "libcompiler_rt",
66 "libdmabufheap",
67 "libgsi",
68 "llndk.libraries.txt",
69 "logpersist.start",
70 "preloaded-classes",
71 "public.libraries.android.txt",
72 "update_engine_sideload",
mrziwang8f86c882024-10-03 12:34:33 -070073 }
74 return &deps
Jihoon Kangac2d1ba2024-10-12 01:44:47 +000075 }).(*[]string)
mrziwang8f86c882024-10-03 12:34:33 -070076
77 depCandidates := mctx.Config().Once(depCandidatesOnceKey, func() interface{} {
78 partitionVars := mctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
79 candidates := slices.Concat(partitionVars.ProductPackages, partitionVars.ProductPackagesDebug)
80 return &candidates
81 }).(*[]string)
82
83 m := mctx.Module()
mrziwang8f86c882024-10-03 12:34:33 -070084 if slices.Contains(*depCandidates, m.Name()) {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +000085 if installInSystem(mctx, m) {
86 fsDepsMutex.Lock()
87 *fsDeps = append(*fsDeps, m.Name())
88 fsDepsMutex.Unlock()
mrziwang8f86c882024-10-03 12:34:33 -070089 }
90 }
Jihoon Kang98047cf2024-10-02 17:13:54 +000091}
92
Cole Faust92ccbe22024-10-03 14:38:37 -070093type filesystemCreatorProps struct {
94 Generated_partition_types []string `blueprint:"mutated"`
95 Unsupported_partition_types []string `blueprint:"mutated"`
96}
97
Jihoon Kang98047cf2024-10-02 17:13:54 +000098type filesystemCreator struct {
99 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -0700100
101 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +0000102}
103
104func filesystemCreatorFactory() android.Module {
105 module := &filesystemCreator{}
106
Cole Faust69788792024-10-10 11:00:36 -0700107 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -0700108 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000109 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
110 module.createInternalModules(ctx)
111 })
112
113 return module
114}
115
116func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Spandan Das7a46f6c2024-10-14 18:41:18 +0000117 partitionTypes := []string{"system"}
118 if ctx.DeviceConfig().SystemExtPath() == "system_ext" { // system_ext exists
119 partitionTypes = append(partitionTypes, "system_ext")
120 }
121 for _, partitionType := range partitionTypes {
Cole Faust92ccbe22024-10-03 14:38:37 -0700122 if f.createPartition(ctx, partitionType) {
123 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
124 } else {
125 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
126 }
127 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000128 f.createDeviceModule(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000129}
130
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000131func (f *filesystemCreator) generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700132 prefix := "soong"
133 if cfg.HasDeviceProduct() {
134 prefix = cfg.DeviceProduct()
135 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000136 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
137}
138
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000139func (f *filesystemCreator) generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
140 return f.generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000141}
142
143func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
144 baseProps := &struct {
145 Name *string
146 }{
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000147 Name: proptools.StringPtr(f.generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000148 }
149
Spandan Das7a46f6c2024-10-14 18:41:18 +0000150 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000151 partitionProps := &filesystem.PartitionNameProperties{}
152 if android.InList("system", f.properties.Generated_partition_types) {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000153 partitionProps.System_partition_name = proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000154 }
Spandan Das7a46f6c2024-10-14 18:41:18 +0000155 if android.InList("system_ext", f.properties.Generated_partition_types) {
156 partitionProps.System_ext_partition_name = proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), "system_ext"))
157 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000158
159 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700160}
161
Spandan Dascbe641a2024-10-14 21:07:34 +0000162var (
163 // https://source.corp.google.com/h/googleplex-android/platform/build/+/639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
164 partitionsWithAconfig = []string{"system", "product", "vendor"}
165)
166
Cole Faust92ccbe22024-10-03 14:38:37 -0700167// Creates a soong module to build the given partition. Returns false if we can't support building
168// it.
169func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000170 baseProps := &struct {
171 Name *string
172 }{
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000173 Name: proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), partitionType)),
Jihoon Kang98047cf2024-10-02 17:13:54 +0000174 }
175
Cole Faust92ccbe22024-10-03 14:38:37 -0700176 fsProps := &filesystem.FilesystemProperties{}
177
178 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
179 // and sometimes don't build.
180 fsProps.Unchecked_module = proptools.BoolPtr(true)
181
Jihoon Kang98047cf2024-10-02 17:13:54 +0000182 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust92ccbe22024-10-03 14:38:37 -0700183 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Jihoon Kang98047cf2024-10-02 17:13:54 +0000184
185 // BOARD_AVB_ENABLE
186 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
187 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700188 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000189 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700190 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000191 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700192 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000193 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
194 }
195
Cole Faust92ccbe22024-10-03 14:38:37 -0700196 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000197 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
Spandan Das7a46f6c2024-10-14 18:41:18 +0000198 fsType := specificPartitionVars.BoardFileSystemType
199 if fsType == "" {
200 fsType = "ext4" //default
201 }
202 fsProps.Type = proptools.StringPtr(fsType)
203 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
204 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
Cole Faust92ccbe22024-10-03 14:38:37 -0700205 return false
206 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000207
Cole Faust92ccbe22024-10-03 14:38:37 -0700208 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000209
Spandan Dascbe641a2024-10-14 21:07:34 +0000210 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(android.InList(partitionType, partitionsWithAconfig))
Jihoon Kang98047cf2024-10-02 17:13:54 +0000211
212 // Identical to that of the generic_system_image
213 fsProps.Fsverity.Inputs = []string{
214 "etc/boot-image.prof",
215 "etc/dirty-image-objects",
216 "etc/preloaded-classes",
217 "etc/classpaths/*.pb",
218 "framework/*",
219 "framework/*/*", // framework/{arch}
220 "framework/oat/*/*", // framework/oat/{arch}
221 }
222
223 // system_image properties that are not set:
224 // - filesystemProperties.Avb_hash_algorithm
225 // - filesystemProperties.File_contexts
226 // - filesystemProperties.Dirs
227 // - filesystemProperties.Symlinks
228 // - filesystemProperties.Fake_timestamp
229 // - filesystemProperties.Uuid
230 // - filesystemProperties.Mount_point
231 // - filesystemProperties.Include_make_built_files
232 // - filesystemProperties.Build_logtags
233 // - filesystemProperties.Fsverity.Libs
234 // - systemImageProperties.Linker_config_src
Cole Faust92ccbe22024-10-03 14:38:37 -0700235 var module android.Module
236 if partitionType == "system" {
237 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
238 } else {
Spandan Dascbe641a2024-10-14 21:07:34 +0000239 // Explicitly set the partition.
240 fsProps.Partition_type = proptools.StringPtr(partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700241 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
242 }
243 module.HideFromMake()
244 return true
245}
246
247func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000248 partitionModuleName := f.generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700249 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
250 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
251 if !ok {
252 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
253 }
254 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
255 // For now, don't allowlist anything. The test will fail, but that's fine in the current
256 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000257 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700258 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000259 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700260
261 builder := android.NewRuleBuilder(pctx, ctx)
262 builder.Command().BuiltTool("file_list_diff").
263 Input(makeFileList).
264 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000265 Text(partitionModuleName).
266 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700267 builder.Command().Text("touch").Output(diffTestResultFile)
268 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
269 return diffTestResultFile
270}
271
272func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
273 hasher := sha256.New()
274 hasher.Write([]byte(message))
275 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
276 file := android.PathForModuleOut(ctx, filename)
277 builder := android.NewRuleBuilder(pctx, ctx)
278 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
279 builder.Command().Text("exit 1 #").Output(file)
280 builder.Build("failing command "+filename, "failing command "+filename)
281 return file
282}
283
284type systemImageDepTagType struct {
285 blueprint.BaseDependencyTag
286}
287
288var generatedFilesystemDepTag systemImageDepTagType
289
290func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
291 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000292 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, f.generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700293 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000294}
295
296func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700297 if ctx.ModuleDir() != "build/soong/fsgen" {
298 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
299 }
300 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000301
mrziwang8f86c882024-10-03 12:34:33 -0700302 content := generateBpContent(ctx, "system")
303 generatedBp := android.PathForOutput(ctx, "soong_generated_product_config.bp")
304 android.WriteFileRule(ctx, generatedBp, content)
305 ctx.Phony("product_config_to_bp", generatedBp)
306
Cole Faust92ccbe22024-10-03 14:38:37 -0700307 var diffTestFiles []android.Path
308 for _, partitionType := range f.properties.Generated_partition_types {
309 diffTestFiles = append(diffTestFiles, f.createDiffTest(ctx, partitionType))
310 }
311 for _, partitionType := range f.properties.Unsupported_partition_types {
312 diffTestFiles = append(diffTestFiles, createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType)))
313 }
314 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000315}
mrziwang8f86c882024-10-03 12:34:33 -0700316
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000317func installInSystem(ctx android.BottomUpMutatorContext, m android.Module) bool {
318 return m.PartitionTag(ctx.DeviceConfig()) == "system" && !m.InstallInData() &&
319 !m.InstallInTestcases() && !m.InstallInSanitizerDir() && !m.InstallInVendorRamdisk() &&
320 !m.InstallInDebugRamdisk() && !m.InstallInRecovery() && !m.InstallInOdm() &&
321 !m.InstallInVendor()
mrziwang8f86c882024-10-03 12:34:33 -0700322}
323
324// TODO: assemble baseProps and fsProps here
325func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
326 // Currently only system partition is supported
327 if partitionType != "system" {
328 return ""
329 }
330
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000331 deps := ctx.Config().Get(collectFsDepsOnceKey).(*[]string)
mrziwang8f86c882024-10-03 12:34:33 -0700332 depProps := &android.PackagingProperties{
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000333 Deps: android.NewSimpleConfigurable(android.SortedUniqueStrings(*deps)),
mrziwang8f86c882024-10-03 12:34:33 -0700334 }
335
336 result, err := proptools.RepackProperties([]interface{}{depProps})
337 if err != nil {
338 ctx.ModuleErrorf(err.Error())
339 }
340
341 file := &parser.File{
342 Defs: []parser.Definition{
343 &parser.Module{
344 Type: "module",
345 Map: *result,
346 },
347 },
348 }
349 bytes, err := parser.Print(file)
350 if err != nil {
351 ctx.ModuleErrorf(err.Error())
352 }
353 return strings.TrimSpace(string(bytes))
354}