Live data from Hacker News

Mach v0.1: Cross-platform Zig graphics

devlog.hexops.com

41–48 of 48 posts

Re: Mach v0.1: Cross-platform Zig graphics

#41
post #40
post #2

i'm following zig's project from HN and i often see cross-compilation and c/c++ mentionned. Why is zig often mentionned in that context ? it seems that cross-compilation and C can be achieved using llvm/clang without a lot of problem ? What is zig accomplishing in that matter that makes it so special ?

Here's the problem statement in a nutshell. Consider the following trivial C program: #include #include int main(void) { unsigned char buf[10]; ssize_t ret; ret = getrandom(buf, sizeof(buf), 0); if (ret != sizeof(buf)) fprintf(stderr, "getrandom() failed\n"); else fprintf(stderr, "getrandom() success\n"); return ret != sizeof(buf); } You have an x86_64 Linux machine running glibc 2.17. Your task is to compile the abo…

It's simpler with clang.

  clang random.c
glibc 2.28 will work with binaries compiled against 2.17.

Re: Mach v0.1: Cross-platform Zig graphics

#42
post #4

Good article, 1 comment, still. > slugging through the depths of hell where only a footnote from Raymond Chen himself will save you The behavior is well documented: https://docs.microsoft.com/en-us/cpp/build/x64-calling-conve... the caller must allocate memory for the return value and pass a pointer to it as the first argument. The remaining arguments are then shifted one argument to the right. The same pointer must…

There is also an updated Raymond Chen footnote: https://devblogs.microsoft.com/oldnewthing/20220113-00/?p=10...

Re: Mach v0.1: Cross-platform Zig graphics

#43
post #40

Earlier quoted context omitted.

Here's the problem statement in a nutshell. Consider the following trivial C program: #include #include int main(void) { unsigned char buf[10]; ssize_t ret; ret = getrandom(buf, sizeof(buf), 0); if (ret != sizeof(buf)) fprintf(stderr, "getrandom() failed\n"); else fprintf(stderr, "getrandom() success\n"); return ret != sizeof(buf); } You have an x86_64 Linux machine running glibc 2.17. Your task is to compile the abo…

It's simpler with clang. clang random.c glibc 2.28 will work with binaries compiled against 2.17.

Unfortunately that won't compile against glibc 2.17 since getrandom(2) was only added to glibc in 2.25. See https://lwn.net/Articles/711013/ for background info,

Re: Mach v0.1: Cross-platform Zig graphics

#44
post #43

Earlier quoted context omitted.

It's simpler with clang. clang random.c glibc 2.28 will work with binaries compiled against 2.17.

Unfortunately that won't compile against glibc 2.17 since getrandom(2) was only added to glibc in 2.25. See https://lwn.net/Articles/711013/ for background info,

nvm then

Re: Mach v0.1: Cross-platform Zig graphics

#45
post #3

Earlier quoted context omitted.

Best place to read more is probably on the Zig project website, specifically this section[1] on how/why Zig's compiler is also a C compiler and the benefits that provides. [1] https://ziglang.org/learn/overview/#zig-is-also-a-c-compiler

So, I read that, and it is vague about whether the zig compiler itself compiles C, or whether it launches a C compiler (cc). --- I will resist making the obvious reference despite having said both "launch" and "zig" in the same sentence.

The zig compiler itself (which is written in Zig) compiles C, `zig cc` just indicates to the zig compiler to parse C compiler options (`zig cc` has the same interface as Clang).

Re: Mach v0.1: Cross-platform Zig graphics

#46
post #11

Author here, thanks for posting! Happy to answer any questions about Mach (or my experience working with Zig.)

Hi Stephen,

The initial example works for me. Truly impressive packaging you've achieved.

I tried to build for musl, but it still links with glibc; is musl supported, or am I doing it wrong?

  motiejus ~/code/mach/gpu $ zig build   -Dtarget=x86_64-linux-musl
  motiejus ~/code/mach/gpu $ ldd zig-out/bin/gpu-hello-triangle
        linux-vdso.so.1 (0x00007fff573a3000)
        libX11.so.6 => /lib/x86_64-linux-gnu/libX11.so.6 (0x00007f188c836000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f188c6f2000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f188c6d0000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f188c50b000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f188c505000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f188c9a7000)
        libxcb.so.1 => /lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f188c4da000)
        libXau.so.6 => /lib/x86_64-linux-gnu/libXau.so.6 (0x00007f188c4d3000)
        libXdmcp.so.6 => /lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f188c2cd000)
        libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f188c2b6000)
        libmd.so.0 => /lib/x86_64-linux-gnu/libmd.so.0 (0x00007f188c2a9000)
Also, I wasn't able to cross-compile to aarch64, because it does not seem to recognize `-Ddawn-from-source=true`, and I wasn't able to figure out where to put the `Options.from_source = true`:

  motiejus ~/code/mach/gpu $ zig build -Ddawn-from-source=true    -Dtarget=aarch64-linux-musl
  error: gpu-dawn binaries for aarch64-linux.3.16...5.10.81-gnu.2.19 not available.
  error: -> open an issue: https://github.com/hexops/mach/issues
  error: -> build from source (takes 5-15 minutes):
  error:        use -Ddawn-from-source=true or set `Options.from_source = true`

  error: the following build command failed with exit code 1:
  /home/motiejus/code/mach/gpu/zig-cache/o/a1cea9683aaa7b7e7a533b8f6e40e10f/build /home/motiejus/code/zig-linux-x86_64-0.10.0-dev.896+e620b692c/zig /home/motiejus/code/mach/gpu /home/motiejus/code/mach/gpu/zig-cache /home/motiejus/.cache/zig -Ddawn-from-source=true -Dtarget=aarch64-linux-musl
  motiejus ~/code/mach/gpu $

Re: Mach v0.1: Cross-platform Zig graphics

#47
post #11

Author here, thanks for posting! Happy to answer any questions about Mach (or my experience working with Zig.)

Hi Stephen, The initial example works for me. Truly impressive packaging you've achieved. I tried to build for musl, but it still links with glibc; is musl supported, or am I doing it wrong? motiejus ~/code/mach/gpu $ zig build -Dtarget=x86_64-linux-musl motiejus ~/code/mach/gpu $ ldd zig-out/bin/gpu-hello-triangle linux-vdso.so.1 (0x00007fff573a3000) libX11.so.6 => /lib/x86_64-linux-gnu/libX11.so.6 (0x00007f188c8360…

(we chatted in the Matrix room, the TL;DR was there are some bugs here I need to fix and indeed aarch64-linux does not work yet :) https://matrix.to/#/!bIJeTLtOHucXQpqIcH:matrix.org/$FgXuXXmH... )

Re: Mach v0.1: Cross-platform Zig graphics

#48

This is awesome! I was hoping someone would take WebGPU/Dawn and package it up in a form that is usable outside Chromium. Building it yourself is nontrivial. Portable graphics has always been a big issue. Back in the day OpenGL was kinda sorta portable, but in practice that never worked out very well. Drivers were buggy, platforms came with different versions and different extensions, Windows didn't come with GL driv…

wgpu-native is easy to use. It's written in Rust, but it exposes a C API, so it's simple to use it from other languages. And building it yourself is also simple because it's a single cargo build away.
Post reply on HN