Skip to content

Commit

Permalink
Merge branch 'master' into sluongng/rm-deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
linzhp authored May 16, 2023
2 parents f4280f5 + c403db6 commit 55812e9
Show file tree
Hide file tree
Showing 24 changed files with 688 additions and 176 deletions.
32 changes: 0 additions & 32 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ tasks:
bazel: 5.4.0 # test minimum supported version of bazel
shell_commands:
- tests/core/cgo/generate_imported_dylib.sh
build_flags:
# Temporary rollback to fix //tests/core/go_path builds
# https://github.com/bazelbuild/continuous-integration/commit/a95a916098d3015bb4ea20b7e33bc7d27d00bffc
- "--remote_download_outputs=all"
- "--build_runfile_links"
build_targets:
- "//..."
test_flags:
Expand Down Expand Up @@ -57,20 +52,12 @@ tasks:
- "--apple_crosstool_top=@local_config_apple_cc//:toolchain"
- "--crosstool_top=@local_config_apple_cc//:toolchain"
- "--host_crosstool_top=@local_config_apple_cc//:toolchain"
# Temporary rollback to fix //tests/core/cgo:versioned_dylib_test
# https://github.com/bazelbuild/continuous-integration/commit/a95a916098d3015bb4ea20b7e33bc7d27d00bffc
- "--remote_download_outputs=all"
- "--build_runfile_links"
build_targets:
- "//..."
test_flags:
- "--apple_crosstool_top=@local_config_apple_cc//:toolchain"
- "--crosstool_top=@local_config_apple_cc//:toolchain"
- "--host_crosstool_top=@local_config_apple_cc//:toolchain"
# Temporary rollback to fix //tests/core/cgo:versioned_dylib_test
# https://github.com/bazelbuild/continuous-integration/commit/a95a916098d3015bb4ea20b7e33bc7d27d00bffc
- "--remote_download_outputs=all"
- "--build_runfile_links"
test_targets:
- "//..."
rbe_ubuntu1604:
Expand All @@ -90,21 +77,10 @@ tasks:
test_targets:
- "--"
- "//..."
- "-//tests/core/go_path:go_path_test"
- "-//tests/core/stdlib:buildid_test"
windows:
build_flags:
# Go requires a C toolchain that accepts options and emits errors like
# gcc or clang. The Go SDK does not support MSVC.
- "--cpu=x64_windows"
- "--compiler=mingw-gcc"
- '--action_env=PATH=C:\tools\msys64\usr\bin;C:\tools\msys64\bin;C:\tools\msys64\mingw64\bin;C:\python3\Scripts\;C:\python3;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\ProgramData\GooGet;C:\Program Files\Google\Compute Engine\metadata_scripts;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin;C:\Program Files\Google\Compute Engine\sysprep;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\tools\msys64\usr\bin;c:\openjdk\bin;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\CMake\bin;c:\ninja;c:\bazel;c:\buildkite'
# NOTE(bazelbuild/bazel#10529): bazel doesn't register the mingw toolchain automatically.
# We also need the host and target platforms to have the mingw constraint value.
- "--extra_toolchains=@local_config_cc//:cc-toolchain-x64_windows_mingw"
- "--host_platform=@io_bazel_rules_go//go/toolchain:windows_amd64_cgo"
- "--platforms=@io_bazel_rules_go//go/toolchain:windows_amd64_cgo"
- "--incompatible_enable_cc_toolchain_resolution"
build_targets:
# BUG(bazelbuild/bazel#6485): Bazel 0.18.0 crashes when loading
# @com_google_protobuf//:protobuf. We have to exclude everything that
Expand Down Expand Up @@ -279,15 +255,7 @@ tasks:
- "-//tests/legacy/test_rundir:go_default_test"
- "-//tests/legacy/transitive_data:go_default_test"
test_flags:
# Go requires a C toolchain that accepts options and emits errors like
# gcc or clang. The Go SDK does not support MSVC.
- "--cpu=x64_windows"
- "--compiler=mingw-gcc"
- '--action_env=PATH=C:\tools\msys64\usr\bin;C:\tools\msys64\bin;C:\tools\msys64\mingw64\bin;C:\python3\Scripts\;C:\python3;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\ProgramData\GooGet;C:\Program Files\Google\Compute Engine\metadata_scripts;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin;C:\Program Files\Google\Compute Engine\sysprep;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\tools\msys64\usr\bin;c:\openjdk\bin;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\CMake\bin;c:\ninja;c:\bazel;c:\buildkite'
- "--extra_toolchains=@local_config_cc//:cc-toolchain-x64_windows_mingw"
- "--host_platform=@io_bazel_rules_go//go/toolchain:windows_amd64_cgo"
- "--platforms=@io_bazel_rules_go//go/toolchain:windows_amd64_cgo"
- "--incompatible_enable_cc_toolchain_resolution"
# On Windows CI, bazel (bazelisk) needs %LocalAppData% to find the cache directory.
# We invoke bazel in tests, so the tests need this, too.
- "--test_env=LOCALAPPDATA"
Expand Down
16 changes: 16 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
common --enable_platform_specific_config

# //tests/core/go_path is incompatible with BwoB, which is enabled in Bazel CI.
# https://github.com/bazelbuild/continuous-integration/commit/a95a916098d3015bb4ea20b7e33bc7d27d00bffc
build --remote_download_outputs=all
build --build_runfile_links
build --experimental_remote_download_regex=.*tests/core/go_path/.*

# Go requires a C toolchain that accepts options and emits errors like
# gcc or clang. The Go SDK does not support MSVC.
build:windows --cpu=x64_windows
build:windows --compiler=mingw-gcc
# NOTE(bazelbuild/bazel#10529): bazel doesn't register the mingw toolchain automatically.
# We also need the host and target platforms to have the mingw constraint value.
build:windows --extra_toolchains=@local_config_cc//:cc-toolchain-x64_windows_mingw
build:windows --host_platform=@io_bazel_rules_go//go/toolchain:windows_amd64_cgo
build:windows --platforms=@io_bazel_rules_go//go/toolchain:windows_amd64_cgo
build:windows --incompatible_enable_cc_toolchain_resolution

build:check --all_incompatible_changes

Expand Down
6 changes: 5 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ go_config(
"//conditions:default": False,
}),
static = "//go/config:static",
strip = "//go/config:strip",
strip = select({
"//go/private:is_strip_always": True,
"//go/private:is_strip_sometimes_fastbuild": True,
"//conditions:default": False,
}),
visibility = ["//visibility:public"],
)

Expand Down
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Go rules for Bazel_
.. _Bazel labels: https://docs.bazel.build/versions/master/build-ref.html#labels
.. _Bazel: https://bazel.build/
.. _Build modes: go/modes.rst
.. _Bzlmod: https://bazel.build/external/overview#bzlmod
.. _Go with Bzlmod: docs/go/core/bzlmod.md
.. _Core rules: docs/go/core/rules.md
.. _Coverage: https://bazel.google.cn/docs/coverage
.. _Dependencies: go/dependencies.rst
Expand Down Expand Up @@ -223,6 +225,9 @@ Several additional tools need to be installed and configured.
Initial project setup
~~~~~~~~~~~~~~~~~~~~~

If you are using Bazel's new external dependency management system `Bzlmod`_,
refer to the dedicated `Go with Bzlmod`_ guide instead.

Create a file at the top of your repository named ``WORKSPACE``, and add the
snippet below (or add to your existing ``WORKSPACE``). This tells Bazel to
fetch rules_go and its dependencies. Bazel will download a recent supported
Expand Down
180 changes: 180 additions & 0 deletions docs/go/core/bzlmod.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
# Go with Bzlmod

This document describes how to use rules_go and Gazelle with Bazel's new external dependency subsystem [Bzlmod](https://bazel.build/external/overview#bzlmod), which is meant to replace `WORKSPACE` files eventually.
Usages of rules_go and Gazelle in `BUILD` files are not affected by this; refer to the existing documentation on rules and configuration options for them.

## Setup

Add the following lines to your `MODULE.bazel` file:

```starlark
bazel_dep(name = "rules_go", version = "0.39.1")
bazel_dep(name = "gazelle", version = "0.29.0")
```

The latest versions are always listed on https://registry.bazel.build/.

If you have WORKSPACE dependencies that reference rules_go and/or Gazelle, you can still use the legacy repository names for the two repositories:

```starlark
bazel_dep(name = "rules_go", version = "0.39.1", repo_name = "io_bazel_rules_go")
bazel_dep(name = "gazelle", version = "0.29.0", repo_name = "bazel_gazelle")
```

## Registering Go SDKs

rules_go automatically downloads and registers a recent Go SDK, so unless a particular version is required, no manual steps are required.

To register a particular version of the Go SDK, use the `go_sdk` module extension:

```starlark
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")

# Download an SDK for the host OS & architecture.
go_sdk.download(version = "1.20.3")

# Alternately, download an SDK for a fixed OS/architecture, e.g. for remote execution.
go_sdk.download(
version = "1.20.3",
goarch = "amd64",
goos = "linux",
)

# Register the Go SDK installed on the host.
go_sdk.host()
```

You can register multiple Go SDKs and select which one to use on a per-target basis using [`go_cross_binary`](rules.md#go_cross_binary).
The usual rules of [toolchain resolution](https://bazel.build/extending/toolchains#toolchain-resolution) apply, with SDKs registered in the root module taking precedence over those registered in dependencies.

### Using a Go SDK

By default, Go SDK repositories are created with mangled names and are not expected to be referenced directly.

For build actions, toolchain resolution is used to select the appropriate SDK for a given target.
[`go_cross_binary`](rules.md#go_cross_binary) can be used to influence the outcome of the resolution.

The `go` tool of the SDK registered for the host is available via the `@rules_go//go` target.
Prefer running it via this target over running `go` directly to ensure that all developers use the same version.
The `@rules_go//go` target can be used in scripts executed via `bazel run`, but cannot be used in build actions.
Note that `go` command arguments starting with `-` require the use of the double dash separator with `bazel run`:

```sh
bazel run @rules_go//go -- mod tidy -v
```

If you really do need direct access to a Go SDK, you can provide the `name` attribute on the `go_sdk.download` or `go_sdk.host` tag and then bring the repository with that name into scope via `use_repo`.
Note that modules using this attribute cannot be added to registries such as the Bazel Central Registry (BCR).
If you have a use case that would require this, please explain it in an issue.

### Not yet supported

* `go_local_sdk`
* `go_wrap_sdk`
* nogo ([#3529](https://github.com/bazelbuild/rules_go/issues/3529))

## Generating BUILD files

Add the following to your top-level BUILD file, including your Go module's path in a [Gazelle directive](https://github.com/bazelbuild/bazel-gazelle#directives):

```starlark
load("@gazelle//:defs.bzl", "gazelle")

# gazelle:prefix github.com/example/project
gazelle(name = "gazelle")
```

Then, use `bazel run //:gazelle` to (re-)generate BUILD files.

## External dependencies

External Go dependencies are managed by the `go_deps` module extension provided by Gazelle.
`go_deps` performs [Minimal Version Selection](https://go.dev/ref/mod#minimal-version-selection) on all transitive Go dependencies of all Bazel modules, so compared to the old WORKSPACE setup, every Bazel module only needs to declare its own Go dependencies.
For every major version of a Go module, there will only ever be a single version in the entire build, just as in regular Go module builds.

### Specifying external dependencies

Even though this is not a strict requirement, for interoperability with Go tooling that isn't Bazel-aware, it is recommended to manage Go dependencies via `go.mod`.
The `go_deps` extension parses this file directly, so external tooling such as `gazelle update-repos` is no longer needed.

Register the `go.mod` file with the `go_deps` extension as follows:

```starlark
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")

# All *direct* Go dependencies of the module have to be listed explicitly.
use_repo(
go_deps,
"com_github_gogo_protobuf",
"com_github_golang_mock",
"com_github_golang_protobuf",
"org_golang_x_net",
)
```

There is [ongoing work](https://github.com/bazelbuild/bazel-gazelle/pull/1511) targeted for Bazel 6.2.0 to automate the generation of the `use_repo` statement.

Alternatively, you can specify a module extension tag to add an individual dependency.
This can be useful for dependencies of generated code that `go mod tidy` would remove. (There is [ongoing work](https://github.com/bazelbuild/bazel-gazelle/pull/1495) to provide a Bazel-aware version of `tidy`.)

```starlark
go_deps.module(
path = "google.golang.org/grpc",
sum = "h1:fPVVDxY9w++VjTZsYvXWqEf9Rqar/e+9zYfxKK+W+YU=",
version = "v1.50.0",
)
```

### Managing `go.mod`

An initial `go.mod` file can be created via

```sh
bazel run @rules_go//go mod init github.com/example/project
```

A dependency can be added via

```sh
bazel run @rules_go//go get golang.org/x/[email protected]
```

### Overrides

The root module can override certain aspects of the dependency resolution performed by the `go_deps` extension.

#### `replace`

[`replace` directives](https://go.dev/ref/mod#go-mod-file-replace) in `go.mod` can be used to replace particular versions of dependencies with other versions or entirely different modules.
At the moment the only supported form is:

```
replace(
golang.org/x/net v1.2.3 => example.com/fork/net v1.4.5
)
```

#### Gazelle directives

Some external Go modules may require tweaking how Gazelle generates BUILD files for them via [Gazelle directives](https://github.com/bazelbuild/bazel-gazelle#directives).
The `go_deps` extension provides a dedicated `go_deps.gazelle_override` tag for this purpose:

```starlark
go_deps.gazelle_override(
directives = [
"gazelle:go_naming_convention go_default_library",
],
path = "github.com/stretchr/testify",
)
```

If you need to use a `gazelle_override` to get a public Go module to build with Bazel, consider contributing the directives to the [public registry for Gazelle directives](https://github.com/bazelbuild/bazel-gazelle/blob/master/internal/bzlmod/directives.bzl) via a PR.
This will allow you to drop the `gazelle_override` tag and also makes the Go module usable in non-root Bazel modules.

### Not yet supported

* Fetching dependencies from Git repositories or via HTTP
* `go.mod` `replace` directives matching all versions of a module
* `go.mod` `replace` directives referencing local files
* `go.mod` `exclude` directices
7 changes: 0 additions & 7 deletions go/config/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
load(
"@bazel_skylib//rules:common_settings.bzl",
"bool_flag",
"bool_setting",
"string_flag",
"string_list_flag",
)
Expand Down Expand Up @@ -34,12 +33,6 @@ bool_flag(
visibility = ["//visibility:public"],
)

bool_setting(
name = "strip",
build_setting_default = False,
visibility = ["//visibility:public"],
)

bool_flag(
name = "debug",
build_setting_default = False,
Expand Down
6 changes: 0 additions & 6 deletions go/modes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ or using `Bazel configuration transitions`_.
| ``CGO_ENABLED=0``). Packages that contain cgo code may still be built, but |
| the cgo code will be filtered out, and the ``cgo`` build tag will be false. |
+-------------------+---------------------+------------------------------------+
| :param:`strip` | :type:`bool` | :value:`false` |
+-------------------+---------------------+------------------------------------+
| Strips symbols from compiled packages and linked binaries (using the ``-w`` |
| flag). May also be set with the ``--strip`` command line option, which |
| affects C/C++ targets, too. |
+-------------------+---------------------+------------------------------------+
| :param:`debug` | :type:`bool` | :value:`false` |
+-------------------+---------------------+------------------------------------+
| Includes debugging information in compiled packages (using the ``-N`` and |
Expand Down
Loading

0 comments on commit 55812e9

Please sign in to comment.