Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package bpf |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "strings" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | _ "android/soong/cc/config" |
| 24 | |
| 25 | "github.com/google/blueprint" |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame^] | 26 | "github.com/google/blueprint/proptools" |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func init() { |
Paul Duffin | 12c7eb8 | 2021-02-24 18:51:54 +0000 | [diff] [blame] | 30 | registerBpfBuildComponents(android.InitRegistrationContext) |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 31 | pctx.Import("android/soong/cc/config") |
| 32 | } |
| 33 | |
| 34 | var ( |
| 35 | pctx = android.NewPackageContext("android/soong/bpf") |
| 36 | |
Ramy Medhat | 8ea054a | 2020-01-27 14:19:44 -0500 | [diff] [blame] | 37 | ccRule = pctx.AndroidRemoteStaticRule("ccRule", android.RemoteRuleSupports{Goma: true}, |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 38 | 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'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame^] | 45 | |
| 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 Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 53 | ) |
| 54 | |
Paul Duffin | 12c7eb8 | 2021-02-24 18:51:54 +0000 | [diff] [blame] | 55 | func registerBpfBuildComponents(ctx android.RegistrationContext) { |
| 56 | ctx.RegisterModuleType("bpf", BpfFactory) |
| 57 | } |
| 58 | |
| 59 | var PrepareForTestWithBpf = android.FixtureRegisterWithContext(registerBpfBuildComponents) |
| 60 | |
markchien | 2f59ec9 | 2020-09-02 16:23:38 +0800 | [diff] [blame] | 61 | // BpfModule interface is used by the apex package to gather information from a bpf module. |
| 62 | type BpfModule interface { |
| 63 | android.Module |
| 64 | |
| 65 | OutputFiles(tag string) (android.Paths, error) |
Ken Chen | fad7f9d | 2021-11-10 22:02:57 +0800 | [diff] [blame] | 66 | |
| 67 | // Returns the sub install directory if the bpf module is included by apex. |
| 68 | SubDir() string |
markchien | 2f59ec9 | 2020-09-02 16:23:38 +0800 | [diff] [blame] | 69 | } |
| 70 | |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 71 | type BpfProperties struct { |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 72 | Srcs []string `android:"path"` |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 73 | Cflags []string |
| 74 | Include_dirs []string |
Ken Chen | fad7f9d | 2021-11-10 22:02:57 +0800 | [diff] [blame] | 75 | Sub_dir string |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame^] | 76 | // If set to true, generate BTF debug info for maps & programs |
| 77 | Btf *bool |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | type bpf struct { |
| 81 | android.ModuleBase |
| 82 | |
| 83 | properties BpfProperties |
| 84 | |
| 85 | objs android.Paths |
| 86 | } |
| 87 | |
| 88 | func (bpf *bpf) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 89 | cflags := []string{ |
| 90 | "-nostdlibinc", |
Kousik Kumar | fb0e251 | 2020-03-25 15:01:27 -0700 | [diff] [blame] | 91 | |
| 92 | // Make paths in deps files relative |
| 93 | "-no-canonical-prefixes", |
| 94 | |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 95 | "-O2", |
| 96 | "-isystem bionic/libc/include", |
| 97 | "-isystem bionic/libc/kernel/uapi", |
| 98 | // The architecture doesn't matter here, but asm/types.h is included by linux/types.h. |
| 99 | "-isystem bionic/libc/kernel/uapi/asm-arm64", |
| 100 | "-isystem bionic/libc/kernel/android/uapi", |
Ken Chen | fd26444 | 2021-12-20 18:22:55 +0800 | [diff] [blame] | 101 | "-I frameworks/libs/net/common/native/bpf_headers/include/bpf", |
Maciej Żenczykowski | 79f6f75 | 2020-02-18 15:38:36 -0800 | [diff] [blame] | 102 | // TODO(b/149785767): only give access to specific file with AID_* constants |
| 103 | "-I system/core/libcutils/include", |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 104 | "-I " + ctx.ModuleDir(), |
| 105 | } |
| 106 | |
| 107 | for _, dir := range android.PathsForSource(ctx, bpf.properties.Include_dirs) { |
| 108 | cflags = append(cflags, "-I "+dir.String()) |
| 109 | } |
| 110 | |
| 111 | cflags = append(cflags, bpf.properties.Cflags...) |
| 112 | |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame^] | 113 | if proptools.Bool(bpf.properties.Btf) { |
| 114 | cflags = append(cflags, "-g") |
| 115 | } |
| 116 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 117 | srcs := android.PathsForModuleSrc(ctx, bpf.properties.Srcs) |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 118 | |
| 119 | for _, src := range srcs { |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame^] | 120 | obj := android.ObjPathWithExt(ctx, "unstripped", src, "o") |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 121 | |
| 122 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 815daf9 | 2019-05-14 16:05:20 -0700 | [diff] [blame] | 123 | Rule: ccRule, |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 124 | Input: src, |
| 125 | Output: obj, |
| 126 | Args: map[string]string{ |
| 127 | "cFlags": strings.Join(cflags, " "), |
| 128 | "ccCmd": "${config.ClangBin}/clang", |
| 129 | }, |
| 130 | }) |
| 131 | |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame^] | 132 | if proptools.Bool(bpf.properties.Btf) { |
| 133 | objStripped := android.ObjPathWithExt(ctx, "", src, "o") |
| 134 | ctx.Build(pctx, android.BuildParams{ |
| 135 | Rule: stripRule, |
| 136 | Input: obj, |
| 137 | Output: objStripped, |
| 138 | Args: map[string]string{ |
| 139 | "stripCmd": "${config.ClangBin}/llvm-strip", |
| 140 | }, |
| 141 | }) |
| 142 | bpf.objs = append(bpf.objs, objStripped.WithoutRel()) |
| 143 | } else { |
| 144 | bpf.objs = append(bpf.objs, obj.WithoutRel()) |
| 145 | } |
| 146 | |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 150 | func (bpf *bpf) AndroidMk() android.AndroidMkData { |
| 151 | return android.AndroidMkData{ |
| 152 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 153 | var names []string |
| 154 | fmt.Fprintln(w) |
| 155 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 156 | fmt.Fprintln(w) |
Ken Chen | fad7f9d | 2021-11-10 22:02:57 +0800 | [diff] [blame] | 157 | localModulePath := "LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/bpf" |
| 158 | if len(bpf.properties.Sub_dir) > 0 { |
| 159 | localModulePath += "/" + bpf.properties.Sub_dir |
| 160 | } |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 161 | for _, obj := range bpf.objs { |
| 162 | objName := name + "_" + obj.Base() |
| 163 | names = append(names, objName) |
| 164 | fmt.Fprintln(w, "include $(CLEAR_VARS)") |
| 165 | fmt.Fprintln(w, "LOCAL_MODULE := ", objName) |
Bob Badour | b499922 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 166 | data.Entries.WriteLicenseVariables(w) |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 167 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", obj.String()) |
| 168 | fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", obj.Base()) |
| 169 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") |
Ken Chen | fad7f9d | 2021-11-10 22:02:57 +0800 | [diff] [blame] | 170 | fmt.Fprintln(w, localModulePath) |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 171 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 172 | fmt.Fprintln(w) |
| 173 | } |
| 174 | fmt.Fprintln(w, "include $(CLEAR_VARS)") |
| 175 | fmt.Fprintln(w, "LOCAL_MODULE := ", name) |
Bob Badour | b499922 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 176 | data.Entries.WriteLicenseVariables(w) |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 177 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(names, " ")) |
| 178 | fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)") |
| 179 | }, |
| 180 | } |
| 181 | } |
| 182 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 183 | // Implements OutputFileFileProducer interface so that the obj output can be used in the data property |
Jaewoong Jung | 5f3fb4b | 2018-12-13 15:01:46 -0800 | [diff] [blame] | 184 | // of other modules. |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 185 | func (bpf *bpf) OutputFiles(tag string) (android.Paths, error) { |
| 186 | switch tag { |
| 187 | case "": |
| 188 | return bpf.objs, nil |
| 189 | default: |
| 190 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 191 | } |
Jaewoong Jung | 5f3fb4b | 2018-12-13 15:01:46 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Ken Chen | fad7f9d | 2021-11-10 22:02:57 +0800 | [diff] [blame] | 194 | func (bpf *bpf) SubDir() string { |
| 195 | return bpf.properties.Sub_dir |
| 196 | } |
| 197 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 198 | var _ android.OutputFileProducer = (*bpf)(nil) |
Jaewoong Jung | 5f3fb4b | 2018-12-13 15:01:46 -0800 | [diff] [blame] | 199 | |
markchien | 2f59ec9 | 2020-09-02 16:23:38 +0800 | [diff] [blame] | 200 | func BpfFactory() android.Module { |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 201 | module := &bpf{} |
| 202 | |
| 203 | module.AddProperties(&module.properties) |
| 204 | |
| 205 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 206 | return module |
| 207 | } |