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