Zig-pkg (en-US)

Zig-pkg (en-US)

Zig 🦎 - Package Manager v0.15.x πŸ‡ΊπŸ‡Έ

Anyone who has been following the development progress of zig already knows that the package manager introduced in v0.11.x has since matured and stabilized, along with many other build system refinements.

e.g.:

  • std.http.Server - HTTP/1.0 & HTTP/1.1;
  • std.http.Client - HTTP/1.0 & HTTP/1.1;
  • std.crypto.tls - 1.2 & 1.3;
  • Build Parallel (-j N);

For anyone not aware of that progress, please see the changelog below and this post.

How to use zig-pkg

  • Download Zig v0.15.x or higher
  • Make on your project build.zig & build.zig.zon file
  • Run zig fetch --save <url> to add a dependency (hash is computed automatically)

e.g:

  • build.zig
    const mylib_dep = b.dependency("mylib", .{ // <== as declared in build.zig.zon
        .target = target, // the same as passing `-Dtarget=<...>` to the library's build.zig script
        .optimize = optimize, // ditto for `-Doptimize=<...>`
    });
    const mylib = mylib_dep.artifact("mylib"); // <== has the location of the dependency files (mylib)

   // your executable config
    exe.linkLibrary(mylib); // <== link mylib
    exe.root_module.addImport("mylib", mylib.module("mylib")); // <== for zig project
    exe.installLibraryHeaders(mylib); // <== get copy mylib headers to zig-out/include (for C or C++ project)

// Glossary:
// mylib_dep: std.Build.Dependency
// (mylib_dep.artifact(...) == mylib): std.Build.Step.Compile (eql to addSharedLib/addStaticLib/addExecutable/addObject)
// mylib.module(...): std.Build.Module
  • build.zig.zon
.{
    .name = .example,
    .version = "0.1.0",
    .dependencies = .{
        .mylib = .{
            .url = "git+https://github.com/username/mylib.git#[commit-hash]",
            .hash = "[multihash - sha256-2]",
        },
    },
    .paths = .{"."},
}
// syntax: zig - nested anon struct
// tip: run `zig fetch --save git+https://github.com/username/mylib.git#[tag]` to fill url+hash automatically

Explained:

However, to get the setting up above to work for all other userdev projects it is necessary to guarantee that the certain commands have been enabled on the library

C/C++ Headers

 exe.installLibraryHeaders(mylib); // <== C/C++ projects

need on mylib build.zig add:

mylib.installHeader(b.path("foo.h"), "foo.h"); // copy single-header file to zig-out/include (rename is optional)
mylib.installHeadersDirectory(b.path("include"), "", .{}); // <== copy all headers (inc. subdir) to zig-out/include
// or
mylib.installHeadersDirectory(b.path("src"), "", .{
        .exclude_extensions = &.{
            "am",
            "gitignore",
        },
    });

It’s usual that some unix-like projects contain headers mixed with source code and other files like makefiles. Therefore it is necessary to determine what will be copied or not! For some projects, preferable to use the options struct of installHeadersDirectory() to filter which files are included.

e.g.:

C++ libraries fork with zig-pkg

C libraries with zig-pkg

Modules

Note: No need build.zig.zon file to local modules!

exe.root_module.addImport("mylib", mylib.module("mylib")); // <== for zig project only

need on mylib build.zig add:

const mylib_module = b.createModule(.{
        .root_source_file = b.path("src/mylib.zig"),
        // optional
        .imports = &.{
        .{ .name = "foo", .module = foo.module(b) },
        .{ .name = "bar", .module = bar.module(b) },
        .{ .name = "baz", .module = baz.module(b) },
    },
    });

// [...]
    mylib.root_module.addImport("mylib", mylib_module);

Hope you enjoyed this brief post!! πŸ˜„

Keep in mind that the zig package manager is now stable and widely used by some of the most known tools like:

Comments