2
2
3
3
const std = @import ("std" );
4
4
5
+ const pkgBuilder = struct {
6
+ mode : std.builtin.OptimizeMode ,
7
+ target : std.zig.CrossTarget ,
8
+ build : * std.Build.CompileStep ,
9
+ };
10
+
5
11
// Although this function looks imperative, note that its job is to
6
12
// declaratively construct a build graph that will be executed by an external
7
13
// runner.
@@ -17,13 +23,9 @@ pub fn build(b: *std.Build) void {
17
23
// set a preferred release mode, allowing the user to decide how to optimize.
18
24
const optimize = b .standardOptimizeOption (.{});
19
25
20
- const pkg_dep = b .option (bool , "fetch" , "Download libzmq with zig-pkg [default: false]" ) orelse false ;
21
-
22
- const libzmq_dep = b .dependency ("libzmq" , .{
23
- .optimize = optimize ,
24
- .target = target ,
25
- });
26
- const libzmq = libzmq_dep .artifact ("zmq" );
26
+ // Options
27
+ const pkg_dep = b .option (bool , "fetch" , "Download libzmq with zig-pkg [default: true]" ) orelse true ;
28
+ const shared = b .option (bool , "shared" , "Build shared library [default: true]" ) orelse true ;
27
29
28
30
const config_header = if (! target .isWindows ()) b .addConfigHeader (.{
29
31
.style = .blank ,
@@ -41,12 +43,11 @@ pub fn build(b: *std.Build) void {
41
43
.include_path = "platform.h" ,
42
44
}, .{});
43
45
44
- const shared = b .option (bool , "shared" , "Build shared Library [default: false]" ) orelse false ;
45
- const lib = if (shared ) b .addSharedLibrary (.{
46
+ const libczmq = if (shared ) b .addSharedLibrary (.{
46
47
.name = "czmq_zig" ,
47
48
// In this case the main source file is merely a path, however, in more
48
49
// complicated build scripts, this could be a generated file.
49
- //.root_source_file = .{ .path = "src/main .zig" },
50
+ //.root_source_file = .{ .path = "src/czmq .zig" },
50
51
.version = .{
51
52
.major = 4 ,
52
53
.minor = 2 ,
@@ -60,54 +61,89 @@ pub fn build(b: *std.Build) void {
60
61
.target = target ,
61
62
.optimize = optimize ,
62
63
});
63
- lib .addConfigHeader (config_header );
64
- lib .addIncludePath ("../../include" );
65
- lib .addIncludePath (config_header .include_path );
66
- lib .addCSourceFiles (lib_src , lib_flags );
64
+ libczmq .addConfigHeader (config_header );
65
+ libczmq .addIncludePath ("../../include" );
66
+ libczmq .addIncludePath (config_header .include_path );
67
+ libczmq .addCSourceFiles (lib_src , lib_flags );
67
68
if (target .isWindows () and shared ) {
68
- lib .linkSystemLibraryName ("ws2_32" );
69
- lib .linkSystemLibraryName ("rpcrt4" );
70
- lib .linkSystemLibraryName ("iphlpapi" );
69
+ libczmq .linkSystemLibraryName ("ws2_32" );
70
+ libczmq .linkSystemLibraryName ("rpcrt4" );
71
+ libczmq .linkSystemLibraryName ("iphlpapi" );
71
72
}
72
- if (pkg_dep )
73
- lib .linkLibrary (libzmq )
74
- else
75
- lib .linkSystemLibrary ("zmq" );
73
+ if (pkg_dep ) {
74
+ const libzmq_dep = b .dependency ("libzmq" , .{
75
+ .optimize = optimize ,
76
+ .target = target ,
77
+ });
78
+ const libzmq = libzmq_dep .artifact ("zmq" );
79
+ libczmq .linkLibrary (libzmq );
80
+ libczmq .installLibraryHeaders (libzmq );
81
+ } else libczmq .linkSystemLibrary ("zmq" );
76
82
if (target .isLinux () and shared ) {
77
- lib .linkSystemLibrary ("dl" );
78
- lib .linkSystemLibrary ("rt" );
83
+ libczmq .linkSystemLibrary ("dl" );
84
+ libczmq .linkSystemLibrary ("rt" );
79
85
}
86
+
87
+ // TODO: No support MSVC libC++ (ucrt/msvcrt/vcruntime)
88
+ // https://github.com/ziglang/zig/issues/4785 - drop replacement for MSVC
80
89
if (target .getAbi () != .msvc ) {
81
- lib .linkLibCpp ();
82
- lib .linkLibC ();
90
+ libczmq .linkLibCpp ();
91
+ libczmq .linkLibC ();
83
92
}
84
93
// This declares intent for the library to be installed into the standard
85
94
// location when the user invokes the "install" step (the default step when
86
95
// running `zig build`).
87
- lib .install ();
88
- lib .installHeadersDirectory ("../../include" , "" );
89
- lib .installLibraryHeaders (libzmq );
96
+ libczmq .install ();
97
+ b .installDirectory (.{
98
+ .source_dir = "../../include" ,
99
+ .install_dir = .header ,
100
+ .install_subdir = "" ,
101
+ .exclude_extensions = &.{"am" },
102
+ });
103
+
104
+ build_test (b , .{ .target = target , .mode = optimize , .build = libczmq });
105
+ buildSample (b , .{ .target = target , .mode = optimize , .build = libczmq }, "hello_czmq" , "../../examples/security/hello.c" );
106
+ }
107
+
108
+ fn buildSample (b : * std.Build.Builder , lib : pkgBuilder , name : []const u8 , file : []const u8 ) void {
109
+ const test_exe = b .addExecutable (.{
110
+ .name = name ,
111
+ .optimize = lib .mode ,
112
+ .target = lib .target ,
113
+ });
114
+ test_exe .linkLibrary (lib .build );
115
+ test_exe .addSystemIncludePath ("../../include" );
116
+ test_exe .addCSourceFile (file , lib_flags );
117
+ if (lib .target .isWindows ())
118
+ test_exe .linkSystemLibraryName ("ws2_32" );
119
+ test_exe .linkLibC ();
120
+ test_exe .install ();
121
+
122
+ const run_cmd = test_exe .run ();
123
+ run_cmd .step .dependOn (b .getInstallStep ());
124
+ if (b .args ) | args | {
125
+ run_cmd .addArgs (args );
126
+ }
90
127
128
+ const run_step = b .step ("run" , b .fmt ("Run the {s}" , .{name }));
129
+ run_step .dependOn (& run_cmd .step );
130
+ }
131
+
132
+ fn build_test (b : * std.Build.Builder , pkg : pkgBuilder ) void {
91
133
// Creates a step for unit testing.
92
134
const libtest = b .addTest (.{
93
135
.root_source_file = .{ .path = "src/czmq.zig" },
94
- .target = target ,
95
- .optimize = optimize ,
136
+ .target = pkg . target ,
137
+ .optimize = pkg . mode ,
96
138
});
97
- libtest .addConfigHeader (config_header );
98
- libtest .addIncludePath (config_header .include_path );
99
- libtest .addIncludePath ("../../include" );
100
- libtest .addCSourceFiles (lib_src , lib_flags );
101
- if (target .isWindows () and shared ) {
139
+ // libtest.addIncludePath("../../include");
140
+ if (pkg .target .isWindows ()) {
102
141
libtest .linkSystemLibraryName ("ws2_32" );
103
142
libtest .linkSystemLibraryName ("rpcrt4" );
104
143
libtest .linkSystemLibraryName ("iphlpapi" );
105
144
}
106
- if (pkg_dep )
107
- libtest .linkLibrary (libzmq )
108
- else
109
- libtest .linkSystemLibrary ("zmq" );
110
- if (target .getAbi () != .msvc ) {
145
+ libtest .linkLibrary (pkg .build );
146
+ if (pkg .target .getAbi () != .msvc ) {
111
147
libtest .linkLibCpp ();
112
148
libtest .linkLibC ();
113
149
}
@@ -126,7 +162,10 @@ const lib_flags: []const []const u8 = &.{
126
162
};
127
163
const lib_src : []const []const u8 = &.{
128
164
"../../src/zactor.c" ,
165
+ "../../src/zargs.c" ,
129
166
"../../src/zarmour.c" ,
167
+ "../../src/zauth.c" ,
168
+ "../../src/zbeacon.c" ,
130
169
"../../src/zcert.c" ,
131
170
"../../src/zcertstore.c" ,
132
171
"../../src/zchunk.c" ,
@@ -137,33 +176,30 @@ const lib_src: []const []const u8 = &.{
137
176
"../../src/zdir_patch.c" ,
138
177
"../../src/zfile.c" ,
139
178
"../../src/zframe.c" ,
179
+ "../../src/zgossip.c" ,
180
+ "../../src/zgossip_msg.c" ,
140
181
"../../src/zhash.c" ,
141
182
"../../src/zhashx.c" ,
183
+ "../../src/zhttp_client.c" ,
184
+ "../../src/zhttp_request.c" ,
185
+ "../../src/zhttp_response.c" ,
186
+ "../../src/zhttp_server.c" ,
187
+ "../../src/zhttp_server_options.c" ,
142
188
"../../src/ziflist.c" ,
143
189
"../../src/zlist.c" ,
144
190
"../../src/zlistx.c" ,
145
191
"../../src/zloop.c" ,
192
+ "../../src/zmonitor.c" ,
146
193
"../../src/zmsg.c" ,
194
+ "../../src/zosc.c" ,
147
195
"../../src/zpoller.c" ,
196
+ "../../src/zproc.c" ,
197
+ "../../src/zproxy.c" ,
198
+ "../../src/zrex.c" ,
148
199
"../../src/zsock.c" ,
149
200
"../../src/zstr.c" ,
150
201
"../../src/zsys.c" ,
151
- "../../src/zuuid.c" ,
152
- "../../src/zauth.c" ,
153
- "../../src/zbeacon.c" ,
154
- "../../src/zgossip.c" ,
155
- "../../src/zmonitor.c" ,
156
- "../../src/zproxy.c" ,
157
- "../../src/zrex.c" ,
158
- "../../src/zgossip_msg.c" ,
159
- "../../src/ztrie.c" ,
160
- "../../src/zargs.c" ,
161
- "../../src/zproc.c" ,
162
202
"../../src/ztimerset.c" ,
163
- "../../src/zhttp_server.c" ,
164
- "../../src/zhttp_client.c" ,
165
- "../../src/zhttp_request.c" ,
166
- "../../src/zhttp_response.c" ,
167
- "../../src/zhttp_server_options.c" ,
168
- "../../src/zosc.c" ,
203
+ "../../src/ztrie.c" ,
204
+ "../../src/zuuid.c" ,
169
205
};
0 commit comments