Skip to content

Build process

Chung Leong edited this page Jun 7, 2024 · 6 revisions

Suppose we have the following Zig file:

/home/eric/project_x/zig/hello.zig

And we want to compile it at ReleaseSmall to:

/home/eric/project_x/lib/hello.zigar/linux.x64.so

Zigar would go through the following steps:

  1. Write the process id to '/tmp/zigar-build/project_x-3127ddfd.pid'. If the file exists already, read the process id and check if the process is still alive. Keep waiting until the other process has gone away.
  2. Create the sub-directory project_x-3127ddfd in /tmp/zigar-build.
  3. Create build-cfg.zig in /tmp/zigar-build/project_x-3127ddfd.
  4. Copy /home/eric/project_x/zig/build.zig to /tmp/zigar-build/project_x-3127ddfd if it exists. If not, copy /home/eric/project_x/node_modules/zigar-compiler/zig/build.zig.
  5. Copy /home/eric/project_x/zig/build.zig.zon to /tmp/zigar-build/project_x-3127ddfd if it exists.
  6. Run the command zig build -Doptimize=ReleaseSmall -Dtarget=linux-x86_64-gnu.
  7. If an error occurs, write the compiler output to /tmp/zigar-build/project_x-3127ddfd/log
  8. Remove /tmp/zigar-build/project_x-3127ddfd if clean is true

The build directory would look like this afterward:

📄 build-cfg.zig
📄 build.zig
📄 build.zig.zon
📁 .zig-cache

build-cfg.zig would contain the following:

pub const module_name = "hello";
pub const module_path = "/home/eric/project_x/zig/hello.zig";
pub const module_dir = "/home/eric/project_x/zig/hello";
pub const exporter_path = "/home/eric/project_x/node_modules/zigar-compiler/zig/exporter-c.zig";
pub const stub_path = "/home/eric/project_x/node_modules/zigar-compiler/zig/stub-c.zig";
pub const output_path = "/home/eric/project_x/lib/hello.zigar/linux.x64.so";
pub const use_libc = true;
pub const is_wasm = false;

Notes

The random string attached to the name of the build directory is the first 8 letters of md5("/home/eric/project_x/zig/hello").

The content of build-cfg.zig is described here.

Clone this wiki locally