im making a library in zig that can be install via the package manger.
in the build.zig of the library im creating a module that points to the main source directory and sets a path to a directory containing assets.
i want to add an example to my project so that the user can clone the repo, call zig build run-example and have the example program i wrote compiled and executed.
i already know how to do that but i dont want it to be done when im using the project as a dependency and i dont know how to check whether or not the build function (of the library project) is being called directly (to run the example) or by a different project using it.
#building example Exe to my library
1 messages · Page 1 of 1 (latest)
none of the steps you set up in your build.zig actually run until they are explicitly invoked. Even if your build.zig sets up both a library_mod meant for other projects to depend on, and example_exe that can be built and run by running zig build run-example, if the other project only imports library_mod then example_exe will never get built
so you don't need to check for anything, the zig build system is already smart enough to only build what is needed and depended on by the consuming project
wait
if i call b.dependecy in the project that uses the library, dosent it just run the build function of the library?
the build function itself does not do anything but construct a dependency graph
what does it mean?
if i run b.installArtifact in the build function and than use it as a dependency wont it build the executeable anyways like people do with static libraries?
no, if I only addImport your module into my exe, and run the "run" step for my build.zig only your module gets build and my exe
the build function does not actually build anything, it creates dependency graph. The build runner is what decides what to actually build
the build runner is the "zig build" command?
build runner is this by default https://github.com/ziglang/zig/blob/master/lib/compiler/build_runner.zig
but it can be changed with command line switch
ok so only the things i explicitly use in my project will be compiled/created if i add my library as a dependency, right?
yup