blob: 8142f10a5eaa1b067089193ac3c8a71746874268 [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"
20 "strings"
21
22 "android/soong/android"
23 _ "android/soong/cc/config"
24
25 "github.com/google/blueprint"
26)
27
28func init() {
markchien2f59ec92020-09-02 16:23:38 +080029 android.RegisterModuleType("bpf", BpfFactory)
Colin Cross38406592018-05-17 11:17:01 -070030 pctx.Import("android/soong/cc/config")
31}
32
33var (
34 pctx = android.NewPackageContext("android/soong/bpf")
35
Ramy Medhat8ea054a2020-01-27 14:19:44 -050036 ccRule = pctx.AndroidRemoteStaticRule("ccRule", android.RemoteRuleSupports{Goma: true},
Colin Cross38406592018-05-17 11:17:01 -070037 blueprint.RuleParams{
38 Depfile: "${out}.d",
39 Deps: blueprint.DepsGCC,
40 Command: "$ccCmd --target=bpf -c $cFlags -MD -MF ${out}.d -o $out $in",
41 CommandDeps: []string{"$ccCmd"},
42 },
43 "ccCmd", "cFlags")
44)
45
markchien2f59ec92020-09-02 16:23:38 +080046// BpfModule interface is used by the apex package to gather information from a bpf module.
47type BpfModule interface {
48 android.Module
49
50 OutputFiles(tag string) (android.Paths, error)
51}
52
Colin Cross38406592018-05-17 11:17:01 -070053type BpfProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -080054 Srcs []string `android:"path"`
Colin Cross38406592018-05-17 11:17:01 -070055 Cflags []string
56 Include_dirs []string
57}
58
59type bpf struct {
60 android.ModuleBase
61
62 properties BpfProperties
63
64 objs android.Paths
65}
66
67func (bpf *bpf) GenerateAndroidBuildActions(ctx android.ModuleContext) {
68 cflags := []string{
69 "-nostdlibinc",
Kousik Kumarfb0e2512020-03-25 15:01:27 -070070
71 // Make paths in deps files relative
72 "-no-canonical-prefixes",
73
Colin Cross38406592018-05-17 11:17:01 -070074 "-O2",
75 "-isystem bionic/libc/include",
76 "-isystem bionic/libc/kernel/uapi",
77 // The architecture doesn't matter here, but asm/types.h is included by linux/types.h.
78 "-isystem bionic/libc/kernel/uapi/asm-arm64",
79 "-isystem bionic/libc/kernel/android/uapi",
Maciej Żenczykowski79f6f752020-02-18 15:38:36 -080080 // TODO(b/149785767): only give access to specific file with AID_* constants
81 "-I system/core/libcutils/include",
Joel Fernandes (Google)a2fd4862019-02-12 12:51:13 -050082 "-I system/bpf/progs/include",
Colin Cross38406592018-05-17 11:17:01 -070083 "-I " + ctx.ModuleDir(),
84 }
85
86 for _, dir := range android.PathsForSource(ctx, bpf.properties.Include_dirs) {
87 cflags = append(cflags, "-I "+dir.String())
88 }
89
90 cflags = append(cflags, bpf.properties.Cflags...)
91
Colin Cross8a497952019-03-05 22:25:09 -080092 srcs := android.PathsForModuleSrc(ctx, bpf.properties.Srcs)
Colin Cross38406592018-05-17 11:17:01 -070093
94 for _, src := range srcs {
95 obj := android.ObjPathWithExt(ctx, "", src, "o")
96
97 ctx.Build(pctx, android.BuildParams{
Colin Cross815daf92019-05-14 16:05:20 -070098 Rule: ccRule,
Colin Cross38406592018-05-17 11:17:01 -070099 Input: src,
100 Output: obj,
101 Args: map[string]string{
102 "cFlags": strings.Join(cflags, " "),
103 "ccCmd": "${config.ClangBin}/clang",
104 },
105 })
106
Colin Cross0adfee52019-04-10 11:30:12 -0700107 bpf.objs = append(bpf.objs, obj.WithoutRel())
Colin Cross38406592018-05-17 11:17:01 -0700108 }
109}
110
Colin Cross38406592018-05-17 11:17:01 -0700111func (bpf *bpf) AndroidMk() android.AndroidMkData {
112 return android.AndroidMkData{
113 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
114 var names []string
115 fmt.Fprintln(w)
116 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
117 fmt.Fprintln(w)
118 for _, obj := range bpf.objs {
119 objName := name + "_" + obj.Base()
120 names = append(names, objName)
121 fmt.Fprintln(w, "include $(CLEAR_VARS)")
122 fmt.Fprintln(w, "LOCAL_MODULE := ", objName)
Bob Badourb4999222021-01-07 03:34:31 +0000123 data.Entries.WriteLicenseVariables(w)
Colin Cross38406592018-05-17 11:17:01 -0700124 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", obj.String())
125 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", obj.Base())
126 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
127 fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/bpf")
128 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
129 fmt.Fprintln(w)
130 }
131 fmt.Fprintln(w, "include $(CLEAR_VARS)")
132 fmt.Fprintln(w, "LOCAL_MODULE := ", name)
Bob Badourb4999222021-01-07 03:34:31 +0000133 data.Entries.WriteLicenseVariables(w)
Colin Cross38406592018-05-17 11:17:01 -0700134 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(names, " "))
135 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
136 },
137 }
138}
139
Colin Cross41955e82019-05-29 14:40:35 -0700140// Implements OutputFileFileProducer interface so that the obj output can be used in the data property
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800141// of other modules.
Colin Cross41955e82019-05-29 14:40:35 -0700142func (bpf *bpf) OutputFiles(tag string) (android.Paths, error) {
143 switch tag {
144 case "":
145 return bpf.objs, nil
146 default:
147 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
148 }
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800149}
150
Colin Cross41955e82019-05-29 14:40:35 -0700151var _ android.OutputFileProducer = (*bpf)(nil)
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800152
markchien2f59ec92020-09-02 16:23:38 +0800153func BpfFactory() android.Module {
Colin Cross38406592018-05-17 11:17:01 -0700154 module := &bpf{}
155
156 module.AddProperties(&module.properties)
157
158 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
159 return module
160}