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