Skip to content

Commit

Permalink
Add GoContext (bazel-contrib#1140)
Browse files Browse the repository at this point in the history
* Add GoContext

This collects up all dependancies that were on the toolchain, but shoudld be in
target configuration (rather than host) and migrates them to a common
go_context_data dependancy.
It then adds a GoContext object, that holds the toolchain, build mode, standard
library and all mode specific attributes. go_context(ctx) will build one of
these and prepare to use it.
This simplifies a lot of the logic in the rules, and helps enforce the isolation
of action methods from ctx attributes.

* Review feedback plus documentation
  • Loading branch information
ianthehat authored Dec 15, 2017
1 parent ecc72b5 commit 9031d58
Show file tree
Hide file tree
Showing 43 changed files with 826 additions and 770 deletions.
11 changes: 11 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ load("@io_bazel_rules_go//go:def.bzl", "gazelle", "go_prefix", "go_path", "go_ve
load("@io_bazel_rules_go//go/private:tools/lines_sorted_test.bzl", "lines_sorted_test")
load("@io_bazel_rules_go//go/private:rules/info.bzl", "go_info")
load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_google_protobuf")
load("@io_bazel_rules_go//go/private:context.bzl", "go_context_data")

go_context_data(
name = "go_context_data",
strip = select({
"@io_bazel_rules_go//go/private:strip-always": "always",
"@io_bazel_rules_go//go/private:strip-sometimes": "sometimes",
"@io_bazel_rules_go//go/private:strip-never": "never",
}),
visibility = ["//visibility:public"],
)

# gazelle:prefix github.com/bazelbuild/rules_go

Expand Down
2 changes: 1 addition & 1 deletion examples/bindata/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("@io_bazel_rules_go//examples/bindata:bindata.bzl", "bindata")
load("@io_bazel_rules_go//extras:bindata.bzl", "bindata")

bindata(
name = "data",
Expand Down
5 changes: 5 additions & 0 deletions extras/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filegroup(
name = "all_rules",
srcs = glob(["*.bzl"]) + ["//go/private:all_rules"],
visibility = ["//visibility:public"],
)
8 changes: 5 additions & 3 deletions examples/bindata/bindata.bzl → extras/bindata.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("@io_bazel_rules_go//go/private:go_repository.bzl", "go_repository")
load("@io_bazel_rules_go//go/private:common.bzl", "declare_file")
load("@io_bazel_rules_go//go:def.bzl", "go_context")

def _bindata_impl(ctx):
out = declare_file(ctx, ext=".go")
go = go_context(ctx)
out = go.declare_file(go, ext=".go")
arguments = ctx.actions.args()
arguments.add([
"-o", out.path,
Expand Down Expand Up @@ -35,5 +35,7 @@ bindata = rule(
"compress": attr.bool(default=True),
"metadata": attr.bool(default=False),
"_bindata": attr.label(allow_files=True, single_file=True, default=Label("@com_github_jteeuwen_go_bindata//go-bindata:go-bindata")),
"_go_context_data": attr.label(default=Label("@io_bazel_rules_go//:go_context_data")),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
)
21 changes: 7 additions & 14 deletions go/private/tools/embed_data.bzl → extras/embed_data.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@io_bazel_rules_go//go/private:common.bzl",
"declare_file",
)
load("@io_bazel_rules_go//go/private:rules/helpers.bzl",
"new_go_library",
"library_to_source",
)
load("@io_bazel_rules_go//go/private:mode.bzl",
"get_mode",
load("@io_bazel_rules_go//go/private:context.bzl", #TODO: This ought to be def
"go_context",
)

def _go_embed_data_impl(ctx):
go = go_context(ctx)
if ctx.attr.src and ctx.attr.srcs:
fail("%s: src and srcs attributes cannot both be specified" % ctx.label)
if ctx.attr.src and ctx.attr.flatten:
Expand All @@ -43,7 +37,7 @@ def _go_embed_data_impl(ctx):
if package == "":
fail("%s: must provide package attribute for go_embed_data rules in the repository root directory" % ctx.label)

out = declare_file(ctx, ext=".go")
out = go.declare_file(go, ext=".go")
args.add([
"-workspace", ctx.workspace_name,
"-label", str(ctx.label),
Expand All @@ -57,9 +51,8 @@ def _go_embed_data_impl(ctx):
args.add("-string")
args.add(srcs)

mode = get_mode(ctx, ctx.attr._go_toolchain_flags)
library = new_go_library(ctx, srcs=srcs)
source = library_to_source(ctx, ctx.attr, library, mode)
library = go.new_library(go, srcs=srcs)
source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())

ctx.actions.run(
outputs = [out],
Expand Down Expand Up @@ -87,7 +80,7 @@ go_embed_data = rule(
executable = True,
cfg = "host",
),
"_go_toolchain_flags": attr.label(default=Label("@io_bazel_rules_go//go/private:go_toolchain_flags")),
"_go_context_data": attr.label(default=Label("@io_bazel_rules_go//:go_context_data")),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
)
Expand Down
40 changes: 32 additions & 8 deletions go/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Core go rules
.. _gazelle: tools/gazelle/README.rst
.. _build constraints: http://golang.org/pkg/go/build/
.. _GoLibrary: providers.rst#GoLibrary
.. _GoSourceList: providers.rst#GoSourceList
.. _GoSource: providers.rst#GoSource
.. _GoArchive: providers.rst#GoArchive
.. _cgo: http://golang.org/cmd/cgo/
.. _"Make variable": https://docs.bazel.build/versions/master/be/make-variables.html
Expand All @@ -15,6 +15,8 @@ Core go rules
.. _cc library deps: https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.deps
.. _pure: modes.rst#pure
.. _static: modes.rst#static
.. _goos: modes.rst#goos
.. _goarch: modes.rst#goarch
.. _mode attributes: modes.rst#mode-attributes

.. role:: param(kbd)
Expand Down Expand Up @@ -58,7 +60,7 @@ Providers
^^^^^^^^^

* GoLibrary_
* GoSourceList_
* GoSource_
* GoArchive_

Attributes
Expand Down Expand Up @@ -94,7 +96,7 @@ Attributes
| :param:`embed` | :type:`label_list` | :value:`None` |
+----------------------------+-----------------------------+---------------------------------------+
| List of Go libraries this test library directly. |
| These may be go_library rules or compatible rules with the GoSourceList_ provider. |
| These may be go_library rules or compatible rules with the GoLibrary_ provider. |
| These can provide both :param:`srcs` and :param:`deps` to this library. |
| See Embedding_ for more information about how and when to use this. |
+----------------------------+-----------------------------+---------------------------------------+
Expand Down Expand Up @@ -167,7 +169,8 @@ Providers
^^^^^^^^^

* GoLibrary_
* GoSourceList_
* GoSource_
* GoArchive_

Attributes
^^^^^^^^^^
Expand Down Expand Up @@ -202,7 +205,7 @@ Attributes
| :param:`embed` | :type:`label_list` | :value:`None` |
+----------------------------+-----------------------------+---------------------------------------+
| List of Go libraries this binary embeds directly. |
| These may be go_library rules or compatible rules with the GoSourceList_ provider. |
| These may be go_library rules or compatible rules with the GoLibrary_ provider. |
| These can provide both :param:`srcs` and :param:`deps` to this binary. |
| See Embedding_ for more information about how and when to use this. |
+----------------------------+-----------------------------+---------------------------------------+
Expand All @@ -223,6 +226,26 @@ Attributes
| This is one of the `mode attributes`_ that controls whether to link in static_ mode. |
| It should be one of :value:`on`, :value:`off` or :value:`auto`. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`goos` | :type:`string` | :value:`auto` |
+----------------------------+-----------------------------+---------------------------------------+
| This is one of the `mode attributes`_ that controls which goos_ to compile and link for. |
| |
| If set to anything other than :value:`auto` this overrideds the default as set by the current |
| target platform, and allows for single builds to make binaries for multiple architectures. |
| |
| Because this has no control over the cc toolchain, it does not work for cgo, so if this |
| attribute is set then :param:`pure` must be set to :value:`on`. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`goarch` | :type:`string` | :value:`auto` |
+----------------------------+-----------------------------+---------------------------------------+
| This is one of the `mode attributes`_ that controls which goarch_ to compile and link for. |
| |
| If set to anything other than :value:`auto` this overrideds the default as set by the current |
| target platform, and allows for single builds to make binaries for multiple architectures. |
| |
| Because this has no control over the cc toolchain, it does not work for cgo, so if this |
| attribute is set then :param:`pure` must be set to :value:`on`. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`gc_goopts` | :type:`string_list` | :value:`[]` |
+----------------------------+-----------------------------+---------------------------------------+
| List of flags to add to the Go compilation command when using the gc compiler. |
Expand Down Expand Up @@ -314,7 +337,7 @@ Attributes
| :param:`embed` | :type:`label_list` | :value:`None` |
+----------------------------+-----------------------------+---------------------------------------+
| List of Go libraries this test embeds directly. |
| These may be go_library rules or compatible rules with the GoSourceList_ provider. |
| These may be go_library rules or compatible rules with the GoLibrary_ provider. |
| These can provide both :param:`srcs` and :param:`deps` to this test. |
| See Embedding_ for more information about how and when to use this. |
+----------------------------+-----------------------------+---------------------------------------+
Expand Down Expand Up @@ -438,7 +461,8 @@ This is used as a way of easily declaring a common set of sources re-used in mul
Providers
^^^^^^^^^

* GoSourceList_
* GoLibrary_
* GoSource_

Attributes
^^^^^^^^^^
Expand All @@ -464,7 +488,7 @@ Attributes
| :param:`embed` | :type:`label_list` | :value:`None` |
+----------------------------+-----------------------------+---------------------------------------+
| List of sources to directly embed in this list. |
| These may be go_library rules or compatible rules with the GoSourceList_ provider. |
| These may be go_library rules or compatible rules with the GoSource_ provider. |
| These can provide both :param:`srcs` and :param:`deps` to this library. |
| See Embedding_ for more information about how and when to use this. |
+----------------------------+-----------------------------+---------------------------------------+
Expand Down
7 changes: 5 additions & 2 deletions go/def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@io_bazel_rules_go//go/private:context.bzl",
go_context = "go_context",
)
load("@io_bazel_rules_go//go/private:go_repository.bzl",
"go_repository",
)
Expand All @@ -22,7 +25,7 @@ load("@io_bazel_rules_go//go/private:repositories.bzl",
"go_rules_dependencies",
"go_register_toolchains",
)
load("@io_bazel_rules_go//go/private:toolchain.bzl",
load("@io_bazel_rules_go//go/private:sdk.bzl",
go_host_sdk = "go_host_sdk",
go_download_sdk = "go_download_sdk",
go_local_sdk = "go_local_sdk",
Expand All @@ -42,7 +45,7 @@ load("@io_bazel_rules_go//go/private:rules/wrappers.bzl",
load("@io_bazel_rules_go//go/private:rules/source.bzl",
_go_source = "go_source",
)
load("@io_bazel_rules_go//go/private:tools/embed_data.bzl",
load("@io_bazel_rules_go//extras:embed_data.bzl",
"go_embed_data",
)
load("@io_bazel_rules_go//go/private:tools/gazelle.bzl",
Expand Down
26 changes: 25 additions & 1 deletion go/modes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The entire transitive set of libraries that a leaf depends on are built in the m
the binary rule. The compiled libraries are distinct and multiple modes can be built in a single pass,
but are shared between leaves building in the same mode.

Currently only static_ and pure_ can be specified as attributes.
Currently only static_, pure_, goos_ and goarch_ can be specified as attributes.
Both of these can take one of the values "on", "off" or "auto", and "auto" is the default.

+--------------+-------------------------------------------------------------------------+
Expand All @@ -88,6 +88,8 @@ following fields that control the bevhaviour of those actions:
* link_
* debug_
* strip_
* goos_
* goarch_

Build modes
-----------
Expand Down Expand Up @@ -148,6 +150,16 @@ strip

Causes debugging information to be stripped from the binaries.

goos
~~~~

This controls which operating system to target.

goarch
~~~~~~

This controls which architecture to target.

Using build modes
-----------------

Expand Down Expand Up @@ -204,3 +216,15 @@ the mode of tests by using
.. code::
bazel test --features=race //...
but in general it is strongly recommended instead to turn it on for specific tests.

.. code::
go_test(
name = "go_default_test",
srcs = ["lib_test.go"],
embed = [":go_default_library"],
race = "on",
)
11 changes: 0 additions & 11 deletions go/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
load("@io_bazel_rules_go//go/private:go_toolchain.bzl", "go_toolchain_flags")

filegroup(
name = "all_rules",
srcs = glob(["**/*.bzl"]),
Expand All @@ -21,12 +19,3 @@ config_setting(
values = {"strip": "never"},
)

go_toolchain_flags(
name = "go_toolchain_flags",
strip = select({
"@io_bazel_rules_go//go/private:strip-always": "always",
"@io_bazel_rules_go//go/private:strip-sometimes": "sometimes",
"@io_bazel_rules_go//go/private:strip-never": "never",
}),
visibility = ["//visibility:public"],
)
32 changes: 0 additions & 32 deletions go/private/actions/action.bzl

This file was deleted.

Loading

0 comments on commit 9031d58

Please sign in to comment.