zigzap / zap

blazingly fast backends in zig
MIT License
1.99k stars 71 forks source link

Configure zap and facil.io as zig packages #3

Closed qbradley closed 1 year ago

qbradley commented 1 year ago

With these changes, it is possible to use the built in zig package manager to make a zig program based on zap.

For an example project, see https://github.com/qbradley/kahawatamu, which currently is just the zap hello world but in its own repository. Clone it and run the following commands will automatically pull down and build zap and facil.io as part of creating the exe:

git clone https://github.com/qbradley/kahawatamu
cd kahawatamu
zig build run

In kahawatamu I had to add both zap and facil.io as dependencies, as I found a way to reference a C project as a static library and a zig projects as a module, but I did not find a way in the current zig code for a package exporting a module to automatically include a static library when referenced.

Maybe it is too soon for these changes, but I wrote them to unblock myself so I'm sharing them in case it is useful to others.

renerocksai commented 1 year ago

Thank you for this PR! I see only one problem with it and that's a consequence of using the new package manager: The patch to facilio is not going to be applied. In the version before this PR, there is an ensurePatch step that applies a tiny little patch to facilio that changes timing info in facilio's logging output from milliseconds to microseconds. Milliseconds might have been appropriate a few years ago, but when I run my code and enable logging, all I get is "0 ms" all the time which isn't particularly useful. Hence, I patched facilio.

To go forward, I see two options:

  1. Get facilio to accept my patch
  2. Fork facilio, apply the patch, and use that as dependency in the build.zig.zon.

I guess, I'll give option 2 a try, then open a PR to the original facil.io. It's totally OK if they don't accept my PR, there might be reasons speaking against microsecond precision, like, maybe breaking test suites etc.

renerocksai commented 1 year ago

Also, I noted that you used a different commit of facil.io: I went for the latest stable 0.7.x branch:

commit 063c9bb86e14bae7441d2e37ab7207d9839a98ce (tag: 0.7.5)

I'll try to fix this.

renerocksai commented 1 year ago
{
    .name = "zap",
    .version = "0.0.1",

    .dependencies = .{
        .@"facil.io" = .{
            .url = "https://github.com/renerocksai/facil.io/archive/1b2fce7e46128c526880bce28e9ce68cd1e23ce2.tar.gz",
            .hash = "1220b1eb9d016ad6c82ea2840acce19726152eec1193f615399170528f160c8c1003",
        }
    }
}

With this new build.zig.zon I get the following error:

warning: FileNotFound: /home/rs/.cache/zig/p/1220b1eb9d016ad6c82ea2840acce19726152eec1193f615399170528f160c8c1003/build.zig
error: FileNotFound

If I change the SHA, it correctly reports the wrong SHA, hash mismatch. When I fix the hash, it claims it can't find the thing.

AAAAH, there's no build.zig in my fork of facil.io. Hm. Hang on, how did it work before then?

renerocksai commented 1 year ago

One more thing: I run make libdump to dump all files required for facil.io into the libdump/ dir. This is also skipped by your approach. I'm checking if this is required or if I can just replace the C source dependencies by their original paths.

renerocksai commented 1 year ago

I managed to create a fork that is identical to what the previous version would have produced. The key thing at the end was to make the C lib install its headers into a single flat dir at the end or else the lib-consumer's C-import would not find #include when would live in a subdir of include/. While I first could circumvent this problem by adding include dirs of zig-cache (hardcoded), I couldn't find a possibility to get the zig-cache dir at build-time of the consumer, hence I opted for flattening the generated include dir in the lib.

renerocksai commented 1 year ago

Thanks for this PR, it served as a good starting point!!! I also updated the README.

qbradley commented 1 year ago

Thank you for wrestling this I to shape! I'll try switching from my fork in my project!

renerocksai commented 1 year ago

FYI

With the latest commits, I managed to get rid of the facil.io dependency in projects that use zap. Only zap needs to be included in build.zig.zon. Latest zig complained about duplicates because both zap and the project using zap would include facil.io.

See the zap-endpoint-tests demo project for how it's done.

I managed to do this with 4 steps:

Removing the C header dependencies was needed because I couldn't figure out how to access and install the header files of a lib that one depends on. And where to put them, etc. The cleaner way is to have no C dependencies - but zig code or zig wrappers for everything. That way, the only thing needed from the facilio-lib project is the built artifact, the static library libfacilio.a.