From 72475ee012f201ca03d4e85c71a2bf37dea0237f Mon Sep 17 00:00:00 2001 From: Fabian Hahn Date: Sun, 29 May 2022 17:35:17 +0100 Subject: [PATCH] added support for Windows targets --- README.md | 14 ++++++++++++++ toolchain/platform/defs.bzl | 7 ++++++- toolchain/private/defs.bzl | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f83ace5..4ae2016 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,8 @@ register_toolchains( "@zig_sdk//toolchain:linux_arm64_gnu.2.28", "@zig_sdk//toolchain:darwin_amd64", "@zig_sdk//toolchain:darwin_arm64", + "@zig_sdk//toolchain:windows_amd64", + "@zig_sdk//toolchain:windows_arm64", ) ``` @@ -318,6 +320,18 @@ is currently not implemented. target macos.10 (Catalina), macos.11 (Big Sur) or macos.12 (Monterey). It currently targets the lowest version, without ability to change it. +## Windows: output file extensions + +Bazel won't use common Windows file extensions for built output binaries. +Instead, it will generate binaries with common Unix extensions that you might +have to manually rename before deploying them to an actual Windows system: + +| Binary type | Bazel extension | Windows extension | +|----------------|-----------------|-------------------| +| Static library | .a | .lib | +| Shared library | .so | .dll | +| Executable | (no extension) | .exe | + # Known Issues In Upstream This section lists issues that I've stumbled into when using `zig cc`, and is diff --git a/toolchain/platform/defs.bzl b/toolchain/platform/defs.bzl index cf42191..07e69a6 100644 --- a/toolchain/platform/defs.bzl +++ b/toolchain/platform/defs.bzl @@ -1,11 +1,16 @@ load("@bazel-zig-cc//toolchain/private:defs.bzl", "LIBCS") _CPUS = (("x86_64", "amd64"), ("aarch64", "arm64")) +_OS = { + "linux": ["linux"], + "macos": ["macos", "darwin"], + "windows": ["windows"], +} def declare_platforms(): # create @zig_sdk//{os}_{arch}_platform entries with zig and go conventions for zigcpu, gocpu in _CPUS: - for bzlos, oss in {"linux": ["linux"], "macos": ["macos", "darwin"]}.items(): + for bzlos, oss in _OS.items(): for os in oss: declare_platform(gocpu, zigcpu, bzlos, os) diff --git a/toolchain/private/defs.bzl b/toolchain/private/defs.bzl index dbb2895..81bc648 100644 --- a/toolchain/private/defs.bzl +++ b/toolchain/private/defs.bzl @@ -40,6 +40,7 @@ def target_structs(): ret = [] for zigcpu, gocpu in (("x86_64", "amd64"), ("aarch64", "arm64")): ret.append(_target_darwin(gocpu, zigcpu)) + ret.append(_target_windows(gocpu, zigcpu)) ret.append(_target_linux_musl(gocpu, zigcpu)) for glibc in _GLIBCS: ret.append(_target_linux_gnu(gocpu, zigcpu, glibc)) @@ -70,6 +71,24 @@ def _target_darwin(gocpu, zigcpu): tool_paths = {"ld": "ld64.lld"}, ) +def _target_windows(gocpu, zigcpu): + return struct( + gotarget = "windows_{}".format(gocpu), + zigtarget = "{}-windows-gnu".format(zigcpu), + includes = [ + "libunwind/include", + "libc/include/any-windows-any", + ], + linkopts = [], + copts = [], + bazel_target_cpu = "x64_windows", + constraint_values = [ + "@platforms//os:windows", + "@platforms//cpu:{}".format(zigcpu), + ], + tool_paths = {"ld": "ld64.lld"}, + ) + def _target_linux_gnu(gocpu, zigcpu, glibc_version): glibc_suffix = "gnu.{}".format(glibc_version)