blob: 13d648235223750f204f15c3ef5cf006257544e6 [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")
Jiyong Park6446b622021-02-01 20:08:28 +0900103 if pkm.KernelVersion() != "" {
104 installDir = installDir.Join(ctx, pkm.KernelVersion())
105 }
106
107 for _, m := range strippedModules {
108 ctx.InstallFile(installDir, filepath.Base(m.String()), m)
109 }
110 ctx.InstallFile(installDir, "modules.load", depmodOut.modulesLoad)
111 ctx.InstallFile(installDir, "modules.dep", depmodOut.modulesDep)
112 ctx.InstallFile(installDir, "modules.softdep", depmodOut.modulesSoftdep)
113 ctx.InstallFile(installDir, "modules.alias", depmodOut.modulesAlias)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000114 pkm.installBlocklistFile(ctx, installDir)
Spandan Daseb426b72024-11-08 03:26:45 +0000115
116 ctx.SetOutputFiles(modules, ".modules")
Jiyong Park6446b622021-02-01 20:08:28 +0900117}
118
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000119func (pkm *prebuiltKernelModules) installBlocklistFile(ctx android.ModuleContext, installDir android.InstallPath) {
120 if pkm.properties.Blocklist_file == nil {
121 return
122 }
123 blocklistOut := android.PathForModuleOut(ctx, "modules.blocklist")
124
125 ctx.Build(pctx, android.BuildParams{
126 Rule: processBlocklistFile,
127 Input: android.PathForModuleSrc(ctx, proptools.String(pkm.properties.Blocklist_file)),
128 Output: blocklistOut,
129 })
130 ctx.InstallFile(installDir, "modules.blocklist", blocklistOut)
131}
132
Jiyong Park6446b622021-02-01 20:08:28 +0900133var (
134 pctx = android.NewPackageContext("android/soong/kernel")
135
136 stripRule = pctx.AndroidStaticRule("strip",
137 blueprint.RuleParams{
138 Command: "$stripCmd -o $out --strip-debug $in",
139 CommandDeps: []string{"$stripCmd"},
140 }, "stripCmd")
141)
142
143func stripDebugSymbols(ctx android.ModuleContext, modules android.Paths) android.OutputPaths {
144 dir := android.PathForModuleOut(ctx, "stripped").OutputPath
145 var outputs android.OutputPaths
146
147 for _, m := range modules {
148 stripped := dir.Join(ctx, filepath.Base(m.String()))
149 ctx.Build(pctx, android.BuildParams{
150 Rule: stripRule,
151 Input: m,
152 Output: stripped,
153 Args: map[string]string{
154 "stripCmd": "${config.ClangBin}/llvm-strip",
155 },
156 })
157 outputs = append(outputs, stripped)
158 }
159
160 return outputs
161}
162
163type depmodOutputs struct {
164 modulesLoad android.OutputPath
165 modulesDep android.OutputPath
166 modulesSoftdep android.OutputPath
167 modulesAlias android.OutputPath
168}
169
Spandan Daseb426b72024-11-08 03:26:45 +0000170var (
171 // system/lib/modules/foo.ko: system/lib/modules/bar.ko
172 // will be converted to
173 // /system/lib/modules/foo.ko: /system/lib/modules/bar.ko
174 addLeadingSlashToPaths = pctx.AndroidStaticRule("add_leading_slash",
175 blueprint.RuleParams{
176 Command: `sed -e 's|\([^: ]*lib/modules/[^: ]*\)|/\1|g' $in > $out`,
177 },
178 )
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000179 // Remove empty lines. Raise an exception if line is _not_ formatted as `blocklist $name.ko`
180 processBlocklistFile = pctx.AndroidStaticRule("process_blocklist_file",
181 blueprint.RuleParams{
182 Command: `rm -rf $out && awk <$in > $out` +
183 ` '/^#/ { print; next }` +
184 ` NF == 0 { next }` +
185 ` NF != 2 || $$1 != "blocklist"` +
186 ` { print "Invalid blocklist line " FNR ": " $$0 >"/dev/stderr";` +
187 ` exit_status = 1; next }` +
188 ` { $$1 = $$1; print }` +
189 ` END { exit exit_status }'`,
190 },
191 )
Spandan Daseb426b72024-11-08 03:26:45 +0000192)
193
194// This is the path in soong intermediates where the .ko files will be copied.
195// The layout should match the layout on device so that depmod can create meaningful modules.* files.
196func modulesDirForAndroidDlkm(ctx android.ModuleContext, modulesDir android.OutputPath, system bool) android.OutputPath {
197 if ctx.InstallInSystemDlkm() || system {
198 // The first component can be either system or system_dlkm
199 // system works because /system/lib/modules is a symlink to /system_dlkm/lib/modules.
200 // system was chosen to match the contents of the kati built modules.dep
201 return modulesDir.Join(ctx, "system", "lib", "modules")
202 } else if ctx.InstallInVendorDlkm() {
203 return modulesDir.Join(ctx, "vendor", "lib", "modules")
204 } else if ctx.InstallInOdmDlkm() {
205 return modulesDir.Join(ctx, "odm", "lib", "modules")
206 } else {
207 // not an android dlkm module.
208 return modulesDir
209 }
210}
211
Spandan Dasad402922024-11-08 03:26:45 +0000212func (pkm *prebuiltKernelModules) runDepmod(ctx android.ModuleContext, modules android.Paths, systemModules android.Paths) depmodOutputs {
Jiyong Park6446b622021-02-01 20:08:28 +0900213 baseDir := android.PathForModuleOut(ctx, "depmod").OutputPath
214 fakeVer := "0.0" // depmod demands this anyway
215 modulesDir := baseDir.Join(ctx, "lib", "modules", fakeVer)
Spandan Daseb426b72024-11-08 03:26:45 +0000216 modulesCpDir := modulesDirForAndroidDlkm(ctx, modulesDir, false)
Jiyong Park6446b622021-02-01 20:08:28 +0900217
218 builder := android.NewRuleBuilder(pctx, ctx)
219
220 // Copy the module files to a temporary dir
Spandan Daseb426b72024-11-08 03:26:45 +0000221 builder.Command().Text("rm").Flag("-rf").Text(modulesCpDir.String())
222 builder.Command().Text("mkdir").Flag("-p").Text(modulesCpDir.String())
Jiyong Park6446b622021-02-01 20:08:28 +0900223 for _, m := range modules {
Spandan Daseb426b72024-11-08 03:26:45 +0000224 builder.Command().Text("cp").Input(m).Text(modulesCpDir.String())
225 }
226
227 modulesDirForSystemDlkm := modulesDirForAndroidDlkm(ctx, modulesDir, true)
228 if len(systemModules) > 0 {
229 builder.Command().Text("mkdir").Flag("-p").Text(modulesDirForSystemDlkm.String())
230 }
231 for _, m := range systemModules {
232 builder.Command().Text("cp").Input(m).Text(modulesDirForSystemDlkm.String())
Jiyong Park6446b622021-02-01 20:08:28 +0900233 }
234
235 // Enumerate modules to load
236 modulesLoad := modulesDir.Join(ctx, "modules.load")
Spandan Dasad402922024-11-08 03:26:45 +0000237 // If Load_by_default is set to false explicitly, create an empty modules.load
238 if pkm.properties.Load_by_default != nil && !*pkm.properties.Load_by_default {
239 builder.Command().Text("rm").Flag("-rf").Text(modulesLoad.String())
240 builder.Command().Text("touch").Output(modulesLoad)
241 } else {
242 var basenames []string
243 for _, m := range modules {
244 basenames = append(basenames, filepath.Base(m.String()))
245 }
246 builder.Command().
247 Text("echo").Flag("\"" + strings.Join(basenames, " ") + "\"").
248 Text("|").Text("tr").Flag("\" \"").Flag("\"\\n\"").
249 Text(">").Output(modulesLoad)
Jiyong Park6446b622021-02-01 20:08:28 +0900250 }
Jiyong Park6446b622021-02-01 20:08:28 +0900251
252 // Run depmod to build modules.dep/softdep/alias files
253 modulesDep := modulesDir.Join(ctx, "modules.dep")
254 modulesSoftdep := modulesDir.Join(ctx, "modules.softdep")
255 modulesAlias := modulesDir.Join(ctx, "modules.alias")
Spandan Daseb426b72024-11-08 03:26:45 +0000256 builder.Command().Text("mkdir").Flag("-p").Text(modulesDir.String())
Jiyong Park6446b622021-02-01 20:08:28 +0900257 builder.Command().
258 BuiltTool("depmod").
259 FlagWithArg("-b ", baseDir.String()).
260 Text(fakeVer).
261 ImplicitOutput(modulesDep).
262 ImplicitOutput(modulesSoftdep).
263 ImplicitOutput(modulesAlias)
264
265 builder.Build("depmod", fmt.Sprintf("depmod %s", ctx.ModuleName()))
266
Spandan Daseb426b72024-11-08 03:26:45 +0000267 finalModulesDep := modulesDep
268 // Add a leading slash to paths in modules.dep of android dlkm
269 if ctx.InstallInSystemDlkm() || ctx.InstallInVendorDlkm() || ctx.InstallInOdmDlkm() {
270 finalModulesDep := modulesDep.ReplaceExtension(ctx, "intermediates")
271 ctx.Build(pctx, android.BuildParams{
272 Rule: addLeadingSlashToPaths,
273 Input: modulesDep,
274 Output: finalModulesDep,
275 })
276 }
277
278 return depmodOutputs{modulesLoad, finalModulesDep, modulesSoftdep, modulesAlias}
Jiyong Park6446b622021-02-01 20:08:28 +0900279}