blob: 4cdfb315ce6672ebf8ce7e6d12aad4f3b46ca010 [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() {
29 android.RegisterModuleType("bpf", bpfFactory)
30 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
46type BpfProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -080047 Srcs []string `android:"path"`
Colin Cross38406592018-05-17 11:17:01 -070048 Cflags []string
49 Include_dirs []string
50}
51
52type bpf struct {
53 android.ModuleBase
54
55 properties BpfProperties
56
57 objs android.Paths
58}
59
60func (bpf *bpf) GenerateAndroidBuildActions(ctx android.ModuleContext) {
61 cflags := []string{
62 "-nostdlibinc",
Kousik Kumarfb0e2512020-03-25 15:01:27 -070063
64 // Make paths in deps files relative
65 "-no-canonical-prefixes",
66
Colin Cross38406592018-05-17 11:17:01 -070067 "-O2",
68 "-isystem bionic/libc/include",
69 "-isystem bionic/libc/kernel/uapi",
70 // The architecture doesn't matter here, but asm/types.h is included by linux/types.h.
71 "-isystem bionic/libc/kernel/uapi/asm-arm64",
72 "-isystem bionic/libc/kernel/android/uapi",
Maciej Żenczykowski79f6f752020-02-18 15:38:36 -080073 // TODO(b/149785767): only give access to specific file with AID_* constants
74 "-I system/core/libcutils/include",
Joel Fernandes (Google)a2fd4862019-02-12 12:51:13 -050075 "-I system/bpf/progs/include",
Colin Cross38406592018-05-17 11:17:01 -070076 "-I " + ctx.ModuleDir(),
77 }
78
79 for _, dir := range android.PathsForSource(ctx, bpf.properties.Include_dirs) {
80 cflags = append(cflags, "-I "+dir.String())
81 }
82
83 cflags = append(cflags, bpf.properties.Cflags...)
84
Colin Cross8a497952019-03-05 22:25:09 -080085 srcs := android.PathsForModuleSrc(ctx, bpf.properties.Srcs)
Colin Cross38406592018-05-17 11:17:01 -070086
87 for _, src := range srcs {
88 obj := android.ObjPathWithExt(ctx, "", src, "o")
89
90 ctx.Build(pctx, android.BuildParams{
Colin Cross815daf92019-05-14 16:05:20 -070091 Rule: ccRule,
Colin Cross38406592018-05-17 11:17:01 -070092 Input: src,
93 Output: obj,
94 Args: map[string]string{
95 "cFlags": strings.Join(cflags, " "),
96 "ccCmd": "${config.ClangBin}/clang",
97 },
98 })
99
Colin Cross0adfee52019-04-10 11:30:12 -0700100 bpf.objs = append(bpf.objs, obj.WithoutRel())
Colin Cross38406592018-05-17 11:17:01 -0700101 }
102}
103
Colin Cross38406592018-05-17 11:17:01 -0700104func (bpf *bpf) AndroidMk() android.AndroidMkData {
105 return android.AndroidMkData{
106 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
107 var names []string
108 fmt.Fprintln(w)
109 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
110 fmt.Fprintln(w)
111 for _, obj := range bpf.objs {
112 objName := name + "_" + obj.Base()
113 names = append(names, objName)
114 fmt.Fprintln(w, "include $(CLEAR_VARS)")
115 fmt.Fprintln(w, "LOCAL_MODULE := ", objName)
116 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", obj.String())
117 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", obj.Base())
118 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
119 fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/bpf")
120 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
121 fmt.Fprintln(w)
122 }
123 fmt.Fprintln(w, "include $(CLEAR_VARS)")
124 fmt.Fprintln(w, "LOCAL_MODULE := ", name)
125 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(names, " "))
126 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
127 },
128 }
129}
130
Colin Cross41955e82019-05-29 14:40:35 -0700131// Implements OutputFileFileProducer interface so that the obj output can be used in the data property
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800132// of other modules.
Colin Cross41955e82019-05-29 14:40:35 -0700133func (bpf *bpf) OutputFiles(tag string) (android.Paths, error) {
134 switch tag {
135 case "":
136 return bpf.objs, nil
137 default:
138 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
139 }
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800140}
141
Colin Cross41955e82019-05-29 14:40:35 -0700142var _ android.OutputFileProducer = (*bpf)(nil)
Jaewoong Jung5f3fb4b2018-12-13 15:01:46 -0800143
Colin Cross38406592018-05-17 11:17:01 -0700144func bpfFactory() android.Module {
145 module := &bpf{}
146
147 module.AddProperties(&module.properties)
148
149 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
150 return module
151}