forked from pixie-io/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl_build_system.bzl
383 lines (359 loc) · 11.8 KB
/
pl_build_system.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Based on envoy(28d5f41) envoy/bazel/envoy_build_system.bzl
# Compute the final copts based on various options.
load("@io_bazel_rules_go//go:def.bzl", "go_context", "go_library")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
def pl_copts():
posix_options = [
# Warnings setup.
"-Wall",
"-Werror",
"-Wextra",
"-Wimplicit-fallthrough",
"-Wfloat-conversion",
"-Wno-deprecated-declarations",
]
# Since abseil's BUILD.bazel doesn't provide any system 'includes', add them in manually here.
# In contrast, libraries like googletest do provide includes, so no need to add those.
manual_system_includes = ["-isystem external/com_google_absl", "-isystem external/org_tensorflow"]
tcmalloc_flags = select({
"@px//bazel:disable_tcmalloc": ["-DABSL_MALLOC_HOOK_MMAP_DISABLE=1"],
"//conditions:default": ["-DTCMALLOC=1"],
}) + select({
"@px//bazel:debug_tcmalloc": ["-DPL_MEMORY_DEBUG_ENABLED=1"],
"//conditions:default": [],
})
# Leaving this here as an example of how to add compiler dependent_flags.
compiler_dependent_flags = select({
"@px//bazel:gcc_build": [
# Since we globally disable these warnings in the .bazelrc file,
# we force them enabled for our own source code.
"-Werror=stringop-truncation",
"-Werror=maybe-uninitialized",
],
"//conditions:default": [
# TODO(yzhao/oazizi): Please remove this after fixing warnings in stirling.
"-Wno-c99-designator",
],
})
return posix_options + manual_system_includes + tcmalloc_flags + compiler_dependent_flags
# Compute the final linkopts based on various options.
def pl_linkopts():
return pl_common_linkopts()
# Compute the test linkopts.
def pl_test_linkopts():
return pl_common_linkopts()
def pl_common_linkopts():
return select({
"//bazel:gcc_build": [
"-pthread",
"-llzma",
"-lrt",
"-ldl",
"-Wl,--hash-style=gnu",
"-lunwind",
],
# The OSX system library transitively links common libraries (e.g., pthread).
"@bazel_tools//tools/osx:darwin": [],
"//conditions:default": [
"-pthread",
"-lunwind",
"-llzma",
"-lrt",
"-ldl",
"-Wl,--hash-style=gnu",
],
}) + select({
"//bazel:use_libcpp": [],
"//conditions:default": ["-lstdc++fs"],
})
def pl_defines():
return ["MAGIC_ENUM_RANGE_MIN=-128", "MAGIC_ENUM_RANGE_MAX=256"]
def _default_external_deps():
return [
"@com_github_gflags_gflags//:gflags",
"@com_github_google_glog//:glog",
"@com_google_absl//absl/base:base",
"@com_google_absl//absl/strings:strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/debugging:symbolize",
"@com_google_absl//absl/debugging:failure_signal_handler",
"@com_google_absl//absl/functional:bind_front",
"@com_github_neargye_magic_enum//:magic_enum",
]
def _default_internal_deps():
return [
"//src/common/base:cc_library",
"//src/common/memory:cc_library",
"//src/common/perf:cc_library",
]
# PL C++ library targets should be specified with this function.
def pl_cc_library_internal(
name,
srcs = [],
hdrs = [],
data = [],
copts = [],
includes = [],
visibility = None,
external_deps = [],
tcmalloc_dep = False,
repository = "",
linkstamp = None,
linkopts = [],
local_defines = [],
defines = [],
tags = [],
testonly = 0,
deps = [],
strip_include_prefix = None):
if tcmalloc_dep:
deps += tcmalloc_external_deps(repository)
cc_library(
name = name,
srcs = srcs,
hdrs = hdrs,
data = data,
copts = pl_copts() + copts,
includes = includes,
visibility = visibility,
tags = tags,
deps = deps + _default_external_deps(),
alwayslink = 1,
linkstatic = 1,
linkstamp = linkstamp,
linkopts = linkopts,
local_defines = local_defines,
defines = pl_defines() + defines,
testonly = testonly,
strip_include_prefix = strip_include_prefix,
)
def pl_cc_library(**kwargs):
kwargs["deps"] = kwargs.get("deps", [])
kwargs["deps"] = kwargs["deps"] + _default_internal_deps()
pl_cc_library_internal(**kwargs)
# PL C++ binary targets should be specified with this function.
def pl_cc_binary(
name,
srcs = [],
data = [],
args = [],
testonly = 0,
visibility = None,
external_deps = [],
repository = "",
stamp = 0,
tags = [],
deps = [],
copts = [],
linkopts = [],
defines = []):
if not linkopts:
linkopts = pl_linkopts()
deps = deps
cc_binary(
name = name,
srcs = srcs,
data = data,
args = args,
copts = pl_copts() + copts,
linkopts = linkopts,
testonly = testonly,
linkstatic = 1,
visibility = visibility,
malloc = tcmalloc_external_dep(repository),
stamp = stamp,
tags = tags,
deps = deps + _default_external_deps() + _default_internal_deps(),
defines = pl_defines() + defines,
)
# PL C++ test targets should be specified with this function.
def pl_cc_test(
name,
srcs = [],
data = [],
repository = "",
deps = [],
tags = [],
shard_count = None,
size = "small",
timeout = "short",
args = [],
defines = [],
coverage = True,
local = False,
flaky = False):
test_lib_tags = list(tags)
if coverage:
test_lib_tags.append("coverage_test_lib")
pl_cc_test_library(
name = name + "_lib",
srcs = srcs,
data = data,
deps = deps,
repository = repository,
tags = test_lib_tags,
defines = defines,
)
cc_test(
name = name,
copts = pl_copts(),
linkopts = pl_test_linkopts(),
linkstatic = 1,
malloc = tcmalloc_external_dep(repository),
deps = [
":" + name + "_lib",
repository + "//src/common/testing:test_main",
repository + "//src/shared/version:test_version_linkstamp",
] + _default_external_deps(),
args = args,
data = data,
tags = tags + ["coverage_test"],
shard_count = shard_count,
size = size,
timeout = timeout,
local = local,
flaky = flaky,
)
# PL C++ test related libraries (that want gtest, gmock) should be specified
# with this function.
def pl_cc_test_library(
name,
srcs = [],
hdrs = [],
data = [],
external_deps = [],
deps = [],
visibility = None,
repository = "",
tags = [],
defines = []):
cc_library(
name = name,
srcs = srcs,
hdrs = hdrs,
data = data,
copts = pl_copts(),
testonly = 1,
deps = deps + [
"@com_google_googletest//:gtest",
repository + "//src/common/testing:cc_library",
] + _default_external_deps(),
tags = tags,
defines = pl_defines() + defines,
visibility = visibility,
alwayslink = 1,
linkstatic = 1,
)
# PL C++ mock targets should be specified with this function.
def pl_cc_mock(name, **kargs):
pl_cc_test_library(name = name, **kargs)
# Dependencies on tcmalloc_and_profiler should be wrapped with this function.
def tcmalloc_external_dep(repository):
return select({
repository + "//bazel:disable_tcmalloc": None,
"//conditions:default": "//third_party:gperftools",
})
# As above, but wrapped in list form for adding to dep lists. This smell seems needed as
# SelectorValue values have to match the attribute type. See
# https://github.com/bazelbuild/bazel/issues/2273.
def tcmalloc_external_deps(repository):
return select({
repository + "//bazel:disable_tcmalloc": [],
"//conditions:default": ["//third_party:gperftools"],
})
def pl_cgo_library(**kwargs):
if "cgo" in kwargs and kwargs["cgo"] and "clinkopts" not in kwargs:
kwargs["clinkopts"] = pl_linkopts() + select({
"@px//bazel:coverage_enabled": ["-lgcov --coverage"],
"//conditions:default": [],
})
kwargs["toolchains"] = ["@bazel_tools//tools/cpp:current_cc_toolchain"]
go_library(**kwargs)
def _pl_bindata_impl(ctx):
"""
Copied from https://github.com/bazelbuild/rules_go/blob/master/extras/bindata.bzl
but updated to change the bindata executable and to strip the bin_dir path.
"""
go = go_context(ctx)
out = go.declare_file(go, ext = ".gen.go")
arguments = ctx.actions.args()
arguments.add_all([
"-o",
out,
"-pkg",
ctx.attr.package,
"-prefix",
ctx.label.package,
])
if ctx.attr.strip_bin_dir:
arguments.add_all([
"-prefix",
ctx.bin_dir.path,
])
if not ctx.attr.compress:
arguments.add("-nocompress")
if not ctx.attr.metadata:
arguments.add("-nometadata")
if not ctx.attr.memcopy:
arguments.add("-nomemcopy")
if not ctx.attr.modtime:
arguments.add_all(["-modtime", "0"])
if ctx.attr.extra_args:
arguments.add_all(ctx.attr.extra_args)
srcs = [f.path for f in ctx.files.srcs]
if ctx.attr.strip_external and any([f.startswith("external/") for f in srcs]):
arguments.add("-prefix", ctx.label.workspace_root + "/" + ctx.label.package)
arguments.add_all(srcs)
ctx.actions.run(
inputs = ctx.files.srcs,
outputs = [out],
mnemonic = "GoBindata",
executable = ctx.executable._bindata,
arguments = [arguments],
)
return [
DefaultInfo(
files = depset([out]),
),
]
pl_bindata = rule(
_pl_bindata_impl,
toolchains = ["@io_bazel_rules_go//go:toolchain"],
attrs = {
"compress": attr.bool(default = True),
"extra_args": attr.string_list(),
"memcopy": attr.bool(default = True),
"metadata": attr.bool(default = False),
"modtime": attr.bool(default = False),
"package": attr.string(mandatory = True),
"srcs": attr.label_list(allow_files = True),
# Modification of the original bindata arguments.
"strip_bin_dir": attr.bool(default = False),
"strip_external": attr.bool(default = False),
"_bindata": attr.label(
executable = True,
cfg = "host",
# Modification of go_bindata repo from kevinburke to the go-bindata repo.
default = "@com_github_go_bindata_go_bindata//go-bindata:go-bindata",
),
"_go_context_data": attr.label(default = "@io_bazel_rules_go//:go_context_data"),
},
)