Skip to content

Commit 4a41f3c

Browse files
committed
fix: building
c headers across modules are painful, more info: ziglang/zig#14719
1 parent 66efaae commit 4a41f3c

File tree

3 files changed

+67
-66
lines changed

3 files changed

+67
-66
lines changed

build.zig

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,88 @@
11
const std = @import("std");
22

3+
// Although this function looks imperative, note that its job is to
4+
// declaratively construct a build graph that will be executed by an external
5+
// runner.
36
pub fn build(b: *std.Build) void {
7+
// Standard target options allows the person running `zig build` to choose
8+
// what target to build for. Here we do not override the defaults, which
9+
// means any target is allowed, and the default is native. Other options
10+
// for restricting supported target set are available.
411
const target = b.standardTargetOptions(.{});
12+
13+
// Standard optimization options allow the person running `zig build` to select
14+
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
15+
// set a preferred release mode, allowing the user to decide how to optimize.
516
const optimize = b.standardOptimizeOption(.{});
617

7-
// Define dependencies.
8-
const zlib = b.dependency("zlib", .{});
18+
// Zlib dependency
19+
const dep_zlib = b.dependency("zlib", .{});
20+
21+
// Zlib headers path
22+
// https://github.com/ziglang/zig/issues/14719
23+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
24+
const allocator = gpa.allocator();
25+
defer _ = gpa.deinit();
26+
const zlib_install_path = dep_zlib.module("zlib").builder.install_path;
27+
const zlib_headers_rel = "/include/zlib";
28+
const zlib_headers = allocator.alloc(u8, zlib_install_path.len + zlib_headers_rel.len) catch {
29+
@panic("out of memory");
30+
};
31+
defer _ = allocator.free(zlib_headers);
32+
std.mem.copy(u8, zlib_headers[0..], zlib_install_path);
33+
std.mem.copy(u8, zlib_headers[zlib_install_path.len..], zlib_headers_rel);
34+
std.debug.print("zlib headers found at: {s}\n", .{zlib_headers});
935

10-
// Define module
11-
const ws_module = b.addModule("ws", .{
36+
// Declare module to expose to package manager to make it available to downstream
37+
const mod = b.addModule("ws", .{
1238
.source_file = .{ .path = "src/main.zig" },
13-
.dependencies = &[_]std.Build.ModuleDependency{.{ .name = "zlib", .module = zlib.module("zlib") }},
39+
.dependencies = &.{.{ .name = "zlib", .module = dep_zlib.module("zlib") }},
1440
});
1541

16-
// Build library.
17-
const ws_lib = b.addStaticLibrary(.{
42+
const lib = b.addStaticLibrary(.{
1843
.name = "ws",
19-
.root_source_file = .{ .path = "src/main.zig" },
44+
// In this case the main source file is merely a path, however, in more
45+
// complicated build scripts, this could be a generated file.
46+
// .root_source_file = .{ .path = "src/main.zig" },
2047
.target = target,
2148
.optimize = optimize,
2249
});
23-
// Link z library and zlib module.
24-
ws_lib.linkLibrary(b.dependency("zlib", .{
50+
51+
// Link zlib
52+
lib.linkLibrary(b.dependency("zlib", .{
2553
.target = target,
2654
.optimize = optimize,
27-
}).artifact("z"));
28-
ws_lib.addModule("zlib", zlib.module("zlib"));
29-
b.installArtifact(ws_lib);
55+
}).artifact("zlib"));
56+
lib.addModule("zlib", dep_zlib.module("zlib"));
57+
58+
// This declares intent for the library to be installed into the standard
59+
// location when the user invokes the "install" step (the default step when
60+
// running `zig build`).
61+
b.installArtifact(lib);
3062

31-
// Build test.
32-
const test_compile = b.addTest(.{
63+
// Creates a step for unit testing. This only builds the test executable
64+
// but does not run it.
65+
const main_tests = b.addTest(.{
3366
.root_source_file = .{ .path = "src/main.zig" },
3467
.target = target,
3568
.optimize = optimize,
3669
});
37-
test_compile.linkLibrary(b.dependency("zlib", .{
70+
71+
// Link zlib
72+
main_tests.addIncludePath(.{ .path = zlib_headers });
73+
main_tests.linkLibrary(b.dependency("zlib", .{
3874
.target = target,
3975
.optimize = optimize,
40-
}).artifact("z"));
41-
test_compile.addModule("zlib", zlib.module("zlib"));
42-
const test_step = b.step("test", "Run unit tests");
43-
test_step.dependOn(&test_compile.step);
76+
}).artifact("zlib"));
77+
main_tests.addModule("zlib", dep_zlib.module("zlib"));
78+
79+
const run_main_tests = b.addRunArtifact(main_tests);
80+
81+
// This creates a build step. It will be visible in the `zig build --help` menu,
82+
// and can be selected like this: `zig build test`
83+
// This will evaluate the `test` step rather than the default, which is "install".
84+
const test_step = b.step("test", "Run library tests");
85+
test_step.dependOn(&run_main_tests.step);
4486

4587
// Build examples.
4688
const bin = b.addExecutable(.{
@@ -49,10 +91,8 @@ pub fn build(b: *std.Build) void {
4991
.target = target,
5092
.optimize = optimize,
5193
});
52-
bin.linkLibrary(ws_lib);
53-
bin.addModule("ws", ws_module);
94+
bin.addIncludePath(.{ .path = zlib_headers });
95+
bin.linkLibrary(lib);
96+
bin.addModule("ws", mod);
5497
b.installArtifact(bin);
5598
}
56-
57-
// to test single file
58-
// $ zig test src/main.zig --deps zlib=zlib --mod zlib::zlib/src/main.zig -l z

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
.version = "0.1.0",
44
.dependencies = .{
55
.zlib = .{
6-
.url = "https://github.com/ianic/zig-zlib/archive/9186b0f5fdfd6c29cd04f7ada7b0113fe1f63611.tar.gz",
7-
.hash = "122001cc3da638f9315f08fb51fe5aace68c254b59bcac3457e20d746565bad7fe04",
6+
.url = "https://github.com/0t4u/zig-zlib/archive/5ac9e76f6f8bc7b9d5d820e01912c57671a8a441.tar.gz",
7+
.hash = "122044ed841b701d59cd10aba7b8f827907d308651f362e1f0acec416c768c8d29ce",
88
},
99
},
1010
}

websocket.zig

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)