Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 1 | // Copyright (C) 2021 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 kernel |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "path/filepath" |
| 20 | "strings" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | _ "android/soong/cc/config" |
| 24 | |
| 25 | "github.com/google/blueprint" |
| 26 | "github.com/google/blueprint/proptools" |
| 27 | ) |
| 28 | |
| 29 | func init() { |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 30 | pctx.Import("android/soong/cc/config") |
Paul Duffin | e5ac250 | 2021-03-29 01:24:49 +0100 | [diff] [blame] | 31 | registerKernelBuildComponents(android.InitRegistrationContext) |
| 32 | } |
| 33 | |
| 34 | func registerKernelBuildComponents(ctx android.RegistrationContext) { |
Spandan Das | 5e33642 | 2024-11-01 22:31:20 +0000 | [diff] [blame] | 35 | ctx.RegisterModuleType("prebuilt_kernel_modules", PrebuiltKernelModulesFactory) |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | type prebuiltKernelModules struct { |
| 39 | android.ModuleBase |
| 40 | |
| 41 | properties prebuiltKernelModulesProperties |
| 42 | |
| 43 | installDir android.InstallPath |
| 44 | } |
| 45 | |
| 46 | type prebuiltKernelModulesProperties struct { |
| 47 | // List or filegroup of prebuilt kernel module files. Should have .ko suffix. |
| 48 | Srcs []string `android:"path,arch_variant"` |
| 49 | |
Spandan Das | eb426b7 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 50 | // List of system_dlkm kernel modules that the local kernel modules depend on. |
| 51 | // The deps will be assembled into intermediates directory for running depmod |
| 52 | // but will not be added to the current module's installed files. |
| 53 | System_deps []string `android:"path,arch_variant"` |
| 54 | |
Spandan Das | ad40292 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 55 | // If false, then srcs will not be included in modules.load. |
| 56 | // This feature is used by system_dlkm |
| 57 | Load_by_default *bool |
| 58 | |
Spandan Das | 6dfcbdf | 2024-11-11 18:43:07 +0000 | [diff] [blame] | 59 | Blocklist_file *string `android:"path"` |
| 60 | |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 61 | // Kernel version that these modules are for. Kernel modules are installed to |
| 62 | // /lib/modules/<kernel_version> directory in the corresponding partition. Default is "". |
| 63 | Kernel_version *string |
Inseob Kim | 6a463f8 | 2023-11-06 18:07:13 +0900 | [diff] [blame] | 64 | |
| 65 | // Whether this module is directly installable to one of the partitions. Default is true |
| 66 | Installable *bool |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // prebuilt_kernel_modules installs a set of prebuilt kernel module files to the correct directory. |
| 70 | // In addition, this module builds modules.load, modules.dep, modules.softdep and modules.alias |
| 71 | // using depmod and installs them as well. |
Spandan Das | 5e33642 | 2024-11-01 22:31:20 +0000 | [diff] [blame] | 72 | func PrebuiltKernelModulesFactory() android.Module { |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 73 | module := &prebuiltKernelModules{} |
| 74 | module.AddProperties(&module.properties) |
| 75 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) |
| 76 | return module |
| 77 | } |
| 78 | |
Inseob Kim | 6a463f8 | 2023-11-06 18:07:13 +0900 | [diff] [blame] | 79 | func (pkm *prebuiltKernelModules) installable() bool { |
| 80 | return proptools.BoolDefault(pkm.properties.Installable, true) |
| 81 | } |
| 82 | |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 83 | func (pkm *prebuiltKernelModules) KernelVersion() string { |
| 84 | return proptools.StringDefault(pkm.properties.Kernel_version, "") |
| 85 | } |
| 86 | |
| 87 | func (pkm *prebuiltKernelModules) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 88 | // do nothing |
| 89 | } |
| 90 | |
| 91 | func (pkm *prebuiltKernelModules) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Inseob Kim | 6a463f8 | 2023-11-06 18:07:13 +0900 | [diff] [blame] | 92 | if !pkm.installable() { |
| 93 | pkm.SkipInstall() |
| 94 | } |
Spandan Das | eb426b7 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 95 | |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 96 | modules := android.PathsForModuleSrc(ctx, pkm.properties.Srcs) |
Spandan Das | eb426b7 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 97 | systemModules := android.PathsForModuleSrc(ctx, pkm.properties.System_deps) |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 98 | |
Spandan Das | ad40292 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 99 | depmodOut := pkm.runDepmod(ctx, modules, systemModules) |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 100 | strippedModules := stripDebugSymbols(ctx, modules) |
| 101 | |
Jiyong Park | 599992b | 2021-02-04 19:40:56 +0900 | [diff] [blame] | 102 | installDir := android.PathForModuleInstall(ctx, "lib", "modules") |
Jihoon Kang | f6b5e8f | 2024-11-27 00:33:30 +0000 | [diff] [blame^] | 103 | // Kernel module is installed to vendor_ramdisk/lib/modules regardless of product |
| 104 | // configuration. This matches the behavior in make and prevents the files from being |
| 105 | // installed in `vendor_ramdisk/first_stage_ramdisk`. |
| 106 | if pkm.InstallInVendorRamdisk() { |
| 107 | installDir = android.PathForModuleInPartitionInstall(ctx, "vendor_ramdisk", "lib", "modules") |
| 108 | } |
| 109 | |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 110 | if pkm.KernelVersion() != "" { |
| 111 | installDir = installDir.Join(ctx, pkm.KernelVersion()) |
| 112 | } |
| 113 | |
| 114 | for _, m := range strippedModules { |
| 115 | ctx.InstallFile(installDir, filepath.Base(m.String()), m) |
| 116 | } |
| 117 | ctx.InstallFile(installDir, "modules.load", depmodOut.modulesLoad) |
| 118 | ctx.InstallFile(installDir, "modules.dep", depmodOut.modulesDep) |
| 119 | ctx.InstallFile(installDir, "modules.softdep", depmodOut.modulesSoftdep) |
| 120 | ctx.InstallFile(installDir, "modules.alias", depmodOut.modulesAlias) |
Spandan Das | 6dfcbdf | 2024-11-11 18:43:07 +0000 | [diff] [blame] | 121 | pkm.installBlocklistFile(ctx, installDir) |
Spandan Das | eb426b7 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 122 | |
| 123 | ctx.SetOutputFiles(modules, ".modules") |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 124 | } |
| 125 | |
Spandan Das | 6dfcbdf | 2024-11-11 18:43:07 +0000 | [diff] [blame] | 126 | func (pkm *prebuiltKernelModules) installBlocklistFile(ctx android.ModuleContext, installDir android.InstallPath) { |
| 127 | if pkm.properties.Blocklist_file == nil { |
| 128 | return |
| 129 | } |
| 130 | blocklistOut := android.PathForModuleOut(ctx, "modules.blocklist") |
| 131 | |
| 132 | ctx.Build(pctx, android.BuildParams{ |
| 133 | Rule: processBlocklistFile, |
| 134 | Input: android.PathForModuleSrc(ctx, proptools.String(pkm.properties.Blocklist_file)), |
| 135 | Output: blocklistOut, |
| 136 | }) |
| 137 | ctx.InstallFile(installDir, "modules.blocklist", blocklistOut) |
| 138 | } |
| 139 | |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 140 | var ( |
| 141 | pctx = android.NewPackageContext("android/soong/kernel") |
| 142 | |
| 143 | stripRule = pctx.AndroidStaticRule("strip", |
| 144 | blueprint.RuleParams{ |
| 145 | Command: "$stripCmd -o $out --strip-debug $in", |
| 146 | CommandDeps: []string{"$stripCmd"}, |
| 147 | }, "stripCmd") |
| 148 | ) |
| 149 | |
| 150 | func stripDebugSymbols(ctx android.ModuleContext, modules android.Paths) android.OutputPaths { |
| 151 | dir := android.PathForModuleOut(ctx, "stripped").OutputPath |
| 152 | var outputs android.OutputPaths |
| 153 | |
| 154 | for _, m := range modules { |
| 155 | stripped := dir.Join(ctx, filepath.Base(m.String())) |
| 156 | ctx.Build(pctx, android.BuildParams{ |
| 157 | Rule: stripRule, |
| 158 | Input: m, |
| 159 | Output: stripped, |
| 160 | Args: map[string]string{ |
| 161 | "stripCmd": "${config.ClangBin}/llvm-strip", |
| 162 | }, |
| 163 | }) |
| 164 | outputs = append(outputs, stripped) |
| 165 | } |
| 166 | |
| 167 | return outputs |
| 168 | } |
| 169 | |
| 170 | type depmodOutputs struct { |
| 171 | modulesLoad android.OutputPath |
| 172 | modulesDep android.OutputPath |
| 173 | modulesSoftdep android.OutputPath |
| 174 | modulesAlias android.OutputPath |
| 175 | } |
| 176 | |
Spandan Das | eb426b7 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 177 | var ( |
| 178 | // system/lib/modules/foo.ko: system/lib/modules/bar.ko |
| 179 | // will be converted to |
| 180 | // /system/lib/modules/foo.ko: /system/lib/modules/bar.ko |
| 181 | addLeadingSlashToPaths = pctx.AndroidStaticRule("add_leading_slash", |
| 182 | blueprint.RuleParams{ |
| 183 | Command: `sed -e 's|\([^: ]*lib/modules/[^: ]*\)|/\1|g' $in > $out`, |
| 184 | }, |
| 185 | ) |
Spandan Das | 6dfcbdf | 2024-11-11 18:43:07 +0000 | [diff] [blame] | 186 | // Remove empty lines. Raise an exception if line is _not_ formatted as `blocklist $name.ko` |
| 187 | processBlocklistFile = pctx.AndroidStaticRule("process_blocklist_file", |
| 188 | blueprint.RuleParams{ |
| 189 | Command: `rm -rf $out && awk <$in > $out` + |
| 190 | ` '/^#/ { print; next }` + |
| 191 | ` NF == 0 { next }` + |
| 192 | ` NF != 2 || $$1 != "blocklist"` + |
| 193 | ` { print "Invalid blocklist line " FNR ": " $$0 >"/dev/stderr";` + |
| 194 | ` exit_status = 1; next }` + |
| 195 | ` { $$1 = $$1; print }` + |
| 196 | ` END { exit exit_status }'`, |
| 197 | }, |
| 198 | ) |
Spandan Das | eb426b7 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 199 | ) |
| 200 | |
| 201 | // This is the path in soong intermediates where the .ko files will be copied. |
| 202 | // The layout should match the layout on device so that depmod can create meaningful modules.* files. |
| 203 | func modulesDirForAndroidDlkm(ctx android.ModuleContext, modulesDir android.OutputPath, system bool) android.OutputPath { |
| 204 | if ctx.InstallInSystemDlkm() || system { |
| 205 | // The first component can be either system or system_dlkm |
| 206 | // system works because /system/lib/modules is a symlink to /system_dlkm/lib/modules. |
| 207 | // system was chosen to match the contents of the kati built modules.dep |
| 208 | return modulesDir.Join(ctx, "system", "lib", "modules") |
| 209 | } else if ctx.InstallInVendorDlkm() { |
| 210 | return modulesDir.Join(ctx, "vendor", "lib", "modules") |
| 211 | } else if ctx.InstallInOdmDlkm() { |
| 212 | return modulesDir.Join(ctx, "odm", "lib", "modules") |
| 213 | } else { |
| 214 | // not an android dlkm module. |
| 215 | return modulesDir |
| 216 | } |
| 217 | } |
| 218 | |
Spandan Das | ad40292 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 219 | func (pkm *prebuiltKernelModules) runDepmod(ctx android.ModuleContext, modules android.Paths, systemModules android.Paths) depmodOutputs { |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 220 | baseDir := android.PathForModuleOut(ctx, "depmod").OutputPath |
| 221 | fakeVer := "0.0" // depmod demands this anyway |
| 222 | modulesDir := baseDir.Join(ctx, "lib", "modules", fakeVer) |
Spandan Das | eb426b7 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 223 | modulesCpDir := modulesDirForAndroidDlkm(ctx, modulesDir, false) |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 224 | |
| 225 | builder := android.NewRuleBuilder(pctx, ctx) |
| 226 | |
| 227 | // Copy the module files to a temporary dir |
Spandan Das | eb426b7 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 228 | builder.Command().Text("rm").Flag("-rf").Text(modulesCpDir.String()) |
| 229 | builder.Command().Text("mkdir").Flag("-p").Text(modulesCpDir.String()) |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 230 | for _, m := range modules { |
Spandan Das | eb426b7 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 231 | builder.Command().Text("cp").Input(m).Text(modulesCpDir.String()) |
| 232 | } |
| 233 | |
| 234 | modulesDirForSystemDlkm := modulesDirForAndroidDlkm(ctx, modulesDir, true) |
| 235 | if len(systemModules) > 0 { |
| 236 | builder.Command().Text("mkdir").Flag("-p").Text(modulesDirForSystemDlkm.String()) |
| 237 | } |
| 238 | for _, m := range systemModules { |
| 239 | builder.Command().Text("cp").Input(m).Text(modulesDirForSystemDlkm.String()) |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // Enumerate modules to load |
| 243 | modulesLoad := modulesDir.Join(ctx, "modules.load") |
Spandan Das | ad40292 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 244 | // If Load_by_default is set to false explicitly, create an empty modules.load |
| 245 | if pkm.properties.Load_by_default != nil && !*pkm.properties.Load_by_default { |
| 246 | builder.Command().Text("rm").Flag("-rf").Text(modulesLoad.String()) |
| 247 | builder.Command().Text("touch").Output(modulesLoad) |
| 248 | } else { |
| 249 | var basenames []string |
| 250 | for _, m := range modules { |
| 251 | basenames = append(basenames, filepath.Base(m.String())) |
| 252 | } |
| 253 | builder.Command(). |
| 254 | Text("echo").Flag("\"" + strings.Join(basenames, " ") + "\""). |
| 255 | Text("|").Text("tr").Flag("\" \"").Flag("\"\\n\""). |
| 256 | Text(">").Output(modulesLoad) |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 257 | } |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 258 | |
| 259 | // Run depmod to build modules.dep/softdep/alias files |
| 260 | modulesDep := modulesDir.Join(ctx, "modules.dep") |
| 261 | modulesSoftdep := modulesDir.Join(ctx, "modules.softdep") |
| 262 | modulesAlias := modulesDir.Join(ctx, "modules.alias") |
Spandan Das | eb426b7 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 263 | builder.Command().Text("mkdir").Flag("-p").Text(modulesDir.String()) |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 264 | builder.Command(). |
| 265 | BuiltTool("depmod"). |
| 266 | FlagWithArg("-b ", baseDir.String()). |
| 267 | Text(fakeVer). |
| 268 | ImplicitOutput(modulesDep). |
| 269 | ImplicitOutput(modulesSoftdep). |
| 270 | ImplicitOutput(modulesAlias) |
| 271 | |
| 272 | builder.Build("depmod", fmt.Sprintf("depmod %s", ctx.ModuleName())) |
| 273 | |
Spandan Das | eb426b7 | 2024-11-08 03:26:45 +0000 | [diff] [blame] | 274 | finalModulesDep := modulesDep |
| 275 | // Add a leading slash to paths in modules.dep of android dlkm |
| 276 | if ctx.InstallInSystemDlkm() || ctx.InstallInVendorDlkm() || ctx.InstallInOdmDlkm() { |
| 277 | finalModulesDep := modulesDep.ReplaceExtension(ctx, "intermediates") |
| 278 | ctx.Build(pctx, android.BuildParams{ |
| 279 | Rule: addLeadingSlashToPaths, |
| 280 | Input: modulesDep, |
| 281 | Output: finalModulesDep, |
| 282 | }) |
| 283 | } |
| 284 | |
| 285 | return depmodOutputs{modulesLoad, finalModulesDep, modulesSoftdep, modulesAlias} |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 286 | } |