Skip to content

Commit 9a3d897

Browse files
amscannegvisor-bot
authored andcommitted
Refactor shared starlark files.
PiperOrigin-RevId: 337581114
1 parent 4ddb58f commit 9a3d897

File tree

6 files changed

+219
-202
lines changed

6 files changed

+219
-202
lines changed

tools/bazeldefs/cc.bzl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""C++ rules."""
2+
3+
load("@bazel_tools//tools/cpp:cc_flags_supplier.bzl", _cc_flags_supplier = "cc_flags_supplier")
4+
load("@rules_cc//cc:defs.bzl", _cc_binary = "cc_binary", _cc_library = "cc_library", _cc_proto_library = "cc_proto_library", _cc_test = "cc_test")
5+
load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", _cc_grpc_library = "cc_grpc_library")
6+
7+
cc_library = _cc_library
8+
cc_flags_supplier = _cc_flags_supplier
9+
cc_proto_library = _cc_proto_library
10+
cc_test = _cc_test
11+
cc_toolchain = "@bazel_tools//tools/cpp:current_cc_toolchain"
12+
gtest = "@com_google_googletest//:gtest"
13+
gbenchmark = "@com_google_benchmark//:benchmark"
14+
grpcpp = "@com_github_grpc_grpc//:grpc++"
15+
vdso_linker_option = "-fuse-ld=gold "
16+
17+
def cc_grpc_library(name, **kwargs):
18+
_cc_grpc_library(name = name, grpc_only = True, **kwargs)
19+
20+
def cc_binary(name, static = False, **kwargs):
21+
"""Run cc_binary.
22+
23+
Args:
24+
name: name of the target.
25+
static: make a static binary if True
26+
**kwargs: the rest of the args.
27+
"""
28+
if static:
29+
# How to statically link a c++ program that uses threads, like for gRPC:
30+
# https://gcc.gnu.org/legacy-ml/gcc-help/2010-05/msg00029.html
31+
if "linkopts" not in kwargs:
32+
kwargs["linkopts"] = []
33+
kwargs["linkopts"] += [
34+
"-static",
35+
"-lstdc++",
36+
"-Wl,--whole-archive",
37+
"-lpthread",
38+
"-Wl,--no-whole-archive",
39+
]
40+
_cc_binary(
41+
name = name,
42+
**kwargs
43+
)

tools/bazeldefs/defs.bzl

Lines changed: 1 addition & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
1-
"""Bazel implementations of standard rules."""
1+
"""Meta and miscellaneous rules."""
22

3-
load("@bazel_gazelle//:def.bzl", _gazelle = "gazelle")
43
load("@bazel_skylib//rules:build_test.bzl", _build_test = "build_test")
54
load("@bazel_skylib//:bzl_library.bzl", _bzl_library = "bzl_library")
6-
load("@bazel_tools//tools/cpp:cc_flags_supplier.bzl", _cc_flags_supplier = "cc_flags_supplier")
7-
load("@io_bazel_rules_go//go:def.bzl", "GoLibrary", _go_binary = "go_binary", _go_context = "go_context", _go_embed_data = "go_embed_data", _go_library = "go_library", _go_path = "go_path", _go_test = "go_test")
8-
load("@io_bazel_rules_go//proto:def.bzl", _go_grpc_library = "go_grpc_library", _go_proto_library = "go_proto_library")
9-
load("@rules_cc//cc:defs.bzl", _cc_binary = "cc_binary", _cc_library = "cc_library", _cc_proto_library = "cc_proto_library", _cc_test = "cc_test")
10-
load("@rules_pkg//:pkg.bzl", _pkg_deb = "pkg_deb", _pkg_tar = "pkg_tar")
11-
load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", _cc_grpc_library = "cc_grpc_library")
125

136
build_test = _build_test
147
bzl_library = _bzl_library
15-
cc_library = _cc_library
16-
cc_flags_supplier = _cc_flags_supplier
17-
cc_proto_library = _cc_proto_library
18-
cc_test = _cc_test
19-
cc_toolchain = "@bazel_tools//tools/cpp:current_cc_toolchain"
20-
gazelle = _gazelle
21-
go_embed_data = _go_embed_data
22-
go_path = _go_path
23-
gtest = "@com_google_googletest//:gtest"
24-
grpcpp = "@com_github_grpc_grpc//:grpc++"
25-
gbenchmark = "@com_google_benchmark//:benchmark"
268
loopback = "//tools/bazeldefs:loopback"
27-
pkg_deb = _pkg_deb
28-
pkg_tar = _pkg_tar
29-
py_binary = native.py_binary
309
rbe_platform = native.platform
3110
rbe_toolchain = native.toolchain
32-
vdso_linker_option = "-fuse-ld=gold "
3311

3412
def short_path(path):
3513
return path
@@ -40,160 +18,6 @@ def proto_library(name, has_services = None, **kwargs):
4018
**kwargs
4119
)
4220

43-
def cc_grpc_library(name, **kwargs):
44-
_cc_grpc_library(name = name, grpc_only = True, **kwargs)
45-
46-
def _go_proto_or_grpc_library(go_library_func, name, **kwargs):
47-
deps = [
48-
dep.replace("_proto", "_go_proto")
49-
for dep in (kwargs.pop("deps", []) or [])
50-
]
51-
go_library_func(
52-
name = name + "_go_proto",
53-
importpath = "gvisor.dev/gvisor/" + native.package_name() + "/" + name + "_go_proto",
54-
proto = ":" + name + "_proto",
55-
deps = deps,
56-
**kwargs
57-
)
58-
59-
def go_proto_library(name, **kwargs):
60-
_go_proto_or_grpc_library(_go_proto_library, name, **kwargs)
61-
62-
def go_grpc_and_proto_libraries(name, **kwargs):
63-
_go_proto_or_grpc_library(_go_grpc_library, name, **kwargs)
64-
65-
def cc_binary(name, static = False, **kwargs):
66-
"""Run cc_binary.
67-
68-
Args:
69-
name: name of the target.
70-
static: make a static binary if True
71-
**kwargs: the rest of the args.
72-
"""
73-
if static:
74-
# How to statically link a c++ program that uses threads, like for gRPC:
75-
# https://gcc.gnu.org/legacy-ml/gcc-help/2010-05/msg00029.html
76-
if "linkopts" not in kwargs:
77-
kwargs["linkopts"] = []
78-
kwargs["linkopts"] += [
79-
"-static",
80-
"-lstdc++",
81-
"-Wl,--whole-archive",
82-
"-lpthread",
83-
"-Wl,--no-whole-archive",
84-
]
85-
_cc_binary(
86-
name = name,
87-
**kwargs
88-
)
89-
90-
def go_binary(name, static = False, pure = False, x_defs = None, **kwargs):
91-
"""Build a go binary.
92-
93-
Args:
94-
name: name of the target.
95-
static: build a static binary.
96-
pure: build without cgo.
97-
x_defs: additional definitions.
98-
**kwargs: rest of the arguments are passed to _go_binary.
99-
"""
100-
if static:
101-
kwargs["static"] = "on"
102-
if pure:
103-
kwargs["pure"] = "on"
104-
_go_binary(
105-
name = name,
106-
x_defs = x_defs,
107-
**kwargs
108-
)
109-
110-
def go_importpath(target):
111-
"""Returns the importpath for the target."""
112-
return target[GoLibrary].importpath
113-
114-
def go_library(name, **kwargs):
115-
_go_library(
116-
name = name,
117-
importpath = "gvisor.dev/gvisor/" + native.package_name(),
118-
**kwargs
119-
)
120-
121-
def go_test(name, pure = False, library = None, **kwargs):
122-
"""Build a go test.
123-
124-
Args:
125-
name: name of the output binary.
126-
pure: should it be built without cgo.
127-
library: the library to embed.
128-
**kwargs: rest of the arguments to pass to _go_test.
129-
"""
130-
if pure:
131-
kwargs["pure"] = "on"
132-
if library:
133-
kwargs["embed"] = [library]
134-
_go_test(
135-
name = name,
136-
**kwargs
137-
)
138-
139-
def go_rule(rule, implementation, **kwargs):
140-
"""Wraps a rule definition with Go attributes.
141-
142-
Args:
143-
rule: rule function (typically rule or aspect).
144-
implementation: implementation function.
145-
**kwargs: other arguments to pass to rule.
146-
147-
Returns:
148-
The result of invoking the rule.
149-
"""
150-
attrs = kwargs.pop("attrs", dict())
151-
attrs["_go_context_data"] = attr.label(default = "@io_bazel_rules_go//:go_context_data")
152-
attrs["_stdlib"] = attr.label(default = "@io_bazel_rules_go//:stdlib")
153-
toolchains = kwargs.get("toolchains", []) + ["@io_bazel_rules_go//go:toolchain"]
154-
return rule(implementation, attrs = attrs, toolchains = toolchains, **kwargs)
155-
156-
def go_test_library(target):
157-
if hasattr(target.attr, "embed") and len(target.attr.embed) > 0:
158-
return target.attr.embed[0]
159-
return None
160-
161-
def go_context(ctx, goos = None, goarch = None, std = False):
162-
"""Extracts a standard Go context struct.
163-
164-
Args:
165-
ctx: the starlark context (required).
166-
goos: the GOOS value.
167-
goarch: the GOARCH value.
168-
std: ignored.
169-
170-
Returns:
171-
A context Go struct with pointers to Go toolchain components.
172-
"""
173-
174-
# We don't change anything for the standard library analysis. All Go files
175-
# are available in all instances. Note that this includes the standard
176-
# library sources, which are analyzed by nogo.
177-
go_ctx = _go_context(ctx)
178-
if goos == None:
179-
goos = go_ctx.sdk.goos
180-
elif goos != go_ctx.sdk.goos:
181-
fail("Internal GOOS (%s) doesn't match GoSdk GOOS (%s)." % (goos, go_ctx.sdk.goos))
182-
if goarch == None:
183-
goarch = go_ctx.sdk.goarch
184-
elif goarch != go_ctx.sdk.goarch:
185-
fail("Internal GOARCH (%s) doesn't match GoSdk GOARCH (%s)." % (goarch, go_ctx.sdk.goarch))
186-
return struct(
187-
go = go_ctx.go,
188-
env = go_ctx.env,
189-
nogo_args = [],
190-
stdlib_srcs = go_ctx.sdk.srcs,
191-
runfiles = depset([go_ctx.go] + go_ctx.sdk.srcs + go_ctx.sdk.tools + go_ctx.stdlib.libs),
192-
goos = go_ctx.sdk.goos,
193-
goarch = go_ctx.sdk.goarch,
194-
tags = go_ctx.tags,
195-
)
196-
19721
def select_arch(amd64 = "amd64", arm64 = "arm64", default = None, **kwargs):
19822
values = {
19923
"@bazel_tools//src/conditions:linux_x86_64": amd64,
@@ -206,12 +30,6 @@ def select_arch(amd64 = "amd64", arm64 = "arm64", default = None, **kwargs):
20630
def select_system(linux = ["__linux__"], **kwargs):
20731
return linux # Only Linux supported.
20832

209-
def select_goarch():
210-
return select_arch(arm64 = "arm64", amd64 = "amd64")
211-
212-
def select_goos():
213-
return select_system(linux = "linux")
214-
21533
def default_installer():
21634
return None
21735

tools/bazeldefs/go.bzl

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
"""Go rules."""
2+
3+
load("@bazel_gazelle//:def.bzl", _gazelle = "gazelle")
4+
load("@io_bazel_rules_go//go:def.bzl", "GoLibrary", _go_binary = "go_binary", _go_context = "go_context", _go_embed_data = "go_embed_data", _go_library = "go_library", _go_path = "go_path", _go_test = "go_test")
5+
load("@io_bazel_rules_go//proto:def.bzl", _go_grpc_library = "go_grpc_library", _go_proto_library = "go_proto_library")
6+
load("//tools/bazeldefs:defs.bzl", "select_arch", "select_system")
7+
8+
gazelle = _gazelle
9+
go_embed_data = _go_embed_data
10+
go_path = _go_path
11+
12+
def _go_proto_or_grpc_library(go_library_func, name, **kwargs):
13+
deps = [
14+
dep.replace("_proto", "_go_proto")
15+
for dep in (kwargs.pop("deps", []) or [])
16+
]
17+
go_library_func(
18+
name = name + "_go_proto",
19+
importpath = "gvisor.dev/gvisor/" + native.package_name() + "/" + name + "_go_proto",
20+
proto = ":" + name + "_proto",
21+
deps = deps,
22+
**kwargs
23+
)
24+
25+
def go_proto_library(name, **kwargs):
26+
_go_proto_or_grpc_library(_go_proto_library, name, **kwargs)
27+
28+
def go_grpc_and_proto_libraries(name, **kwargs):
29+
_go_proto_or_grpc_library(_go_grpc_library, name, **kwargs)
30+
31+
def go_binary(name, static = False, pure = False, x_defs = None, **kwargs):
32+
"""Build a go binary.
33+
34+
Args:
35+
name: name of the target.
36+
static: build a static binary.
37+
pure: build without cgo.
38+
x_defs: additional definitions.
39+
**kwargs: rest of the arguments are passed to _go_binary.
40+
"""
41+
if static:
42+
kwargs["static"] = "on"
43+
if pure:
44+
kwargs["pure"] = "on"
45+
_go_binary(
46+
name = name,
47+
x_defs = x_defs,
48+
**kwargs
49+
)
50+
51+
def go_importpath(target):
52+
"""Returns the importpath for the target."""
53+
return target[GoLibrary].importpath
54+
55+
def go_library(name, **kwargs):
56+
_go_library(
57+
name = name,
58+
importpath = "gvisor.dev/gvisor/" + native.package_name(),
59+
**kwargs
60+
)
61+
62+
def go_test(name, pure = False, library = None, **kwargs):
63+
"""Build a go test.
64+
65+
Args:
66+
name: name of the output binary.
67+
pure: should it be built without cgo.
68+
library: the library to embed.
69+
**kwargs: rest of the arguments to pass to _go_test.
70+
"""
71+
if pure:
72+
kwargs["pure"] = "on"
73+
if library:
74+
kwargs["embed"] = [library]
75+
_go_test(
76+
name = name,
77+
**kwargs
78+
)
79+
80+
def go_rule(rule, implementation, **kwargs):
81+
"""Wraps a rule definition with Go attributes.
82+
83+
Args:
84+
rule: rule function (typically rule or aspect).
85+
implementation: implementation function.
86+
**kwargs: other arguments to pass to rule.
87+
88+
Returns:
89+
The result of invoking the rule.
90+
"""
91+
attrs = kwargs.pop("attrs", dict())
92+
attrs["_go_context_data"] = attr.label(default = "@io_bazel_rules_go//:go_context_data")
93+
attrs["_stdlib"] = attr.label(default = "@io_bazel_rules_go//:stdlib")
94+
toolchains = kwargs.get("toolchains", []) + ["@io_bazel_rules_go//go:toolchain"]
95+
return rule(implementation, attrs = attrs, toolchains = toolchains, **kwargs)
96+
97+
def go_test_library(target):
98+
if hasattr(target.attr, "embed") and len(target.attr.embed) > 0:
99+
return target.attr.embed[0]
100+
return None
101+
102+
def go_context(ctx, goos = None, goarch = None, std = False):
103+
"""Extracts a standard Go context struct.
104+
105+
Args:
106+
ctx: the starlark context (required).
107+
goos: the GOOS value.
108+
goarch: the GOARCH value.
109+
std: ignored.
110+
111+
Returns:
112+
A context Go struct with pointers to Go toolchain components.
113+
"""
114+
115+
# We don't change anything for the standard library analysis. All Go files
116+
# are available in all instances. Note that this includes the standard
117+
# library sources, which are analyzed by nogo.
118+
go_ctx = _go_context(ctx)
119+
if goos == None:
120+
goos = go_ctx.sdk.goos
121+
elif goos != go_ctx.sdk.goos:
122+
fail("Internal GOOS (%s) doesn't match GoSdk GOOS (%s)." % (goos, go_ctx.sdk.goos))
123+
if goarch == None:
124+
goarch = go_ctx.sdk.goarch
125+
elif goarch != go_ctx.sdk.goarch:
126+
fail("Internal GOARCH (%s) doesn't match GoSdk GOARCH (%s)." % (goarch, go_ctx.sdk.goarch))
127+
return struct(
128+
go = go_ctx.go,
129+
env = go_ctx.env,
130+
nogo_args = [],
131+
stdlib_srcs = go_ctx.sdk.srcs,
132+
runfiles = depset([go_ctx.go] + go_ctx.sdk.srcs + go_ctx.sdk.tools + go_ctx.stdlib.libs),
133+
goos = go_ctx.sdk.goos,
134+
goarch = go_ctx.sdk.goarch,
135+
tags = go_ctx.tags,
136+
)
137+
138+
def select_goarch():
139+
return select_arch(arm64 = "arm64", amd64 = "amd64")
140+
141+
def select_goos():
142+
return select_system(linux = "linux")

0 commit comments

Comments
 (0)