blob: f10b2ba112d20bc4e22aa7504f20d5d1c7797cc8 [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{} {
55 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",
73 }
74 return &deps
75 }).(*[]string)
76
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()) {
85 if installInSystem(mctx, m) {
mrziwangc7e58c92024-10-11 09:49:48 -070086 fsDepsMutex.Lock()
mrziwang8f86c882024-10-03 12:34:33 -070087 *fsDeps = append(*fsDeps, m.Name())
mrziwangc7e58c92024-10-11 09:49:48 -070088 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 Dasefc456a2024-10-10 21:24:34 +0000117 for _, partitionType := range []string{"system", "system_ext"} {
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 Kangf1c79ca2024-10-09 20:18:38 +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
135func (f *filesystemCreator) generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
136 return f.generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
137}
138
139func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
140 baseProps := &struct {
141 Name *string
142 }{
143 Name: proptools.StringPtr(f.generatedModuleName(ctx.Config(), "device")),
144 }
145
Spandan Dasefc456a2024-10-10 21:24:34 +0000146 // Currently, only the system and system_ext 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) {
149 partitionProps.System_partition_name = proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), "system"))
150 }
Spandan Dasefc456a2024-10-10 21:24:34 +0000151 if android.InList("system_ext", f.properties.Generated_partition_types) {
152 partitionProps.System_ext_partition_name = proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), "system_ext"))
153 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000154
155 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700156}
157
158// Creates a soong module to build the given partition. Returns false if we can't support building
159// it.
160func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000161 baseProps := &struct {
162 Name *string
163 }{
Cole Faust92ccbe22024-10-03 14:38:37 -0700164 Name: proptools.StringPtr(f.generatedModuleNameForPartition(ctx.Config(), partitionType)),
Jihoon Kang98047cf2024-10-02 17:13:54 +0000165 }
166
Cole Faust92ccbe22024-10-03 14:38:37 -0700167 fsProps := &filesystem.FilesystemProperties{}
168
169 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
170 // and sometimes don't build.
171 fsProps.Unchecked_module = proptools.BoolPtr(true)
172
Jihoon Kang98047cf2024-10-02 17:13:54 +0000173 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust92ccbe22024-10-03 14:38:37 -0700174 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Jihoon Kang98047cf2024-10-02 17:13:54 +0000175
176 // BOARD_AVB_ENABLE
177 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
178 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700179 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000180 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700181 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000182 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700183 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000184 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
185 }
186
Cole Faust92ccbe22024-10-03 14:38:37 -0700187 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000188 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
Spandan Dasefc456a2024-10-10 21:24:34 +0000189 fsType := specificPartitionVars.BoardFileSystemType
190 if fsType == "" {
191 fsType = "ext4" //default
192 }
193 fsProps.Type = proptools.StringPtr(fsType)
194 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
195 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
Cole Faust92ccbe22024-10-03 14:38:37 -0700196 return false
197 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000198
Cole Faust92ccbe22024-10-03 14:38:37 -0700199 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000200
201 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
202
203 // Identical to that of the generic_system_image
204 fsProps.Fsverity.Inputs = []string{
205 "etc/boot-image.prof",
206 "etc/dirty-image-objects",
207 "etc/preloaded-classes",
208 "etc/classpaths/*.pb",
209 "framework/*",
210 "framework/*/*", // framework/{arch}
211 "framework/oat/*/*", // framework/oat/{arch}
212 }
213
214 // system_image properties that are not set:
215 // - filesystemProperties.Avb_hash_algorithm
216 // - filesystemProperties.File_contexts
217 // - filesystemProperties.Dirs
218 // - filesystemProperties.Symlinks
219 // - filesystemProperties.Fake_timestamp
220 // - filesystemProperties.Uuid
221 // - filesystemProperties.Mount_point
222 // - filesystemProperties.Include_make_built_files
223 // - filesystemProperties.Build_logtags
224 // - filesystemProperties.Fsverity.Libs
225 // - systemImageProperties.Linker_config_src
Cole Faust92ccbe22024-10-03 14:38:37 -0700226 var module android.Module
227 if partitionType == "system" {
228 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
229 } else {
230 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
231 }
232 module.HideFromMake()
233 return true
234}
235
236func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
237 partitionModuleName := f.generatedModuleNameForPartition(ctx.Config(), partitionType)
238 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
239 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
240 if !ok {
241 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
242 }
243 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
244 // For now, don't allowlist anything. The test will fail, but that's fine in the current
245 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000246 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700247 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000248 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700249
250 builder := android.NewRuleBuilder(pctx, ctx)
251 builder.Command().BuiltTool("file_list_diff").
252 Input(makeFileList).
253 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000254 Text(partitionModuleName).
255 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700256 builder.Command().Text("touch").Output(diffTestResultFile)
257 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
258 return diffTestResultFile
259}
260
261func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
262 hasher := sha256.New()
263 hasher.Write([]byte(message))
264 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
265 file := android.PathForModuleOut(ctx, filename)
266 builder := android.NewRuleBuilder(pctx, ctx)
267 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
268 builder.Command().Text("exit 1 #").Output(file)
269 builder.Build("failing command "+filename, "failing command "+filename)
270 return file
271}
272
273type systemImageDepTagType struct {
274 blueprint.BaseDependencyTag
275}
276
277var generatedFilesystemDepTag systemImageDepTagType
278
279func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
280 for _, partitionType := range f.properties.Generated_partition_types {
281 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, f.generatedModuleNameForPartition(ctx.Config(), partitionType))
282 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000283}
284
285func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700286 if ctx.ModuleDir() != "build/soong/fsgen" {
287 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
288 }
289 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000290
mrziwang8f86c882024-10-03 12:34:33 -0700291 content := generateBpContent(ctx, "system")
292 generatedBp := android.PathForOutput(ctx, "soong_generated_product_config.bp")
293 android.WriteFileRule(ctx, generatedBp, content)
294 ctx.Phony("product_config_to_bp", generatedBp)
295
Cole Faust92ccbe22024-10-03 14:38:37 -0700296 var diffTestFiles []android.Path
297 for _, partitionType := range f.properties.Generated_partition_types {
298 diffTestFiles = append(diffTestFiles, f.createDiffTest(ctx, partitionType))
299 }
300 for _, partitionType := range f.properties.Unsupported_partition_types {
301 diffTestFiles = append(diffTestFiles, createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType)))
302 }
303 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000304}
mrziwang8f86c882024-10-03 12:34:33 -0700305
306func installInSystem(ctx android.BottomUpMutatorContext, m android.Module) bool {
307 return m.PartitionTag(ctx.DeviceConfig()) == "system" && !m.InstallInData() &&
308 !m.InstallInTestcases() && !m.InstallInSanitizerDir() && !m.InstallInVendorRamdisk() &&
309 !m.InstallInDebugRamdisk() && !m.InstallInRecovery() && !m.InstallInOdm() &&
310 !m.InstallInVendor()
311}
312
313// TODO: assemble baseProps and fsProps here
314func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
315 // Currently only system partition is supported
316 if partitionType != "system" {
317 return ""
318 }
319
320 deps := ctx.Config().Get(collectFsDepsOnceKey).(*[]string)
321 depProps := &android.PackagingProperties{
322 Deps: android.NewSimpleConfigurable(android.SortedUniqueStrings(*deps)),
323 }
324
325 result, err := proptools.RepackProperties([]interface{}{depProps})
326 if err != nil {
327 ctx.ModuleErrorf(err.Error())
328 }
329
330 file := &parser.File{
331 Defs: []parser.Definition{
332 &parser.Module{
333 Type: "module",
334 Map: *result,
335 },
336 },
337 }
338 bytes, err := parser.Print(file)
339 if err != nil {
340 ctx.ModuleErrorf(err.Error())
341 }
342 return strings.TrimSpace(string(bytes))
343}