blob: 2b3fbaeac1b00b5df849c424f374654eb7e84a65 [file] [log] [blame]
Jiyong Park6f0f6882020-11-12 13:14:30 +09001// Copyright (C) 2020 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 "fmt"
19
20 "android/soong/android"
Jiyong Park65b62242020-11-25 12:44:59 +090021
22 "github.com/google/blueprint"
Jiyong Park6f0f6882020-11-12 13:14:30 +090023)
24
25func init() {
26 android.RegisterModuleType("android_filesystem", filesystemFactory)
27}
28
29type filesystem struct {
30 android.ModuleBase
31 android.PackagingBase
Jiyong Park65c49f52020-11-24 14:23:26 +090032
33 output android.OutputPath
34 installDir android.InstallPath
Jiyong Park6f0f6882020-11-12 13:14:30 +090035}
36
Jiyong Park65c49f52020-11-24 14:23:26 +090037// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
38// image. The filesystem images are expected to be mounted in the target device, which means the
39// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
40// The modules are placed in the filesystem image just like they are installed to the ordinary
41// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +090042func filesystemFactory() android.Module {
43 module := &filesystem{}
44 android.InitPackageModule(module)
45 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
46 return module
47}
48
Jiyong Park12a719c2021-01-07 15:31:24 +090049var dependencyTag = struct {
50 blueprint.BaseDependencyTag
51 android.InstallAlwaysNeededDependencyTag
52}{}
Jiyong Park65b62242020-11-25 12:44:59 +090053
Jiyong Park6f0f6882020-11-12 13:14:30 +090054func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +090055 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +090056}
57
Jiyong Park65c49f52020-11-24 14:23:26 +090058func (f *filesystem) installFileName() string {
59 return f.BaseModuleName() + ".img"
60}
61
Jiyong Park6f0f6882020-11-12 13:14:30 +090062var pctx = android.NewPackageContext("android/soong/filesystem")
63
64func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
65 zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath
66 f.CopyDepsToZip(ctx, zipFile)
67
68 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -080069 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +090070 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -080071 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +090072 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
73 Input(zipFile)
74
75 mkuserimg := ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs")
76 propFile := android.PathForModuleOut(ctx, "prop").OutputPath
77 // TODO(jiyong): support more filesystem types other than ext4
78 propsText := fmt.Sprintf(`mount_point=system\n`+
79 `fs_type=ext4\n`+
80 `use_dynamic_partition_size=true\n`+
81 `ext_mkuserimg=%s\n`, mkuserimg.String())
82 builder.Command().Text("echo").Flag("-e").Flag(`"` + propsText + `"`).
83 Text(">").Output(propFile).
84 Implicit(mkuserimg)
85
Jiyong Park12a719c2021-01-07 15:31:24 +090086 f.output = android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -080087 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +090088 Text(rootDir.String()). // input directory
89 Input(propFile).
Jiyong Park65c49f52020-11-24 14:23:26 +090090 Output(f.output).
Jiyong Park6f0f6882020-11-12 13:14:30 +090091 Text(rootDir.String()) // directory where to find fs_config_files|dirs
92
93 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -080094 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +090095
96 f.installDir = android.PathForModuleInstall(ctx, "etc")
97 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
98}
99
100var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
101
102// Implements android.AndroidMkEntriesProvider
103func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
104 return []android.AndroidMkEntries{android.AndroidMkEntries{
105 Class: "ETC",
106 OutputFile: android.OptionalPathForPath(f.output),
107 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
108 func(entries *android.AndroidMkEntries) {
109 entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
110 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
111 },
112 },
113 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900114}
Jiyong Park12a719c2021-01-07 15:31:24 +0900115
116// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
117// package to have access to the output file.
118type Filesystem interface {
119 android.Module
120 OutputPath() android.Path
121}
122
123var _ Filesystem = (*filesystem)(nil)
124
125func (f *filesystem) OutputPath() android.Path {
126 return f.output
127}