summaryrefslogtreecommitdiff
path: root/filesystem/system_other.go
blob: cad4263378ce811d0bab103a3f4f995c610291d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (C) 2024 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filesystem

import (
	"android/soong/android"
	"path/filepath"
	"strings"

	"github.com/google/blueprint"
	"github.com/google/blueprint/proptools"
)

type SystemOtherImageProperties struct {
	// The system_other image always requires a reference to the system image. The system_other
	// partition gets built into the system partition's "b" slot in a/b partition builds. Thus, it
	// copies most of its configuration from the system image, such as filesystem type, avb signing
	// info, etc. Including it here does not automatically mean that it will pick up the system
	// image's dexpropt files, it must also be listed in Preinstall_dexpreopt_files_from for that.
	System_image *string

	// This system_other partition will include all the dexpreopt files from the apps on these
	// partitions.
	Preinstall_dexpreopt_files_from []string
}

type systemOtherImage struct {
	android.ModuleBase
	android.DefaultableModuleBase
	properties SystemOtherImageProperties
}

// The system_other image is the default contents of the "b" slot of the system image.
// It contains the dexpreopt files of all the apps on the device, for a faster first boot.
// Afterwards, at runtime, it will be used as a regular b slot for OTA updates, and the initial
// dexpreopt files will be deleted.
func SystemOtherImageFactory() android.Module {
	module := &systemOtherImage{}
	module.AddProperties(&module.properties)
	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
	android.InitDefaultableModule(module)
	return module
}

type systemImageDeptag struct {
	blueprint.BaseDependencyTag
}

var systemImageDependencyTag = systemImageDeptag{}

type dexpreoptDeptag struct {
	blueprint.BaseDependencyTag
}

var dexpreoptDependencyTag = dexpreoptDeptag{}

func (m *systemOtherImage) DepsMutator(ctx android.BottomUpMutatorContext) {
	if proptools.String(m.properties.System_image) == "" {
		ctx.ModuleErrorf("system_image property must be set")
		return
	}
	ctx.AddDependency(ctx.Module(), systemImageDependencyTag, *m.properties.System_image)
	ctx.AddDependency(ctx.Module(), dexpreoptDependencyTag, m.properties.Preinstall_dexpreopt_files_from...)
}

func (m *systemOtherImage) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	systemImage := ctx.GetDirectDepProxyWithTag(*m.properties.System_image, systemImageDependencyTag)
	systemInfo, ok := android.OtherModuleProvider(ctx, systemImage, FilesystemProvider)
	if !ok {
		ctx.PropertyErrorf("system_image", "Expected system_image module to provide FilesystemProvider")
		return
	}

	output := android.PathForModuleOut(ctx, "system_other.img")
	stagingDir := android.PathForModuleOut(ctx, "staging_dir")

	builder := android.NewRuleBuilder(pctx, ctx)
	builder.Command().Textf("rm -rf %s && mkdir -p %s", stagingDir, stagingDir)

	for _, otherPartition := range m.properties.Preinstall_dexpreopt_files_from {
		dexModule := ctx.GetDirectDepProxyWithTag(otherPartition, dexpreoptDependencyTag)
		_, ok := android.OtherModuleProvider(ctx, dexModule, FilesystemProvider)
		if !ok {
			ctx.PropertyErrorf("preinstall_dexpreopt_files_from", "Expected module %q to provide FilesystemProvider", otherPartition)
			return
		}
		// TODO(b/390269431): Install dex files to the staging dir
	}

	// Most of the time, if build_image were to call a host tool, it accepts the path to the
	// host tool in a field in the prop file. However, it doesn't have that option for fec, which
	// it expects to just be on the PATH. Add fec to the PATH.
	fec := ctx.Config().HostToolPath(ctx, "fec")
	pathToolDirs := []string{filepath.Dir(fec.String())}

	builder.Command().
		Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
		BuiltTool("build_image").
		Text(stagingDir.String()). // input directory
		Input(systemInfo.BuildImagePropFile).
		Implicits(systemInfo.BuildImagePropFileDeps).
		Implicit(fec).
		Output(output).
		Text(stagingDir.String())

	builder.Build("build_system_other", "build system other")

	ctx.SetOutputFiles(android.Paths{output}, "")
	ctx.CheckbuildFile(output)
}