blob: 1c00dd35e94816fc431f5aaf608f411c9e7db4d5 [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")
Spandan Das7a42d1c2025-02-12 01:32:21 +000088 stagingDirTimestamp := android.PathForModuleOut(ctx, "staging_dir.timestamp")
Cole Faust74ee4e02025-01-16 14:55:35 -080089
90 builder := android.NewRuleBuilder(pctx, ctx)
91 builder.Command().Textf("rm -rf %s && mkdir -p %s", stagingDir, stagingDir)
92
Cole Faustb8e280f2025-01-16 16:33:26 -080093 specs := make(map[string]android.PackagingSpec)
Cole Faust74ee4e02025-01-16 14:55:35 -080094 for _, otherPartition := range m.properties.Preinstall_dexpreopt_files_from {
95 dexModule := ctx.GetDirectDepProxyWithTag(otherPartition, dexpreoptDependencyTag)
Cole Faustb8e280f2025-01-16 16:33:26 -080096 fsInfo, ok := android.OtherModuleProvider(ctx, dexModule, FilesystemProvider)
Cole Faust74ee4e02025-01-16 14:55:35 -080097 if !ok {
98 ctx.PropertyErrorf("preinstall_dexpreopt_files_from", "Expected module %q to provide FilesystemProvider", otherPartition)
99 return
100 }
Cole Faustb8e280f2025-01-16 16:33:26 -0800101 // Merge all the packaging specs into 1 map
102 for k := range fsInfo.SpecsForSystemOther {
103 if _, ok := specs[k]; ok {
104 ctx.ModuleErrorf("Packaging spec %s given by two different partitions", k)
105 continue
106 }
107 specs[k] = fsInfo.SpecsForSystemOther[k]
108 }
109 }
110
111 // TOOD: CopySpecsToDir only exists on PackagingBase, but doesn't use any fields from it. Clean this up.
112 (&android.PackagingBase{}).CopySpecsToDir(ctx, builder, specs, stagingDir)
113
114 if len(m.properties.Preinstall_dexpreopt_files_from) > 0 {
115 builder.Command().Textf("touch %s", filepath.Join(stagingDir.String(), "system-other-odex-marker"))
Cole Faust74ee4e02025-01-16 14:55:35 -0800116 }
Spandan Das7a42d1c2025-02-12 01:32:21 +0000117 builder.Command().Textf("touch").Output(stagingDirTimestamp)
118 builder.Build("assemble_filesystem_staging_dir", "Assemble filesystem staging dir")
Cole Faust74ee4e02025-01-16 14:55:35 -0800119
120 // Most of the time, if build_image were to call a host tool, it accepts the path to the
121 // host tool in a field in the prop file. However, it doesn't have that option for fec, which
122 // it expects to just be on the PATH. Add fec to the PATH.
123 fec := ctx.Config().HostToolPath(ctx, "fec")
124 pathToolDirs := []string{filepath.Dir(fec.String())}
125
Spandan Das7a42d1c2025-02-12 01:32:21 +0000126 builder = android.NewRuleBuilder(pctx, ctx)
Cole Faust74ee4e02025-01-16 14:55:35 -0800127 builder.Command().
128 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
129 BuiltTool("build_image").
130 Text(stagingDir.String()). // input directory
131 Input(systemInfo.BuildImagePropFile).
132 Implicits(systemInfo.BuildImagePropFileDeps).
133 Implicit(fec).
Spandan Das7a42d1c2025-02-12 01:32:21 +0000134 Implicit(stagingDirTimestamp).
Cole Faust74ee4e02025-01-16 14:55:35 -0800135 Output(output).
136 Text(stagingDir.String())
137
138 builder.Build("build_system_other", "build system other")
139
Spandan Das7a42d1c2025-02-12 01:32:21 +0000140 // Create a hermetic system_other.img with pinned timestamps
141 builder = android.NewRuleBuilder(pctx, ctx)
142 outputHermetic := android.PathForModuleOut(ctx, "for_target_files", "system_other.img")
143 outputHermeticPropFile := m.propFileForHermeticImg(ctx, builder, systemInfo.BuildImagePropFile)
144 builder.Command().
145 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
146 BuiltTool("build_image").
147 Text(stagingDir.String()). // input directory
148 Input(outputHermeticPropFile).
149 Implicits(systemInfo.BuildImagePropFileDeps).
150 Implicit(fec).
151 Implicit(stagingDirTimestamp).
152 Output(outputHermetic).
153 Text(stagingDir.String())
154
155 builder.Build("build_system_other_hermetic", "build system other")
156
157 fsInfo := FilesystemInfo{
158 Output: output,
159 OutputHermetic: outputHermetic,
160 RootDir: stagingDir,
161 }
162
163 android.SetProvider(ctx, FilesystemProvider, fsInfo)
164
Cole Faust74ee4e02025-01-16 14:55:35 -0800165 ctx.SetOutputFiles(android.Paths{output}, "")
166 ctx.CheckbuildFile(output)
167}
Spandan Das7a42d1c2025-02-12 01:32:21 +0000168
169func (f *systemOtherImage) propFileForHermeticImg(ctx android.ModuleContext, builder *android.RuleBuilder, inputPropFile android.Path) android.Path {
170 propFilePinnedTimestamp := android.PathForModuleOut(ctx, "for_target_files", "prop")
171 builder.Command().Textf("cat").Input(inputPropFile).Flag(">").Output(propFilePinnedTimestamp).
172 Textf(" && echo use_fixed_timestamp=true >> %s", propFilePinnedTimestamp)
173 return propFilePinnedTimestamp
174}