Subject: Re: Cross-compilation with zig

From: Motiejus Jakštys <[email-masked]>
Date: Mon, 6 Sep 2021 08:14:46 +0300

+ ~motiejus/[email-masked] (please subscribe[0]). Also
switching from top-down to bottom-up posting.

On Sun, Sep 5, 2021 at 10:14 PM Adam Bouhenguel <[email-masked]> wrote:
>
> Thanks for adding me to the thread, Motiejus.
>
> Steeve, I'm very curious to hear how your investigation goes. Please keep me posted!
>
> I've been bitten a few times now by regressions in zig cc. Both 0.7.1 and 0.9-dev have problems with C++ exceptions and break runtime behavior of third party libraries I'm using.

Zig 0.7.1 to zig 0.9-dev moved from clang-11 to clang-12, so, from the
surface, I would attribute behavior changes to this. Did you narrow
this down to something specific?

> Seems like the ability to build a set of specific projects in bazel (with a hermetic zig cc based toolchain) might be helpful for keeping everyone on the same page.

Have you noticed regressions with `zig cc` when upgrading zig? Since
it is covered by "bug stability program[1]", I expect zig upstream to
be quite responsive with regressions there. If C++ behavior changes,
it's probably a fault of a new clang, which should be reported to
Apple (likely with help of zig developers, since they have done this
many times).

> To this end, what do you folks think about having a small suite of acceptance tests for the toolchain?

We already have //test:hello[2]. Feel free to add yours.

> As a bonus we might be able to use qemu to ensure that the cross-compiled artifacts actually work.

I am looking for multi-arch build support in sr.ht, which is "not yet
available"[3], which would be ideal.

Currently I am cross-compiling to a few architectures and checking
whether the resulting "file" command returns an expected string. I
agree qemu-system-<arch> is a good next step. What are your
architectures of interest?

Motiejus

[0]: https://lists.sr.ht/~motiejus/bazel-zig-cc
[1]: https://ziglang.org/download/0.8.0/release-notes.html#zig-cc
[2]: https://git.sr.ht/~motiejus/bazel-zig-cc/tree/main/item/test/BUILD
[3]: https://man.sr.ht/builds.sr.ht/compatibility.md
[4]: https://git.sr.ht/~motiejus/bazel-zig-cc/tree/main/item/.build.yml

> Cheers,
> Adam
>
> On Thu, Sep 2, 2021, 06:31 Motiejus Jakštys <[email-masked]> wrote:
>>
>> Let me know how it goes. I set up a mailing list on srht for patches, if any.
>>
>> On Thu, Sep 2, 2021, 15:42 Steeve Morin <[email-masked]> wrote:
>>>
>>> Hey Motiejus,
>>>
>>> Thank for working on that!
>>> I'll see if I can hack on it one weekend. What I'd like though is have the `zig cc` toolchain have the same level of features as the "host" toolchain.
>>> And it feels to me that doing the toolchain.bzl file by hand isn't the right way to do it. I'm still looking into how bazel generates it and see if we can let it do it.
>>>
>>> Steeve
>>>
>>>
>>> On Wed, 1 Sept 2021 at 21:03, Motiejus Jakštys <[email-masked]> wrote:
>>>>
>>>> Zhongpeng, thank you for bringing the group together. +Adam, who created the initial toolchain (mine is a fork of Adam's initial hard work), and he has obvious interest in the toolchain too.
>>>>
>>>> Steeve, my zig-cc toolchain is hosted here. I am obviously interested in solving all the outlined issues, and rules_go#2894 is one of them. I am currently very limited in capacity myself, but happy to merge improvements, either the mentioned issues or anything else you find reasonably necessary for usage of the toolchain.
>>>>
>>>> Motiejus
>>>>
>>>> On Wed, Sep 1, 2021 at 9:17 PM Zhongpeng Lin <[email-masked]> wrote:
>>>>>
>>>>> Hello Motiejus,
>>>>>
>>>>> In today's Bazel-Go maintainers meeting, Steeve (cc'ed) mentioned that he wanted to work on zig-based toolchain for rules_go. Steeve is also an expert in cgo support in rules_go. Checking the last email you sent, it seems you've made good progress on that. Do you still need help on rules_go#2894? Steeve is probably the most knowledgeable person for you to talk to if you need.
>>>>>

---

From: Motiejus Jakštys <[email-masked]>
Date: Tue, 14 Sep 2021 15:18:04 +0300

On Tue, Sep 14, 2021 at 2:21 AM Steeve Morin <[email-masked]> wrote:
>
> Alright you guys, I found a way to use bazel's auto toolchain configuration with zig!
> It's rather archaic for now, but it works, I got protoc compiled and running for both macos and linux.
>
> Still needs some love, I'll post updates in the next few days.

Looking forward to anything that will simplify it's setup/boilerplate.

> The main idea is to give to @bazel_tools//tools/cpp:unix_cc_configure.bzl configure_unix_toolchain() what it want, and that's it!

Interesting!

I have slightly updated the toolchain today:
- added support for darwin-arm64 and linux-arm64 host (though don't
have an arm64 machine to test this with).
- tried to upgrade zig itself, but cross-compilation to darwin-amd64
fails with it; there seems to be a regression somewhere. I will look
into it sometime soon.

... so please rebase before sending patches.

Regards,
Motiejus

---

From: Motiejus Jakštys <[email-masked]>
Date: Fri, 29 Oct 2021 07:45:31 +0300

> Looking forward to anything that will simplify it's setup/boilerplate.

Hi all,

the way plans are panning out now, at some point in 2022H1 bazel-zig-cc
will be scrutinized by much more competent colleagues than myself,
before being moved to github.com/uber (or equivalent). After that major
changes will be much harder to make.

Steeve, if you are still considering on major changes in bazel-zig-cc,
this year is better than next year.

Motiejus