1- """Bazel implementations of standard rules."""
1+ """Meta and miscellaneous rules."""
22
3- load ("@bazel_gazelle//:def.bzl" , _gazelle = "gazelle" )
43load ("@bazel_skylib//rules:build_test.bzl" , _build_test = "build_test" )
54load ("@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
136build_test = _build_test
147bzl_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"
268loopback = "//tools/bazeldefs:loopback"
27- pkg_deb = _pkg_deb
28- pkg_tar = _pkg_tar
29- py_binary = native .py_binary
309rbe_platform = native .platform
3110rbe_toolchain = native .toolchain
32- vdso_linker_option = "-fuse-ld=gold "
3311
3412def 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-
19721def 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):
20630def 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-
21533def default_installer ():
21634 return None
21735
0 commit comments