Skip to content

Commit

Permalink
Add iOS toolchains, platforms, and constraint_values (#2090)
Browse files Browse the repository at this point in the history
Refactored the description of target platforms. The source of truth is
now the PLATFORMS table go/private/platforms.bzl. Declarations for
config_settings in //go/platform; constraint_values and aliases in
//go/toolchain; and toolchains in @go_sdk are generated from this
table.

In addition to the normal GOOS_GOARCH list, there are some additional
entries for iOS. iOS platforms still have GOOS=darwin, but they are
compatible with a different constraint_value
(@bazel_tools//platforms:ios instead of @bazel_tools//platforms:osx).

There are also separate platforms for pure mode, and cgo (with _cgo suffix).
Cross compilation may still be done with (for example)
--platforms=@io_bazel_rules_go//go/platforms:linux_amd64, and this
no longer requires a configured C/C++ toolchain. To cross-compile with cgo,
use --platforms=@io_bazel_rules_go//go/platforms:linux_amd64_cgo.
A compatible configured C/C++ toolchain is required in that case.

Fixes #2079
  • Loading branch information
Jay Conrod authored Jun 13, 2019
1 parent 20ab486 commit f2373c9
Show file tree
Hide file tree
Showing 20 changed files with 583 additions and 274 deletions.
11 changes: 10 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_path")
load("@io_bazel_rules_go//go/private:tools/lines_sorted_test.bzl", "lines_sorted_test")
load("@io_bazel_rules_go//go/private:rules/nogo.bzl", "nogo")
load("@io_bazel_rules_go//go/private:rules/info.bzl", "go_info")
load("@io_bazel_rules_go//go/private:context.bzl", "go_context_data")
load("@io_bazel_rules_go//go/private:context.bzl", "cgo_context_data", "go_context_data")
load("@io_bazel_rules_go//go/private:rules/stdlib.bzl", "stdlib")

stdlib(
Expand Down Expand Up @@ -59,6 +59,8 @@ nogo(
],
)

# go_context_data collects build options and is depended on by all Go targets.
# It may depend on cgo_context_data via go_toolchain.
go_context_data(
name = "go_context_data",
strip = select({
Expand All @@ -69,6 +71,13 @@ go_context_data(
visibility = ["//visibility:public"],
)

# cgo_context_data collects information about the C/C++ toolchain.
# go_toolchains may depend on this in order to support cgo.
cgo_context_data(
name = "cgo_context_data",
visibility = ["//visibility:public"],
)

lines_sorted_test(
name = "contributors_sorted_test",
size = "small",
Expand Down
123 changes: 32 additions & 91 deletions go/platform/list.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,113 +12,54 @@
# See the License for the specific language governing permissions and
# limitations under the License.

GOOS = {
"android": None,
"darwin": "@bazel_tools//platforms:osx",
"dragonfly": None,
"freebsd": "@bazel_tools//platforms:freebsd",
"linux": "@bazel_tools//platforms:linux",
"nacl": None,
"netbsd": None,
"openbsd": None,
"plan9": None,
"solaris": None,
"windows": "@bazel_tools//platforms:windows",
"js": None,
}

GOARCH = {
"386": "@bazel_tools//platforms:x86_32",
"amd64": "@bazel_tools//platforms:x86_64",
"amd64p32": None,
"arm": "@bazel_tools//platforms:arm",
"arm64": "@bazel_tools//platforms:aarch64",
"mips": None,
"mips64": None,
"mips64le": None,
"mipsle": None,
"ppc64": None,
"ppc64le": "@bazel_tools//platforms:ppc",
"s390x": "@bazel_tools//platforms:s390x",
"wasm": None,
}

GOOS_GOARCH = (
("android", "386"),
("android", "amd64"),
("android", "arm"),
("android", "arm64"),
("darwin", "386"),
("darwin", "amd64"),
("darwin", "arm"),
("darwin", "arm64"),
("dragonfly", "amd64"),
("freebsd", "386"),
("freebsd", "amd64"),
("freebsd", "arm"),
("linux", "386"),
("linux", "amd64"),
("linux", "arm"),
("linux", "arm64"),
("linux", "mips"),
("linux", "mips64"),
("linux", "mips64le"),
("linux", "mipsle"),
("linux", "ppc64"),
("linux", "ppc64le"),
("linux", "s390x"),
("nacl", "386"),
("nacl", "amd64p32"),
("nacl", "arm"),
("netbsd", "386"),
("netbsd", "amd64"),
("netbsd", "arm"),
("openbsd", "386"),
("openbsd", "amd64"),
("openbsd", "arm"),
("plan9", "386"),
("plan9", "amd64"),
("plan9", "arm"),
("solaris", "amd64"),
("windows", "386"),
("windows", "amd64"),
("js", "wasm"),
load(
"@io_bazel_rules_go//go/private:platforms.bzl",
"PLATFORMS",
_GOARCH = "GOARCH_CONSTRAINTS",
_GOOS = "GOOS_CONSTRAINTS",
_GOOS_GOARCH = "GOOS_GOARCH",
_MSAN_GOOS_GOARCH = "MSAN_GOOS_GOARCH",
_RACE_GOOS_GOARCH = "RACE_GOOS_GOARCH",
)

RACE_GOOS_GOARCH = (
("darwin", "amd64"),
("freebsd", "amd64"),
("linux", "amd64"),
("windows", "amd64"),
)
GOOS_GOARCH = _GOOS_GOARCH
GOOS = _GOOS
GOARCH = _GOARCH
RACE_GOOS_GOARCH = _RACE_GOOS_GOARCH
MSAN_GOOS_GOARCH = _MSAN_GOOS_GOARCH

MSAN_GOOS_GOARCH = (
("linux", "amd64"),
)
def _os_constraint(goos):
if goos == "darwin":
return "@io_bazel_rules_go//go/toolchain:is_darwin"
else:
return "@io_bazel_rules_go//go/toolchain:" + goos

def _arch_constraint(goarch):
return "@io_bazel_rules_go//go/toolchain:" + goarch

def declare_config_settings():
"""Generates config_setting targets for each goos, goarch, and valid
goos_goarch pair. These targets may be used in select expressions.
Each target refers to a corresponding constraint_value in //go/toolchain.
Note that the "darwin" targets are true when building for either
macOS or iOS.
"""
for goos in GOOS:
native.config_setting(
name = goos,
constraint_values = ["//go/toolchain:" + goos],
constraint_values = [_os_constraint(goos)],
)
for goarch in GOARCH:
native.config_setting(
name = goarch,
constraint_values = ["//go/toolchain:" + goarch],
constraint_values = [_arch_constraint(goarch)],
)
for goos, goarch in GOOS_GOARCH:
native.config_setting(
name = goos + "_" + goarch,
constraint_values = [
"//go/toolchain:" + goos,
"//go/toolchain:" + goarch,
_os_constraint(goos),
_arch_constraint(goarch),
],
)

def generate_toolchain_names():
# Keep in sync with generate_toolchains
return [
"go_{}_{}".format(target_goos, target_goarch)
for target_goos, target_goarch in GOOS_GOARCH
]
66 changes: 37 additions & 29 deletions go/private/actions/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ def emit_archive(go, source = None):
cxxopts = [f for fs in source.cxxopts for f in fs.split(" ")]
clinkopts = [f for fs in source.clinkopts for f in fs.split(" ")]

cgo_inputs = depset()
cgo_deps = depset()
if source.cgo:
if source.cgo and not go.mode.pure:
cgo = cgo_configure(
go,
srcs = split.go + split.c + split.asm + split.cxx + split.headers,
Expand All @@ -94,33 +92,43 @@ def emit_archive(go, source = None):
cxxopts = cxxopts,
clinkopts = clinkopts,
)
runfiles = runfiles.merge(cgo.runfiles)
cgo_inputs = cgo.inputs
cgo_deps = cgo.deps
cppopts = cgo.cppopts
copts = cgo.copts
cxxopts = cgo.cxxopts
clinkopts = cgo.clinkopts

emit_compilepkg(
go,
sources = split.go + split.c + split.asm + split.cxx + split.headers,
cover = source.cover,
importpath = effective_importpath_pkgpath(source.library)[0],
importmap = source.library.importmap,
archives = direct,
out_lib = out_lib,
out_export = out_export,
gc_goopts = source.gc_goopts,
cgo = source.cgo,
cgo_inputs = cgo_inputs,
cppopts = cppopts,
copts = copts,
cxxopts = cxxopts,
clinkopts = clinkopts,
cgo_archives = source.cgo_archives,
testfilter = testfilter,
)
runfiles = runfiles.merge(cgo.runfiles)
emit_compilepkg(
go,
sources = split.go + split.c + split.asm + split.cxx + split.headers,
cover = source.cover,
importpath = effective_importpath_pkgpath(source.library)[0],
importmap = source.library.importmap,
archives = direct,
out_lib = out_lib,
out_export = out_export,
gc_goopts = source.gc_goopts,
cgo = True,
cgo_inputs = cgo.inputs,
cppopts = cgo.cppopts,
copts = cgo.copts,
cxxopts = cgo.cxxopts,
clinkopts = cgo.clinkopts,
cgo_archives = source.cgo_archives,
testfilter = testfilter,
)
else:
cgo_deps = depset()
emit_compilepkg(
go,
sources = split.go + split.c + split.asm + split.cxx + split.headers,
cover = source.cover,
importpath = effective_importpath_pkgpath(source.library)[0],
importmap = source.library.importmap,
archives = direct,
out_lib = out_lib,
out_export = out_export,
gc_goopts = source.gc_goopts,
cgo = False,
cgo_archives = source.cgo_archives,
testfilter = testfilter,
)
else:
cgo_deps = source.cgo_deps

Expand Down
15 changes: 9 additions & 6 deletions go/private/actions/stdlib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ def _build_stdlib(go):
args.add_all(link_mode_args(go.mode))
go.actions.write(root_file, "")
env = go.env
env.update({
"CC": go.cgo_tools.c_compiler_path,
"CGO_CFLAGS": " ".join(go.cgo_tools.c_compile_options),
"CGO_LDFLAGS": " ".join(extldflags_from_cc_toolchain(go)),
})
if go.mode.pure:
env.update({"CGO_ENABLED": "0"})
else:
env.update({
"CGO_ENABLED": "1",
"CC": go.cgo_tools.c_compiler_path,
"CGO_CFLAGS": " ".join(go.cgo_tools.c_compile_options),
"CGO_LDFLAGS": " ".join(extldflags_from_cc_toolchain(go)),
})
inputs = (go.sdk.srcs +
go.sdk.headers +
go.sdk.tools +
Expand All @@ -89,4 +93,3 @@ def _build_stdlib(go):
root_file = root_file,
libs = [pkg],
)

7 changes: 7 additions & 0 deletions go/private/compat/compat_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
load("@io_bazel_rules_go//go/private:skylib/lib/versions.bzl", "versions")

def _go_rules_compat_impl(ctx):
darwin_tpl_path = ctx.path(Label("@io_bazel_rules_go//go/private:compat/darwin.bzl.tpl"))
if "mac" in ctx.os.name:
default_darwin_constraint_value = "@io_bazel_rules_go//go/toolchain:is_darwin"
else:
default_darwin_constraint_value = "@io_bazel_rules_go//go/toolchain:not_darwin"
ctx.template("darwin.bzl", darwin_tpl_path, {"{default_darwin_constraint_value}": default_darwin_constraint_value})

ctx.file("BUILD.bazel")
ctx.symlink(ctx.attr.impl, "compat.bzl")

Expand Down
15 changes: 15 additions & 0 deletions go/private/compat/darwin.bzl.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

DEFAULT_DARWIN_CONSTRAINT_VALUE = "{default_darwin_constraint_value}"
Loading

0 comments on commit f2373c9

Please sign in to comment.