blob: 5d2533fc2ded4bddac9cdb9a3658d8a800123a8a [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"
Colin Cross38406592018-05-17 11:17:01 -070024
25 "github.com/google/blueprint"
Connor O'Brien25739652021-12-02 20:09:45 -080026 "github.com/google/blueprint/proptools"
Colin Cross38406592018-05-17 11:17:01 -070027)
28
29func init() {
Paul Duffin12c7eb82021-02-24 18:51:54 +000030 registerBpfBuildComponents(android.InitRegistrationContext)
Colin Cross38406592018-05-17 11:17:01 -070031 pctx.Import("android/soong/cc/config")
32}
33
34var (
35 pctx = android.NewPackageContext("android/soong/bpf")
36
Ramy Medhat8ea054a2020-01-27 14:19:44 -050037 ccRule = pctx.AndroidRemoteStaticRule("ccRule", android.RemoteRuleSupports{Goma: true},
Colin Cross38406592018-05-17 11:17:01 -070038 blueprint.RuleParams{
39 Depfile: "${out}.d",
40 Deps: blueprint.DepsGCC,
41 Command: "$ccCmd --target=bpf -c $cFlags -MD -MF ${out}.d -o $out $in",
42 CommandDeps: []string{"$ccCmd"},
43 },
44 "ccCmd", "cFlags")
Connor O'Brien25739652021-12-02 20:09:45 -080045
46 stripRule = pctx.AndroidStaticRule("stripRule",
47 blueprint.RuleParams{
48 Command: `$stripCmd --strip-unneeded --remove-section=.rel.BTF ` +
49 `--remove-section=.rel.BTF.ext --remove-section=.BTF.ext $in -o $out`,
50 CommandDeps: []string{"$stripCmd"},
51 },
52 "stripCmd")
Colin Cross38406592018-05-17 11:17:01 -070053)
54
Paul Duffin12c7eb82021-02-24 18:51:54 +000055func registerBpfBuildComponents(ctx android.RegistrationContext) {
56 ctx.RegisterModuleType("bpf", BpfFactory)
57}
58
59var PrepareForTestWithBpf = android.FixtureRegisterWithContext(registerBpfBuildComponents)
60
markchien2f59ec92020-09-02 16:23:38 +080061// BpfModule interface is used by the apex package to gather information from a bpf module.
62type BpfModule interface {
63 android.Module
64
65 OutputFiles(tag string) (android.Paths, error)
Ken Chenfad7f9d2021-11-10 22:02:57 +080066
67 // Returns the sub install directory if the bpf module is included by apex.
68 SubDir() string
markchien2f59ec92020-09-02 16:23:38 +080069}
70
Colin Cross38406592018-05-17 11:17:01 -070071type BpfProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -080072 Srcs []string `android:"path"`
Colin Cross38406592018-05-17 11:17:01 -070073 Cflags []string
74 Include_dirs []string
Ken Chenfad7f9d2021-11-10 22:02:57 +080075 Sub_dir string
Connor O'Brien25739652021-12-02 20:09:45 -080076 // If set to true, generate BTF debug info for maps & programs
Steven Moreland606c5e92019-12-12 14:23:42 -080077 Btf *bool
78 Vendor *bool
79
80 VendorInternal bool `blueprint:"mutated"`
Colin Cross38406592018-05-17 11:17:01 -070081}
82
83type bpf struct {
84 android.ModuleBase
85
86 properties BpfProperties
87
88 objs android.Paths
89}
90
Steven Moreland606c5e92019-12-12 14:23:42 -080091var _ android.ImageInterface = (*bpf)(nil)
92
93func (bpf *bpf) ImageMutatorBegin(ctx android.BaseModuleContext) {}
94
95func (bpf *bpf) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
96 return !proptools.Bool(bpf.properties.Vendor)
97}
98
99func (bpf *bpf) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
100 return false
101}
102
103func (bpf *bpf) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
104 return false
105}
106
107func (bpf *bpf) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
108 return false
109}
110
111func (bpf *bpf) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
112 return false
113}
114
115func (bpf *bpf) ExtraImageVariations(ctx android.BaseModuleContext) []string {
116 if proptools.Bool(bpf.properties.Vendor) {
117 return []string{"vendor"}
118 }
119 return nil
120}
121
122func (bpf *bpf) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
123 bpf.properties.VendorInternal = variation == "vendor"
124}
125
Colin Cross38406592018-05-17 11:17:01 -0700126func (bpf *bpf) GenerateAndroidBuildActions(ctx android.ModuleContext) {
127 cflags := []string{
128 "-nostdlibinc",
Kousik Kumarfb0e2512020-03-25 15:01:27 -0700129
130 // Make paths in deps files relative
131 "-no-canonical-prefixes",
132
Colin Cross38406592018-05-17 11:17:01 -0700133 "-O2",
134 "-isystem bionic/libc/include",
135 "-isystem bionic/libc/kernel/uapi",
136 // The architecture doesn't matter here, but asm/types.h is included by linux/types.h.
137 "-isystem bionic/libc/kernel/uapi/asm-arm64",
138 "-isystem bionic/libc/kernel/android/uapi",
Ken Chenfd264442021-12-20 18:22:55 +0800139 "-I frameworks/libs/net/common/native/bpf_headers/include/bpf",
Maciej Żenczykowski79f6f752020-02-18 15:38:36 -0800140 // TODO(b/149785767): only give access to specific file with AID_* constants
141 "-I system/core/libcutils/include",
Colin Cross38406592018-05-17 11:17:01 -0700142 "-I " + ctx.ModuleDir(),
143 }
144
145 for _, dir := range android.PathsForSource(ctx, bpf.properties.Include_dirs) {
146 cflags = append(cflags, "-I "+dir.String())
147 }
148
149 cflags = append(cflags, bpf.properties.Cflags...)
150
Connor O'Brien25739652021-12-02 20:09:45 -0800151 if proptools.Bool(bpf.properties.Btf) {
152 cflags = append(cflags, "-g")
153 }
154
Colin Cross8a497952019-03-05 22:25:09 -0800155 srcs := android.PathsForModuleSrc(ctx, bpf.properties.Srcs)
Colin Cross38406592018-05-17 11:17:01 -0700156
157 for _, src := range srcs {
Ken Chen5372a242022-07-07 17:48:06 +0800158 if strings.ContainsRune(filepath.Base(src.String()), '_') {
159 ctx.ModuleErrorf("invalid character '_' in source name")
160 }
Connor O'Brien25739652021-12-02 20:09:45 -0800161 obj := android.ObjPathWithExt(ctx, "unstripped", src, "o")
Colin Cross38406592018-05-17 11:17:01 -0700162
163 ctx.Build(pctx, android.BuildParams{
Colin Cross815daf92019-05-14 16:05:20 -0700164 Rule: ccRule,
Colin Cross38406592018-05-17 11:17:01 -0700165 Input: src,
166 Output: obj,
167 Args: map[string]string{
168 "cFlags": strings.Join(cflags, " "),
169 "ccCmd": "${config.ClangBin}/clang",
170 },
171 })
172
Connor O'Brien25739652021-12-02 20:09:45 -0800173 if proptools.Bool(bpf.properties.Btf) {
174 objStripped := android.ObjPathWithExt(ctx, "", src, "o")
175 ctx.Build(pctx, android.BuildParams{
Steven Moreland606c5e92019-12-12 14:23:42 -0800176 Rule: stripRule,
177 Input: obj,
Connor O'Brien25739652021-12-02 20:09:45 -0800178 Output: objStripped,
179 Args: map[string]string{
180 "stripCmd": "${config.ClangBin}/llvm-strip",
181 },
182 })
183 bpf.objs = append(bpf.objs, objStripped.WithoutRel())
184 } else {
185 bpf.objs = append(bpf.objs, obj.WithoutRel())
186 }
187
Colin Cross38406592018-05-17 11:17:01 -0700188 }
189}
190
Colin Cross38406592018-05-17 11:17:01 -0700191func (bpf *bpf) AndroidMk() android.AndroidMkData {
192 return android.AndroidMkData{
193 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
194 var names []string
195 fmt.Fprintln(w)
196 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
197 fmt.Fprintln(w)
Steven Moreland606c5e92019-12-12 14:23:42 -0800198 var localModulePath string
199 if bpf.properties.VendorInternal {
200 localModulePath = "LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_ETC)/bpf"
201 } else {
202 localModulePath = "LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/bpf"
203 }
Ken Chenfad7f9d2021-11-10 22:02:57 +0800204 if len(bpf.properties.Sub_dir) > 0 {
205 localModulePath += "/" + bpf.properties.Sub_dir
206 }
Colin Cross38406592018-05-17 11:17:01 -0700207 for _, obj := range bpf.objs {
208 objName := name + "_" + obj.Base()
209 names = append(names, objName)
210 fmt.Fprintln(w, "include $(CLEAR_VARS)")
211 fmt.Fprintln(w, "LOCAL_MODULE := ", objName)
Bob Badourb4999222021-01-07 03:34:31 +0000212 data.Entries.WriteLicenseVariables(w)
Colin Cross38406592018-05-17 11:17:01 -0700213 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", obj.String())
214 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", obj.Base())
215 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
Ken Chenfad7f9d2021-11-10 22:02:57 +0800216 fmt.Fprintln(w, localModulePath)
Colin Cross38406592018-05-17 11:17:01 -0700217 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
218 fmt.Fprintln(w)
219 }
220 fmt.Fprintln(w, "include $(CLEAR_VARS)")
221 fmt.Fprintln(w, "LOCAL_MODULE := ", name)
Bob Badourb4999222021-01-07 03:34:31 +0000222 data.Entries.WriteLicenseVariables(w)
Colin Cross38406592018-05-17 11:17:01 -0700223 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(names, " "))
224 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
225 },
226 }
227}
228
Colin Cross41955e82019-05-29 14:40:35 -0700229// Implements OutputFileFileProducer interface so that the obj output can be used in the data property
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800230// of other modules.
Colin Cross41955e82019-05-29 14:40:35 -0700231func (bpf *bpf) OutputFiles(tag string) (android.Paths, error) {
232 switch tag {
233 case "":
234 return bpf.objs, nil
235 default:
236 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
237 }
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800238}
239
Ken Chenfad7f9d2021-11-10 22:02:57 +0800240func (bpf *bpf) SubDir() string {
241 return bpf.properties.Sub_dir
242}
243
Colin Cross41955e82019-05-29 14:40:35 -0700244var _ android.OutputFileProducer = (*bpf)(nil)
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800245
markchien2f59ec92020-09-02 16:23:38 +0800246func BpfFactory() android.Module {
Colin Cross38406592018-05-17 11:17:01 -0700247 module := &bpf{}
248
249 module.AddProperties(&module.properties)
250
251 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
252 return module
253}