blob: 5309e901258c7c5023e78445677b236de1f6e418 [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
Cole Faust3c02ffa2025-02-19 15:20:20 -080026var (
27 systemOtherPropFileTweaks = pctx.AndroidStaticRule("system_other_prop_file_tweaks", blueprint.RuleParams{
28 Command: `rm -rf $out && sed -e 's@^mount_point=/$$@mount_point=system_other@g' -e 's@^partition_name=system$$@partition_name=system_other@g' $in > $out`,
29 })
30)
31
Cole Faust74ee4e02025-01-16 14:55:35 -080032type SystemOtherImageProperties struct {
33 // The system_other image always requires a reference to the system image. The system_other
34 // partition gets built into the system partition's "b" slot in a/b partition builds. Thus, it
35 // copies most of its configuration from the system image, such as filesystem type, avb signing
36 // info, etc. Including it here does not automatically mean that it will pick up the system
37 // image's dexpropt files, it must also be listed in Preinstall_dexpreopt_files_from for that.
38 System_image *string
39
40 // This system_other partition will include all the dexpreopt files from the apps on these
41 // partitions.
42 Preinstall_dexpreopt_files_from []string
43}
44
45type systemOtherImage struct {
46 android.ModuleBase
47 android.DefaultableModuleBase
48 properties SystemOtherImageProperties
49}
50
51// The system_other image is the default contents of the "b" slot of the system image.
52// It contains the dexpreopt files of all the apps on the device, for a faster first boot.
53// Afterwards, at runtime, it will be used as a regular b slot for OTA updates, and the initial
54// dexpreopt files will be deleted.
55func SystemOtherImageFactory() android.Module {
56 module := &systemOtherImage{}
57 module.AddProperties(&module.properties)
Cole Faustb8e280f2025-01-16 16:33:26 -080058 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust74ee4e02025-01-16 14:55:35 -080059 android.InitDefaultableModule(module)
60 return module
61}
62
63type systemImageDeptag struct {
64 blueprint.BaseDependencyTag
65}
66
67var systemImageDependencyTag = systemImageDeptag{}
68
69type dexpreoptDeptag struct {
70 blueprint.BaseDependencyTag
71}
72
73var dexpreoptDependencyTag = dexpreoptDeptag{}
74
75func (m *systemOtherImage) DepsMutator(ctx android.BottomUpMutatorContext) {
76 if proptools.String(m.properties.System_image) == "" {
77 ctx.ModuleErrorf("system_image property must be set")
78 return
79 }
80 ctx.AddDependency(ctx.Module(), systemImageDependencyTag, *m.properties.System_image)
81 ctx.AddDependency(ctx.Module(), dexpreoptDependencyTag, m.properties.Preinstall_dexpreopt_files_from...)
82}
83
84func (m *systemOtherImage) GenerateAndroidBuildActions(ctx android.ModuleContext) {
85 systemImage := ctx.GetDirectDepProxyWithTag(*m.properties.System_image, systemImageDependencyTag)
86 systemInfo, ok := android.OtherModuleProvider(ctx, systemImage, FilesystemProvider)
87 if !ok {
88 ctx.PropertyErrorf("system_image", "Expected system_image module to provide FilesystemProvider")
89 return
90 }
91
92 output := android.PathForModuleOut(ctx, "system_other.img")
93 stagingDir := android.PathForModuleOut(ctx, "staging_dir")
Spandan Das7a42d1c2025-02-12 01:32:21 +000094 stagingDirTimestamp := android.PathForModuleOut(ctx, "staging_dir.timestamp")
Cole Faust74ee4e02025-01-16 14:55:35 -080095
96 builder := android.NewRuleBuilder(pctx, ctx)
97 builder.Command().Textf("rm -rf %s && mkdir -p %s", stagingDir, stagingDir)
98
Cole Faustb8e280f2025-01-16 16:33:26 -080099 specs := make(map[string]android.PackagingSpec)
Cole Faust74ee4e02025-01-16 14:55:35 -0800100 for _, otherPartition := range m.properties.Preinstall_dexpreopt_files_from {
101 dexModule := ctx.GetDirectDepProxyWithTag(otherPartition, dexpreoptDependencyTag)
Cole Faustb8e280f2025-01-16 16:33:26 -0800102 fsInfo, ok := android.OtherModuleProvider(ctx, dexModule, FilesystemProvider)
Cole Faust74ee4e02025-01-16 14:55:35 -0800103 if !ok {
104 ctx.PropertyErrorf("preinstall_dexpreopt_files_from", "Expected module %q to provide FilesystemProvider", otherPartition)
105 return
106 }
Cole Faustb8e280f2025-01-16 16:33:26 -0800107 // Merge all the packaging specs into 1 map
108 for k := range fsInfo.SpecsForSystemOther {
109 if _, ok := specs[k]; ok {
110 ctx.ModuleErrorf("Packaging spec %s given by two different partitions", k)
111 continue
112 }
113 specs[k] = fsInfo.SpecsForSystemOther[k]
114 }
115 }
116
117 // TOOD: CopySpecsToDir only exists on PackagingBase, but doesn't use any fields from it. Clean this up.
118 (&android.PackagingBase{}).CopySpecsToDir(ctx, builder, specs, stagingDir)
119
120 if len(m.properties.Preinstall_dexpreopt_files_from) > 0 {
121 builder.Command().Textf("touch %s", filepath.Join(stagingDir.String(), "system-other-odex-marker"))
Cole Faust74ee4e02025-01-16 14:55:35 -0800122 }
Spandan Das7a42d1c2025-02-12 01:32:21 +0000123 builder.Command().Textf("touch").Output(stagingDirTimestamp)
124 builder.Build("assemble_filesystem_staging_dir", "Assemble filesystem staging dir")
Cole Faust74ee4e02025-01-16 14:55:35 -0800125
126 // Most of the time, if build_image were to call a host tool, it accepts the path to the
127 // host tool in a field in the prop file. However, it doesn't have that option for fec, which
128 // it expects to just be on the PATH. Add fec to the PATH.
129 fec := ctx.Config().HostToolPath(ctx, "fec")
130 pathToolDirs := []string{filepath.Dir(fec.String())}
131
Cole Faust3c02ffa2025-02-19 15:20:20 -0800132 // In make, the exact same prop file is used for both system and system_other. However, I
133 // believe make goes through a different build_image code path that is based on the name of
134 // the output file. So it sees the output file is named system_other.img and makes some changes.
135 // We don't use that codepath, so make the changes manually to the prop file.
136 propFile := android.PathForModuleOut(ctx, "prop")
137 ctx.Build(pctx, android.BuildParams{
138 Rule: systemOtherPropFileTweaks,
139 Input: systemInfo.BuildImagePropFile,
140 Output: propFile,
141 })
142
Spandan Das7a42d1c2025-02-12 01:32:21 +0000143 builder = android.NewRuleBuilder(pctx, ctx)
Cole Faust74ee4e02025-01-16 14:55:35 -0800144 builder.Command().
145 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
146 BuiltTool("build_image").
147 Text(stagingDir.String()). // input directory
Cole Faust3c02ffa2025-02-19 15:20:20 -0800148 Input(propFile).
Cole Faust74ee4e02025-01-16 14:55:35 -0800149 Implicits(systemInfo.BuildImagePropFileDeps).
150 Implicit(fec).
Spandan Das7a42d1c2025-02-12 01:32:21 +0000151 Implicit(stagingDirTimestamp).
Cole Faust74ee4e02025-01-16 14:55:35 -0800152 Output(output).
153 Text(stagingDir.String())
154
155 builder.Build("build_system_other", "build system other")
156
Spandan Das7a42d1c2025-02-12 01:32:21 +0000157 // Create a hermetic system_other.img with pinned timestamps
158 builder = android.NewRuleBuilder(pctx, ctx)
159 outputHermetic := android.PathForModuleOut(ctx, "for_target_files", "system_other.img")
Cole Faust3c02ffa2025-02-19 15:20:20 -0800160 outputHermeticPropFile := m.propFileForHermeticImg(ctx, builder, propFile)
Spandan Das7a42d1c2025-02-12 01:32:21 +0000161 builder.Command().
162 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
163 BuiltTool("build_image").
164 Text(stagingDir.String()). // input directory
165 Input(outputHermeticPropFile).
166 Implicits(systemInfo.BuildImagePropFileDeps).
167 Implicit(fec).
168 Implicit(stagingDirTimestamp).
169 Output(outputHermetic).
170 Text(stagingDir.String())
171
172 builder.Build("build_system_other_hermetic", "build system other")
173
174 fsInfo := FilesystemInfo{
175 Output: output,
176 OutputHermetic: outputHermetic,
177 RootDir: stagingDir,
178 }
179
180 android.SetProvider(ctx, FilesystemProvider, fsInfo)
181
Cole Faust74ee4e02025-01-16 14:55:35 -0800182 ctx.SetOutputFiles(android.Paths{output}, "")
183 ctx.CheckbuildFile(output)
184}
Spandan Das7a42d1c2025-02-12 01:32:21 +0000185
186func (f *systemOtherImage) propFileForHermeticImg(ctx android.ModuleContext, builder *android.RuleBuilder, inputPropFile android.Path) android.Path {
187 propFilePinnedTimestamp := android.PathForModuleOut(ctx, "for_target_files", "prop")
188 builder.Command().Textf("cat").Input(inputPropFile).Flag(">").Output(propFilePinnedTimestamp).
189 Textf(" && echo use_fixed_timestamp=true >> %s", propFilePinnedTimestamp)
190 return propFilePinnedTimestamp
191}