-
Notifications
You must be signed in to change notification settings - Fork 3
Build process
Chung Leong edited this page Apr 17, 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:
- Recursively scan
/home/eric/project_x/zig
. - Obtain the last-modified time of
/home/eric/project_x/lib/hello.zigar/linux.x64.so
. - Check if any file in
/home/eric/project_x/zig
is newer than the .so file. Exit if none is. - 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.
- Create the sub-directory
project_x-3127ddfd
in/tmp/zigar-build
. - Create
build-cfg.zig
in/tmp/zigar-build/project_x-3127ddfd
. - 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/
. - Copy
/home/eric/project_x/zig/build.zig.zon
to/tmp/zigar-build/project_x-3127ddfd
if it exists. - Run the command
zig build -Doptimize=ReleaseSmall -Dtarget=linux-x86_64-gnu
. - If an error occurs, write the compiler output to
/tmp/zigar-build/project_x-3127ddfd/log
- Remove
/tmp/zigar-build/project_x-3127ddfd
ifclean
istrue
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;
If you have a custom build file that references files outside the directory holding your Zig file, you might want to create symlinks to them in that directory so the scan would pick up any modifications.
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.