blob: 2490455171d448dde5bbdab6d5ab66c870aebe81 [file] [log] [blame]
Jiyong Park6446b622021-02-01 20:08:28 +09001// 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
15package kernel
16
17import (
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
29func init() {
Jiyong Park6446b622021-02-01 20:08:28 +090030 pctx.Import("android/soong/cc/config")
Paul Duffine5ac2502021-03-29 01:24:49 +010031 registerKernelBuildComponents(android.InitRegistrationContext)
32}
33
34func registerKernelBuildComponents(ctx android.RegistrationContext) {
Spandan Das5e336422024-11-01 22:31:20 +000035 ctx.RegisterModuleType("prebuilt_kernel_modules", PrebuiltKernelModulesFactory)
Jiyong Park6446b622021-02-01 20:08:28 +090036}
37
38type prebuiltKernelModules struct {
39 android.ModuleBase
40
41 properties prebuiltKernelModulesProperties
42
43 installDir android.InstallPath
44}
45
46type prebuiltKernelModulesProperties struct {
47 // List or filegroup of prebuilt kernel module files. Should have .ko suffix.
48 Srcs []string `android:"path,arch_variant"`
49
Spandan Daseb426b72024-11-08 03:26:45 +000050 // 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 Dasad402922024-11-08 03:26:45 +000055 // 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 Das6dfcbdf2024-11-11 18:43:07 +000059 Blocklist_file *string `android:"path"`
60
Jiyong Park6446b622021-02-01 20:08:28 +090061 // 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 Kim6a463f82023-11-06 18:07:13 +090064
65 // Whether this module is directly installable to one of the partitions. Default is true
66 Installable *bool
Jiyong Park6446b622021-02-01 20:08:28 +090067}
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 Das5e336422024-11-01 22:31:20 +000072func PrebuiltKernelModulesFactory() android.Module {
Jiyong Park6446b622021-02-01 20:08:28 +090073 module := &prebuiltKernelModules{}
74 module.AddProperties(&module.properties)
75 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
76 return module
77}
78
Inseob Kim6a463f82023-11-06 18:07:13 +090079func (pkm *prebuiltKernelModules) installable() bool {
80 return proptools.BoolDefault(pkm.properties.Installable, true)
81}
82
Jiyong Park6446b622021-02-01 20:08:28 +090083func (pkm *prebuiltKernelModules) KernelVersion() string {
84 return proptools.StringDefault(pkm.properties.Kernel_version, "")
85}
86
87func (pkm *prebuiltKernelModules) DepsMutator(ctx android.BottomUpMutatorContext) {
88 // do nothing
89}
90
91func (pkm *prebuiltKernelModules) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim6a463f82023-11-06 18:07:13 +090092 if !pkm.installable() {
93 pkm.SkipInstall()
94 }
Spandan Daseb426b72024-11-08 03:26:45 +000095
Jiyong Park6446b622021-02-01 20:08:28 +090096 modules := android.PathsForModuleSrc(ctx, pkm.properties.Srcs)
Spandan Daseb426b72024-11-08 03:26:45 +000097 systemModules := android.PathsForModuleSrc(ctx, pkm.properties.System_deps)
Jiyong Park6446b622021-02-01 20:08:28 +090098
Spandan Dasad402922024-11-08 03:26:45 +000099 depmodOut := pkm.runDepmod(ctx, modules, systemModules)
Jiyong Park6446b622021-02-01 20:08:28 +0900100 strippedModules := stripDebugSymbols(ctx, modules)
101
Jiyong Park599992b2021-02-04 19:40:56 +0900102 installDir := android.PathForModuleInstall(ctx, "lib", "modules")
Jihoon Kangf6b5e8f2024-11-27 00:33:30 +0000103 // 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 Park6446b622021-02-01 20:08:28 +0900110 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 Das6dfcbdf2024-11-11 18:43:07 +0000121 pkm.installBlocklistFile(ctx, installDir)
Spandan Daseb426b72024-11-08 03:26:45 +0000122
123 ctx.SetOutputFiles(modules, ".modules")
Jiyong Park6446b622021-02-01 20:08:28 +0900124}
125
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000126func (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 Park6446b622021-02-01 20:08:28 +0900140var (
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
150func 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
170type depmodOutputs struct {
171 modulesLoad android.OutputPath
172 modulesDep android.OutputPath
173 modulesSoftdep android.OutputPath
174 modulesAlias android.OutputPath
175}
176
Spandan Daseb426b72024-11-08 03:26:45 +0000177var (
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 Das6dfcbdf2024-11-11 18:43:07 +0000186 // 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 Daseb426b72024-11-08 03:26:45 +0000199)
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.
203func 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 Dasad402922024-11-08 03:26:45 +0000219func (pkm *prebuiltKernelModules) runDepmod(ctx android.ModuleContext, modules android.Paths, systemModules android.Paths) depmodOutputs {
Jiyong Park6446b622021-02-01 20:08:28 +0900220 baseDir := android.PathForModuleOut(ctx, "depmod").OutputPath
221 fakeVer := "0.0" // depmod demands this anyway
222 modulesDir := baseDir.Join(ctx, "lib", "modules", fakeVer)
Spandan Daseb426b72024-11-08 03:26:45 +0000223 modulesCpDir := modulesDirForAndroidDlkm(ctx, modulesDir, false)
Jiyong Park6446b622021-02-01 20:08:28 +0900224
225 builder := android.NewRuleBuilder(pctx, ctx)
226
227 // Copy the module files to a temporary dir
Spandan Daseb426b72024-11-08 03:26:45 +0000228 builder.Command().Text("rm").Flag("-rf").Text(modulesCpDir.String())
229 builder.Command().Text("mkdir").Flag("-p").Text(modulesCpDir.String())
Jiyong Park6446b622021-02-01 20:08:28 +0900230 for _, m := range modules {
Spandan Daseb426b72024-11-08 03:26:45 +0000231 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 Park6446b622021-02-01 20:08:28 +0900240 }
241
242 // Enumerate modules to load
243 modulesLoad := modulesDir.Join(ctx, "modules.load")
Spandan Dasad402922024-11-08 03:26:45 +0000244 // 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 Park6446b622021-02-01 20:08:28 +0900257 }
Jiyong Park6446b622021-02-01 20:08:28 +0900258
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 Daseb426b72024-11-08 03:26:45 +0000263 builder.Command().Text("mkdir").Flag("-p").Text(modulesDir.String())
Jiyong Park6446b622021-02-01 20:08:28 +0900264 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 Daseb426b72024-11-08 03:26:45 +0000274 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 Park6446b622021-02-01 20:08:28 +0900286}