blob: 28fe1ce49152e0303e5ace8bb1e9cb6ab1800a5a [file] [log] [blame]
Cole Faust74ee4e02025-01-16 14:55:35 -08001// 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 filesystem
16
17import (
18 "android/soong/android"
19 "path/filepath"
20 "strings"
21
22 "github.com/google/blueprint"
23 "github.com/google/blueprint/proptools"
24)
25
26type SystemOtherImageProperties struct {
27 // The system_other image always requires a reference to the system image. The system_other
28 // partition gets built into the system partition's "b" slot in a/b partition builds. Thus, it
29 // copies most of its configuration from the system image, such as filesystem type, avb signing
30 // info, etc. Including it here does not automatically mean that it will pick up the system
31 // image's dexpropt files, it must also be listed in Preinstall_dexpreopt_files_from for that.
32 System_image *string
33
34 // This system_other partition will include all the dexpreopt files from the apps on these
35 // partitions.
36 Preinstall_dexpreopt_files_from []string
37}
38
39type systemOtherImage struct {
40 android.ModuleBase
41 android.DefaultableModuleBase
42 properties SystemOtherImageProperties
43}
44
45// The system_other image is the default contents of the "b" slot of the system image.
46// It contains the dexpreopt files of all the apps on the device, for a faster first boot.
47// Afterwards, at runtime, it will be used as a regular b slot for OTA updates, and the initial
48// dexpreopt files will be deleted.
49func SystemOtherImageFactory() android.Module {
50 module := &systemOtherImage{}
51 module.AddProperties(&module.properties)
Cole Faustb8e280f2025-01-16 16:33:26 -080052 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust74ee4e02025-01-16 14:55:35 -080053 android.InitDefaultableModule(module)
54 return module
55}
56
57type systemImageDeptag struct {
58 blueprint.BaseDependencyTag
59}
60
61var systemImageDependencyTag = systemImageDeptag{}
62
63type dexpreoptDeptag struct {
64 blueprint.BaseDependencyTag
65}
66
67var dexpreoptDependencyTag = dexpreoptDeptag{}
68
69func (m *systemOtherImage) DepsMutator(ctx android.BottomUpMutatorContext) {
70 if proptools.String(m.properties.System_image) == "" {
71 ctx.ModuleErrorf("system_image property must be set")
72 return
73 }
74 ctx.AddDependency(ctx.Module(), systemImageDependencyTag, *m.properties.System_image)
75 ctx.AddDependency(ctx.Module(), dexpreoptDependencyTag, m.properties.Preinstall_dexpreopt_files_from...)
76}
77
78func (m *systemOtherImage) GenerateAndroidBuildActions(ctx android.ModuleContext) {
79 systemImage := ctx.GetDirectDepProxyWithTag(*m.properties.System_image, systemImageDependencyTag)
80 systemInfo, ok := android.OtherModuleProvider(ctx, systemImage, FilesystemProvider)
81 if !ok {
82 ctx.PropertyErrorf("system_image", "Expected system_image module to provide FilesystemProvider")
83 return
84 }
85
86 output := android.PathForModuleOut(ctx, "system_other.img")
87 stagingDir := android.PathForModuleOut(ctx, "staging_dir")
88
89 builder := android.NewRuleBuilder(pctx, ctx)
90 builder.Command().Textf("rm -rf %s && mkdir -p %s", stagingDir, stagingDir)
91
Cole Faustb8e280f2025-01-16 16:33:26 -080092 specs := make(map[string]android.PackagingSpec)
Cole Faust74ee4e02025-01-16 14:55:35 -080093 for _, otherPartition := range m.properties.Preinstall_dexpreopt_files_from {
94 dexModule := ctx.GetDirectDepProxyWithTag(otherPartition, dexpreoptDependencyTag)
Cole Faustb8e280f2025-01-16 16:33:26 -080095 fsInfo, ok := android.OtherModuleProvider(ctx, dexModule, FilesystemProvider)
Cole Faust74ee4e02025-01-16 14:55:35 -080096 if !ok {
97 ctx.PropertyErrorf("preinstall_dexpreopt_files_from", "Expected module %q to provide FilesystemProvider", otherPartition)
98 return
99 }
Cole Faustb8e280f2025-01-16 16:33:26 -0800100 // Merge all the packaging specs into 1 map
101 for k := range fsInfo.SpecsForSystemOther {
102 if _, ok := specs[k]; ok {
103 ctx.ModuleErrorf("Packaging spec %s given by two different partitions", k)
104 continue
105 }
106 specs[k] = fsInfo.SpecsForSystemOther[k]
107 }
108 }
109
110 // TOOD: CopySpecsToDir only exists on PackagingBase, but doesn't use any fields from it. Clean this up.
111 (&android.PackagingBase{}).CopySpecsToDir(ctx, builder, specs, stagingDir)
112
113 if len(m.properties.Preinstall_dexpreopt_files_from) > 0 {
114 builder.Command().Textf("touch %s", filepath.Join(stagingDir.String(), "system-other-odex-marker"))
Cole Faust74ee4e02025-01-16 14:55:35 -0800115 }
116
117 // Most of the time, if build_image were to call a host tool, it accepts the path to the
118 // host tool in a field in the prop file. However, it doesn't have that option for fec, which
119 // it expects to just be on the PATH. Add fec to the PATH.
120 fec := ctx.Config().HostToolPath(ctx, "fec")
121 pathToolDirs := []string{filepath.Dir(fec.String())}
122
123 builder.Command().
124 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
125 BuiltTool("build_image").
126 Text(stagingDir.String()). // input directory
127 Input(systemInfo.BuildImagePropFile).
128 Implicits(systemInfo.BuildImagePropFileDeps).
129 Implicit(fec).
130 Output(output).
131 Text(stagingDir.String())
132
133 builder.Build("build_system_other", "build system other")
134
135 ctx.SetOutputFiles(android.Paths{output}, "")
136 ctx.CheckbuildFile(output)
137}