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" |
Ken Chen | 5372a24 | 2022-07-07 17:48:06 +0800 | [diff] [blame] | 20 | "path/filepath" |
Connor O'Brien | 3e739cf | 2022-08-17 15:45:52 -0700 | [diff] [blame] | 21 | "runtime" |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 22 | "strings" |
| 23 | |
| 24 | "android/soong/android" |
Connor O'Brien | 6a288bc | 2022-11-10 13:58:48 -0800 | [diff] [blame] | 25 | "android/soong/cc" |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 26 | |
| 27 | "github.com/google/blueprint" |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame] | 28 | "github.com/google/blueprint/proptools" |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func init() { |
Paul Duffin | 12c7eb8 | 2021-02-24 18:51:54 +0000 | [diff] [blame] | 32 | registerBpfBuildComponents(android.InitRegistrationContext) |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 33 | pctx.Import("android/soong/cc/config") |
Connor O'Brien | 6a288bc | 2022-11-10 13:58:48 -0800 | [diff] [blame] | 34 | pctx.StaticVariable("relPwd", cc.PwdPrefix()) |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | var ( |
| 38 | pctx = android.NewPackageContext("android/soong/bpf") |
| 39 | |
Ramy Medhat | 8ea054a | 2020-01-27 14:19:44 -0500 | [diff] [blame] | 40 | ccRule = pctx.AndroidRemoteStaticRule("ccRule", android.RemoteRuleSupports{Goma: true}, |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 41 | blueprint.RuleParams{ |
| 42 | Depfile: "${out}.d", |
| 43 | Deps: blueprint.DepsGCC, |
Connor O'Brien | 3e739cf | 2022-08-17 15:45:52 -0700 | [diff] [blame] | 44 | Command: "$relPwd $ccCmd --target=bpf -c $cFlags -MD -MF ${out}.d -o $out $in", |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 45 | CommandDeps: []string{"$ccCmd"}, |
| 46 | }, |
| 47 | "ccCmd", "cFlags") |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame] | 48 | |
| 49 | stripRule = pctx.AndroidStaticRule("stripRule", |
| 50 | blueprint.RuleParams{ |
| 51 | Command: `$stripCmd --strip-unneeded --remove-section=.rel.BTF ` + |
| 52 | `--remove-section=.rel.BTF.ext --remove-section=.BTF.ext $in -o $out`, |
| 53 | CommandDeps: []string{"$stripCmd"}, |
| 54 | }, |
| 55 | "stripCmd") |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 56 | ) |
| 57 | |
Paul Duffin | 12c7eb8 | 2021-02-24 18:51:54 +0000 | [diff] [blame] | 58 | func registerBpfBuildComponents(ctx android.RegistrationContext) { |
| 59 | ctx.RegisterModuleType("bpf", BpfFactory) |
| 60 | } |
| 61 | |
| 62 | var PrepareForTestWithBpf = android.FixtureRegisterWithContext(registerBpfBuildComponents) |
| 63 | |
markchien | 2f59ec9 | 2020-09-02 16:23:38 +0800 | [diff] [blame] | 64 | // BpfModule interface is used by the apex package to gather information from a bpf module. |
| 65 | type BpfModule interface { |
| 66 | android.Module |
| 67 | |
Ken Chen | fad7f9d | 2021-11-10 22:02:57 +0800 | [diff] [blame] | 68 | // Returns the sub install directory if the bpf module is included by apex. |
| 69 | SubDir() string |
markchien | 2f59ec9 | 2020-09-02 16:23:38 +0800 | [diff] [blame] | 70 | } |
| 71 | |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 72 | type BpfProperties struct { |
Zi Wang | 4877c72 | 2022-08-11 18:05:13 +0000 | [diff] [blame] | 73 | // source paths to the files. |
| 74 | Srcs []string `android:"path"` |
| 75 | |
| 76 | // additional cflags that should be used to build the bpf variant of |
| 77 | // the C/C++ module. |
| 78 | Cflags []string |
| 79 | |
| 80 | // directories (relative to the root of the source tree) that will |
| 81 | // be added to the include paths using -I. |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 82 | Include_dirs []string |
Zi Wang | 4877c72 | 2022-08-11 18:05:13 +0000 | [diff] [blame] | 83 | |
| 84 | // optional subdirectory under which this module is installed into. |
| 85 | Sub_dir string |
| 86 | |
| 87 | // if set to true, generate BTF debug info for maps & programs. |
| 88 | Btf *bool |
| 89 | |
Steven Moreland | 606c5e9 | 2019-12-12 14:23:42 -0800 | [diff] [blame] | 90 | Vendor *bool |
| 91 | |
| 92 | VendorInternal bool `blueprint:"mutated"` |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | type bpf struct { |
| 96 | android.ModuleBase |
| 97 | |
| 98 | properties BpfProperties |
| 99 | |
| 100 | objs android.Paths |
| 101 | } |
| 102 | |
Steven Moreland | 606c5e9 | 2019-12-12 14:23:42 -0800 | [diff] [blame] | 103 | var _ android.ImageInterface = (*bpf)(nil) |
| 104 | |
| 105 | func (bpf *bpf) ImageMutatorBegin(ctx android.BaseModuleContext) {} |
| 106 | |
| 107 | func (bpf *bpf) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
| 108 | return !proptools.Bool(bpf.properties.Vendor) |
| 109 | } |
| 110 | |
| 111 | func (bpf *bpf) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 112 | return false |
| 113 | } |
| 114 | |
| 115 | func (bpf *bpf) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 116 | return false |
| 117 | } |
| 118 | |
| 119 | func (bpf *bpf) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 120 | return false |
| 121 | } |
| 122 | |
| 123 | func (bpf *bpf) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { |
| 124 | return false |
| 125 | } |
| 126 | |
| 127 | func (bpf *bpf) ExtraImageVariations(ctx android.BaseModuleContext) []string { |
| 128 | if proptools.Bool(bpf.properties.Vendor) { |
| 129 | return []string{"vendor"} |
| 130 | } |
| 131 | return nil |
| 132 | } |
| 133 | |
| 134 | func (bpf *bpf) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { |
| 135 | bpf.properties.VendorInternal = variation == "vendor" |
| 136 | } |
| 137 | |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 138 | func (bpf *bpf) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 139 | cflags := []string{ |
| 140 | "-nostdlibinc", |
Kousik Kumar | fb0e251 | 2020-03-25 15:01:27 -0700 | [diff] [blame] | 141 | |
| 142 | // Make paths in deps files relative |
| 143 | "-no-canonical-prefixes", |
| 144 | |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 145 | "-O2", |
| 146 | "-isystem bionic/libc/include", |
| 147 | "-isystem bionic/libc/kernel/uapi", |
| 148 | // The architecture doesn't matter here, but asm/types.h is included by linux/types.h. |
| 149 | "-isystem bionic/libc/kernel/uapi/asm-arm64", |
| 150 | "-isystem bionic/libc/kernel/android/uapi", |
Motomu Utsumi | 4459155 | 2023-08-22 12:03:16 +0900 | [diff] [blame] | 151 | "-I packages/modules/Connectivity/staticlibs/native/bpf_headers/include/bpf", |
Maciej Żenczykowski | 79f6f75 | 2020-02-18 15:38:36 -0800 | [diff] [blame] | 152 | // TODO(b/149785767): only give access to specific file with AID_* constants |
| 153 | "-I system/core/libcutils/include", |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 154 | "-I " + ctx.ModuleDir(), |
| 155 | } |
| 156 | |
| 157 | for _, dir := range android.PathsForSource(ctx, bpf.properties.Include_dirs) { |
| 158 | cflags = append(cflags, "-I "+dir.String()) |
| 159 | } |
| 160 | |
| 161 | cflags = append(cflags, bpf.properties.Cflags...) |
| 162 | |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame] | 163 | if proptools.Bool(bpf.properties.Btf) { |
| 164 | cflags = append(cflags, "-g") |
Connor O'Brien | 3e739cf | 2022-08-17 15:45:52 -0700 | [diff] [blame] | 165 | if runtime.GOOS != "darwin" { |
| 166 | cflags = append(cflags, "-fdebug-prefix-map=/proc/self/cwd=") |
| 167 | } |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 170 | srcs := android.PathsForModuleSrc(ctx, bpf.properties.Srcs) |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 171 | |
| 172 | for _, src := range srcs { |
Ken Chen | 5372a24 | 2022-07-07 17:48:06 +0800 | [diff] [blame] | 173 | if strings.ContainsRune(filepath.Base(src.String()), '_') { |
| 174 | ctx.ModuleErrorf("invalid character '_' in source name") |
| 175 | } |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame] | 176 | obj := android.ObjPathWithExt(ctx, "unstripped", src, "o") |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 177 | |
| 178 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 815daf9 | 2019-05-14 16:05:20 -0700 | [diff] [blame] | 179 | Rule: ccRule, |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 180 | Input: src, |
| 181 | Output: obj, |
| 182 | Args: map[string]string{ |
| 183 | "cFlags": strings.Join(cflags, " "), |
| 184 | "ccCmd": "${config.ClangBin}/clang", |
| 185 | }, |
| 186 | }) |
| 187 | |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame] | 188 | if proptools.Bool(bpf.properties.Btf) { |
| 189 | objStripped := android.ObjPathWithExt(ctx, "", src, "o") |
| 190 | ctx.Build(pctx, android.BuildParams{ |
Steven Moreland | 606c5e9 | 2019-12-12 14:23:42 -0800 | [diff] [blame] | 191 | Rule: stripRule, |
| 192 | Input: obj, |
Connor O'Brien | 2573965 | 2021-12-02 20:09:45 -0800 | [diff] [blame] | 193 | Output: objStripped, |
| 194 | Args: map[string]string{ |
| 195 | "stripCmd": "${config.ClangBin}/llvm-strip", |
| 196 | }, |
| 197 | }) |
| 198 | bpf.objs = append(bpf.objs, objStripped.WithoutRel()) |
| 199 | } else { |
| 200 | bpf.objs = append(bpf.objs, obj.WithoutRel()) |
| 201 | } |
| 202 | |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 203 | } |
Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame] | 204 | |
| 205 | installDir := android.PathForModuleInstall(ctx, "etc", "bpf") |
| 206 | if len(bpf.properties.Sub_dir) > 0 { |
| 207 | installDir = installDir.Join(ctx, bpf.properties.Sub_dir) |
| 208 | } |
| 209 | for _, obj := range bpf.objs { |
| 210 | ctx.PackageFile(installDir, obj.Base(), obj) |
| 211 | } |
| 212 | |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 213 | android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: srcs.Strings()}) |
mrziwang | e6c8581 | 2024-05-22 14:36:09 -0700 | [diff] [blame] | 214 | |
| 215 | ctx.SetOutputFiles(bpf.objs, "") |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 218 | func (bpf *bpf) AndroidMk() android.AndroidMkData { |
| 219 | return android.AndroidMkData{ |
| 220 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 221 | var names []string |
| 222 | fmt.Fprintln(w) |
| 223 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 224 | fmt.Fprintln(w) |
Steven Moreland | 606c5e9 | 2019-12-12 14:23:42 -0800 | [diff] [blame] | 225 | var localModulePath string |
| 226 | if bpf.properties.VendorInternal { |
| 227 | localModulePath = "LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_ETC)/bpf" |
| 228 | } else { |
| 229 | localModulePath = "LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/bpf" |
| 230 | } |
Ken Chen | fad7f9d | 2021-11-10 22:02:57 +0800 | [diff] [blame] | 231 | if len(bpf.properties.Sub_dir) > 0 { |
| 232 | localModulePath += "/" + bpf.properties.Sub_dir |
| 233 | } |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 234 | for _, obj := range bpf.objs { |
| 235 | objName := name + "_" + obj.Base() |
| 236 | names = append(names, objName) |
Sasha Smundak | 5c4729d | 2022-12-01 10:49:23 -0800 | [diff] [blame] | 237 | fmt.Fprintln(w, "include $(CLEAR_VARS)", " # bpf.bpf.obj") |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 238 | fmt.Fprintln(w, "LOCAL_MODULE := ", objName) |
| 239 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", obj.String()) |
| 240 | fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", obj.Base()) |
| 241 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") |
Ken Chen | fad7f9d | 2021-11-10 22:02:57 +0800 | [diff] [blame] | 242 | fmt.Fprintln(w, localModulePath) |
LaMont Jones | b509938 | 2024-01-10 23:42:36 +0000 | [diff] [blame] | 243 | // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here. |
| 244 | for _, extra := range data.Extra { |
| 245 | extra(w, nil) |
| 246 | } |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 247 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 248 | fmt.Fprintln(w) |
| 249 | } |
Sasha Smundak | 5c4729d | 2022-12-01 10:49:23 -0800 | [diff] [blame] | 250 | fmt.Fprintln(w, "include $(CLEAR_VARS)", " # bpf.bpf") |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 251 | fmt.Fprintln(w, "LOCAL_MODULE := ", name) |
Sasha Smundak | dcb6129 | 2022-12-08 10:41:33 -0800 | [diff] [blame] | 252 | android.AndroidMkEmitAssignList(w, "LOCAL_REQUIRED_MODULES", names) |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 253 | fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)") |
| 254 | }, |
| 255 | } |
| 256 | } |
| 257 | |
Ken Chen | fad7f9d | 2021-11-10 22:02:57 +0800 | [diff] [blame] | 258 | func (bpf *bpf) SubDir() string { |
| 259 | return bpf.properties.Sub_dir |
| 260 | } |
| 261 | |
markchien | 2f59ec9 | 2020-09-02 16:23:38 +0800 | [diff] [blame] | 262 | func BpfFactory() android.Module { |
Colin Cross | 3840659 | 2018-05-17 11:17:01 -0700 | [diff] [blame] | 263 | module := &bpf{} |
| 264 | |
| 265 | module.AddProperties(&module.properties) |
| 266 | |
| 267 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 268 | return module |
| 269 | } |