Add libc constraint and libc aware toolchains
- Also get rid of @bazel_skylib dependency
This commit is contained in:
@@ -4,6 +4,8 @@ package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
|
||||
|
||||
declare_files(
|
||||
zig_include_root = {zig_include_root},
|
||||
)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
load("@bazel_skylib//lib:shell.bzl", "shell")
|
||||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
||||
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_user_netrc", "use_netrc")
|
||||
load("@bazel-zig-cc//toolchain/private:defs.bzl", "DEFAULT_INCLUDE_DIRECTORIES", "ZIG_TOOL_PATH", "target_structs")
|
||||
@@ -42,15 +41,15 @@ _HOST_PLATFORM_SHA256 = {
|
||||
"macos-x86_64": "78220a4460a7c0f563d7365313fcd3ea028ed38166ebac55ba22f17ab6404851",
|
||||
}
|
||||
|
||||
def register_toolchains(
|
||||
register = [],
|
||||
def toolchains(
|
||||
version = _VERSION,
|
||||
url_formats = [URL_FORMAT_JAKSTYS],
|
||||
host_platform_sha256 = _HOST_PLATFORM_SHA256):
|
||||
"""
|
||||
Download zig toolchain and register some.
|
||||
@param register registers the given toolchains to the system using
|
||||
native.register_toolchains(). See README for possible choices.
|
||||
Download zig toolchain and declare bazel toolchains.
|
||||
The platforms are not registered automatically, that should be done by
|
||||
the user with register_toolchains() in the WORKSPACE file. See README
|
||||
for possible choices.
|
||||
"""
|
||||
zig_repository(
|
||||
name = "zig_sdk",
|
||||
@@ -65,9 +64,6 @@ def register_toolchains(
|
||||
},
|
||||
)
|
||||
|
||||
toolchains = ["@zig_sdk//toolchain:%s" % t for t in register]
|
||||
native.register_toolchains(*toolchains)
|
||||
|
||||
ZIG_TOOL_WRAPPER = """#!/bin/bash
|
||||
set -e
|
||||
|
||||
@@ -95,6 +91,9 @@ _ZIG_TOOLS = [
|
||||
"wasm-ld", # WebAssembly
|
||||
]
|
||||
|
||||
def _quote(s):
|
||||
return "'" + s.replace("'", "'\\''") + "'"
|
||||
|
||||
def _zig_repository_impl(repository_ctx):
|
||||
arch = repository_ctx.os.arch
|
||||
if arch == "amd64":
|
||||
@@ -139,34 +138,28 @@ def _zig_repository_impl(repository_ctx):
|
||||
content = _fcntl_h,
|
||||
)
|
||||
|
||||
repository_ctx.symlink(
|
||||
Label("//toolchain/platform:BUILD"),
|
||||
"platform/BUILD",
|
||||
)
|
||||
for dest, src in {
|
||||
"platform/BUILD": "//toolchain/platform:BUILD",
|
||||
"toolchain/BUILD": "//toolchain/toolchain:BUILD",
|
||||
"libc/BUILD": "//toolchain/libc:BUILD",
|
||||
"libc_aware/platform/BUILD": "//toolchain/libc_aware/platform:BUILD",
|
||||
"libc_aware/toolchain/BUILD": "//toolchain/libc_aware/toolchain:BUILD",
|
||||
}.items():
|
||||
repository_ctx.symlink(Label(src), dest)
|
||||
|
||||
repository_ctx.template(
|
||||
"BUILD",
|
||||
Label("//toolchain:BUILD.sdk.bazel"),
|
||||
executable = False,
|
||||
substitutions = {
|
||||
"{zig_include_root}": shell.quote(zig_include_root),
|
||||
},
|
||||
)
|
||||
|
||||
repository_ctx.symlink(
|
||||
Label("//toolchain/toolchain:BUILD"),
|
||||
"toolchain/BUILD",
|
||||
)
|
||||
|
||||
repository_ctx.template(
|
||||
"private/BUILD",
|
||||
Label("//toolchain/private:BUILD.sdk.bazel"),
|
||||
executable = False,
|
||||
substitutions = {
|
||||
"{absolute_path}": shell.quote(str(repository_ctx.path(""))),
|
||||
"{zig_include_root}": shell.quote(zig_include_root),
|
||||
},
|
||||
)
|
||||
for dest, src in {
|
||||
"BUILD": "//toolchain:BUILD.sdk.bazel",
|
||||
"private/BUILD": "//toolchain/private:BUILD.sdk.bazel",
|
||||
}.items():
|
||||
repository_ctx.template(
|
||||
dest,
|
||||
Label(src),
|
||||
executable = False,
|
||||
substitutions = {
|
||||
"{absolute_path}": _quote(str(repository_ctx.path(""))),
|
||||
"{zig_include_root}": _quote(zig_include_root),
|
||||
},
|
||||
)
|
||||
|
||||
zig_repository = repository_rule(
|
||||
attrs = {
|
||||
|
||||
17
toolchain/libc/BUILD
Normal file
17
toolchain/libc/BUILD
Normal file
@@ -0,0 +1,17 @@
|
||||
load("@bazel-zig-cc//toolchain/libc:defs.bzl", "declare_libcs")
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
constraint_setting(
|
||||
name = "variant",
|
||||
default_constraint_value = "unconstrained",
|
||||
)
|
||||
|
||||
constraint_value(
|
||||
name = "unconstrained",
|
||||
constraint_setting = "variant",
|
||||
)
|
||||
|
||||
declare_libcs()
|
||||
8
toolchain/libc/defs.bzl
Normal file
8
toolchain/libc/defs.bzl
Normal file
@@ -0,0 +1,8 @@
|
||||
load("@bazel-zig-cc//toolchain/private:defs.bzl", "LIBCS")
|
||||
|
||||
def declare_libcs():
|
||||
for libc in LIBCS:
|
||||
native.constraint_value(
|
||||
name = libc,
|
||||
constraint_setting = "variant",
|
||||
)
|
||||
7
toolchain/libc_aware/platform/BUILD
Normal file
7
toolchain/libc_aware/platform/BUILD
Normal file
@@ -0,0 +1,7 @@
|
||||
load("@bazel-zig-cc//toolchain/platform:defs.bzl", "declare_libc_aware_platforms")
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
declare_libc_aware_platforms()
|
||||
7
toolchain/libc_aware/toolchain/BUILD
Normal file
7
toolchain/libc_aware/toolchain/BUILD
Normal file
@@ -0,0 +1,7 @@
|
||||
load("@bazel-zig-cc//toolchain/toolchain:defs.bzl", "declare_libc_aware_toolchains")
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
declare_libc_aware_toolchains()
|
||||
@@ -1,3 +1,7 @@
|
||||
load("@bazel-zig-cc//toolchain/platform:defs.bzl", "declare_platforms")
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
declare_platforms()
|
||||
|
||||
@@ -1,17 +1,40 @@
|
||||
load("@bazel-zig-cc//toolchain/private:defs.bzl", "LIBCS")
|
||||
|
||||
_CPUS = (("x86_64", "amd64"), ("aarch64", "arm64"))
|
||||
|
||||
def declare_platforms():
|
||||
# create @zig_sdk//{os}_{arch}_platform entries with zig and go conventions
|
||||
for zigcpu, gocpu in (("x86_64", "amd64"), ("aarch64", "arm64")):
|
||||
for zigcpu, gocpu in _CPUS:
|
||||
for bzlos, oss in {"linux": ["linux"], "macos": ["macos", "darwin"]}.items():
|
||||
for os in oss:
|
||||
constraint_values = [
|
||||
"@platforms//os:{}".format(bzlos),
|
||||
"@platforms//cpu:{}".format(zigcpu),
|
||||
]
|
||||
native.platform(
|
||||
name = "{os}_{zigcpu}".format(os = os, zigcpu = zigcpu),
|
||||
constraint_values = constraint_values,
|
||||
)
|
||||
native.platform(
|
||||
name = "{os}_{gocpu}".format(os = os, gocpu = gocpu),
|
||||
constraint_values = constraint_values,
|
||||
)
|
||||
declare_platform(gocpu, zigcpu, bzlos, os)
|
||||
|
||||
def declare_libc_aware_platforms():
|
||||
# create @zig_sdk//{os}_{arch}_platform entries with zig and go conventions
|
||||
# with libc specified
|
||||
for zigcpu, gocpu in _CPUS:
|
||||
for libc in LIBCS:
|
||||
declare_platform(
|
||||
gocpu,
|
||||
zigcpu,
|
||||
"linux",
|
||||
"linux",
|
||||
suffix = "_{}".format(libc),
|
||||
extra_constraints = ["@zig_sdk//libc:{}".format(libc)],
|
||||
)
|
||||
|
||||
def declare_platform(gocpu, zigcpu, bzlos, os, suffix = "", extra_constraints = []):
|
||||
constraint_values = [
|
||||
"@platforms//os:{}".format(bzlos),
|
||||
"@platforms//cpu:{}".format(zigcpu),
|
||||
] + extra_constraints
|
||||
|
||||
native.platform(
|
||||
name = "{os}_{zigcpu}{suffix}".format(os = os, zigcpu = zigcpu, suffix = suffix),
|
||||
constraint_values = constraint_values,
|
||||
)
|
||||
|
||||
native.platform(
|
||||
name = "{os}_{gocpu}{suffix}".format(os = os, gocpu = gocpu, suffix = suffix),
|
||||
constraint_values = constraint_values,
|
||||
)
|
||||
|
||||
@@ -28,6 +28,8 @@ _GLIBCS = [
|
||||
"2.34",
|
||||
]
|
||||
|
||||
LIBCS = ["musl", "gnu"] + ["gnu.{}".format(glibc) for glibc in _GLIBCS]
|
||||
|
||||
def target_structs():
|
||||
ret = []
|
||||
for zigcpu, gocpu in (("x86_64", "amd64"), ("aarch64", "arm64")):
|
||||
@@ -100,6 +102,7 @@ def _target_linux_gnu(gocpu, zigcpu, glibc_version = ""):
|
||||
"@platforms//os:linux",
|
||||
"@platforms//cpu:{}".format(zigcpu),
|
||||
],
|
||||
libc_constraint = "@zig_sdk//libc:{}".format(glibc_suffix),
|
||||
tool_paths = {"ld": "ld.lld"},
|
||||
)
|
||||
|
||||
@@ -120,5 +123,6 @@ def _target_linux_musl(gocpu, zigcpu):
|
||||
"@platforms//os:linux",
|
||||
"@platforms//cpu:{}".format(zigcpu),
|
||||
],
|
||||
libc_constraint = "@zig_sdk//libc:musl",
|
||||
tool_paths = {"ld": "ld.lld"},
|
||||
)
|
||||
|
||||
@@ -1,25 +1,46 @@
|
||||
load("@bazel-zig-cc//toolchain/private:defs.bzl", "DEFAULT_INCLUDE_DIRECTORIES", "ZIG_TOOL_PATH", "target_structs")
|
||||
load("@bazel-zig-cc//toolchain/private:defs.bzl", "target_structs")
|
||||
|
||||
def declare_toolchains():
|
||||
for target_config in target_structs():
|
||||
gotarget = target_config.gotarget
|
||||
zigtarget = target_config.zigtarget
|
||||
|
||||
# register two kinds of toolchain targets: Go and Zig conventions.
|
||||
# Go convention: amd64/arm64, linux/darwin
|
||||
native.toolchain(
|
||||
name = gotarget,
|
||||
exec_compatible_with = None,
|
||||
target_compatible_with = target_config.constraint_values,
|
||||
toolchain = "@zig_sdk//private:%s_cc" % zigtarget,
|
||||
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
|
||||
)
|
||||
# if the toolchain is libc aware, create two variants for it: one that
|
||||
# is only selected if libc is not expicitly set and another one that is
|
||||
# only selected if the specific libc variant is selected.
|
||||
extra_constraints = []
|
||||
if hasattr(target_config, "libc_constraint"):
|
||||
extra_constraints = ["@zig_sdk//libc:unconstrained"]
|
||||
|
||||
# Zig convention: x86_64/aarch64, linux/macos
|
||||
native.toolchain(
|
||||
name = zigtarget,
|
||||
exec_compatible_with = None,
|
||||
target_compatible_with = target_config.constraint_values,
|
||||
toolchain = "@zig_sdk//private:%s_cc" % zigtarget,
|
||||
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
|
||||
)
|
||||
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + extra_constraints)
|
||||
|
||||
def declare_libc_aware_toolchains():
|
||||
for target_config in target_structs():
|
||||
gotarget = target_config.gotarget
|
||||
zigtarget = target_config.zigtarget
|
||||
|
||||
# if the toolchain is libc aware, create two variants for it: one that
|
||||
# is only selected if libc is not expicitly set and another one that is
|
||||
# only selected if the specific libc variant is selected.
|
||||
if hasattr(target_config, "libc_constraint"):
|
||||
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + [target_config.libc_constraint])
|
||||
|
||||
def _declare_toolchain(gotarget, zigtarget, target_compatible_with):
|
||||
# register two kinds of toolchain targets: Go and Zig conventions.
|
||||
# Go convention: amd64/arm64, linux/darwin
|
||||
native.toolchain(
|
||||
name = gotarget,
|
||||
exec_compatible_with = None,
|
||||
target_compatible_with = target_compatible_with,
|
||||
toolchain = "@zig_sdk//private:%s_cc" % zigtarget,
|
||||
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
|
||||
)
|
||||
|
||||
# Zig convention: x86_64/aarch64, linux/macos
|
||||
native.toolchain(
|
||||
name = zigtarget,
|
||||
exec_compatible_with = None,
|
||||
target_compatible_with = target_compatible_with,
|
||||
toolchain = "@zig_sdk//private:%s_cc" % zigtarget,
|
||||
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user