blob: cad4263378ce811d0bab103a3f4f995c610291d1 [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)
52 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
53 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
92 for _, otherPartition := range m.properties.Preinstall_dexpreopt_files_from {
93 dexModule := ctx.GetDirectDepProxyWithTag(otherPartition, dexpreoptDependencyTag)
94 _, ok := android.OtherModuleProvider(ctx, dexModule, FilesystemProvider)
95 if !ok {
96 ctx.PropertyErrorf("preinstall_dexpreopt_files_from", "Expected module %q to provide FilesystemProvider", otherPartition)
97 return
98 }
99 // TODO(b/390269431): Install dex files to the staging dir
100 }
101
102 // Most of the time, if build_image were to call a host tool, it accepts the path to the
103 // host tool in a field in the prop file. However, it doesn't have that option for fec, which
104 // it expects to just be on the PATH. Add fec to the PATH.
105 fec := ctx.Config().HostToolPath(ctx, "fec")
106 pathToolDirs := []string{filepath.Dir(fec.String())}
107
108 builder.Command().
109 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
110 BuiltTool("build_image").
111 Text(stagingDir.String()). // input directory
112 Input(systemInfo.BuildImagePropFile).
113 Implicits(systemInfo.BuildImagePropFileDeps).
114 Implicit(fec).
115 Output(output).
116 Text(stagingDir.String())
117
118 builder.Build("build_system_other", "build system other")
119
120 ctx.SetOutputFiles(android.Paths{output}, "")
121 ctx.CheckbuildFile(output)
122}