@@ -17,6 +17,8 @@ Go rules for Bazel_
17
17
.. _go_test : go/core.rst#go_test
18
18
.. _go_download_sdk : go/toolchains.rst#go_download_sdk
19
19
.. _go_register_toolchains : go/toolchains.rst#go_register_toolchains
20
+ .. _go_proto_library : proto/core.rst#go_proto_library
21
+ .. _go_proto_compiler : proto/core.rst#go_proto_compiler
20
22
.. _bazel-go-discuss : https://groups.google.com/forum/#!forum/bazel-go-discuss
21
23
.. _Bazel labels : https://docs.bazel.build/versions/master/build-ref.html#labels
22
24
.. _#265 : https://github.com/bazelbuild/rules_go/issues/265
@@ -37,6 +39,8 @@ Travis Bazel CI
37
39
|travis | |bazelci |
38
40
======== =========
39
41
42
+ Mailing list: `bazel-go-discuss `_
43
+
40
44
Announcements
41
45
-------------
42
46
@@ -53,19 +57,26 @@ February 20, 2018
53
57
.. contents ::
54
58
55
59
56
- Quick links
57
- -----------
60
+ Documentation
61
+ -------------
62
+
63
+ * `Core API <go/core.rst >`_
64
+
65
+ * `go_binary `_
66
+ * `go_library `_
67
+ * `go_test `_
58
68
59
- * Mailing list: `bazel-go-discuss `_
60
- * `Core api <go/core.rst >`_
61
69
* `Workspace rules <go/workspace.rst >`_
62
- * `Toolchains <go/toolchains.rst >`_
63
70
* `Protobuf rules <proto/core.rst >`_
71
+
72
+ * `go_proto_library `_
73
+ * `go_proto_compiler `_
74
+
75
+ * `Toolchains <go/toolchains.rst >`_
64
76
* `Extra rules <go/extras.rst >`_
65
77
* `Deprecated rules <go/deprecated.rst >`_
66
78
* `Build modes <go/modes.rst >`_
67
79
68
-
69
80
Overview
70
81
--------
71
82
@@ -321,6 +332,54 @@ Gazelle will automatically add a ``data`` attribute like the one above if you
321
332
have a ``testdata `` directory *unless * it contains buildable .go files or
322
333
build files, in which case, ``testdata `` is treated as a normal package.
323
334
335
+ How do I cross-compile?
336
+ ~~~~~~~~~~~~~~~~~~~~~~~
337
+
338
+ You can cross-compile by setting the ``--platforms `` flag on the command line.
339
+ For example:
340
+
341
+ .. code ::
342
+
343
+ $ bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 //cmd
344
+
345
+ Platform-specific sources with build tags or filename suffixes are filtered
346
+ automatically at compile time. You can selectively include platform-specific
347
+ dependencies with ``select `` expressions (Gazelle does this automatically).
348
+
349
+ .. code :: bzl
350
+
351
+ go_library(
352
+ name = "go_default_library",
353
+ srcs = [
354
+ "foo_linux.go",
355
+ "foo_windows.go",
356
+ ],
357
+ deps = select({
358
+ "@io_bazel_rules_go//go/platform:linux_amd64": [
359
+ "//bar_linux:go_default_library",
360
+ ],
361
+ "@io_bazel_rules_go//go/platform:windows_amd64": [
362
+ "//bar_windows:go_default_library",
363
+ ],
364
+ "//conditions:default": [],
365
+ }),
366
+ )
367
+
368
+ rules_go can generate pure Go binaries for any platform the Go SDK supports. If
369
+ your project includes cgo code, has C/C++ dependencies, or requires external
370
+ linking, you'll need to `write a CROSSTOOL file
371
+ <https://github.com/bazelbuild/bazel/wiki/Yet-Another-CROSSTOOL-Writing-Tutorial> `_
372
+ for your toolchain and set the ``--cpu `` flag on the command line, in addition
373
+ to setting ``--platforms ``. You'll also need to set ``pure = "off" `` on your
374
+ ``go_binary ``. We don't fully support this yet, but people have gotten this to
375
+ work in some cases.
376
+
377
+ In some cases, you may want to set the ``goos `` and ``goarch `` attributes of
378
+ ``go_binary ``. This will cross-compile a binary for a specific platform.
379
+ This is necessary when you need to produce multiple binaries for different
380
+ platforms in a single build. However, note that ``select `` expressions will
381
+ not work correctly when using these attributes.
382
+
324
383
How do I access ``go_binary `` executables from ``go_test ``?
325
384
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
326
385
@@ -340,19 +399,35 @@ For example:
340
399
go_binary(
341
400
name = "cmd",
342
401
srcs = ["cmd.go"],
343
- importpath = "example.com/cmd",
344
402
)
345
403
346
404
go_test(
347
405
name = "cmd_test",
348
406
srcs = ["cmd_test.go"],
349
407
args = ["$(location :cmd)"],
350
408
data = [":cmd"],
351
- importpath = "example.com/test",
352
409
)
353
410
354
411
See `reproducible_binary `_ for a complete example.
355
412
413
+ Alternatively, you can set the ``out `` attribute of `go_binary `_ to a specific
414
+ filename. Note that when ``out `` is set, the binary won't be cached when
415
+ changing configurations.
416
+
417
+ .. code :: bzl
418
+
419
+ go_binary(
420
+ name = "cmd",
421
+ srcs = ["cmd.go"],
422
+ out = "cmd",
423
+ )
424
+
425
+ go_test(
426
+ name = "cmd_test",
427
+ srcs = ["cmd_test.go"],
428
+ data = [":cmd"],
429
+ )
430
+
356
431
How do I run Bazel on Travis CI?
357
432
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
358
433
0 commit comments