blob: deb465dd62d18fb192ba38b69e34d2cfcf5294dd [file] [log] [blame]
Colin Cross38406592018-05-17 11:17:01 -07001// Copyright (C) 2018 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 bpf
16
17import (
18 "fmt"
19 "io"
Ken Chen5372a242022-07-07 17:48:06 +080020 "path/filepath"
Connor O'Brien3e739cf2022-08-17 15:45:52 -070021 "runtime"
Colin Cross38406592018-05-17 11:17:01 -070022 "strings"
23
24 "android/soong/android"
Connor O'Brien6a288bc2022-11-10 13:58:48 -080025 "android/soong/cc"
Colin Cross38406592018-05-17 11:17:01 -070026
27 "github.com/google/blueprint"
Connor O'Brien25739652021-12-02 20:09:45 -080028 "github.com/google/blueprint/proptools"
Colin Cross38406592018-05-17 11:17:01 -070029)
30
31func init() {
Paul Duffin12c7eb82021-02-24 18:51:54 +000032 registerBpfBuildComponents(android.InitRegistrationContext)
Colin Cross38406592018-05-17 11:17:01 -070033 pctx.Import("android/soong/cc/config")
Connor O'Brien6a288bc2022-11-10 13:58:48 -080034 pctx.StaticVariable("relPwd", cc.PwdPrefix())
Colin Cross38406592018-05-17 11:17:01 -070035}
36
37var (
38 pctx = android.NewPackageContext("android/soong/bpf")
39
Ramy Medhat8ea054a2020-01-27 14:19:44 -050040 ccRule = pctx.AndroidRemoteStaticRule("ccRule", android.RemoteRuleSupports{Goma: true},
Colin Cross38406592018-05-17 11:17:01 -070041 blueprint.RuleParams{
42 Depfile: "${out}.d",
43 Deps: blueprint.DepsGCC,
Connor O'Brien3e739cf2022-08-17 15:45:52 -070044 Command: "$relPwd $ccCmd --target=bpf -c $cFlags -MD -MF ${out}.d -o $out $in",
Colin Cross38406592018-05-17 11:17:01 -070045 CommandDeps: []string{"$ccCmd"},
46 },
47 "ccCmd", "cFlags")
Connor O'Brien25739652021-12-02 20:09:45 -080048
49 stripRule = pctx.AndroidStaticRule("stripRule",
50 blueprint.RuleParams{
51 Command: `$stripCmd --strip-unneeded --remove-section=.rel.BTF ` +
52 `--remove-section=.rel.BTF.ext --remove-section=.BTF.ext $in -o $out`,
53 CommandDeps: []string{"$stripCmd"},
54 },
55 "stripCmd")
Colin Cross38406592018-05-17 11:17:01 -070056)
57
Paul Duffin12c7eb82021-02-24 18:51:54 +000058func registerBpfBuildComponents(ctx android.RegistrationContext) {
Neill Kaprona898bb82024-08-30 20:55:22 +000059 ctx.RegisterModuleType("bpf_defaults", defaultsFactory)
Paul Duffin12c7eb82021-02-24 18:51:54 +000060 ctx.RegisterModuleType("bpf", BpfFactory)
61}
62
Yu Liu0a37d422025-02-13 02:05:00 +000063type BpfInfo struct {
64 SubDir string
65}
66
67var BpfInfoProvider = blueprint.NewProvider[BpfInfo]()
68
Paul Duffin12c7eb82021-02-24 18:51:54 +000069var PrepareForTestWithBpf = android.FixtureRegisterWithContext(registerBpfBuildComponents)
70
markchien2f59ec92020-09-02 16:23:38 +080071// BpfModule interface is used by the apex package to gather information from a bpf module.
72type BpfModule interface {
73 android.Module
74
Ken Chenfad7f9d2021-11-10 22:02:57 +080075 // Returns the sub install directory if the bpf module is included by apex.
76 SubDir() string
markchien2f59ec92020-09-02 16:23:38 +080077}
78
Colin Cross38406592018-05-17 11:17:01 -070079type BpfProperties struct {
Zi Wang4877c722022-08-11 18:05:13 +000080 // source paths to the files.
81 Srcs []string `android:"path"`
82
83 // additional cflags that should be used to build the bpf variant of
84 // the C/C++ module.
85 Cflags []string
86
Neill Kaprona898bb82024-08-30 20:55:22 +000087 // list of directories relative to the root of the source tree that
88 // will be added to the include paths using -I.
89 // If possible, don't use this. If adding paths from the current
90 // directory, use local_include_dirs. If adding paths from other
91 // modules, use export_include_dirs in that module.
Colin Cross38406592018-05-17 11:17:01 -070092 Include_dirs []string
Zi Wang4877c722022-08-11 18:05:13 +000093
Neill Kaprona898bb82024-08-30 20:55:22 +000094 // list of directories relative to the Blueprint file that will be
95 // added to the include path using -I.
96 Local_include_dirs []string
Zi Wang4877c722022-08-11 18:05:13 +000097 // optional subdirectory under which this module is installed into.
98 Sub_dir string
99
100 // if set to true, generate BTF debug info for maps & programs.
101 Btf *bool
102
Steven Moreland606c5e92019-12-12 14:23:42 -0800103 Vendor *bool
104
105 VendorInternal bool `blueprint:"mutated"`
Colin Cross38406592018-05-17 11:17:01 -0700106}
107
108type bpf struct {
109 android.ModuleBase
Neill Kaprona898bb82024-08-30 20:55:22 +0000110 android.DefaultableModuleBase
Colin Cross38406592018-05-17 11:17:01 -0700111 properties BpfProperties
112
113 objs android.Paths
114}
115
Steven Moreland606c5e92019-12-12 14:23:42 -0800116var _ android.ImageInterface = (*bpf)(nil)
117
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700118func (bpf *bpf) ImageMutatorBegin(ctx android.ImageInterfaceContext) {}
Steven Moreland606c5e92019-12-12 14:23:42 -0800119
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700120func (bpf *bpf) VendorVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang47e91842024-06-19 00:51:16 +0000121 return proptools.Bool(bpf.properties.Vendor)
122}
123
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700124func (bpf *bpf) ProductVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang47e91842024-06-19 00:51:16 +0000125 return false
126}
127
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700128func (bpf *bpf) CoreVariantNeeded(ctx android.ImageInterfaceContext) bool {
Steven Moreland606c5e92019-12-12 14:23:42 -0800129 return !proptools.Bool(bpf.properties.Vendor)
130}
131
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700132func (bpf *bpf) RamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Steven Moreland606c5e92019-12-12 14:23:42 -0800133 return false
134}
135
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700136func (bpf *bpf) VendorRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Steven Moreland606c5e92019-12-12 14:23:42 -0800137 return false
138}
139
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700140func (bpf *bpf) DebugRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Steven Moreland606c5e92019-12-12 14:23:42 -0800141 return false
142}
143
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700144func (bpf *bpf) RecoveryVariantNeeded(ctx android.ImageInterfaceContext) bool {
Steven Moreland606c5e92019-12-12 14:23:42 -0800145 return false
146}
147
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700148func (bpf *bpf) ExtraImageVariations(ctx android.ImageInterfaceContext) []string {
Steven Moreland606c5e92019-12-12 14:23:42 -0800149 return nil
150}
151
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700152func (bpf *bpf) SetImageVariation(ctx android.ImageInterfaceContext, variation string) {
Steven Moreland606c5e92019-12-12 14:23:42 -0800153 bpf.properties.VendorInternal = variation == "vendor"
154}
155
Colin Cross38406592018-05-17 11:17:01 -0700156func (bpf *bpf) GenerateAndroidBuildActions(ctx android.ModuleContext) {
157 cflags := []string{
158 "-nostdlibinc",
Kousik Kumarfb0e2512020-03-25 15:01:27 -0700159
160 // Make paths in deps files relative
161 "-no-canonical-prefixes",
162
Colin Cross38406592018-05-17 11:17:01 -0700163 "-O2",
Neill Kapron50a7bf82024-07-30 22:03:27 +0000164 "-Wall",
165 "-Werror",
166 "-Wextra",
167
Colin Cross38406592018-05-17 11:17:01 -0700168 "-isystem bionic/libc/include",
169 "-isystem bionic/libc/kernel/uapi",
170 // The architecture doesn't matter here, but asm/types.h is included by linux/types.h.
171 "-isystem bionic/libc/kernel/uapi/asm-arm64",
172 "-isystem bionic/libc/kernel/android/uapi",
Maciej Żenczykowski57af3392024-08-19 16:18:52 -0700173 "-I packages/modules/Connectivity/bpf/headers/include",
Maciej Żenczykowski79f6f752020-02-18 15:38:36 -0800174 // TODO(b/149785767): only give access to specific file with AID_* constants
175 "-I system/core/libcutils/include",
Colin Cross38406592018-05-17 11:17:01 -0700176 "-I " + ctx.ModuleDir(),
177 }
178
Neill Kaprona898bb82024-08-30 20:55:22 +0000179 for _, dir := range android.PathsForModuleSrc(ctx, bpf.properties.Local_include_dirs) {
180 cflags = append(cflags, "-I "+dir.String())
181 }
182
Colin Cross38406592018-05-17 11:17:01 -0700183 for _, dir := range android.PathsForSource(ctx, bpf.properties.Include_dirs) {
184 cflags = append(cflags, "-I "+dir.String())
185 }
186
187 cflags = append(cflags, bpf.properties.Cflags...)
188
Neill Kapron50a7bf82024-07-30 22:03:27 +0000189 if proptools.BoolDefault(bpf.properties.Btf, true) {
Connor O'Brien25739652021-12-02 20:09:45 -0800190 cflags = append(cflags, "-g")
Connor O'Brien3e739cf2022-08-17 15:45:52 -0700191 if runtime.GOOS != "darwin" {
192 cflags = append(cflags, "-fdebug-prefix-map=/proc/self/cwd=")
193 }
Connor O'Brien25739652021-12-02 20:09:45 -0800194 }
195
Colin Cross8a497952019-03-05 22:25:09 -0800196 srcs := android.PathsForModuleSrc(ctx, bpf.properties.Srcs)
Colin Cross38406592018-05-17 11:17:01 -0700197
198 for _, src := range srcs {
Ken Chen5372a242022-07-07 17:48:06 +0800199 if strings.ContainsRune(filepath.Base(src.String()), '_') {
200 ctx.ModuleErrorf("invalid character '_' in source name")
201 }
Connor O'Brien25739652021-12-02 20:09:45 -0800202 obj := android.ObjPathWithExt(ctx, "unstripped", src, "o")
Colin Cross38406592018-05-17 11:17:01 -0700203
204 ctx.Build(pctx, android.BuildParams{
Colin Cross815daf92019-05-14 16:05:20 -0700205 Rule: ccRule,
Colin Cross38406592018-05-17 11:17:01 -0700206 Input: src,
207 Output: obj,
208 Args: map[string]string{
209 "cFlags": strings.Join(cflags, " "),
210 "ccCmd": "${config.ClangBin}/clang",
211 },
212 })
213
Neill Kapron50a7bf82024-07-30 22:03:27 +0000214 if proptools.BoolDefault(bpf.properties.Btf, true) {
Connor O'Brien25739652021-12-02 20:09:45 -0800215 objStripped := android.ObjPathWithExt(ctx, "", src, "o")
216 ctx.Build(pctx, android.BuildParams{
Steven Moreland606c5e92019-12-12 14:23:42 -0800217 Rule: stripRule,
218 Input: obj,
Connor O'Brien25739652021-12-02 20:09:45 -0800219 Output: objStripped,
220 Args: map[string]string{
221 "stripCmd": "${config.ClangBin}/llvm-strip",
222 },
223 })
224 bpf.objs = append(bpf.objs, objStripped.WithoutRel())
225 } else {
226 bpf.objs = append(bpf.objs, obj.WithoutRel())
227 }
228
Colin Cross38406592018-05-17 11:17:01 -0700229 }
Jiyong Park06c4cdc2024-02-16 15:35:03 +0900230
231 installDir := android.PathForModuleInstall(ctx, "etc", "bpf")
232 if len(bpf.properties.Sub_dir) > 0 {
233 installDir = installDir.Join(ctx, bpf.properties.Sub_dir)
234 }
235 for _, obj := range bpf.objs {
236 ctx.PackageFile(installDir, obj.Base(), obj)
237 }
238
Yu Liu0a37d422025-02-13 02:05:00 +0000239 android.SetProvider(ctx, BpfInfoProvider, BpfInfo{
240 SubDir: bpf.SubDir(),
241 })
242
mrziwange6c85812024-05-22 14:36:09 -0700243 ctx.SetOutputFiles(bpf.objs, "")
Colin Cross38406592018-05-17 11:17:01 -0700244}
245
Colin Cross38406592018-05-17 11:17:01 -0700246func (bpf *bpf) AndroidMk() android.AndroidMkData {
247 return android.AndroidMkData{
248 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
249 var names []string
250 fmt.Fprintln(w)
251 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
252 fmt.Fprintln(w)
Steven Moreland606c5e92019-12-12 14:23:42 -0800253 var localModulePath string
254 if bpf.properties.VendorInternal {
255 localModulePath = "LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_ETC)/bpf"
256 } else {
257 localModulePath = "LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/bpf"
258 }
Ken Chenfad7f9d2021-11-10 22:02:57 +0800259 if len(bpf.properties.Sub_dir) > 0 {
260 localModulePath += "/" + bpf.properties.Sub_dir
261 }
Colin Cross38406592018-05-17 11:17:01 -0700262 for _, obj := range bpf.objs {
263 objName := name + "_" + obj.Base()
264 names = append(names, objName)
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800265 fmt.Fprintln(w, "include $(CLEAR_VARS)", " # bpf.bpf.obj")
Colin Cross38406592018-05-17 11:17:01 -0700266 fmt.Fprintln(w, "LOCAL_MODULE := ", objName)
267 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", obj.String())
268 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", obj.Base())
269 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
Ken Chenfad7f9d2021-11-10 22:02:57 +0800270 fmt.Fprintln(w, localModulePath)
LaMont Jonesb5099382024-01-10 23:42:36 +0000271 // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here.
272 for _, extra := range data.Extra {
273 extra(w, nil)
274 }
Colin Cross38406592018-05-17 11:17:01 -0700275 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
276 fmt.Fprintln(w)
277 }
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800278 fmt.Fprintln(w, "include $(CLEAR_VARS)", " # bpf.bpf")
Colin Cross38406592018-05-17 11:17:01 -0700279 fmt.Fprintln(w, "LOCAL_MODULE := ", name)
Sasha Smundakdcb61292022-12-08 10:41:33 -0800280 android.AndroidMkEmitAssignList(w, "LOCAL_REQUIRED_MODULES", names)
Colin Cross38406592018-05-17 11:17:01 -0700281 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
282 },
283 }
284}
285
Neill Kaprona898bb82024-08-30 20:55:22 +0000286type Defaults struct {
287 android.ModuleBase
288 android.DefaultsModuleBase
289}
290
291func defaultsFactory() android.Module {
292 return DefaultsFactory()
293}
294
295func DefaultsFactory(props ...interface{}) android.Module {
296 module := &Defaults{}
297
298 module.AddProperties(props...)
299 module.AddProperties(&BpfProperties{})
300
301 android.InitDefaultsModule(module)
302
303 return module
304}
305
Ken Chenfad7f9d2021-11-10 22:02:57 +0800306func (bpf *bpf) SubDir() string {
307 return bpf.properties.Sub_dir
308}
309
markchien2f59ec92020-09-02 16:23:38 +0800310func BpfFactory() android.Module {
Colin Cross38406592018-05-17 11:17:01 -0700311 module := &bpf{}
312
313 module.AddProperties(&module.properties)
314
315 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Neill Kaprona898bb82024-08-30 20:55:22 +0000316 android.InitDefaultableModule(module)
317
Colin Cross38406592018-05-17 11:17:01 -0700318 return module
319}