@@ -4,28 +4,15 @@ pub fn build(b: *std.Build) !void {
4
4
const target = b .standardTargetOptions (.{});
5
5
const optimize = b .standardOptimizeOption (.{});
6
6
7
- const upstream = b .dependency ("highway" , .{});
8
-
9
7
const module = b .addModule ("highway" , .{
10
8
.root_source_file = b .path ("main.zig" ),
11
9
.target = target ,
12
10
.optimize = optimize ,
13
11
});
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
+ };
29
16
30
17
var flags = std .ArrayList ([]const u8 ).init (b .allocator );
31
18
defer flags .deinit ();
@@ -70,8 +57,62 @@ pub fn build(b: *std.Build) !void {
70
57
"-fno-exceptions" ,
71
58
});
72
59
}
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
+ }
73
115
74
- lib .addCSourceFiles (.{ .flags = flags .items , .files = &.{"bridge.cpp" } });
75
116
lib .addCSourceFiles (.{
76
117
.root = upstream .path ("" ),
77
118
.flags = flags .items ,
@@ -92,20 +133,5 @@ pub fn build(b: *std.Build) !void {
92
133
);
93
134
94
135
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 ;
111
137
}
0 commit comments