blob: ed0c390baed3cec37f159362d45d077a2d753fe6 [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) {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000117 for _, partitionType := range []string{"system"} {
Cole Faust92ccbe22024-10-03 14:38:37 -0700118 if f.createPartition(ctx, partitionType) {
119 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
120 } else {
121 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
122 }
123 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000124 f.createDeviceModule(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000125}
126
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000127func (f *filesystemCreator) generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700128 prefix := "soong"
129 if cfg.HasDeviceProduct() {
130 prefix = cfg.DeviceProduct()
131 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000132 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
133}
134
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000135func (f *filesystemCreator) generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
136 return f.generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000137}
138
139func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
140 baseProps := &struct {
141 Name *string
142 }{
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000143 Name: proptools.StringPtr(f.generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000144 }
145
Priyanka Advani (xWF)41e4c992024-10-11 16:53:20 +0000146 // Currently, only the system partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000147 partitionProps := &filesystem.PartitionNameProperties{}
148 if android.InList("system", f.properties.Generated_partition_types) {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000149 partitionProps.System_partition_name = proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000150 }
151
152 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700153}
154
155// Creates a soong module to build the given partition. Returns false if we can't support building
156// it.
157func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000158 baseProps := &struct {
159 Name *string
160 }{
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000161 Name: proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), partitionType)),
Jihoon Kang98047cf2024-10-02 17:13:54 +0000162 }
163
Cole Faust92ccbe22024-10-03 14:38:37 -0700164 fsProps := &filesystem.FilesystemProperties{}
165
166 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
167 // and sometimes don't build.
168 fsProps.Unchecked_module = proptools.BoolPtr(true)
169
Jihoon Kang98047cf2024-10-02 17:13:54 +0000170 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust92ccbe22024-10-03 14:38:37 -0700171 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Jihoon Kang98047cf2024-10-02 17:13:54 +0000172
173 // BOARD_AVB_ENABLE
174 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
175 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700176 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000177 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700178 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000179 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700180 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000181 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
182 }
183
Cole Faust92ccbe22024-10-03 14:38:37 -0700184 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000185 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
Priyanka Advani (xWF)41e4c992024-10-11 16:53:20 +0000186 fsProps.Type = proptools.StringPtr(specificPartitionVars.BoardFileSystemType)
187 if *fsProps.Type != "ext4" {
188 // TODO(b/372522486): Support other FS types.
189 // Currently the android_filesystem module type only supports ext4:
190 // https://cs.android.com/android/platform/superproject/main/+/main:build/soong/filesystem/filesystem.go;l=416;drc=98047cfd07944b297a12d173453bc984806760d2
Cole Faust92ccbe22024-10-03 14:38:37 -0700191 return false
192 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000193
Cole Faust92ccbe22024-10-03 14:38:37 -0700194 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000195
196 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
197
198 // Identical to that of the generic_system_image
199 fsProps.Fsverity.Inputs = []string{
200 "etc/boot-image.prof",
201 "etc/dirty-image-objects",
202 "etc/preloaded-classes",
203 "etc/classpaths/*.pb",
204 "framework/*",
205 "framework/*/*", // framework/{arch}
206 "framework/oat/*/*", // framework/oat/{arch}
207 }
208
209 // system_image properties that are not set:
210 // - filesystemProperties.Avb_hash_algorithm
211 // - filesystemProperties.File_contexts
212 // - filesystemProperties.Dirs
213 // - filesystemProperties.Symlinks
214 // - filesystemProperties.Fake_timestamp
215 // - filesystemProperties.Uuid
216 // - filesystemProperties.Mount_point
217 // - filesystemProperties.Include_make_built_files
218 // - filesystemProperties.Build_logtags
219 // - filesystemProperties.Fsverity.Libs
220 // - systemImageProperties.Linker_config_src
Cole Faust92ccbe22024-10-03 14:38:37 -0700221 var module android.Module
222 if partitionType == "system" {
223 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
224 } else {
225 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
226 }
227 module.HideFromMake()
228 return true
229}
230
231func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000232 partitionModuleName := f.generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700233 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
234 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
235 if !ok {
236 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
237 }
238 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
239 // For now, don't allowlist anything. The test will fail, but that's fine in the current
240 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000241 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700242 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000243 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700244
245 builder := android.NewRuleBuilder(pctx, ctx)
246 builder.Command().BuiltTool("file_list_diff").
247 Input(makeFileList).
248 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000249 Text(partitionModuleName).
250 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700251 builder.Command().Text("touch").Output(diffTestResultFile)
252 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
253 return diffTestResultFile
254}
255
256func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
257 hasher := sha256.New()
258 hasher.Write([]byte(message))
259 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
260 file := android.PathForModuleOut(ctx, filename)
261 builder := android.NewRuleBuilder(pctx, ctx)
262 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
263 builder.Command().Text("exit 1 #").Output(file)
264 builder.Build("failing command "+filename, "failing command "+filename)
265 return file
266}
267
268type systemImageDepTagType struct {
269 blueprint.BaseDependencyTag
270}
271
272var generatedFilesystemDepTag systemImageDepTagType
273
274func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
275 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000276 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, f.generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700277 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000278}
279
280func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700281 if ctx.ModuleDir() != "build/soong/fsgen" {
282 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
283 }
284 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000285
mrziwang8f86c882024-10-03 12:34:33 -0700286 content := generateBpContent(ctx, "system")
287 generatedBp := android.PathForOutput(ctx, "soong_generated_product_config.bp")
288 android.WriteFileRule(ctx, generatedBp, content)
289 ctx.Phony("product_config_to_bp", generatedBp)
290
Cole Faust92ccbe22024-10-03 14:38:37 -0700291 var diffTestFiles []android.Path
292 for _, partitionType := range f.properties.Generated_partition_types {
293 diffTestFiles = append(diffTestFiles, f.createDiffTest(ctx, partitionType))
294 }
295 for _, partitionType := range f.properties.Unsupported_partition_types {
296 diffTestFiles = append(diffTestFiles, createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType)))
297 }
298 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000299}
mrziwang8f86c882024-10-03 12:34:33 -0700300
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000301func installInSystem(ctx android.BottomUpMutatorContext, m android.Module) bool {
302 return m.PartitionTag(ctx.DeviceConfig()) == "system" && !m.InstallInData() &&
303 !m.InstallInTestcases() && !m.InstallInSanitizerDir() && !m.InstallInVendorRamdisk() &&
304 !m.InstallInDebugRamdisk() && !m.InstallInRecovery() && !m.InstallInOdm() &&
305 !m.InstallInVendor()
mrziwang8f86c882024-10-03 12:34:33 -0700306}
307
308// TODO: assemble baseProps and fsProps here
309func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
310 // Currently only system partition is supported
311 if partitionType != "system" {
312 return ""
313 }
314
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000315 deps := ctx.Config().Get(collectFsDepsOnceKey).(*[]string)
mrziwang8f86c882024-10-03 12:34:33 -0700316 depProps := &android.PackagingProperties{
Jihoon Kangac2d1ba2024-10-12 01:44:47 +0000317 Deps: android.NewSimpleConfigurable(android.SortedUniqueStrings(*deps)),
mrziwang8f86c882024-10-03 12:34:33 -0700318 }
319
320 result, err := proptools.RepackProperties([]interface{}{depProps})
321 if err != nil {
322 ctx.ModuleErrorf(err.Error())
323 }
324
325 file := &parser.File{
326 Defs: []parser.Definition{
327 &parser.Module{
328 Type: "module",
329 Map: *result,
330 },
331 },
332 }
333 bytes, err := parser.Print(file)
334 if err != nil {
335 ctx.ModuleErrorf(err.Error())
336 }
337 return strings.TrimSpace(string(bytes))
338}