8                             
                                                                                                                                                                                                                                                                              888                           
                   S SSXSSSXX%S@X%X@X%X@          8 X X@S@X@X@X@X@@@@@@@@@@@@@8             XXX@X@X@X@@@@@@@@@@@@8@8@8@8            888888888888888888888888888888                                                     88888888888888888888                   888888888888    88@8                          
                    %SX               X S         @S%@                       8XS            8@X@                       @@@            888                       888                                                    8888               888                 88       88X    888888                        
                       X%               SSS       XSX  @                       8S@          8X  8X                      @@@@            8@8                       888                                                  88  88               888               88       888    88  88tX                      
                         S                % S     S%X   X                        8%X        8X    8                       XX@            @@8@                      8888                                                88    88              888              88       888    88    8888                    
                           S                SSX   %X@     S                       8S@@      8      8S                       8@@            @X@                       888                8                              88      8               888            88       888    88      888                   
                            X S S S S S S XS 8X    @S      % SSX8 SSXSSSXSSSXX%S@X88X@@     8S       8%XX@X@X@X@X@@@@@@@@@@@XXS@             X@X@X@X@@@@@@@@@@@@8@8@88@888            88888                            88       88              888           8        8@8    88        888                 
                             X                @    S                %S                      @        8               8       8X%             8@X               @X                    8888@8@8                          88         88             88           88       888    8          8888               
                            .S  .S            S    XS                S                      XS       @               8       8SS             8X8               XX                     @8X@@8X88                        88           8            88           88       888    88           888              
                            :S    :S          S    XX                S                      S        X               @       8 X             8X                8@                       @@@X8X8X@                      88            88          88           88       888    88             888            
                            ;S      :        .S   .S                 S                       X       %               X       @S              8S8               8X                         @@SXX8S@@                    88              88       888           88       888888888              8888          
                            tS       ;S      ;S   :X        .        S                       S                       S       X%@             8SS               8S                           X@XS8S@@@                  8@8               8888888888           88       88      88              88@          
                            %X         tS    tS   ;X%       :       .S S S S                 S                       %       S%X             @SS               8S                             8XS8S8@@X                  @8@               888                88       8       88 8             88          
                            SX           %   %X   tS        ;       :X X X X S               S                               %X@             X @               8                                8X8S8%XX@                 @@@@              8888              88       8       8   88           88          
                            XX             S SX   %8.       t       ;        :X S            S                                XX             SSS               @S                                  8SXSXSXS            XX   @X@               @88             88       8       8     88         88          
                            @X              X8;   SS%       %       t          ;S.          :S                                S               XS               X                                     8S8X8SXS          8@XX   8@@               @@@           8@       8       8       8        88          
                            8@               X8   X8t       S       %            tS;        ;S       :                        X%              SSX              SS                                      8@@X88 X        8X8 @   8X@S              @@@@         @8       8       8        88      88          
                            8X               @S   @XS       X       S             %@;S      tS       ;               .        XS              XS  S             S                                       8S8SSSSX8      8X  8X@   8XS               8@@        @X       @       8          88    88          
                            8X               8@   888       @       XXXXXXSXSXSXS@St8:%     %X       t               :       .S               SS                S                                     @ XSS8 SX        8@    8S@   8S@               8@@      XX       @       @            88  88          
          8@8X8X8@8X8X8@8X8@8@               8X   8XS       8       @8                      SX       %               ;       :X               XS     S          S                                   X XSX88S           8X     8S@8  8X@               88@8    8@       X       @              @@88          
           8XSXS@S@SXXXX@S8888               8X   888       8       8X                      XX       S               t       ;X%             .S        S        S                                 % @X 88XS            @S       8 X XX@8 X X@S@X@X@X@@8@8@%   8X       8@@888@XXX              @@@          
            8888             8               8@   888       8       8X                      @X       X               %       tS              :X         .       S                                SS@ %88X              XX         @                      8S   8S       8@@X@XS8 8%              @@          
              8X8            8               88   888       8       8X                      @X       @               S       %8.             tX%          :    .S                              X% S8888                %           X                     8    8S       8SS       8X@            8@          
                8@8          8               8@   888       8       8@                      8@       @               X       SS%             %X            ;S  ;S                            X S 888                    X            S                   @S   8        8S          8S@          8@          
                 8@8X        8               8X   8@8       8       8X8X8@8X8               8X       8               @       X8t   XXSXSX%SS@S88             tXS                           S% 888S                      S              %                 X    @S       8SS          8X@         8X          
                   8X8X      8               8X   8@8       8@8X8X8X8%XXX@XX888             8X8      8               8       @XS     X@SX8%8;8 :SSX%SSX%S%XtXS:8 %                      :X S 888                        S                S              SSX   X S      @ S            8S        8X          
                     8X8     8               8X     8@8     8                 8@8            888     8               8     88S        @8%8                      S8:%                 ;S:@ 8 88                           X                 S           SSX     S%XS    XSX              8 X     8           
                       8@8   8               88      8@8X   8                  8@8X            8@8   8               8   88S8           8S8@                      SXS                tXS 8 8                               S                          XX         %X    SSX               @ S    @S          
                        8@8X 8               8@        8X8  8                    8X8             8@8 8               8  888               8X8                       XXX                %8                                   .S                      X%              S   S                  XSX X S          
                          8X8X               8X          8@8X                     8X8X            8@8@               8@8@                   8@8                      @8SX                                                    :X S               S S%%                X @%8                   S @8           
                            8S8X8@8X8X8@8X8X8S@           8X8X8X8@8X8X8@8X8X8@8X8X8@@@8S            8S8@8X8X8@8X8X8@8SX                      888X8X8@8X8@8X8@8X8@8@88S@8XS                                                     ;S S.S S S % S S X S                    XS                     %  S          
                                                                                                                                                                                                                                                                         X                       S          
                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                            

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:

*************************


  • Golang 1.18+ [>]
  • Zig 0.10.0-dev+ [>]
  • An x86_64 architecture.











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@88: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%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               
                                                            
                                                            

  • bridge.go
  • package main
    
    import "fmt"
    
    // #cgo CFLAGS: -I.
    // #cgo LDFLAGS: -L. -lzgo
    // #include ❬zgo.h❭
    import "C"
    
    func main() {
       fmt.Printf("Invoking zig library!\n")
       fmt.Println("Done ", C.x(10))
    }
    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.


  • zgo.zig
  • const std = @import("std");
    
    pub export fn x(y: c_int) c_int {
        return y+2;
    }
    A simple example function, we export the symbol in the library. Note we use the C ABI compatible type c_int.


  • build.zig
  • const std = @import("std");
    const Builder = std.build.Builder;
    const builtin = std.builtin;
    
    pub fn build(b: *Builder) void {
        const mode = b.standardReleaseOptions();
        const lib = b.addStaticLibrary("zgo", "zgo.zig");
        lib.bundle_compiler_rt = true;
        lib.use_stage1 = false;
        lib.emit_h = true;
        lib.emit_bin = .{ .emit_to = "libzgo.a"};
        lib.setBuildMode(mode);
        lib.install();
    
        const go = build_go(b);
        const make_step = b.step("go", "Make go executable");
        make_step.dependOn(&go.step);
    }
    
    fn build_go(b: *std.build.Builder) *std.build.RunStep {
    
        const go = b.addSystemCommand(
            &[_][]const u8{
                "go",
                "build",
                "-ldflags", "-linkmode external -extldflags -static",
                "bridge.go",
            },
        );
        return go;
    }
    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.


  • (alternatively) Makefile
  • static:
           zig build-lib -fno-stage1 -femit-h zgo.zig
           go build -ldflags "-linkmode external -extldflags -static" bridge.go
    
    dynamic:
           zig build-lib -dynamic -fno-stage1 -femit-h zgo.zig
           go build -ldflags "-linkmode external -extldflags -dynamic" bridge.go


    Calling Zig!
    linux@solar ~/P/zgo> ./bridge
       Invoking zig library!
       Done  12


    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