diff options
author | 2015-12-01 10:49:07 +0000 | |
---|---|---|
committer | 2015-12-01 18:23:07 +0000 | |
commit | 85c82397e65734449feeff7341a0432456723d00 (patch) | |
tree | 946cc31898458deeae2fb917f1b653e562340eea /tools/build_rules/go | |
parent | b4c590450fd9bd1a6613b8ec8e2d71738638f4a3 (diff) |
Go rules now set GOOS and GOARCH environment variables
based on Bazel's "cpu" attribute.
--
Change-Id: I4a625c206b2ec424a9d9eab2d20ec29447b4d2e1
Reviewed-on: https://bazel-review.git.corp.google.com/#/c/2266
MOS_MIGRATED_REVID=109064353
Diffstat (limited to 'tools/build_rules/go')
-rw-r--r-- | tools/build_rules/go/def.bzl | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/tools/build_rules/go/def.bzl b/tools/build_rules/go/def.bzl index 194b996108..54faf2286e 100644 --- a/tools/build_rules/go/def.bzl +++ b/tools/build_rules/go/def.bzl @@ -95,7 +95,34 @@ def symlink_tree_commands(dest_dir, artifact_dict): ] return cmds +def go_environment_vars(ctx): + """Return a map of environment variables for use with actions, based on + the arguments. Uses the ctx.fragments.cpp.cpu attribute, if present, + and picks a default of target_os="linux" and target_arch="amd64" + otherwise. + Args: + The skylark Context. + + Returns: + A dict of environment variables for running Go tool commands that build for + the target OS and architecture. + """ + bazel_to_go_toolchain = {"k8": {"GOOS": "linux", + "GOARCH": "amd64"}, + "piii": {"GOOS": "linux", + "GOARCH": "386"}, + "darwin": {"GOOS": "darwin", + "GOARCH": "amd64"}, + "freebsd": {"GOOS": "freebsd", + "GOARCH": "amd64"}, + "armeabi-v7a": {"GOOS": "linux", + "GOARCH": "arm"}, + "arm": {"GOOS": "linux", + "GOARCH": "arm"}} + return bazel_to_go_toolchain.get(ctx.fragments.cpp.cpu, + {"GOOS": "linux", + "GOARCH": "amd64"}) def emit_go_compile_action(ctx, sources, deps, out_lib): """Construct the command line for compiling Go code. @@ -160,7 +187,8 @@ def emit_go_compile_action(ctx, sources, deps, out_lib): inputs = inputs + ctx.files.toolchain, outputs = [out_lib], mnemonic = "GoCompile", - command = " && ".join(cmds)) + command = " && ".join(cmds), + env = go_environment_vars(ctx)) def go_library_impl(ctx): @@ -221,7 +249,8 @@ def emit_go_link_action(ctx, transitive_libs, lib, executable): inputs = list(transitive_libs) + [lib] + ctx.files.toolchain, outputs = [executable], command = ' && '.join(cmds), - mnemonic = "GoLink") + mnemonic = "GoLink", + env = go_environment_vars(ctx)) def go_binary_impl(ctx): @@ -260,7 +289,8 @@ def go_test_impl(ctx): executable = ctx.executable.test_generator, outputs = [main_go], mnemonic = "GoTestGenTest", - arguments = args) + arguments = args, + env = go_environment_vars(ctx)) emit_go_compile_action( ctx, set([main_go]), ctx.attr.deps + [lib_result], ctx.outputs.main_lib) @@ -309,6 +339,7 @@ go_library_outputs = { go_library = rule( go_library_impl, attrs = go_library_attrs, + fragments = ["cpp"], outputs = go_library_outputs) go_binary = rule( @@ -317,6 +348,7 @@ go_binary = rule( attrs = go_library_attrs + { "stamp": attr.bool(default=False), }, + fragments = ["cpp"], outputs = go_library_outputs) go_test = rule( @@ -329,6 +361,7 @@ go_test = rule( default=Label("//tools/build_rules/go/tools:generate_test_main"), cfg=HOST_CFG), }, + fragments = ["cpp"], outputs = { "lib" : "%{name}.a", "main_lib": "%{name}_main_test.a", |