Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package filesystem |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
| 20 | "android/soong/android" |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint" |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | func init() { |
| 26 | android.RegisterModuleType("android_filesystem", filesystemFactory) |
| 27 | } |
| 28 | |
| 29 | type filesystem struct { |
| 30 | android.ModuleBase |
| 31 | android.PackagingBase |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 32 | |
| 33 | output android.OutputPath |
| 34 | installDir android.InstallPath |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 35 | } |
| 36 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 37 | // 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 Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 42 | func filesystemFactory() android.Module { |
| 43 | module := &filesystem{} |
| 44 | android.InitPackageModule(module) |
| 45 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 46 | return module |
| 47 | } |
| 48 | |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame^] | 49 | var dependencyTag = struct { |
| 50 | blueprint.BaseDependencyTag |
| 51 | android.InstallAlwaysNeededDependencyTag |
| 52 | }{} |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 53 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 54 | func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 55 | f.AddDeps(ctx, dependencyTag) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 56 | } |
| 57 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 58 | func (f *filesystem) installFileName() string { |
| 59 | return f.BaseModuleName() + ".img" |
| 60 | } |
| 61 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 62 | var pctx = android.NewPackageContext("android/soong/filesystem") |
| 63 | |
| 64 | func (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 Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 69 | builder := android.NewRuleBuilder(pctx, ctx) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 70 | builder.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 71 | BuiltTool("zipsync"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 72 | 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 Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame^] | 86 | f.output = android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 87 | builder.Command().BuiltTool("build_image"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 88 | Text(rootDir.String()). // input directory |
| 89 | Input(propFile). |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 90 | Output(f.output). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 91 | Text(rootDir.String()) // directory where to find fs_config_files|dirs |
| 92 | |
| 93 | // rootDir is not deleted. Might be useful for quick inspection. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 94 | builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName())) |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 95 | |
| 96 | f.installDir = android.PathForModuleInstall(ctx, "etc") |
| 97 | ctx.InstallFile(f.installDir, f.installFileName(), f.output) |
| 98 | } |
| 99 | |
| 100 | var _ android.AndroidMkEntriesProvider = (*filesystem)(nil) |
| 101 | |
| 102 | // Implements android.AndroidMkEntriesProvider |
| 103 | func (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 Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 114 | } |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame^] | 115 | |
| 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. |
| 118 | type Filesystem interface { |
| 119 | android.Module |
| 120 | OutputPath() android.Path |
| 121 | } |
| 122 | |
| 123 | var _ Filesystem = (*filesystem)(nil) |
| 124 | |
| 125 | func (f *filesystem) OutputPath() android.Path { |
| 126 | return f.output |
| 127 | } |