-
Notifications
You must be signed in to change notification settings - Fork 454
/
Copy pathpl_build_system.bzl
511 lines (467 loc) · 15.7 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# 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_docker//go:image.bzl", "go_image")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_context", "go_library", "go_test")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("@rules_python//python:defs.bzl", "py_test")
load("//bazel:toolchain_transitions.bzl", "qemu_interactive_runner")
pl_boringcrypto_go_sdk = ["1.20.13"]
pl_supported_go_sdk_versions = ["1.18", "1.19", "1.20", "1.21", "1.22", "1.23"]
# The last version in this list corresponds to the boringcrypto go sdk version.
pl_all_supported_go_sdk_versions = pl_supported_go_sdk_versions + pl_boringcrypto_go_sdk
def pl_go_sdk_version_template_to_label(tpl, version):
# If version matches sdk configured to use boringcrypto
# the label name should not contain the sdk version string
if version in pl_boringcrypto_go_sdk:
return tpl % "boringcrypto"
return tpl % version.replace(".", "_")
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",
]
def pl_default_features():
return [
"-external_dep",
]
# PL C++ library targets should be specified with this function.
def pl_cc_library_internal(
name,
srcs = [],
hdrs = [],
data = [],
copts = [],
includes = [],
visibility = None,
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,
features = pl_default_features(),
)
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,
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,
features = pl_default_features(),
)
def _default_empty_list(name, kwargs):
if not name in kwargs:
kwargs[name] = []
def pl_cc_musl_binary(name, **kwargs):
_default_empty_list("copts", kwargs)
kwargs["copts"] = kwargs["copts"] + ["-nostdlib", "-nostdinc"]
_default_empty_list("additional_linker_inputs", kwargs)
kwargs["additional_linker_inputs"] = kwargs["additional_linker_inputs"] + [
"@org_libc_musl//:crt1.o",
"@org_libc_musl//:crti.o",
"@org_libc_musl//:crtn.o",
]
linkshared = "linkshared" in kwargs and kwargs["linkshared"]
start_libs = []
if not linkshared:
start_libs = start_libs + ["$(location @org_libc_musl//:crt1.o)"]
start_libs = start_libs + ["$(location @org_libc_musl//:crti.o)"]
end_libs = ["$(location @org_libc_musl//:crtn.o)"]
linkopts = [
"-nostdlib",
"-nodefaultlibs",
"-nostartfiles",
"-lgcc",
]
_default_empty_list("linkopts", kwargs)
kwargs["linkopts"] = start_libs + kwargs["linkopts"] + linkopts + end_libs
_default_empty_list("deps", kwargs)
kwargs["deps"] = kwargs["deps"] + ["@org_libc_musl//:musl"]
cc_binary(name = name, **kwargs)
# 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,
**kwargs):
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 + ["//bazel/test_runners:test_runner_dep"],
tags = tags + ["coverage_test"],
shard_count = shard_count,
size = size,
timeout = timeout,
local = local,
flaky = flaky,
features = pl_default_features(),
**kwargs
)
# PL C++ test related libraries (that want gtest, gmock) should be specified
# with this function.
def pl_cc_test_library(
name,
srcs = [],
hdrs = [],
data = [],
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,
features = pl_default_features(),
)
# 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 = "exec",
# 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"),
},
)
def pl_go_image(**kwargs):
base = "//:pl_go_base_image"
if "base" not in kwargs:
kwargs["base"] = base
go_image(
**kwargs
)
def _add_no_pie(kwargs):
if "gc_linkopts" not in kwargs:
kwargs["gc_linkopts"] = []
kwargs["gc_linkopts"].append("-extldflags")
kwargs["gc_linkopts"].append("-no-pie")
def _add_test_runner(kwargs):
if "data" not in kwargs:
kwargs["data"] = []
kwargs["data"].append("//bazel/test_runners:test_runner_dep")
def _add_no_sysroot(kwargs):
if "target_compatible_with" not in kwargs:
kwargs["target_compatible_with"] = []
kwargs["target_compatible_with"] = kwargs["target_compatible_with"] + select({
"//bazel/cc_toolchains:libc_version_glibc_host": [],
"//conditions:default": ["@platforms//:incompatible"],
})
def pl_go_test(**kwargs):
_add_no_pie(kwargs)
_add_test_runner(kwargs)
go_test(**kwargs)
def pl_go_binary(**kwargs):
_add_no_pie(kwargs)
go_binary(**kwargs)
def pl_py_test(**kwargs):
_add_test_runner(kwargs)
_add_no_sysroot(kwargs)
py_test(**kwargs)
def pl_sh_test(**kwargs):
_add_test_runner(kwargs)
native.sh_test(**kwargs)
def pl_cc_bpf_test(**kwargs):
pl_cc_test(**kwargs)
qemu_interactive_runner(
name = kwargs["name"] + "_qemu_interactive",
test = ":" + kwargs["name"],
tags = [
"manual",
"qemu_interactive",
],
testonly = True,
)
def pl_sh_bpf_test(**kwargs):
pl_sh_test(**kwargs)
qemu_interactive_runner(
name = kwargs["name"] + "_qemu_interactive",
test = ":" + kwargs["name"],
tags = [
"manual",
"qemu_interactive",
],
testonly = True,
)