Skip to content

Commit 2c2fd4c

Browse files
committed
pkg(highway): Enable system-integration
Move bridge.cpp to being a module cSourceFile instead of part of the staticlib Dynamic link against highway with pkg-config name 'libhwy.pc' when system-integrated
1 parent 73c7943 commit 2c2fd4c

File tree

2 files changed

+66
-36
lines changed

2 files changed

+66
-36
lines changed

pkg/highway/build.zig

+60-34
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,15 @@ pub fn build(b: *std.Build) !void {
44
const target = b.standardTargetOptions(.{});
55
const optimize = b.standardOptimizeOption(.{});
66

7-
const upstream = b.dependency("highway", .{});
8-
97
const module = b.addModule("highway", .{
108
.root_source_file = b.path("main.zig"),
119
.target = target,
1210
.optimize = optimize,
1311
});
14-
15-
const lib = b.addStaticLibrary(.{
16-
.name = "highway",
17-
.target = target,
18-
.optimize = optimize,
19-
});
20-
lib.linkLibCpp();
21-
lib.addIncludePath(upstream.path(""));
22-
module.addIncludePath(upstream.path(""));
23-
24-
if (target.result.os.tag.isDarwin()) {
25-
const apple_sdk = @import("apple_sdk");
26-
try apple_sdk.addPaths(b, lib.root_module);
27-
try apple_sdk.addPaths(b, module);
28-
}
12+
const dynamic_link_opts: std.Build.Module.LinkSystemLibraryOptions = .{
13+
.preferred_link_mode = .dynamic,
14+
.search_strategy = .mode_first,
15+
};
2916

3017
var flags = std.ArrayList([]const u8).init(b.allocator);
3118
defer flags.deinit();
@@ -70,8 +57,62 @@ pub fn build(b: *std.Build) !void {
7057
"-fno-exceptions",
7158
});
7259
}
60+
var test_exe: ?*std.Build.Step.Compile = null;
61+
if (target.query.isNative()) {
62+
test_exe = b.addTest(.{
63+
.name = "test",
64+
.root_source_file = b.path("main.zig"),
65+
.target = target,
66+
.optimize = optimize,
67+
});
68+
const tests_run = b.addRunArtifact(test_exe.?);
69+
const test_step = b.step("test", "Run tests");
70+
test_step.dependOn(&tests_run.step);
71+
var it = module.import_table.iterator();
72+
while (it.next()) |entry| test_exe.?.root_module.addImport(entry.key_ptr.*, entry.value_ptr.*);
73+
74+
// Uncomment this if we're debugging tests
75+
// b.installArtifact(test_exe.?);
76+
}
77+
78+
module.addCSourceFiles(.{ .flags = flags.items, .files = &.{"bridge.cpp"} });
79+
80+
if (b.systemIntegrationOption("highway", .{})) {
81+
module.linkSystemLibrary("libhwy", dynamic_link_opts);
82+
// TODO
83+
} else {
84+
const lib = try buildLib(b, module, .{
85+
.target = target,
86+
.optimize = optimize,
87+
.flags = flags,
88+
});
89+
if (test_exe) |exe| {
90+
exe.linkLibrary(lib);
91+
}
92+
}
93+
}
94+
95+
fn buildLib(b: *std.Build, module: *std.Build.Module, options: anytype) !*std.Build.Step.Compile {
96+
const target = options.target;
97+
const optimize = options.optimize;
98+
const flags = options.flags;
99+
100+
const upstream = b.dependency("highway", .{});
101+
const lib = b.addStaticLibrary(.{
102+
.name = "highway",
103+
.target = target,
104+
.optimize = optimize,
105+
});
106+
lib.linkLibCpp();
107+
lib.addIncludePath(upstream.path(""));
108+
module.addIncludePath(upstream.path(""));
109+
110+
if (target.result.os.tag.isDarwin()) {
111+
const apple_sdk = @import("apple_sdk");
112+
try apple_sdk.addPaths(b, lib.root_module);
113+
try apple_sdk.addPaths(b, module);
114+
}
73115

74-
lib.addCSourceFiles(.{ .flags = flags.items, .files = &.{"bridge.cpp"} });
75116
lib.addCSourceFiles(.{
76117
.root = upstream.path(""),
77118
.flags = flags.items,
@@ -92,20 +133,5 @@ pub fn build(b: *std.Build) !void {
92133
);
93134

94135
b.installArtifact(lib);
95-
96-
{
97-
const test_exe = b.addTest(.{
98-
.name = "test",
99-
.root_source_file = b.path("main.zig"),
100-
.target = target,
101-
.optimize = optimize,
102-
});
103-
test_exe.linkLibrary(lib);
104-
105-
var it = module.import_table.iterator();
106-
while (it.next()) |entry| test_exe.root_module.addImport(entry.key_ptr.*, entry.value_ptr.*);
107-
const tests_run = b.addRunArtifact(test_exe);
108-
const test_step = b.step("test", "Run tests");
109-
test_step.dependOn(&tests_run.step);
110-
}
136+
return lib;
111137
}

src/build/SharedDeps.zig

+6-2
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,12 @@ pub fn add(
411411
.target = target,
412412
.optimize = optimize,
413413
});
414-
step.linkLibrary(highway_dep.artifact("highway"));
415-
try static_libs.append(highway_dep.artifact("highway").getEmittedBin());
414+
if (b.systemIntegrationOption("highway", .{})) {
415+
step.linkSystemLibrary2("libhwy", dynamic_link_opts);
416+
} else {
417+
step.linkLibrary(highway_dep.artifact("highway"));
418+
try static_libs.append(highway_dep.artifact("highway").getEmittedBin());
419+
}
416420

417421
// utfcpp - This is used as a dependency on our hand-written C++ code
418422
const utfcpp_dep = b.dependency("utfcpp", .{

0 commit comments

Comments
 (0)