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