47

Porting Zig to NetBSD - a fun, speedy port

 5 years ago
source link: https://www.tuicool.com/articles/hit/yayq2ee
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

I recently ported Ziglang to NetBSD . I've done other languages in the past, and this was a great experience.

These are some of the reasons why:

  • Zig doesn't need itself to bootstrap. This is seen as a mark of mature languages, and indeed Zig is in an earlier phase of development than others, but it makes porting so much easier.
    Contrast with Rust, that continually needs anyone porting to build their own bootstrap kits repeatedly (and updating them fairly often).
  • Like Go, Zig attempts to provide an alternative to libc. Go has made the same choice, and has logic to do the syscalls itself.
    They have eventually discovered that this is a bad idea for some OSes that don't consider syscalls the compatibility boundary but system libraries (Darwin, Solaris) and had to offer the option anyway.
    Since this is more of an after-thought, it's just as hard as teaching it about syscalls.
    Calling libc is far more natural in Zig and as easy as doing it in C , so an initial porting step through libc is far easier.
  • While Zig has its own build system, the building of Zig itself begins with CMake, a very standard tool. It was so standard that pkgsrc's CMake logic automatically did everything for me!
  • And of course, someone has done a port to FreeBSD, so paved most of the way to a NetBSD port.
    NetBSD isn't perfectly compatible with FreeBSD, but it does have kevent, kqueue and others like it. Thanks, myfreeweb.
    I've returned the favour by fixing one issue discovered during the port that also affects the FreeBSD port.
    (For this reason, I recommend waiting until the NetBSD logic is merged before embarking on another BSD port: most of your lines are about to have merge conflicts)

Running the testsuite for the first time and seeing my first failure, I expected debugging to be hard: there is little output from the tests. It gave the following message:

cast causes pointer to be null
Build failed. The following command failed:
/home/fly/.local/share/zig/stage1/artifact/eAOLj9znD5gnpXrclDVMQKOVbq0G47HUAWFGtHsPdhvOZUIC
qGZRL9o8QK-SugoB/build /home/fly/zig/build/bin/zig /home/fly/zig /home/fly/zig/zig-cache test
It turned out that running the same command in GDB produced a legible backtrace 6B3qqev.png!web

GDB just worked, so finding the cause of the problem was straightforward.

Overall, porting Zig was a fun and speedy experience. It's a shame I can't keep porting it, because it's already done.

I've added it to pkgsrc , so you can easily go out and try it, including on NetBSD.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK