blob: ac33b76aed4ea597fd0bd01dbb83725a443a0b7d [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 Park65b62242020-11-25 12:44:59 +090049var dependencyTag = struct{ blueprint.BaseDependencyTag }{}
50
Jiyong Park6f0f6882020-11-12 13:14:30 +090051func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +090052 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +090053}
54
Jiyong Park65c49f52020-11-24 14:23:26 +090055func (f *filesystem) installFileName() string {
56 return f.BaseModuleName() + ".img"
57}
58
Jiyong Park6f0f6882020-11-12 13:14:30 +090059var pctx = android.NewPackageContext("android/soong/filesystem")
60
61func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
62 zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath
63 f.CopyDepsToZip(ctx, zipFile)
64
65 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
66 builder := android.NewRuleBuilder()
67 builder.Command().
68 BuiltTool(ctx, "zipsync").
69 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
70 Input(zipFile)
71
72 mkuserimg := ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs")
73 propFile := android.PathForModuleOut(ctx, "prop").OutputPath
74 // TODO(jiyong): support more filesystem types other than ext4
75 propsText := fmt.Sprintf(`mount_point=system\n`+
76 `fs_type=ext4\n`+
77 `use_dynamic_partition_size=true\n`+
78 `ext_mkuserimg=%s\n`, mkuserimg.String())
79 builder.Command().Text("echo").Flag("-e").Flag(`"` + propsText + `"`).
80 Text(">").Output(propFile).
81 Implicit(mkuserimg)
82
Jiyong Park65c49f52020-11-24 14:23:26 +090083 f.output = android.PathForModuleOut(ctx, "filesystem.img").OutputPath
Jiyong Park6f0f6882020-11-12 13:14:30 +090084 builder.Command().BuiltTool(ctx, "build_image").
85 Text(rootDir.String()). // input directory
86 Input(propFile).
Jiyong Park65c49f52020-11-24 14:23:26 +090087 Output(f.output).
Jiyong Park6f0f6882020-11-12 13:14:30 +090088 Text(rootDir.String()) // directory where to find fs_config_files|dirs
89
90 // rootDir is not deleted. Might be useful for quick inspection.
91 builder.Build(pctx, ctx, "build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +090092
93 f.installDir = android.PathForModuleInstall(ctx, "etc")
94 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
95}
96
97var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
98
99// Implements android.AndroidMkEntriesProvider
100func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
101 return []android.AndroidMkEntries{android.AndroidMkEntries{
102 Class: "ETC",
103 OutputFile: android.OptionalPathForPath(f.output),
104 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
105 func(entries *android.AndroidMkEntries) {
106 entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
107 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
108 },
109 },
110 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900111}