ZGo (part 1): Calling Zig from Go===============================For those unfamiliar with Zig, it aims to be a modern C/C++ alternative with a focus on simplicity, and performance. There's no macros or hidden flow control, but there is comptime metaprogramming. This is a balanced approach that helps reduce hidden behaviors while keeping comptime features in the hands of the programmer. Interop with C has always been a key feature of Zig. And with progress on the Zig stage2 compiler doors are opened to more easy integrations. stage2 is the self-hosted compiler, written in Zig and compiled by stage1. Whereas stage1 of the Zig compiler is written in C++ and compiled by Clang. (For more information on how the internals of the compiler works, I recommend reading Mitchell Hashimoto's series of posts). It turns out to be quite simple to interface with Zig from Go. By utilizing Cgo and the new features of the zig compiler we can easily make calls into Zig. Environment Assumptions:*************************
Files:****** |
8@8888@88888% X88X8@XXXX@XX%8888888 88 @888@88X8888@8X@8@X%@%. t@88@88888 8X888@8S .: 88X::S ;8@88@S@ 8: 8@8 :8@@ . .@88%8t8 .8X%:@S8 888@8 S.;@ .88@8 ;; . ;88888 @888X;:: . t 8888S. . @S8@@X8: 88X8@8%@@8X@88t;t;88Xt8@8X8SX888 X@@88@88888@;X;@ X8S@8@8SX8@888@ 8@88888X8888 :%S SS;@@X@@8888888 @@X88888@8X8@;. @%;8X@88888888S8 8X88S8S88S@8@8 @ X8X@SX88888888SX 8888888X8888X888@SX8@@888888@8888 SX@8S8S88@S8S8888XSX888@8@@88X8S@ @X@8S88888S888X888@8X 8:888X88888 8@S88tt8XX ;;X8@@@8X:8 X8S888X8X8 ; SXX@@8@: @8:XXX 88888.tX8 @8X888SS8X@ S8 XX88;8 8t8SSX8. 8@%88X88888888888X8 @X888@. 8@X8@88@8 X88XS 8@8888@@ 8%888S8 @8@8%X 8XX@X@@S 8 888S888 8S@88t.8SSS%:.8%% ;SS@8 .88888888 8X88S888:.@ .@ 8:t8;Xt.8X%8S8S8tt 8X8SXS;XX 8:. S@;:. @S8S888@ 8X8888X: %8X@S%X@8SS%X%St8@888S8: 888888X@@@X;8%8X8@St8t8;8@X888888 X88888@8SXSSt8@88XS%8@@XSS8S8888 8X88S@88%8SX8S88SX88X%8@8S8%8S8 8@X888@8S888S88888X@@888888888 88X88888888X888S88@888888S8; 8X88888X888S8888@88@88 . X 88; 88@8888888X8S88888 .@;8 X8X8 8@X%888X ;88 |
Key is the Cgo decorations for CFLAGS including the current directory. And LDFLAGS to search for libraries in the current directory. We also include our autogenerated header file zgo.h which is emitted by the zig stage2 compiler.
A simple example function, we export the symbol in the library. Note we use the C ABI compatible type c_int. The builder breaks the process down into build step objects that can depend on eachother and contain build semantics. Generally compiler flags are exposed as fields or methods. Comparing this to the Makefile below will give you an idea of what this does. We override the default output location with .emit_bin for simplicity.Running zig build && zig build go will compile the executable.
Calling Zig!
Thoughts:*************Zig makes a good companion to Go in a number of regards. The integrated build system has been one of my favorite parts of the Zig experience. I rarely opt for writing Makefiles when I can write a build.zig.
The portability of the Zig compiler and cross-target capabilities make for an ideal part of any build pipeline. Zig is a useful tool to interop with existing libraries, squeezing a bit more performance out of a hot path, or maintaining builds of exisiting projects. (Part 2) Calling Go from Zig |