blob: 644539426ac7ef860bec556e5772cb6e4aa0aeb0 [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) {
59 ctx.RegisterModuleType("bpf", BpfFactory)
60}
61
62var PrepareForTestWithBpf = android.FixtureRegisterWithContext(registerBpfBuildComponents)
63
markchien2f59ec92020-09-02 16:23:38 +080064// BpfModule interface is used by the apex package to gather information from a bpf module.
65type BpfModule interface {
66 android.Module
67
Ken Chenfad7f9d2021-11-10 22:02:57 +080068 // Returns the sub install directory if the bpf module is included by apex.
69 SubDir() string
markchien2f59ec92020-09-02 16:23:38 +080070}
71
Colin Cross38406592018-05-17 11:17:01 -070072type BpfProperties struct {
Zi Wang4877c722022-08-11 18:05:13 +000073 // source paths to the files.
74 Srcs []string `android:"path"`
75
76 // additional cflags that should be used to build the bpf variant of
77 // the C/C++ module.
78 Cflags []string
79
80 // directories (relative to the root of the source tree) that will
81 // be added to the include paths using -I.
Colin Cross38406592018-05-17 11:17:01 -070082 Include_dirs []string
Zi Wang4877c722022-08-11 18:05:13 +000083
84 // optional subdirectory under which this module is installed into.
85 Sub_dir string
86
87 // if set to true, generate BTF debug info for maps & programs.
88 Btf *bool
89
Steven Moreland606c5e92019-12-12 14:23:42 -080090 Vendor *bool
91
92 VendorInternal bool `blueprint:"mutated"`
Colin Cross38406592018-05-17 11:17:01 -070093}
94
95type bpf struct {
96 android.ModuleBase
97
98 properties BpfProperties
99
100 objs android.Paths
101}
102
Steven Moreland606c5e92019-12-12 14:23:42 -0800103var _ android.ImageInterface = (*bpf)(nil)
104
105func (bpf *bpf) ImageMutatorBegin(ctx android.BaseModuleContext) {}
106
Jihoon Kang47e91842024-06-19 00:51:16 +0000107func (bpf *bpf) VendorVariantNeeded(ctx android.BaseModuleContext) bool {
108 return proptools.Bool(bpf.properties.Vendor)
109}
110
111func (bpf *bpf) ProductVariantNeeded(ctx android.BaseModuleContext) bool {
112 return false
113}
114
Steven Moreland606c5e92019-12-12 14:23:42 -0800115func (bpf *bpf) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
116 return !proptools.Bool(bpf.properties.Vendor)
117}
118
119func (bpf *bpf) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
120 return false
121}
122
123func (bpf *bpf) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
124 return false
125}
126
127func (bpf *bpf) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
128 return false
129}
130
131func (bpf *bpf) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
132 return false
133}
134
135func (bpf *bpf) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Steven Moreland606c5e92019-12-12 14:23:42 -0800136 return nil
137}
138
Jihoon Kang7583e832024-06-13 21:25:45 +0000139func (bpf *bpf) SetImageVariation(ctx android.BaseModuleContext, variation string) {
Steven Moreland606c5e92019-12-12 14:23:42 -0800140 bpf.properties.VendorInternal = variation == "vendor"
141}
142
Colin Cross38406592018-05-17 11:17:01 -0700143func (bpf *bpf) GenerateAndroidBuildActions(ctx android.ModuleContext) {
144 cflags := []string{
145 "-nostdlibinc",
Kousik Kumarfb0e2512020-03-25 15:01:27 -0700146
147 // Make paths in deps files relative
148 "-no-canonical-prefixes",
149
Colin Cross38406592018-05-17 11:17:01 -0700150 "-O2",
Neill Kapron50a7bf82024-07-30 22:03:27 +0000151 "-Wall",
152 "-Werror",
153 "-Wextra",
154
Colin Cross38406592018-05-17 11:17:01 -0700155 "-isystem bionic/libc/include",
156 "-isystem bionic/libc/kernel/uapi",
157 // The architecture doesn't matter here, but asm/types.h is included by linux/types.h.
158 "-isystem bionic/libc/kernel/uapi/asm-arm64",
159 "-isystem bionic/libc/kernel/android/uapi",
Motomu Utsumi44591552023-08-22 12:03:16 +0900160 "-I packages/modules/Connectivity/staticlibs/native/bpf_headers/include/bpf",
Maciej Żenczykowski79f6f752020-02-18 15:38:36 -0800161 // TODO(b/149785767): only give access to specific file with AID_* constants
162 "-I system/core/libcutils/include",
Colin Cross38406592018-05-17 11:17:01 -0700163 "-I " + ctx.ModuleDir(),
164 }
165
166 for _, dir := range android.PathsForSource(ctx, bpf.properties.Include_dirs) {
167 cflags = append(cflags, "-I "+dir.String())
168 }
169
170 cflags = append(cflags, bpf.properties.Cflags...)
171
Neill Kapron50a7bf82024-07-30 22:03:27 +0000172 if proptools.BoolDefault(bpf.properties.Btf, true) {
Connor O'Brien25739652021-12-02 20:09:45 -0800173 cflags = append(cflags, "-g")
Connor O'Brien3e739cf2022-08-17 15:45:52 -0700174 if runtime.GOOS != "darwin" {
175 cflags = append(cflags, "-fdebug-prefix-map=/proc/self/cwd=")
176 }
Connor O'Brien25739652021-12-02 20:09:45 -0800177 }
178
Colin Cross8a497952019-03-05 22:25:09 -0800179 srcs := android.PathsForModuleSrc(ctx, bpf.properties.Srcs)
Colin Cross38406592018-05-17 11:17:01 -0700180
181 for _, src := range srcs {
Ken Chen5372a242022-07-07 17:48:06 +0800182 if strings.ContainsRune(filepath.Base(src.String()), '_') {
183 ctx.ModuleErrorf("invalid character '_' in source name")
184 }
Connor O'Brien25739652021-12-02 20:09:45 -0800185 obj := android.ObjPathWithExt(ctx, "unstripped", src, "o")
Colin Cross38406592018-05-17 11:17:01 -0700186
187 ctx.Build(pctx, android.BuildParams{
Colin Cross815daf92019-05-14 16:05:20 -0700188 Rule: ccRule,
Colin Cross38406592018-05-17 11:17:01 -0700189 Input: src,
190 Output: obj,
191 Args: map[string]string{
192 "cFlags": strings.Join(cflags, " "),
193 "ccCmd": "${config.ClangBin}/clang",
194 },
195 })
196
Neill Kapron50a7bf82024-07-30 22:03:27 +0000197 if proptools.BoolDefault(bpf.properties.Btf, true) {
Connor O'Brien25739652021-12-02 20:09:45 -0800198 objStripped := android.ObjPathWithExt(ctx, "", src, "o")
199 ctx.Build(pctx, android.BuildParams{
Steven Moreland606c5e92019-12-12 14:23:42 -0800200 Rule: stripRule,
201 Input: obj,
Connor O'Brien25739652021-12-02 20:09:45 -0800202 Output: objStripped,
203 Args: map[string]string{
204 "stripCmd": "${config.ClangBin}/llvm-strip",
205 },
206 })
207 bpf.objs = append(bpf.objs, objStripped.WithoutRel())
208 } else {
209 bpf.objs = append(bpf.objs, obj.WithoutRel())
210 }
211
Colin Cross38406592018-05-17 11:17:01 -0700212 }
Jiyong Park06c4cdc2024-02-16 15:35:03 +0900213
214 installDir := android.PathForModuleInstall(ctx, "etc", "bpf")
215 if len(bpf.properties.Sub_dir) > 0 {
216 installDir = installDir.Join(ctx, bpf.properties.Sub_dir)
217 }
218 for _, obj := range bpf.objs {
219 ctx.PackageFile(installDir, obj.Base(), obj)
220 }
221
Colin Cross40213022023-12-13 15:19:49 -0800222 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: srcs.Strings()})
mrziwange6c85812024-05-22 14:36:09 -0700223
224 ctx.SetOutputFiles(bpf.objs, "")
Colin Cross38406592018-05-17 11:17:01 -0700225}
226
Colin Cross38406592018-05-17 11:17:01 -0700227func (bpf *bpf) AndroidMk() android.AndroidMkData {
228 return android.AndroidMkData{
229 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
230 var names []string
231 fmt.Fprintln(w)
232 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
233 fmt.Fprintln(w)
Steven Moreland606c5e92019-12-12 14:23:42 -0800234 var localModulePath string
235 if bpf.properties.VendorInternal {
236 localModulePath = "LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_ETC)/bpf"
237 } else {
238 localModulePath = "LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/bpf"
239 }
Ken Chenfad7f9d2021-11-10 22:02:57 +0800240 if len(bpf.properties.Sub_dir) > 0 {
241 localModulePath += "/" + bpf.properties.Sub_dir
242 }
Colin Cross38406592018-05-17 11:17:01 -0700243 for _, obj := range bpf.objs {
244 objName := name + "_" + obj.Base()
245 names = append(names, objName)
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800246 fmt.Fprintln(w, "include $(CLEAR_VARS)", " # bpf.bpf.obj")
Colin Cross38406592018-05-17 11:17:01 -0700247 fmt.Fprintln(w, "LOCAL_MODULE := ", objName)
248 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", obj.String())
249 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", obj.Base())
250 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
Ken Chenfad7f9d2021-11-10 22:02:57 +0800251 fmt.Fprintln(w, localModulePath)
LaMont Jonesb5099382024-01-10 23:42:36 +0000252 // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here.
253 for _, extra := range data.Extra {
254 extra(w, nil)
255 }
Colin Cross38406592018-05-17 11:17:01 -0700256 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
257 fmt.Fprintln(w)
258 }
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800259 fmt.Fprintln(w, "include $(CLEAR_VARS)", " # bpf.bpf")
Colin Cross38406592018-05-17 11:17:01 -0700260 fmt.Fprintln(w, "LOCAL_MODULE := ", name)
Sasha Smundakdcb61292022-12-08 10:41:33 -0800261 android.AndroidMkEmitAssignList(w, "LOCAL_REQUIRED_MODULES", names)
Colin Cross38406592018-05-17 11:17:01 -0700262 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
263 },
264 }
265}
266
Ken Chenfad7f9d2021-11-10 22:02:57 +0800267func (bpf *bpf) SubDir() string {
268 return bpf.properties.Sub_dir
269}
270
markchien2f59ec92020-09-02 16:23:38 +0800271func BpfFactory() android.Module {
Colin Cross38406592018-05-17 11:17:01 -0700272 module := &bpf{}
273
274 module.AddProperties(&module.properties)
275
276 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
277 return module
278}