News for hackers. Stuff that matters.
Re:Zig Build System by kristoff_it on Friday April 14, 2023 @06:44AM (#35567884)
You can invoke `zig cc` with the same flags that you would pass to `clang`. Zig cc takes your arguments, inspects them and applies some transformations and then Zig invokes its internal copy of clang with the resulting flags. One example of transformation is enabling ubsan when building in debug mode, another is `-target` which makes Zig add some sysroot flags to enable cross-compilation.

In this case all the file operations are handled by clang, Zig basically just sets up all the advanced flags for you when it makes sense to do so. One last example of CLI rewriting is related to the caching system: Zig cc knows how to cache a build by passing to clang the same kind of flags that cmake (or other build systems) would.

There's another C-related feature of Zig that works differently: `@cImport()`, which is a builtin that allows you to import C header files directly into a Zig script, in order to use from Zig all the types exposed by the header file. This one translates the C syntax in equivalent Zig syntax. I believe it uses clang's code to parse the C code into an AST, but then it's all Zig logic from there.

Lastly, we have a C frontend project going (arocc, linked by ptato) on that we plan to eventually upstream into Zig. This would be a replacement for clang and would work by parsing the C code and translating it into Zig IR, similarly to how the D programming language does it. The only limitation of this approach is that it would only support C, not C++, so we would either have to still keep clang around for C++, or ditch clang but then lose C++ compilation support. That said, even in the case where we keep clang around for C++ support, it would be worth having a custom C frontend for the Zig compiler in order to have more fine-grained control over the compilation process than what we can get from clang, plus the fact that it would make debug builds faster, since we could avoid invoking LLVM completely in that case (ie debug builds) even if you depend on C code.

Re:Zig Build System by whitehexagon on Friday April 14, 2023 @06:58AM (#35567985)
How does this work on the assembly support side? I can see inline assembly, something called global assembly, but is Zig build also able to build standalone assembly main.asm type files?
Re:Zig Build System by kristoff_it on Friday April 14, 2023 @08:57AM (#35569051)
From the `zig build-exe` help menu:

    Supported file types:
                      .zig    Zig source code
                        .o    ELF object file
                        .o    Mach-O (macOS) object file
                        .o    WebAssembly object file
                      .obj    COFF (Windows) object file
                      .lib    COFF (Windows) static library
                        .a    ELF static library
                        .a    Mach-O (macOS) static library
                        .a    WebAssembly static library
                       .so    ELF shared object (dynamic link)
                      .dll    Windows Dynamic Link Library
                    .dylib    Mach-O (macOS) dynamic library
                      .tbd    (macOS) text-based dylib definition
                        .s    Target-specific assembly source code
                        .S    Assembly with C preprocessor (requires LLVM extensions)
                        .c    C source code (requires LLVM extensions)
    .cxx .cc .C .cpp .stub    C++ source code (requires LLVM extensions)
                        .m    Objective-C source code (requires LLVM extensions)
                       .mm    Objective-C++ source code (requires LLVM extensions)
                       .bc    LLVM IR Module (requires LLVM extensions)
                       .cu    Cuda source code (requires LLVM extensions)
Re:Zig Build System by whitehexagon on Friday April 14, 2023 @10:01AM (#35569668)
Ah! thanks, I'd been using 'zig --help build-obj' vs 'zig build-obj -h'.

And just to say the WebAssembly support is amazing, well done and thanks!