-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathgo_rules.bzl
389 lines (351 loc) · 13.3 KB
/
go_rules.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.
# TODO(cjhopman): This was generated by scripts/hacks/rules_shim_with_docs.py,
# but should be manually edited going forward. There may be some errors in
# the generated docs, and so those should be verified to be accurate and
# well-formatted (and then delete this TODO)
load("@prelude//decls:test_common.bzl", "test_common")
load(":common.bzl", "buck", "prelude_rule")
load(":cxx_common.bzl", "cxx_common")
load(":go_common.bzl", "go_common")
load(":re_test_common.bzl", "re_test_common")
BuildMode = ["executable", "c_shared", "c_archive"]
GoTestCoverStepMode = ["set", "count", "atomic", "none"]
go_binary = prelude_rule(
name = "go_binary",
docs = """
A go\\_binary() rule builds a native executable from the supplied set of Go source files
and dependencies. The files supplied are expected to be in the main package, implicitly.
""",
examples = """
For more examples, check out our [integration tests](https://github.com/facebook/buck/tree/dev/test/com/facebook/buck/features/go/testdata).
```
go_binary(
name='greet',
srcs=[
'main.go',
],
deps=[
':greeting',
],
)
go_library(
name='greeting',
srcs=[
'greeting.go',
],
deps=[
':join',
],
)
go_library(
name='join',
srcs=[
'join.go',
],
)
```
""",
further = None,
attrs = (
# @unsorted-dict-items
go_common.package_name_arg() |
go_common.srcs_arg() |
go_common.deps_arg() |
go_common.link_style_arg() |
go_common.link_mode_arg() |
go_common.compiler_flags_arg() |
go_common.assembler_flags_arg() |
go_common.linker_flags_arg() |
go_common.external_linker_flags_arg() |
go_common.embedcfg_arg() |
go_common.package_root_arg() |
go_common.cgo_enabled_arg() |
go_common.race_arg() |
go_common.asan_arg() |
go_common.build_tags_arg() |
cxx_common.headers_arg() |
cxx_common.header_namespace_arg() |
go_common.cxx_preprocessor_flags_arg() |
go_common.cxx_compiler_flags_arg() |
{
"resources": attrs.list(attrs.source(), default = [], doc = """
Static files to be symlinked into the working directory of the test. You can access these in your
by opening the files as relative paths, e.g. `ioutil.ReadFile("testdata/input")`.
"""),
"contacts": attrs.list(attrs.string(), default = []),
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
"labels": attrs.list(attrs.string(), default = []),
"licenses": attrs.list(attrs.source(), default = []),
"platform": attrs.option(attrs.string(), default = None),
}
),
)
go_exported_library = prelude_rule(
name = "go_exported_library",
docs = """
A go\\_exported\\_library() rule builds a C library from the supplied set of Go source files
and dependencies. This is done via `-buildmode` flag and "//export" annotations in the code.
""",
examples = """
For more examples, check out our [integration tests](https://github.com/facebook/buck/tree/dev/test/com/facebook/buck/features/go/testdata).
```
go_exported_library(
name = "shared",
srcs = ["main.go"],
build_mode = "c_shared",
compiler_flags = ["-shared"],
deps = [":example"],
)
go_library(
name = "example",
package_name = "cgo",
srcs = [
"export-to-c.go", # file with //export annotations
],
compiler_flags = [],
headers = [],
)
cxx_genrule(
name = "cgo_exported_headers",
out = "includes",
cmd = (
"mkdir -p $OUT && " +
"cat `dirname $(location :shared)`/includes/*.h > $OUT/_cgo_export.h"
),
)
prebuilt_cxx_library(
name = "cxx_so_with_header",
header_dirs = [":cgo_exported_headers"],
shared_lib = ":shared",
)
```
""",
further = None,
attrs = (
# @unsorted-dict-items
go_common.package_name_arg() |
go_common.srcs_arg() |
go_common.deps_arg() |
{
"build_mode": attrs.enum(BuildMode, doc = """
Determines the build mode (equivalent of `-buildmode`). Can be
one of the following values: `c_archive`, `c_shared`.
This argument is valid only if at there is at least one `cgo_library declared in deps.
In addition you should make sure that `-shared` flag is added to `compiler_flags`
and go version under `go.goroot` is compiled with that flag present in:
`gcflags`, `ldflags` and `asmflags``
"""),
} |
cxx_common.headers_arg() |
cxx_common.header_namespace_arg() |
go_common.cxx_preprocessor_flags_arg() |
go_common.cxx_compiler_flags_arg() |
go_common.link_style_arg() |
go_common.link_mode_arg() |
go_common.compiler_flags_arg() |
go_common.assembler_flags_arg() |
go_common.linker_flags_arg() |
go_common.external_linker_flags_arg() |
go_common.package_root_arg() |
go_common.cgo_enabled_arg() |
go_common.race_arg() |
go_common.asan_arg() |
go_common.build_tags_arg() |
go_common.generate_exported_header() |
{
"resources": attrs.list(attrs.source(), default = [], doc = """
Static files to be symlinked into the working directory of the test. You can access these in your
by opening the files as relative paths, e.g. `ioutil.ReadFile("testdata/input")`.
"""),
"contacts": attrs.list(attrs.string(), default = []),
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
"embedcfg": attrs.option(attrs.source(), default = None),
"labels": attrs.list(attrs.string(), default = []),
"licenses": attrs.list(attrs.source(), default = []),
"platform": attrs.option(attrs.string(), default = None),
}
),
)
go_library = prelude_rule(
name = "go_library",
docs = """
A go\\_library() rule builds a native library from the supplied set of Go source files
and dependencies.
""",
examples = """
For more examples, check out our [integration tests](https://github.com/facebook/buck/tree/dev/test/com/facebook/buck/features/go/testdata).
```
go_library(
name='greeting',
srcs=[
'greeting.go',
],
deps=[
':join',
],
)
```
""",
further = None,
attrs = (
# @unsorted-dict-items
go_common.srcs_arg() |
go_common.package_name_arg() |
go_common.deps_arg() |
go_common.compiler_flags_arg() |
go_common.assembler_flags_arg() |
go_common.embedcfg_arg() |
go_common.package_root_arg() |
go_common.override_cgo_enabled_arg() |
cxx_common.headers_arg() |
cxx_common.header_namespace_arg() |
go_common.cxx_preprocessor_flags_arg() |
go_common.cxx_compiler_flags_arg() |
go_common.external_linker_flags_arg() |
go_common.link_style_arg() |
go_common.generate_exported_header() |
{
"contacts": attrs.list(attrs.string(), default = []),
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
"labels": attrs.list(attrs.string(), default = []),
"licenses": attrs.list(attrs.source(), default = []),
}
),
)
go_test = prelude_rule(
name = "go_test",
docs = """
A `go_test()` rule builds a native binary from the
specified Go source and resource files—and a generated main file. It's
similar to the `go test` command.
If your test requires static files you should specify these in
the **resources** argument. If you do not specify these
files, they won't be available when your test runs.
""",
examples = """
For more examples, check out our [integration tests](https://github.com/facebook/buck/tree/dev/test/com/facebook/buck/features/go/testdata).
```
go_library(
name='greeting',
srcs=[
'greeting.go',
],
deps=[
':join',
],
)
go_test(
name='greeting-test',
srcs=[
'greeting_ext_test.go',
],
deps=[
':greeting'
],
)
go_test(
name='greeting-internal-test',
package_name='greeting',
srcs=[
'greeting.go',
'greeting_test.go',
],
deps=[
':join',
],
)
# Or
go_test(
name='greeting-better-internal-test',
srcs=['greeting_test.go'],
library=':greeting',
)
```
""",
further = None,
attrs = (
# @unsorted-dict-items
buck.inject_test_env_arg() |
go_common.srcs_arg() |
{
"library": attrs.option(attrs.dep(), default = None, doc = """
Specify the library that this internal test is testing. This will copy the `srcs`,
`package_name` and `deps` from the target specified so you don't have
to duplicate them.
"""),
"package_name": attrs.option(attrs.string(), default = None, doc = """
Sets the full name of the test package being compiled. This defaults to the path from the buck
root with "\\_test" appended. (e.g. given a ./.buckconfig, a rule in ./a/b/BUCK defaults to package "a/b\\_test")
Note: if you want to test packages internally (i.e. same package name), use the `library`
parameter instead of setting `package_name` to include the tested source files.
"""),
"coverage_mode": attrs.option(attrs.enum(GoTestCoverStepMode), default = None, doc = """
Test coverage functionality will be included in the executable. Modes: set, count, atomic
"""),
} |
go_common.deps_arg() |
go_common.link_style_arg() |
go_common.link_mode_arg() |
go_common.compiler_flags_arg() |
go_common.assembler_flags_arg() |
go_common.linker_flags_arg() |
go_common.external_linker_flags_arg() |
go_common.embedcfg_arg() |
go_common.package_root_arg() |
go_common.cgo_enabled_arg() |
go_common.race_arg() |
go_common.asan_arg() |
go_common.build_tags_arg() |
cxx_common.headers_arg() |
cxx_common.header_namespace_arg() |
go_common.cxx_preprocessor_flags_arg() |
go_common.cxx_compiler_flags_arg() |
{
"resources": attrs.list(attrs.source(), default = [], doc = """
Static files that are symlinked into the working directory of the
test. You can access these files in your test by opening them using
relative paths, such as `ioutil.ReadFile("testdata/input")`.
"""),
} |
buck.test_label_arg() |
buck.test_rule_timeout_ms() |
{
"env": attrs.dict(key = attrs.string(), value = attrs.arg(), sorted = False, default = {}, doc = """
A map of environment variables and values to set when running the test.
"""),
} |
buck.run_test_separately_arg(run_test_separately_type = attrs.bool(default = False)) |
{
"contacts": attrs.list(attrs.string(), default = []),
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
"licenses": attrs.list(attrs.source(), default = []),
"platform": attrs.option(attrs.string(), default = None),
"runner": attrs.option(attrs.dep(), default = None),
"specs": attrs.option(attrs.arg(json = True), default = None),
} |
re_test_common.test_args() |
test_common.attributes()
),
)
go_bootstrap_binary = prelude_rule(
name = "go_bootstrap_binary",
attrs = (
go_common.srcs_arg() |
{
"entrypoints": attrs.list(attrs.string(), default = [], doc = """Package name or file names"""),
"workdir": attrs.string(default = "", doc = """Change to subdir before running the command"""),
}
),
)
go_rules = struct(
go_binary = go_binary,
go_bootstrap_binary = go_bootstrap_binary,
go_exported_library = go_exported_library,
go_library = go_library,
go_test = go_test,
)