xref: /aosp_15_r20/external/bazelbuild-rules_go/go/toolchains.rst (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1*9bb1b549SSpandan DasGo toolchains
2*9bb1b549SSpandan Das=============
3*9bb1b549SSpandan Das
4*9bb1b549SSpandan Das.. _Args: https://docs.bazel.build/versions/master/skylark/lib/Args.html
5*9bb1b549SSpandan Das.. _Bazel toolchains: https://docs.bazel.build/versions/master/toolchains.html
6*9bb1b549SSpandan Das.. _Go website: https://golang.org/
7*9bb1b549SSpandan Das.. _GoArchive: providers.rst#goarchive
8*9bb1b549SSpandan Das.. _GoLibrary: providers.rst#golibrary
9*9bb1b549SSpandan Das.. _GoSDK: providers.rst#gosdk
10*9bb1b549SSpandan Das.. _GoSource: providers.rst#gosource
11*9bb1b549SSpandan Das.. _binary distribution: https://golang.org/dl/
12*9bb1b549SSpandan Das.. _compilation modes: modes.rst#compilation-modes
13*9bb1b549SSpandan Das.. _control the version: `Forcing the Go version`_
14*9bb1b549SSpandan Das.. _core: core.bzl
15*9bb1b549SSpandan Das.. _forked version of Go: `Registering a custom SDK`_
16*9bb1b549SSpandan Das.. _go assembly: https://golang.org/doc/asm
17*9bb1b549SSpandan Das.. _go sdk rules: `The SDK`_
18*9bb1b549SSpandan Das.. _go/platform/list.bzl: platform/list.bzl
19*9bb1b549SSpandan Das.. _installed SDK: `Using the installed Go sdk`_
20*9bb1b549SSpandan Das.. _nogo: nogo.rst#nogo
21*9bb1b549SSpandan Das.. _register: Registration_
22*9bb1b549SSpandan Das.. _register_toolchains: https://docs.bazel.build/versions/master/skylark/lib/globals.html#register_toolchains
23*9bb1b549SSpandan Das.. _toolchain resolution: https://bazel.build/extending/toolchains#toolchain-resolution
24*9bb1b549SSpandan Das
25*9bb1b549SSpandan Das.. role:: param(kbd)
26*9bb1b549SSpandan Das.. role:: type(emphasis)
27*9bb1b549SSpandan Das.. role:: value(code)
28*9bb1b549SSpandan Das.. |mandatory| replace:: **mandatory value**
29*9bb1b549SSpandan Das
30*9bb1b549SSpandan DasThe Go toolchain is at the heart of the Go rules, and is the mechanism used to
31*9bb1b549SSpandan Dascustomize the behavior of the core_ Go rules.
32*9bb1b549SSpandan Das
33*9bb1b549SSpandan Das.. contents:: :depth: 2
34*9bb1b549SSpandan Das
35*9bb1b549SSpandan Das-----
36*9bb1b549SSpandan Das
37*9bb1b549SSpandan DasOverview
38*9bb1b549SSpandan Das--------
39*9bb1b549SSpandan Das
40*9bb1b549SSpandan DasThe Go toolchain consists of three main layers: `the SDK`_, `the toolchain`_,
41*9bb1b549SSpandan Dasand `the context`_.
42*9bb1b549SSpandan Das
43*9bb1b549SSpandan DasThe SDK
44*9bb1b549SSpandan Das~~~~~~~
45*9bb1b549SSpandan Das
46*9bb1b549SSpandan DasThe Go SDK (more commonly known as the Go distribution) is a directory tree
47*9bb1b549SSpandan Dascontaining sources for the Go toolchain and standard library and pre-compiled
48*9bb1b549SSpandan Dasbinaries for the same. You can download this from by visiting the `Go website`_
49*9bb1b549SSpandan Dasand downloading a `binary distribution`_.
50*9bb1b549SSpandan Das
51*9bb1b549SSpandan DasThere are several Bazel rules for obtaining and configuring a Go SDK:
52*9bb1b549SSpandan Das
53*9bb1b549SSpandan Das* `go_download_sdk`_: downloads a toolchain for a specific version of Go for a
54*9bb1b549SSpandan Das  specific operating system and architecture.
55*9bb1b549SSpandan Das* `go_host_sdk`_: uses the toolchain installed on the system where Bazel is
56*9bb1b549SSpandan Das  run. The toolchain's location is specified with the ``GOROOT`` or by running
57*9bb1b549SSpandan Das  ``go env GOROOT``.
58*9bb1b549SSpandan Das* `go_local_sdk`_: like `go_host_sdk`_, but uses the toolchain in a specific
59*9bb1b549SSpandan Das  directory on the host system.
60*9bb1b549SSpandan Das* `go_wrap_sdk`_: configures a toolchain downloaded with another Bazel
61*9bb1b549SSpandan Das  repository rule.
62*9bb1b549SSpandan Das
63*9bb1b549SSpandan DasBy default, if none of the above rules are used, the `go_register_toolchains`_
64*9bb1b549SSpandan Dasfunction creates a repository named ``@go_sdk`` using `go_download_sdk`_, using
65*9bb1b549SSpandan Dasa recent version of Go for the host operating system and architecture.
66*9bb1b549SSpandan Das
67*9bb1b549SSpandan DasSDKs are specific to a host platform (e.g., ``linux_amd64``) and a version of
68*9bb1b549SSpandan DasGo. They may target all platforms that Go supports. The Go SDK is naturally
69*9bb1b549SSpandan Dascross compiling.
70*9bb1b549SSpandan Das
71*9bb1b549SSpandan DasBy default, all ``go_binary``, ``go_test``, etc. rules will use the first declared
72*9bb1b549SSpandan DasGo SDK. If you would like to build a target using a specific Go SDK version, first
73*9bb1b549SSpandan Dasensure that you have declared a Go SDK of that version using one of the above rules
74*9bb1b549SSpandan Das(`go_download_sdk`_, `go_host_sdk`_, `go_local_sdk`_, `go_wrap_sdk`_). Then you
75*9bb1b549SSpandan Dascan specify the sdk version to build with when running a ``bazel build`` by passing
76*9bb1b549SSpandan Dasthe flag ``--@io_bazel_rules_go//go/toolchain:sdk_version="version"`` where
77*9bb1b549SSpandan Das``"version"`` is the SDK version you would like to build with, eg. ``"1.18.3"``.
78*9bb1b549SSpandan DasThe SDK version can omit the patch, or include a prerelease part, eg. ``"1"``,
79*9bb1b549SSpandan Das``"1.18"``, ``"1.18.0"``, and ``"1.19.0beta1"`` are all valid values for ``sdk_version``.
80*9bb1b549SSpandan DasWhen ``go_host_sdk`` is used, ``"version"`` can be set to ``host`` to refer to the host Go SDK.
81*9bb1b549SSpandan DasIt can also be set ``remote`` to match any non-host version.
82*9bb1b549SSpandan Das
83*9bb1b549SSpandan DasThe toolchain
84*9bb1b549SSpandan Das~~~~~~~~~~~~~
85*9bb1b549SSpandan Das
86*9bb1b549SSpandan DasThe workspace rules above declare `Bazel toolchains`_ with `go_toolchain`_
87*9bb1b549SSpandan Dasimplementations for each target platform that Go supports. Wrappers around
88*9bb1b549SSpandan Dasthe rules register these toolchains automatically. Bazel will select a
89*9bb1b549SSpandan Dasregistered toolchain automatically based on the execution and target platforms,
90*9bb1b549SSpandan Dasspecified with ``--host_platform`` and ``--platforms``, respectively.
91*9bb1b549SSpandan Das
92*9bb1b549SSpandan DasThe workspace rules define the toolchains in a separate repository from the
93*9bb1b549SSpandan DasSDK. For example, if the SDK repository is `@go_sdk`, the toolchains will be
94*9bb1b549SSpandan Dasdefined in `@go_sdk_toolchains`. The `@go_sdk_toolchains` repository must be
95*9bb1b549SSpandan Daseagerly fetched in order to register the toolchain, but fetching the `@go_sdk`
96*9bb1b549SSpandan Dasrepository may be delayed until the toolchain is needed to build something. To
97*9bb1b549SSpandan Dasactivate lazily fetching the SDK, you must provide a `version` attribute to the
98*9bb1b549SSpandan Dasworkspace rule that defines the SDK (`go_download_sdk`, `go_host_sdk`, `go_local_sdk`,
99*9bb1b549SSpandan Das`go_wrap_sdk`, or `go_register_toolchains`). The value must match the actual
100*9bb1b549SSpandan Dasversion of the SDK; rules_go will validate this when the toolchain is used.
101*9bb1b549SSpandan Das
102*9bb1b549SSpandan DasThe toolchain itself should be considered opaque. You should only access
103*9bb1b549SSpandan Dasits contents through `the context`_.
104*9bb1b549SSpandan Das
105*9bb1b549SSpandan DasThe context
106*9bb1b549SSpandan Das~~~~~~~~~~~
107*9bb1b549SSpandan Das
108*9bb1b549SSpandan DasThe context is the type you need if you are writing custom rules that need
109*9bb1b549SSpandan Dasto be compatible with rules_go. It provides information about the SDK, the
110*9bb1b549SSpandan Dastoolchain, and the standard library. It also provides a convenient way to
111*9bb1b549SSpandan Dasdeclare mode-specific files, and to create actions for compiling, linking,
112*9bb1b549SSpandan Dasand more.
113*9bb1b549SSpandan Das
114*9bb1b549SSpandan DasCustomizing
115*9bb1b549SSpandan Das-----------
116*9bb1b549SSpandan Das
117*9bb1b549SSpandan DasNormal usage
118*9bb1b549SSpandan Das~~~~~~~~~~~~
119*9bb1b549SSpandan Das
120*9bb1b549SSpandan DasThis is an example of normal usage for the other examples to be compared
121*9bb1b549SSpandan Dasagainst. This will download and use a specific version of Go for the host
122*9bb1b549SSpandan Dasplatform.
123*9bb1b549SSpandan Das
124*9bb1b549SSpandan Das.. code:: bzl
125*9bb1b549SSpandan Das
126*9bb1b549SSpandan Das    # WORKSPACE
127*9bb1b549SSpandan Das
128*9bb1b549SSpandan Das    load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
129*9bb1b549SSpandan Das
130*9bb1b549SSpandan Das    go_rules_dependencies()
131*9bb1b549SSpandan Das
132*9bb1b549SSpandan Das    go_register_toolchains(version = "1.15.5")
133*9bb1b549SSpandan Das
134*9bb1b549SSpandan Das
135*9bb1b549SSpandan DasUsing the installed Go SDK
136*9bb1b549SSpandan Das~~~~~~~~~~~~~~~~~~~~~~~~~~
137*9bb1b549SSpandan Das
138*9bb1b549SSpandan DasYou can use the Go SDK that's installed on the system where Bazel is running.
139*9bb1b549SSpandan DasThis may result in faster builds, since there's no need to download an SDK,
140*9bb1b549SSpandan Dasbut builds won't be reproducible across systems with different SDKs installed.
141*9bb1b549SSpandan Das
142*9bb1b549SSpandan Das.. code:: bzl
143*9bb1b549SSpandan Das
144*9bb1b549SSpandan Das    # WORKSPACE
145*9bb1b549SSpandan Das
146*9bb1b549SSpandan Das    load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
147*9bb1b549SSpandan Das
148*9bb1b549SSpandan Das    go_rules_dependencies()
149*9bb1b549SSpandan Das
150*9bb1b549SSpandan Das    go_register_toolchains(version = "host")
151*9bb1b549SSpandan Das
152*9bb1b549SSpandan Das
153*9bb1b549SSpandan DasRegistering a custom SDK
154*9bb1b549SSpandan Das~~~~~~~~~~~~~~~~~~~~~~~~
155*9bb1b549SSpandan Das
156*9bb1b549SSpandan DasIf you download the SDK through another repository rule, you can configure
157*9bb1b549SSpandan Dasit with ``go_wrap_sdk``. It must still be named ``go_sdk``, but this is a
158*9bb1b549SSpandan Dastemporary limitation that will be removed in the future.
159*9bb1b549SSpandan Das
160*9bb1b549SSpandan Das.. code:: bzl
161*9bb1b549SSpandan Das
162*9bb1b549SSpandan Das    # WORKSPACE
163*9bb1b549SSpandan Das
164*9bb1b549SSpandan Das    load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains", "go_wrap_sdk")
165*9bb1b549SSpandan Das
166*9bb1b549SSpandan Das    unknown_download_sdk(
167*9bb1b549SSpandan Das        name = "go",
168*9bb1b549SSpandan Das        ...,
169*9bb1b549SSpandan Das    )
170*9bb1b549SSpandan Das
171*9bb1b549SSpandan Das    go_wrap_sdk(
172*9bb1b549SSpandan Das        name = "go_sdk",
173*9bb1b549SSpandan Das        root_file = "@go//:README.md",
174*9bb1b549SSpandan Das    )
175*9bb1b549SSpandan Das
176*9bb1b549SSpandan Das    go_rules_dependencies()
177*9bb1b549SSpandan Das
178*9bb1b549SSpandan Das    go_register_toolchains()
179*9bb1b549SSpandan Das
180*9bb1b549SSpandan Das
181*9bb1b549SSpandan DasWriting new Go rules
182*9bb1b549SSpandan Das~~~~~~~~~~~~~~~~~~~~
183*9bb1b549SSpandan Das
184*9bb1b549SSpandan DasIf you are writing a new Bazel rule that uses the Go toolchain, you need to
185*9bb1b549SSpandan Dasdo several things to ensure you have full access to the toolchain and common
186*9bb1b549SSpandan Dasdependencies.
187*9bb1b549SSpandan Das
188*9bb1b549SSpandan Das* Declare a dependency on a toolchain of type
189*9bb1b549SSpandan Das  ``@io_bazel_rules_go//go:toolchain``. Bazel will select an appropriate,
190*9bb1b549SSpandan Das  registered toolchain automatically.
191*9bb1b549SSpandan Das* Declare an implicit attribute named ``_go_context_data`` that defaults to
192*9bb1b549SSpandan Das  ``@io_bazel_rules_go//:go_context_data``. This target gathers configuration
193*9bb1b549SSpandan Das  information and several common dependencies.
194*9bb1b549SSpandan Das* Use the ``go_context`` function to gain access to `the context`_. This is
195*9bb1b549SSpandan Das  your main interface to the Go toolchain.
196*9bb1b549SSpandan Das
197*9bb1b549SSpandan Das.. code:: bzl
198*9bb1b549SSpandan Das
199*9bb1b549SSpandan Das    load("@io_bazel_rules_go//go:def.bzl", "go_context")
200*9bb1b549SSpandan Das
201*9bb1b549SSpandan Das    def _my_rule_impl(ctx):
202*9bb1b549SSpandan Das        go = go_context(ctx)
203*9bb1b549SSpandan Das        ...
204*9bb1b549SSpandan Das
205*9bb1b549SSpandan Das    my_rule = rule(
206*9bb1b549SSpandan Das        implementation = _my_rule_impl,
207*9bb1b549SSpandan Das        attrs = {
208*9bb1b549SSpandan Das            ...
209*9bb1b549SSpandan Das            "_go_context_data": attr.label(
210*9bb1b549SSpandan Das                default = "@io_bazel_rules_go//:go_context_data",
211*9bb1b549SSpandan Das            ),
212*9bb1b549SSpandan Das        },
213*9bb1b549SSpandan Das        toolchains = ["@io_bazel_rules_go//go:toolchain"],
214*9bb1b549SSpandan Das    )
215*9bb1b549SSpandan Das
216*9bb1b549SSpandan Das
217*9bb1b549SSpandan DasRules and functions
218*9bb1b549SSpandan Das-------------------
219*9bb1b549SSpandan Das
220*9bb1b549SSpandan Dasgo_register_toolchains
221*9bb1b549SSpandan Das~~~~~~~~~~~~~~~~~~~~~~
222*9bb1b549SSpandan Das
223*9bb1b549SSpandan DasInstalls the Go toolchains. If :param:`version` is specified, it sets the
224*9bb1b549SSpandan DasSDK version to use (for example, :value:`"1.15.5"`).
225*9bb1b549SSpandan Das
226*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
227*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
228*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
229*9bb1b549SSpandan Das| :param:`version`               | :type:`string`              | |mandatory|                       |
230*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
231*9bb1b549SSpandan Das| Specifies the version of Go to download if one has not been declared.                            |
232*9bb1b549SSpandan Das|                                                                                                  |
233*9bb1b549SSpandan Das| If a toolchain was already declared with `go_download_sdk`_ or a similar rule,                   |
234*9bb1b549SSpandan Das| this parameter may not be set.                                                                   |
235*9bb1b549SSpandan Das|                                                                                                  |
236*9bb1b549SSpandan Das| Normally this is set to a Go version like :value:`"1.15.5"`. It may also be                      |
237*9bb1b549SSpandan Das| set to :value:`"host"`, which will cause rules_go to use the Go toolchain                        |
238*9bb1b549SSpandan Das| installed on the host system (found using ``GOROOT`` or ``PATH``).                               |
239*9bb1b549SSpandan Das|                                                                                                  |
240*9bb1b549SSpandan Das| If ``version`` is specified and is not set to :value:`"host"`, the SDK will be fetched only when |
241*9bb1b549SSpandan Das| the build uses a Go toolchain and `toolchain resolution`_ results in  this SDK being chosen.     |
242*9bb1b549SSpandan Das| Otherwise it will be fetched unconditionally.                                                    |
243*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
244*9bb1b549SSpandan Das| :param:`nogo`                  | :type:`label`               | :value:`None`                     |
245*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
246*9bb1b549SSpandan Das| The ``nogo`` attribute refers to a nogo_ rule that builds a binary                               |
247*9bb1b549SSpandan Das| used for static analysis. The ``nogo`` binary will be used alongside the                         |
248*9bb1b549SSpandan Das| Go compiler when building packages.                                                              |
249*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
250*9bb1b549SSpandan Das| :param:`experiments`           | :type:`string_list`         | :value:`[]`                       |
251*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
252*9bb1b549SSpandan Das| Go experiments to enable via `GOEXPERIMENT`.                                                     |
253*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
254*9bb1b549SSpandan Das
255*9bb1b549SSpandan Dasgo_download_sdk
256*9bb1b549SSpandan Das~~~~~~~~~~~~~~~
257*9bb1b549SSpandan Das
258*9bb1b549SSpandan DasThis downloads a Go SDK for use in toolchains.
259*9bb1b549SSpandan Das
260*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
261*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                           |
262*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
263*9bb1b549SSpandan Das| :param:`name`                  | :type:`string`              | |mandatory|                                 |
264*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
265*9bb1b549SSpandan Das| A unique name for this SDK. This should almost always be :value:`go_sdk` if                                |
266*9bb1b549SSpandan Das| you want the SDK to be used by toolchains.                                                                 |
267*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
268*9bb1b549SSpandan Das| :param:`goos`                  | :type:`string`              | :value:`None`                               |
269*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
270*9bb1b549SSpandan Das| The operating system the binaries in the SDK are intended to run on.                                       |
271*9bb1b549SSpandan Das| By default, this is detected automatically, but if you're building on                                      |
272*9bb1b549SSpandan Das| an unusual platform, or if you're using remote execution and the execution                                 |
273*9bb1b549SSpandan Das| platform is different than the host, you may need to specify this explictly.                               |
274*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
275*9bb1b549SSpandan Das| :param:`goarch`                | :type:`string`              | :value:`None`                               |
276*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
277*9bb1b549SSpandan Das| The architecture the binaries in the SDK are intended to run on.                                           |
278*9bb1b549SSpandan Das| By default, this is detected automatically, but if you're building on                                      |
279*9bb1b549SSpandan Das| an unusual platform, or if you're using remote execution and the execution                                 |
280*9bb1b549SSpandan Das| platform is different than the host, you may need to specify this explictly.                               |
281*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
282*9bb1b549SSpandan Das| :param:`version`               | :type:`string`              | :value:`latest Go version`                  |
283*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
284*9bb1b549SSpandan Das| The version of Go to download, for example ``1.12.5``. If unspecified,                                     |
285*9bb1b549SSpandan Das| ``go_download_sdk`` will list available versions of Go from golang.org, then                               |
286*9bb1b549SSpandan Das| pick the highest version. If ``version`` is specified but ``sdks`` is                                      |
287*9bb1b549SSpandan Das| unspecified, ``go_download_sdk`` will list available versions on golang.org                                |
288*9bb1b549SSpandan Das| to determine the correct file name and SHA-256 sum.                                                        |
289*9bb1b549SSpandan Das| If ``version`` is specified, the SDK will be fetched only when the build uses a Go toolchain and           |
290*9bb1b549SSpandan Das| `toolchain resolution`_ results in this SDK being chosen. Otherwise it will be fetched unconditionally.    |
291*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
292*9bb1b549SSpandan Das| :param:`urls`                  | :type:`string_list`         | :value:`[https://dl.google.com/go/{}]`      |
293*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
294*9bb1b549SSpandan Das| A list of mirror urls to the binary distribution of a Go SDK. These must contain the `{}`                  |
295*9bb1b549SSpandan Das| used to substitute the sdk filename being fetched (using `.format`.                                        |
296*9bb1b549SSpandan Das| It defaults to the official repository :value:`"https://dl.google.com/go/{}"`.                             |
297*9bb1b549SSpandan Das|                                                                                                            |
298*9bb1b549SSpandan Das| This attribute is seldom used. It is only needed for downloading Go from                                   |
299*9bb1b549SSpandan Das| an alternative location (for example, an internal mirror).                                                 |
300*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
301*9bb1b549SSpandan Das| :param:`strip_prefix`          | :type:`string`              | :value:`"go"`                               |
302*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
303*9bb1b549SSpandan Das| A directory prefix to strip from the extracted files.                                                      |
304*9bb1b549SSpandan Das| Used with ``urls``.                                                                                        |
305*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
306*9bb1b549SSpandan Das| :param:`sdks`                  | :type:`string_list_dict`    | :value:`see description`                    |
307*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
308*9bb1b549SSpandan Das| This consists of a set of mappings from the host platform tuple to a list of filename and                  |
309*9bb1b549SSpandan Das| sha256 for that file. The filename is combined the :param:`urls` to produce the final download             |
310*9bb1b549SSpandan Das| urls to use.                                                                                               |
311*9bb1b549SSpandan Das|                                                                                                            |
312*9bb1b549SSpandan Das| This option is seldom used. It is only needed for downloading a modified                                   |
313*9bb1b549SSpandan Das| Go distribution (with a different SHA-256 sum) or a version of Go                                          |
314*9bb1b549SSpandan Das| not supported by rules_go (for example, a beta or release candidate).                                      |
315*9bb1b549SSpandan Das+--------------------------------+-----------------------------+---------------------------------------------+
316*9bb1b549SSpandan Das
317*9bb1b549SSpandan Das**Example**:
318*9bb1b549SSpandan Das
319*9bb1b549SSpandan Das.. code:: bzl
320*9bb1b549SSpandan Das
321*9bb1b549SSpandan Das    load(
322*9bb1b549SSpandan Das        "@io_bazel_rules_go//go:deps.bzl",
323*9bb1b549SSpandan Das        "go_download_sdk",
324*9bb1b549SSpandan Das        "go_register_toolchains",
325*9bb1b549SSpandan Das        "go_rules_dependencies",
326*9bb1b549SSpandan Das    )
327*9bb1b549SSpandan Das
328*9bb1b549SSpandan Das    go_download_sdk(
329*9bb1b549SSpandan Das        name = "go_sdk",
330*9bb1b549SSpandan Das        goos = "linux",
331*9bb1b549SSpandan Das        goarch = "amd64",
332*9bb1b549SSpandan Das        version = "1.18.1",
333*9bb1b549SSpandan Das        sdks = {
334*9bb1b549SSpandan Das            # NOTE: In most cases the whole sdks attribute is not needed.
335*9bb1b549SSpandan Das            # There are 2 "common" reasons you might want it:
336*9bb1b549SSpandan Das            #
337*9bb1b549SSpandan Das            # 1. You need to use an modified GO SDK, or an unsupported version
338*9bb1b549SSpandan Das            #    (for example, a beta or release candidate)
339*9bb1b549SSpandan Das            #
340*9bb1b549SSpandan Das            # 2. You want to avoid the dependency on the index file for the
341*9bb1b549SSpandan Das            #    SHA-256 checksums. In this case, You can get the expected
342*9bb1b549SSpandan Das            #    filenames and checksums from https://go.dev/dl/
343*9bb1b549SSpandan Das            "linux_amd64": ("go1.18.1.linux-amd64.tar.gz", "b3b815f47ababac13810fc6021eb73d65478e0b2db4b09d348eefad9581a2334"),
344*9bb1b549SSpandan Das            "darwin_amd64": ("go1.18.1.darwin-amd64.tar.gz", "3703e9a0db1000f18c0c7b524f3d378aac71219b4715a6a4c5683eb639f41a4d"),
345*9bb1b549SSpandan Das        },
346*9bb1b549SSpandan Das    )
347*9bb1b549SSpandan Das
348*9bb1b549SSpandan Das    go_rules_dependencies()
349*9bb1b549SSpandan Das
350*9bb1b549SSpandan Das    go_register_toolchains()
351*9bb1b549SSpandan Das
352*9bb1b549SSpandan Dasgo_host_sdk
353*9bb1b549SSpandan Das~~~~~~~~~~~
354*9bb1b549SSpandan Das
355*9bb1b549SSpandan DasThis detects and configures the host Go SDK for use in toolchains.
356*9bb1b549SSpandan Das
357*9bb1b549SSpandan DasIf the ``GOROOT`` environment variable is set, the SDK in that directory is
358*9bb1b549SSpandan Dasused. Otherwise, ``go env GOROOT`` is used.
359*9bb1b549SSpandan Das
360*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
361*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
362*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
363*9bb1b549SSpandan Das| :param:`name`                  | :type:`string`              | |mandatory|                       |
364*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
365*9bb1b549SSpandan Das| A unique name for this SDK. This should almost always be :value:`go_sdk` if you want the SDK     |
366*9bb1b549SSpandan Das| to be used by toolchains.                                                                        |
367*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
368*9bb1b549SSpandan Das| :param:`version`               | :type:`string`              | :value:`None`                     |
369*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
370*9bb1b549SSpandan Das| The version of Go installed on the host. If specified, `go_host_sdk` will create its repository  |
371*9bb1b549SSpandan Das| only when the build uses a Go toolchain and `toolchain resolution`_ results in this SDK being    |
372*9bb1b549SSpandan Das| chosen. Otherwise it will be created unconditionally.                                            |
373*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
374*9bb1b549SSpandan Das| :param:`experiments`           | :type:`string_list`         | :value:`[]`                       |
375*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
376*9bb1b549SSpandan Das| Go experiments to enable via `GOEXPERIMENT`.                                                     |
377*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
378*9bb1b549SSpandan Das
379*9bb1b549SSpandan Dasgo_local_sdk
380*9bb1b549SSpandan Das~~~~~~~~~~~~
381*9bb1b549SSpandan Das
382*9bb1b549SSpandan DasThis prepares a local path to use as the Go SDK in toolchains.
383*9bb1b549SSpandan Das
384*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
385*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
386*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
387*9bb1b549SSpandan Das| :param:`name`                  | :type:`string`              | |mandatory|                       |
388*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
389*9bb1b549SSpandan Das| A unique name for this SDK. This should almost always be :value:`go_sdk` if you want the SDK     |
390*9bb1b549SSpandan Das| to be used by toolchains.                                                                        |
391*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
392*9bb1b549SSpandan Das| :param:`path`                  | :type:`string`              | :value:`""`                       |
393*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
394*9bb1b549SSpandan Das| The local path to a pre-installed Go SDK. The path must contain the go binary, the tools it      |
395*9bb1b549SSpandan Das| invokes and the standard library sources.                                                        |
396*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
397*9bb1b549SSpandan Das| :param:`version`               | :type:`string`              | :value:`None`                     |
398*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
399*9bb1b549SSpandan Das| The version of the Go SDK. If specified, `go_local_sdk` will create its repository only when the |
400*9bb1b549SSpandan Das| build uses a Go toolchain and `toolchain resolution`_ results in this SDK being chosen.          |
401*9bb1b549SSpandan Das| Otherwise it will be created unconditionally.                                                    |
402*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
403*9bb1b549SSpandan Das| :param:`experiments`           | :type:`string_list`         | :value:`[]`                       |
404*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
405*9bb1b549SSpandan Das| Go experiments to enable via `GOEXPERIMENT`.                                                     |
406*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
407*9bb1b549SSpandan Das
408*9bb1b549SSpandan Das
409*9bb1b549SSpandan Dasgo_wrap_sdk
410*9bb1b549SSpandan Das~~~~~~~~~~~
411*9bb1b549SSpandan Das
412*9bb1b549SSpandan DasThis configures an SDK that was downloaded or located with another repository
413*9bb1b549SSpandan Dasrule.
414*9bb1b549SSpandan Das
415*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
416*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
417*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
418*9bb1b549SSpandan Das| :param:`name`                  | :type:`string`              | |mandatory|                       |
419*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
420*9bb1b549SSpandan Das| A unique name for this SDK. This should almost always be :value:`go_sdk` if you want the SDK     |
421*9bb1b549SSpandan Das| to be used by toolchains.                                                                        |
422*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
423*9bb1b549SSpandan Das| :param:`root_file`             | :type:`label`               | :value:`None`                     |
424*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
425*9bb1b549SSpandan Das| A Bazel label referencing a file in the root directory of the SDK. Used to                       |
426*9bb1b549SSpandan Das| determine the GOROOT for the SDK. This attribute and `root_files` cannot be both provided.       |
427*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
428*9bb1b549SSpandan Das| :param:`root_files`            | :type:`string_dict`         | :value:`None`                     |
429*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
430*9bb1b549SSpandan Das| A set of mappings from the host platform to a Bazel label referencing a file in the SDK's root   |
431*9bb1b549SSpandan Das| directory. This attribute and `root_file` cannot be both provided.                               |
432*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
433*9bb1b549SSpandan Das| :param:`version`               | :type:`string`              | :value:`None`                     |
434*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
435*9bb1b549SSpandan Das| The version of the Go SDK. If specified, `go_wrap_sdk` will create its repository only when the  |
436*9bb1b549SSpandan Das| build uses a Go toolchain and `toolchain resolution`_ results in this SDK being chosen.          |
437*9bb1b549SSpandan Das| Otherwise it will be created unconditionally.                                                    |
438*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
439*9bb1b549SSpandan Das| :param:`experiments`           | :type:`string_list`         | :value:`[]`                       |
440*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
441*9bb1b549SSpandan Das| Go experiments to enable via `GOEXPERIMENT`.                                                     |
442*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
443*9bb1b549SSpandan Das
444*9bb1b549SSpandan Das
445*9bb1b549SSpandan Das**Example:**
446*9bb1b549SSpandan Das
447*9bb1b549SSpandan Das.. code:: bzl
448*9bb1b549SSpandan Das
449*9bb1b549SSpandan Das    load(
450*9bb1b549SSpandan Das        "@io_bazel_rules_go//go:deps.bzl",
451*9bb1b549SSpandan Das        "go_register_toolchains",
452*9bb1b549SSpandan Das        "go_rules_dependencies",
453*9bb1b549SSpandan Das        "go_wrap_sdk",
454*9bb1b549SSpandan Das    )
455*9bb1b549SSpandan Das
456*9bb1b549SSpandan Das    go_wrap_sdk(
457*9bb1b549SSpandan Das        name = "go_sdk",
458*9bb1b549SSpandan Das        root_file = "@other_repo//go:README.md",
459*9bb1b549SSpandan Das    )
460*9bb1b549SSpandan Das
461*9bb1b549SSpandan Das    go_rules_dependencies()
462*9bb1b549SSpandan Das
463*9bb1b549SSpandan Das    go_register_toolchains()
464*9bb1b549SSpandan Das
465*9bb1b549SSpandan Dasgo_toolchain
466*9bb1b549SSpandan Das~~~~~~~~~~~~
467*9bb1b549SSpandan Das
468*9bb1b549SSpandan DasThis declares a toolchain that may be used with toolchain type
469*9bb1b549SSpandan Das:value:`"@io_bazel_rules_go//go:toolchain"`.
470*9bb1b549SSpandan Das
471*9bb1b549SSpandan DasNormally, ``go_toolchain`` rules are declared and registered in repositories
472*9bb1b549SSpandan Dasconfigured with `go_download_sdk`_, `go_host_sdk`_, `go_local_sdk`_, or
473*9bb1b549SSpandan Das`go_wrap_sdk`_. You usually won't need to declare these explicitly.
474*9bb1b549SSpandan Das
475*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
476*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
477*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
478*9bb1b549SSpandan Das| :param:`name`                  | :type:`string`              | |mandatory|                       |
479*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
480*9bb1b549SSpandan Das| A unique name for the toolchain.                                                                 |
481*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
482*9bb1b549SSpandan Das| :param:`goos`                  | :type:`string`              | |mandatory|                       |
483*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
484*9bb1b549SSpandan Das| The target operating system. Must be a standard ``GOOS`` value.                                  |
485*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
486*9bb1b549SSpandan Das| :param:`goarch`                | :type:`string`              | |mandatory|                       |
487*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
488*9bb1b549SSpandan Das| The target architecture. Must be a standard ``GOARCH`` value.                                    |
489*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
490*9bb1b549SSpandan Das| :param:`sdk`                   | :type:`label`               | |mandatory|                       |
491*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
492*9bb1b549SSpandan Das| The SDK this toolchain is based on. The target must provide `GoSDK`_. This is                    |
493*9bb1b549SSpandan Das| usually a `go_sdk`_ rule.                                                                        |
494*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
495*9bb1b549SSpandan Das| :param:`link_flags`            | :type:`string_list`         | :value:`[]`                       |
496*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
497*9bb1b549SSpandan Das| Flags passed to the Go external linker.                                                          |
498*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
499*9bb1b549SSpandan Das| :param:`cgo_link_flags`        | :type:`string_list`         | :value:`[]`                       |
500*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
501*9bb1b549SSpandan Das| Flags passed to the external linker (if it is used).                                             |
502*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
503*9bb1b549SSpandan Das
504*9bb1b549SSpandan Dasgo_context
505*9bb1b549SSpandan Das~~~~~~~~~~
506*9bb1b549SSpandan Das
507*9bb1b549SSpandan DasThis collects the information needed to form and return a :type:`GoContext` from
508*9bb1b549SSpandan Dasa rule ctx.  It uses the attributes and the toolchains.
509*9bb1b549SSpandan Das
510*9bb1b549SSpandan Das.. code:: bzl
511*9bb1b549SSpandan Das
512*9bb1b549SSpandan Das  def _my_rule_impl(ctx):
513*9bb1b549SSpandan Das      go = go_context(ctx)
514*9bb1b549SSpandan Das      ...
515*9bb1b549SSpandan Das
516*9bb1b549SSpandan Das
517*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
518*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
519*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
520*9bb1b549SSpandan Das| :param:`ctx`                   | :type:`ctx`                 | |mandatory|                       |
521*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
522*9bb1b549SSpandan Das| The Bazel ctx object for the current rule.                                                       |
523*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
524*9bb1b549SSpandan Das
525*9bb1b549SSpandan DasThe context object
526*9bb1b549SSpandan Das~~~~~~~~~~~~~~~~~~
527*9bb1b549SSpandan Das
528*9bb1b549SSpandan Das``GoContext`` is never returned by a rule, instead you build one using
529*9bb1b549SSpandan Das``go_context(ctx)`` in the top of any custom starlark rule that wants to interact
530*9bb1b549SSpandan Daswith the go rules.  It provides all the information needed to create go actions,
531*9bb1b549SSpandan Dasand create or interact with the other go providers.
532*9bb1b549SSpandan Das
533*9bb1b549SSpandan DasWhen you get a ``GoContext`` from a context it exposes a number of fields
534*9bb1b549SSpandan Dasand methods.
535*9bb1b549SSpandan Das
536*9bb1b549SSpandan DasAll methods take the ``GoContext`` as the only positional argument. All other
537*9bb1b549SSpandan Dasarguments must be passed as keyword arguments. This allows us to re-order and
538*9bb1b549SSpandan Dasdeprecate individual parameters over time.
539*9bb1b549SSpandan Das
540*9bb1b549SSpandan DasFields
541*9bb1b549SSpandan Das^^^^^^
542*9bb1b549SSpandan Das
543*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
544*9bb1b549SSpandan Das| **Name**                       | **Type**                                                        |
545*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
546*9bb1b549SSpandan Das| :param:`toolchain`             | :type:`ToolchainInfo`                                           |
547*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
548*9bb1b549SSpandan Das| The underlying toolchain. This should be considered an opaque type subject to change.            |
549*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
550*9bb1b549SSpandan Das| :param:`sdk`                   | :type:`GoSDK`                                                   |
551*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
552*9bb1b549SSpandan Das| The SDK in use. This may be used to access sources, packages, and tools.                         |
553*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
554*9bb1b549SSpandan Das| :param:`mode`                  | :type:`Mode`                                                    |
555*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
556*9bb1b549SSpandan Das| Controls the compilation setup affecting things like enabling profilers and sanitizers.          |
557*9bb1b549SSpandan Das| See `compilation modes`_ for more information about the allowed values.                          |
558*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
559*9bb1b549SSpandan Das| :param:`root`                  | :type:`string`                                                  |
560*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
561*9bb1b549SSpandan Das| Path of the effective GOROOT. If :param:`stdlib` is set, this is the same                        |
562*9bb1b549SSpandan Das| as ``go.stdlib.root_file.dirname``. Otherwise, this is the same as                               |
563*9bb1b549SSpandan Das| ``go.sdk.root_file.dirname``.                                                                    |
564*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
565*9bb1b549SSpandan Das| :param:`go`                    | :type:`File`                                                    |
566*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
567*9bb1b549SSpandan Das| The main "go" binary used to run go sdk tools.                                                   |
568*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
569*9bb1b549SSpandan Das| :param:`stdlib`                | :type:`GoStdLib`                                                |
570*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
571*9bb1b549SSpandan Das| The standard library and tools to use in this build mode. This may be the                        |
572*9bb1b549SSpandan Das| pre-compiled standard library that comes with the SDK, or it may be compiled                     |
573*9bb1b549SSpandan Das| in a different directory for this mode.                                                          |
574*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
575*9bb1b549SSpandan Das| :param:`actions`               | :type:`ctx.actions`                                             |
576*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
577*9bb1b549SSpandan Das| The actions structure from the Bazel context, which has all the methods for building new         |
578*9bb1b549SSpandan Das| bazel actions.                                                                                   |
579*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
580*9bb1b549SSpandan Das| :param:`exe_extension`         | :type:`string`                                                  |
581*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
582*9bb1b549SSpandan Das| The suffix to use for all executables in this build mode. Mostly used when generating the output |
583*9bb1b549SSpandan Das| filenames of binary rules.                                                                       |
584*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
585*9bb1b549SSpandan Das| :param:`shared_extension`      | :type:`string`                                                  |
586*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
587*9bb1b549SSpandan Das| The suffix to use for shared libraries in this build mode. Mostly used when                      |
588*9bb1b549SSpandan Das| generating output filenames of binary rules.                                                     |
589*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
590*9bb1b549SSpandan Das| :param:`crosstool`             | :type:`list of File`                                            |
591*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
592*9bb1b549SSpandan Das| The files you need to add to the inputs of an action in order to use the cc toolchain.           |
593*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
594*9bb1b549SSpandan Das| :param:`package_list`          | :type:`File`                                                    |
595*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
596*9bb1b549SSpandan Das| A file that contains the package list of the standard library.                                   |
597*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
598*9bb1b549SSpandan Das| :param:`env`                   | :type:`dict of string to string`                                |
599*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
600*9bb1b549SSpandan Das| Environment variables to pass to actions. Includes ``GOARCH``, ``GOOS``,                         |
601*9bb1b549SSpandan Das| ``GOROOT``, ``GOROOT_FINAL``, ``CGO_ENABLED``, and ``PATH``.                                     |
602*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
603*9bb1b549SSpandan Das| :param:`tags`                  | :type:`list of string`                                          |
604*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
605*9bb1b549SSpandan Das| List of build tags used to filter source files.                                                  |
606*9bb1b549SSpandan Das+--------------------------------+-----------------------------------------------------------------+
607*9bb1b549SSpandan Das
608*9bb1b549SSpandan DasMethods
609*9bb1b549SSpandan Das^^^^^^^
610*9bb1b549SSpandan Das
611*9bb1b549SSpandan Das* Action generators
612*9bb1b549SSpandan Das
613*9bb1b549SSpandan Das  * archive_
614*9bb1b549SSpandan Das  * binary_
615*9bb1b549SSpandan Das  * link_
616*9bb1b549SSpandan Das
617*9bb1b549SSpandan Das* Helpers
618*9bb1b549SSpandan Das
619*9bb1b549SSpandan Das  * args_
620*9bb1b549SSpandan Das  * `declare_file`_
621*9bb1b549SSpandan Das  * `library_to_source`_
622*9bb1b549SSpandan Das  * `new_library`_
623*9bb1b549SSpandan Das
624*9bb1b549SSpandan Das
625*9bb1b549SSpandan Dasarchive
626*9bb1b549SSpandan Das+++++++
627*9bb1b549SSpandan Das
628*9bb1b549SSpandan DasThis emits actions to compile Go code into an archive.  It supports embedding,
629*9bb1b549SSpandan Dascgo dependencies, coverage, and assembling and packing .s files.
630*9bb1b549SSpandan Das
631*9bb1b549SSpandan DasIt returns a GoArchive_.
632*9bb1b549SSpandan Das
633*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
634*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
635*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
636*9bb1b549SSpandan Das| :param:`go`                    | :type:`GoContext`           | |mandatory|                       |
637*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
638*9bb1b549SSpandan Das| This must be the same GoContext object you got this function from.                               |
639*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
640*9bb1b549SSpandan Das| :param:`source`                | :type:`GoSource`            | |mandatory|                       |
641*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
642*9bb1b549SSpandan Das| The GoSource_ that should be compiled into an archive.                                           |
643*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
644*9bb1b549SSpandan Das
645*9bb1b549SSpandan Das
646*9bb1b549SSpandan Dasbinary
647*9bb1b549SSpandan Das++++++
648*9bb1b549SSpandan Das
649*9bb1b549SSpandan DasThis emits actions to compile and link Go code into a binary.  It supports
650*9bb1b549SSpandan Dasembedding, cgo dependencies, coverage, and assembling and packing .s files.
651*9bb1b549SSpandan Das
652*9bb1b549SSpandan DasIt returns a tuple containing GoArchive_, the output executable file, and
653*9bb1b549SSpandan Dasa ``runfiles`` object.
654*9bb1b549SSpandan Das
655*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
656*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
657*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
658*9bb1b549SSpandan Das| :param:`go`                    | :type:`GoContext`           | |mandatory|                       |
659*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
660*9bb1b549SSpandan Das| This must be the same GoContext object you got this function from.                               |
661*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
662*9bb1b549SSpandan Das| :param:`name`                  | :type:`string`              | :value:`""`                       |
663*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
664*9bb1b549SSpandan Das| The base name of the generated binaries. Required if :param:`executable` is not given.           |
665*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
666*9bb1b549SSpandan Das| :param:`source`                | :type:`GoSource`            | |mandatory|                       |
667*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
668*9bb1b549SSpandan Das| The GoSource_ that should be compiled and linked.                                                |
669*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
670*9bb1b549SSpandan Das| :param:`test_archives`         | :type:`list GoArchiveData`  | :value:`[]`                       |
671*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
672*9bb1b549SSpandan Das| List of archives for libraries under test. See link_.                                            |
673*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
674*9bb1b549SSpandan Das| :param:`gc_linkopts`           | :type:`string_list`         | :value:`[]`                       |
675*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
676*9bb1b549SSpandan Das| Go link options.                                                                                 |
677*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
678*9bb1b549SSpandan Das| :param:`version_file`          | :type:`File`                | :value:`None`                     |
679*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
680*9bb1b549SSpandan Das| Version file used for link stamping. See link_.                                                  |
681*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
682*9bb1b549SSpandan Das| :param:`info_file`             | :type:`File`                | :value:`None`                     |
683*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
684*9bb1b549SSpandan Das| Info file used for link stamping. See link_.                                                     |
685*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
686*9bb1b549SSpandan Das| :param:`executable`            | :type:`File`                | :value:`None`                     |
687*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
688*9bb1b549SSpandan Das| Optional output file to write. If not set, ``binary`` will generate an output                    |
689*9bb1b549SSpandan Das| file name based on ``name``, the target platform, and the link mode.                             |
690*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
691*9bb1b549SSpandan Das
692*9bb1b549SSpandan Das
693*9bb1b549SSpandan Daslink
694*9bb1b549SSpandan Das++++
695*9bb1b549SSpandan Das
696*9bb1b549SSpandan DasThe link function adds an action that runs ``go tool link`` on a library.
697*9bb1b549SSpandan Das
698*9bb1b549SSpandan DasIt does not return anything.
699*9bb1b549SSpandan Das
700*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
701*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
702*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
703*9bb1b549SSpandan Das| :param:`go`                    | :type:`GoContext`           | |mandatory|                       |
704*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
705*9bb1b549SSpandan Das| This must be the same GoContext object you got this function from.                               |
706*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
707*9bb1b549SSpandan Das| :param:`archive`               | :type:`GoArchive`           | |mandatory|                       |
708*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
709*9bb1b549SSpandan Das| The library to link.                                                                             |
710*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
711*9bb1b549SSpandan Das| :param:`test_archives`         | :type:`GoArchiveData list`  | :value:`[]`                       |
712*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
713*9bb1b549SSpandan Das| List of archives for libraries under test. These are excluded from linking                       |
714*9bb1b549SSpandan Das| if transitive dependencies of :param:`archive` have the same package paths.                      |
715*9bb1b549SSpandan Das| This is useful for linking external test archives that depend internal test                      |
716*9bb1b549SSpandan Das| archives.                                                                                        |
717*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
718*9bb1b549SSpandan Das| :param:`executable`            | :type:`File`                | |mandatory|                       |
719*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
720*9bb1b549SSpandan Das| The binary to produce.                                                                           |
721*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
722*9bb1b549SSpandan Das| :param:`gc_linkopts`           | :type:`string_list`         | :value:`[]`                       |
723*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
724*9bb1b549SSpandan Das| Basic link options, these may be adjusted by the :param:`mode`.                                  |
725*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
726*9bb1b549SSpandan Das| :param:`version_file`          | :type:`File`                | :value:`None`                     |
727*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
728*9bb1b549SSpandan Das| Version file used for link stamping.                                                             |
729*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
730*9bb1b549SSpandan Das| :param:`info_file`             | :type:`File`                | :value:`None`                     |
731*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
732*9bb1b549SSpandan Das| Info file used for link stamping.                                                                |
733*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
734*9bb1b549SSpandan Das
735*9bb1b549SSpandan Das
736*9bb1b549SSpandan Dasargs
737*9bb1b549SSpandan Das++++
738*9bb1b549SSpandan Das
739*9bb1b549SSpandan DasThis creates a new Args_ object, using the ``ctx.actions.args`` method. The
740*9bb1b549SSpandan Dasobject is pre-populated with standard arguments used by all the go toolchain
741*9bb1b549SSpandan Dasbuilders.
742*9bb1b549SSpandan Das
743*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
744*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
745*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
746*9bb1b549SSpandan Das| :param:`go`                    | :type:`GoContext`           | |mandatory|                       |
747*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
748*9bb1b549SSpandan Das| This must be the same GoContext object you got this function from.                               |
749*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
750*9bb1b549SSpandan Das
751*9bb1b549SSpandan Dasdeclare_file
752*9bb1b549SSpandan Das++++++++++++
753*9bb1b549SSpandan Das
754*9bb1b549SSpandan DasThis is the equivalent of ``ctx.actions.declare_file``. It uses the
755*9bb1b549SSpandan Dascurrent build mode to make the filename unique between configurations.
756*9bb1b549SSpandan Das
757*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
758*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
759*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
760*9bb1b549SSpandan Das| :param:`go`                    | :type:`GoContext`           | |mandatory|                       |
761*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
762*9bb1b549SSpandan Das| This must be the same GoContext object you got this function from.                               |
763*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
764*9bb1b549SSpandan Das| :param:`path`                  | :type:`string`              | :value:`""`                       |
765*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
766*9bb1b549SSpandan Das| A path for this file, including the basename of the file.                                        |
767*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
768*9bb1b549SSpandan Das| :param:`ext`                   | :type:`string`              | :value:`""`                       |
769*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
770*9bb1b549SSpandan Das| The extension to use for the file.                                                               |
771*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
772*9bb1b549SSpandan Das| :param:`name`                  | :type:`string`              | :value:`""`                       |
773*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
774*9bb1b549SSpandan Das| A name to use for this file. If path is not present, this becomes a prefix to the path.          |
775*9bb1b549SSpandan Das| If this is not set, the current rule name is used in it's place.                                 |
776*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
777*9bb1b549SSpandan Das
778*9bb1b549SSpandan Daslibrary_to_source
779*9bb1b549SSpandan Das+++++++++++++++++
780*9bb1b549SSpandan Das
781*9bb1b549SSpandan DasThis is used to build a GoSource object for a given GoLibrary in the current
782*9bb1b549SSpandan Dasbuild mode.
783*9bb1b549SSpandan Das
784*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
785*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
786*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
787*9bb1b549SSpandan Das| :param:`go`                    | :type:`GoContext`           | |mandatory|                       |
788*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
789*9bb1b549SSpandan Das| This must be the same GoContext object you got this function from.                               |
790*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
791*9bb1b549SSpandan Das| :param:`attr`                  | :type:`ctx.attr`            | |mandatory|                       |
792*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
793*9bb1b549SSpandan Das| The attributes of the target being analyzed. For most rules, this should be                      |
794*9bb1b549SSpandan Das| ``ctx.attr``. Rules can also pass in a ``struct`` with the same fields.                          |
795*9bb1b549SSpandan Das|                                                                                                  |
796*9bb1b549SSpandan Das| ``library_to_source`` looks for fields corresponding to the attributes of                        |
797*9bb1b549SSpandan Das| ``go_library`` and ``go_binary``. This includes ``srcs``, ``deps``, ``embed``,                   |
798*9bb1b549SSpandan Das| and so on. All fields are optional (and may not be defined in the struct                         |
799*9bb1b549SSpandan Das| argument at all), but if they are present, they must have the same types and                     |
800*9bb1b549SSpandan Das| allowed values as in ``go_library`` and ``go_binary``. For example, ``srcs``                     |
801*9bb1b549SSpandan Das| must be a list of ``Targets``; ``gc_goopts`` must be a list of strings.                          |
802*9bb1b549SSpandan Das|                                                                                                  |
803*9bb1b549SSpandan Das| As an exception, ``deps``, if present, must be a list containing either                          |
804*9bb1b549SSpandan Das| ``Targets`` or ``GoArchives``.                                                                   |
805*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
806*9bb1b549SSpandan Das| :param:`library`               | :type:`GoLibrary`           | |mandatory|                       |
807*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
808*9bb1b549SSpandan Das| The GoLibrary_ that you want to build a GoSource_ object for in the current build mode.          |
809*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
810*9bb1b549SSpandan Das| :param:`coverage_instrumented` | :type:`bool`                | |mandatory|                       |
811*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
812*9bb1b549SSpandan Das| This controls whether cover is enabled for this specific library in this mode.                   |
813*9bb1b549SSpandan Das| This should generally be the value of ctx.coverage_instrumented()                                |
814*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
815*9bb1b549SSpandan Das
816*9bb1b549SSpandan Dasnew_library
817*9bb1b549SSpandan Das+++++++++++
818*9bb1b549SSpandan Das
819*9bb1b549SSpandan DasThis creates a new GoLibrary.  You can add extra fields to the go library by
820*9bb1b549SSpandan Dasproviding extra named parameters to this function, they will be visible to the
821*9bb1b549SSpandan Dasresolver when it is invoked.
822*9bb1b549SSpandan Das
823*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
824*9bb1b549SSpandan Das| **Name**                       | **Type**                    | **Default value**                 |
825*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
826*9bb1b549SSpandan Das| :param:`go`                    | :type:`GoContext`           | |mandatory|                       |
827*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
828*9bb1b549SSpandan Das| This must be the same GoContext object you got this function from.                               |
829*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
830*9bb1b549SSpandan Das| :param:`resolver`              | :type:`function`            | :value:`None`                     |
831*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
832*9bb1b549SSpandan Das| This is the function that gets invoked when converting from a GoLibrary to a GoSource.           |
833*9bb1b549SSpandan Das| The function's signature must be                                                                 |
834*9bb1b549SSpandan Das|                                                                                                  |
835*9bb1b549SSpandan Das| .. code:: bzl                                                                                    |
836*9bb1b549SSpandan Das|                                                                                                  |
837*9bb1b549SSpandan Das|     def _stdlib_library_to_source(go, attr, source, merge)                                       |
838*9bb1b549SSpandan Das|                                                                                                  |
839*9bb1b549SSpandan Das| attr is the attributes of the rule being processed                                               |
840*9bb1b549SSpandan Das| source is the dictionary of GoSource fields being generated                                      |
841*9bb1b549SSpandan Das| merge is a helper you can call to merge                                                          |
842*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
843*9bb1b549SSpandan Das| :param:`importable`            | :type:`bool`                | |mandatory|                       |
844*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
845*9bb1b549SSpandan Das| This controls whether the GoLibrary_ is supposed to be importable. This is generally only false  |
846*9bb1b549SSpandan Das| for the "main" libraries that are built just before linking.                                     |
847*9bb1b549SSpandan Das+--------------------------------+-----------------------------+-----------------------------------+
848