blob: 60a410d7fcebc79c436886085182f2ae287d6b75 [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"
Colin Cross38406592018-05-17 11:17:01 -070021 "strings"
22
23 "android/soong/android"
Zi Wangb3cb38c2022-09-23 16:36:11 -070024 "android/soong/bazel"
Zi Wangaa981ed2022-10-04 16:59:31 -070025 "android/soong/bazel/cquery"
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")
34}
35
36var (
37 pctx = android.NewPackageContext("android/soong/bpf")
38
Ramy Medhat8ea054a2020-01-27 14:19:44 -050039 ccRule = pctx.AndroidRemoteStaticRule("ccRule", android.RemoteRuleSupports{Goma: true},
Colin Cross38406592018-05-17 11:17:01 -070040 blueprint.RuleParams{
41 Depfile: "${out}.d",
42 Deps: blueprint.DepsGCC,
43 Command: "$ccCmd --target=bpf -c $cFlags -MD -MF ${out}.d -o $out $in",
44 CommandDeps: []string{"$ccCmd"},
45 },
46 "ccCmd", "cFlags")
Connor O'Brien25739652021-12-02 20:09:45 -080047
48 stripRule = pctx.AndroidStaticRule("stripRule",
49 blueprint.RuleParams{
50 Command: `$stripCmd --strip-unneeded --remove-section=.rel.BTF ` +
51 `--remove-section=.rel.BTF.ext --remove-section=.BTF.ext $in -o $out`,
52 CommandDeps: []string{"$stripCmd"},
53 },
54 "stripCmd")
Colin Cross38406592018-05-17 11:17:01 -070055)
56
Paul Duffin12c7eb82021-02-24 18:51:54 +000057func registerBpfBuildComponents(ctx android.RegistrationContext) {
58 ctx.RegisterModuleType("bpf", BpfFactory)
59}
60
61var PrepareForTestWithBpf = android.FixtureRegisterWithContext(registerBpfBuildComponents)
62
markchien2f59ec92020-09-02 16:23:38 +080063// BpfModule interface is used by the apex package to gather information from a bpf module.
64type BpfModule interface {
65 android.Module
66
67 OutputFiles(tag string) (android.Paths, error)
Ken Chenfad7f9d2021-11-10 22:02:57 +080068
69 // Returns the sub install directory if the bpf module is included by apex.
70 SubDir() string
markchien2f59ec92020-09-02 16:23:38 +080071}
72
Colin Cross38406592018-05-17 11:17:01 -070073type BpfProperties struct {
Zi Wang4877c722022-08-11 18:05:13 +000074 // source paths to the files.
75 Srcs []string `android:"path"`
76
77 // additional cflags that should be used to build the bpf variant of
78 // the C/C++ module.
79 Cflags []string
80
81 // directories (relative to the root of the source tree) that will
82 // be added to the include paths using -I.
Colin Cross38406592018-05-17 11:17:01 -070083 Include_dirs []string
Zi Wang4877c722022-08-11 18:05:13 +000084
85 // optional subdirectory under which this module is installed into.
86 Sub_dir string
87
88 // if set to true, generate BTF debug info for maps & programs.
89 Btf *bool
90
Steven Moreland606c5e92019-12-12 14:23:42 -080091 Vendor *bool
92
93 VendorInternal bool `blueprint:"mutated"`
Colin Cross38406592018-05-17 11:17:01 -070094}
95
96type bpf struct {
97 android.ModuleBase
Zi Wangb3cb38c2022-09-23 16:36:11 -070098 android.BazelModuleBase
Colin Cross38406592018-05-17 11:17:01 -070099
100 properties BpfProperties
101
102 objs android.Paths
103}
104
Steven Moreland606c5e92019-12-12 14:23:42 -0800105var _ android.ImageInterface = (*bpf)(nil)
106
107func (bpf *bpf) ImageMutatorBegin(ctx android.BaseModuleContext) {}
108
109func (bpf *bpf) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
110 return !proptools.Bool(bpf.properties.Vendor)
111}
112
113func (bpf *bpf) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
114 return false
115}
116
117func (bpf *bpf) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
118 return false
119}
120
121func (bpf *bpf) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
122 return false
123}
124
125func (bpf *bpf) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
126 return false
127}
128
129func (bpf *bpf) ExtraImageVariations(ctx android.BaseModuleContext) []string {
130 if proptools.Bool(bpf.properties.Vendor) {
131 return []string{"vendor"}
132 }
133 return nil
134}
135
136func (bpf *bpf) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
137 bpf.properties.VendorInternal = variation == "vendor"
138}
139
Colin Cross38406592018-05-17 11:17:01 -0700140func (bpf *bpf) GenerateAndroidBuildActions(ctx android.ModuleContext) {
141 cflags := []string{
142 "-nostdlibinc",
Kousik Kumarfb0e2512020-03-25 15:01:27 -0700143
144 // Make paths in deps files relative
145 "-no-canonical-prefixes",
146
Colin Cross38406592018-05-17 11:17:01 -0700147 "-O2",
148 "-isystem bionic/libc/include",
149 "-isystem bionic/libc/kernel/uapi",
150 // The architecture doesn't matter here, but asm/types.h is included by linux/types.h.
151 "-isystem bionic/libc/kernel/uapi/asm-arm64",
152 "-isystem bionic/libc/kernel/android/uapi",
Ken Chenfd264442021-12-20 18:22:55 +0800153 "-I frameworks/libs/net/common/native/bpf_headers/include/bpf",
Maciej Żenczykowski79f6f752020-02-18 15:38:36 -0800154 // TODO(b/149785767): only give access to specific file with AID_* constants
155 "-I system/core/libcutils/include",
Colin Cross38406592018-05-17 11:17:01 -0700156 "-I " + ctx.ModuleDir(),
157 }
158
159 for _, dir := range android.PathsForSource(ctx, bpf.properties.Include_dirs) {
160 cflags = append(cflags, "-I "+dir.String())
161 }
162
163 cflags = append(cflags, bpf.properties.Cflags...)
164
Connor O'Brien25739652021-12-02 20:09:45 -0800165 if proptools.Bool(bpf.properties.Btf) {
166 cflags = append(cflags, "-g")
167 }
168
Colin Cross8a497952019-03-05 22:25:09 -0800169 srcs := android.PathsForModuleSrc(ctx, bpf.properties.Srcs)
Colin Cross38406592018-05-17 11:17:01 -0700170
171 for _, src := range srcs {
Ken Chen5372a242022-07-07 17:48:06 +0800172 if strings.ContainsRune(filepath.Base(src.String()), '_') {
173 ctx.ModuleErrorf("invalid character '_' in source name")
174 }
Connor O'Brien25739652021-12-02 20:09:45 -0800175 obj := android.ObjPathWithExt(ctx, "unstripped", src, "o")
Colin Cross38406592018-05-17 11:17:01 -0700176
177 ctx.Build(pctx, android.BuildParams{
Colin Cross815daf92019-05-14 16:05:20 -0700178 Rule: ccRule,
Colin Cross38406592018-05-17 11:17:01 -0700179 Input: src,
180 Output: obj,
181 Args: map[string]string{
182 "cFlags": strings.Join(cflags, " "),
183 "ccCmd": "${config.ClangBin}/clang",
184 },
185 })
186
Connor O'Brien25739652021-12-02 20:09:45 -0800187 if proptools.Bool(bpf.properties.Btf) {
188 objStripped := android.ObjPathWithExt(ctx, "", src, "o")
189 ctx.Build(pctx, android.BuildParams{
Steven Moreland606c5e92019-12-12 14:23:42 -0800190 Rule: stripRule,
191 Input: obj,
Connor O'Brien25739652021-12-02 20:09:45 -0800192 Output: objStripped,
193 Args: map[string]string{
194 "stripCmd": "${config.ClangBin}/llvm-strip",
195 },
196 })
197 bpf.objs = append(bpf.objs, objStripped.WithoutRel())
198 } else {
199 bpf.objs = append(bpf.objs, obj.WithoutRel())
200 }
201
Colin Cross38406592018-05-17 11:17:01 -0700202 }
203}
204
Colin Cross38406592018-05-17 11:17:01 -0700205func (bpf *bpf) AndroidMk() android.AndroidMkData {
206 return android.AndroidMkData{
207 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
208 var names []string
209 fmt.Fprintln(w)
210 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
211 fmt.Fprintln(w)
Steven Moreland606c5e92019-12-12 14:23:42 -0800212 var localModulePath string
213 if bpf.properties.VendorInternal {
214 localModulePath = "LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_ETC)/bpf"
215 } else {
216 localModulePath = "LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/bpf"
217 }
Ken Chenfad7f9d2021-11-10 22:02:57 +0800218 if len(bpf.properties.Sub_dir) > 0 {
219 localModulePath += "/" + bpf.properties.Sub_dir
220 }
Colin Cross38406592018-05-17 11:17:01 -0700221 for _, obj := range bpf.objs {
222 objName := name + "_" + obj.Base()
223 names = append(names, objName)
224 fmt.Fprintln(w, "include $(CLEAR_VARS)")
225 fmt.Fprintln(w, "LOCAL_MODULE := ", objName)
Bob Badourb4999222021-01-07 03:34:31 +0000226 data.Entries.WriteLicenseVariables(w)
Colin Cross38406592018-05-17 11:17:01 -0700227 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", obj.String())
228 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", obj.Base())
229 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
Ken Chenfad7f9d2021-11-10 22:02:57 +0800230 fmt.Fprintln(w, localModulePath)
Colin Cross38406592018-05-17 11:17:01 -0700231 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
232 fmt.Fprintln(w)
233 }
234 fmt.Fprintln(w, "include $(CLEAR_VARS)")
235 fmt.Fprintln(w, "LOCAL_MODULE := ", name)
Bob Badourb4999222021-01-07 03:34:31 +0000236 data.Entries.WriteLicenseVariables(w)
Colin Cross38406592018-05-17 11:17:01 -0700237 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(names, " "))
238 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
239 },
240 }
241}
242
Zi Wangaa981ed2022-10-04 16:59:31 -0700243var _ android.MixedBuildBuildable = (*bpf)(nil)
244
245func (bpf *bpf) IsMixedBuildSupported(ctx android.BaseModuleContext) bool {
246 return true
247}
248
249func (bpf *bpf) QueueBazelCall(ctx android.BaseModuleContext) {
250 bazelCtx := ctx.Config().BazelContext
251 bazelCtx.QueueBazelRequest(
252 bpf.GetBazelLabel(ctx, bpf),
253 cquery.GetOutputFiles,
254 android.GetConfigKey(ctx))
255}
256
257func (bpf *bpf) ProcessBazelQueryResponse(ctx android.ModuleContext) {
258 bazelCtx := ctx.Config().BazelContext
259 objPaths, err := bazelCtx.GetOutputFiles(bpf.GetBazelLabel(ctx, bpf), android.GetConfigKey(ctx))
260 if err != nil {
261 ctx.ModuleErrorf(err.Error())
262 return
263 }
264
265 bazelOuts := android.Paths{}
266 for _, p := range objPaths {
267 bazelOuts = append(bazelOuts, android.PathForBazelOut(ctx, p))
268 }
269 bpf.objs = bazelOuts
270}
271
Colin Cross41955e82019-05-29 14:40:35 -0700272// Implements OutputFileFileProducer interface so that the obj output can be used in the data property
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800273// of other modules.
Colin Cross41955e82019-05-29 14:40:35 -0700274func (bpf *bpf) OutputFiles(tag string) (android.Paths, error) {
275 switch tag {
276 case "":
277 return bpf.objs, nil
278 default:
279 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
280 }
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800281}
282
Ken Chenfad7f9d2021-11-10 22:02:57 +0800283func (bpf *bpf) SubDir() string {
284 return bpf.properties.Sub_dir
285}
286
Colin Cross41955e82019-05-29 14:40:35 -0700287var _ android.OutputFileProducer = (*bpf)(nil)
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800288
markchien2f59ec92020-09-02 16:23:38 +0800289func BpfFactory() android.Module {
Colin Cross38406592018-05-17 11:17:01 -0700290 module := &bpf{}
291
292 module.AddProperties(&module.properties)
293
294 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Zi Wangb3cb38c2022-09-23 16:36:11 -0700295 android.InitBazelModule(module)
Colin Cross38406592018-05-17 11:17:01 -0700296 return module
297}
Zi Wangb3cb38c2022-09-23 16:36:11 -0700298
299type bazelBpfAttributes struct {
300 Srcs bazel.LabelListAttribute
301 Copts bazel.StringListAttribute
302 Absolute_includes bazel.StringListAttribute
303 Btf *bool
304 // TODO(b/249528391): Add support for sub_dir
305}
306
307// bpf bp2build converter
308func (b *bpf) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
309 if ctx.ModuleType() != "bpf" {
310 return
311 }
312
313 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, b.properties.Srcs))
314 copts := bazel.MakeStringListAttribute(b.properties.Cflags)
315 absolute_includes := bazel.MakeStringListAttribute(b.properties.Include_dirs)
316 btf := b.properties.Btf
317
318 attrs := bazelBpfAttributes{
319 Srcs: srcs,
320 Copts: copts,
321 Absolute_includes: absolute_includes,
322 Btf: btf,
323 }
324 props := bazel.BazelTargetModuleProperties{
325 Rule_class: "bpf",
326 Bzl_load_location: "//build/bazel/rules/bpf:bpf.bzl",
327 }
328
329 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: b.Name()}, &attrs)
330}