Skip to content

Commit adbe9f1

Browse files
authored
Minor documentation fixes (bazel-contrib#1403)
* Fix typo in roadmap.rst. * Add FAQ on cross-compilation to README.rst. * Rearrange links in README.rst. [skip ci] [ci skip]
1 parent 9b3a85e commit adbe9f1

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

README.rst

+83-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Go rules for Bazel_
1717
.. _go_test: go/core.rst#go_test
1818
.. _go_download_sdk: go/toolchains.rst#go_download_sdk
1919
.. _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
2022
.. _bazel-go-discuss: https://groups.google.com/forum/#!forum/bazel-go-discuss
2123
.. _Bazel labels: https://docs.bazel.build/versions/master/build-ref.html#labels
2224
.. _#265: https://github.com/bazelbuild/rules_go/issues/265
@@ -37,6 +39,8 @@ Travis Bazel CI
3739
|travis| |bazelci|
3840
======== =========
3941

42+
Mailing list: `bazel-go-discuss`_
43+
4044
Announcements
4145
-------------
4246

@@ -53,19 +57,26 @@ February 20, 2018
5357
.. contents::
5458

5559

56-
Quick links
57-
-----------
60+
Documentation
61+
-------------
62+
63+
* `Core API <go/core.rst>`_
64+
65+
* `go_binary`_
66+
* `go_library`_
67+
* `go_test`_
5868

59-
* Mailing list: `bazel-go-discuss`_
60-
* `Core api <go/core.rst>`_
6169
* `Workspace rules <go/workspace.rst>`_
62-
* `Toolchains <go/toolchains.rst>`_
6370
* `Protobuf rules <proto/core.rst>`_
71+
72+
* `go_proto_library`_
73+
* `go_proto_compiler`_
74+
75+
* `Toolchains <go/toolchains.rst>`_
6476
* `Extra rules <go/extras.rst>`_
6577
* `Deprecated rules <go/deprecated.rst>`_
6678
* `Build modes <go/modes.rst>`_
6779

68-
6980
Overview
7081
--------
7182

@@ -321,6 +332,54 @@ Gazelle will automatically add a ``data`` attribute like the one above if you
321332
have a ``testdata`` directory *unless* it contains buildable .go files or
322333
build files, in which case, ``testdata`` is treated as a normal package.
323334

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+
324383
How do I access ``go_binary`` executables from ``go_test``?
325384
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
326385

@@ -340,19 +399,35 @@ For example:
340399
go_binary(
341400
name = "cmd",
342401
srcs = ["cmd.go"],
343-
importpath = "example.com/cmd",
344402
)
345403
346404
go_test(
347405
name = "cmd_test",
348406
srcs = ["cmd_test.go"],
349407
args = ["$(location :cmd)"],
350408
data = [":cmd"],
351-
importpath = "example.com/test",
352409
)
353410
354411
See `reproducible_binary`_ for a complete example.
355412

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+
356431
How do I run Bazel on Travis CI?
357432
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
358433

roadmap.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ generally valid in a Bazel environment. Tools that collect build metadata with
6363
`go/build`_ produce incomplete or inaccurate results since `go/build`_ does not
6464
understand Bazel.
6565

66-
hWe are developing a new framework for collecting build metadata that will
66+
We are developing a new framework for collecting build metadata that will
6767
decouple tools from the build system. This framework is important for both Bazel
6868
and vgo, which will be the primary Go build system in future releases. Tools
6969
using framework will be aware of generated code in Bazel workspaces.

0 commit comments

Comments
 (0)