Skip to content

Commit 452a0f2

Browse files
committed
add zig 0.11 support
1 parent def8b66 commit 452a0f2

File tree

4 files changed

+189
-79
lines changed

4 files changed

+189
-79
lines changed

build.zig

Lines changed: 17 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,23 @@
11
const std = @import("std");
2+
const builtin = @import("builtin");
23

3-
pub fn build(b: *std.Build) void {
4-
const target = b.standardTargetOptions(.{});
5-
const optimize = b.standardOptimizeOption(.{});
4+
const min_zig_string = "0.11.0";
65

7-
const msgpack = b.addModule("msgpack", .{
8-
.root_source_file = .{
9-
.path = "src/msgpack.zig",
10-
},
11-
});
6+
const current_zig = builtin.zig_version;
127

13-
const msgpack_rpc = b.addModule("msgpack_rpc", .{
14-
.root_source_file = .{
15-
.path = "src/msgpack_rpc.zig",
16-
},
17-
.imports = &.{
18-
.{
19-
.name = "msgpack",
20-
.module = msgpack,
21-
},
22-
},
23-
});
24-
25-
const test_step = b.step("test", "Run unit tests");
26-
27-
const msgpack_unit_tests = b.addTest(.{
28-
.root_source_file = .{ .path = "src/msgpack_uint_test.zig" },
29-
.target = target,
30-
.optimize = optimize,
31-
});
32-
msgpack_unit_tests.root_module.addImport("msgpack", msgpack);
33-
const run_msgpack_tests = b.addRunArtifact(msgpack_unit_tests);
34-
test_step.dependOn(&run_msgpack_tests.step);
35-
36-
const msgpack_rpc_unit_tests = b.addTest(.{
37-
.root_source_file = .{ .path = "src/msgpack_rpc_unit_test.zig" },
38-
.target = target,
39-
.optimize = optimize,
40-
});
41-
msgpack_rpc_unit_tests.root_module.addImport("msgpack_rpc", msgpack_rpc);
42-
const run_msgpack_rpc_tests = b.addRunArtifact(msgpack_rpc_unit_tests);
43-
test_step.dependOn(&run_msgpack_rpc_tests.step);
44-
45-
{
46-
//// build test cli
47-
const build_step = b.step("build_dev", "build cli test");
48-
const exe = b.addExecutable(.{
49-
.name = "msgpack_rpc",
50-
.root_source_file = .{ .path = "src/dev.zig" },
51-
.target = target,
52-
.optimize = optimize,
53-
});
54-
55-
exe.root_module.addImport("msgpack_rpc", msgpack_rpc);
56-
57-
const install = b.addInstallArtifact(exe, .{});
58-
59-
build_step.dependOn(&install.step);
60-
61-
const run_exe = b.addRunArtifact(exe);
62-
63-
run_exe.step.dependOn(&install.step);
64-
65-
const run_step = b.step("dev", "Run the cli");
66-
67-
run_step.dependOn(&run_exe.step);
8+
comptime {
9+
const min_zig = std.SemanticVersion.parse(min_zig_string) catch unreachable;
10+
if (current_zig.order(min_zig) == .lt) {
11+
const err_msg = std.fmt.comptimePrint(
12+
"Your Zig version v{} does not meet the minimum build requirement of v{}",
13+
.{ current_zig, min_zig },
14+
);
15+
@compileError(err_msg);
6816
}
6917
}
18+
19+
pub const build =
20+
if (current_zig.minor == 11)
21+
@import("build_11.zig").build
22+
else
23+
@import("build_12.zig").build;

build_11.zig

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
const msgpack = b.addModule("msgpack", .{
8+
.source_file = .{
9+
.path = "src/msgpack.zig",
10+
},
11+
});
12+
13+
const msgpack_rpc = b.addModule("msgpack_rpc", .{
14+
.source_file = .{
15+
.path = "src/msgpack_rpc.zig",
16+
},
17+
.dependencies = &.{
18+
.{
19+
.name = "msgpack",
20+
.module = msgpack,
21+
},
22+
},
23+
});
24+
25+
const test_step = b.step("test", "Run unit tests");
26+
27+
const msgpack_unit_tests = b.addTest(.{
28+
.root_source_file = .{
29+
.path = "src/msgpack_uint_test.zig",
30+
},
31+
.target = target,
32+
.optimize = optimize,
33+
});
34+
35+
msgpack_unit_tests.addModule("msgpack", msgpack);
36+
const run_msgpack_tests = b.addRunArtifact(msgpack_unit_tests);
37+
test_step.dependOn(&run_msgpack_tests.step);
38+
39+
const msgpack_rpc_unit_tests = b.addTest(.{
40+
.root_source_file = .{
41+
.path = "src/msgpack_rpc_unit_test.zig",
42+
},
43+
.target = target,
44+
.optimize = optimize,
45+
});
46+
msgpack_rpc_unit_tests.addModule("msgpack_rpc", msgpack_rpc);
47+
const run_msgpack_rpc_tests = b.addRunArtifact(msgpack_rpc_unit_tests);
48+
test_step.dependOn(&run_msgpack_rpc_tests.step);
49+
50+
{
51+
//// build test cli
52+
const build_step = b.step("build_dev", "build cli test");
53+
const exe = b.addExecutable(.{
54+
.name = "msgpack_rpc",
55+
.root_source_file = .{ .path = "src/dev.zig" },
56+
.target = target,
57+
.optimize = optimize,
58+
});
59+
60+
exe.addModule("msgpack_rpc", msgpack_rpc);
61+
62+
const install = b.addInstallArtifact(exe, .{});
63+
64+
build_step.dependOn(&install.step);
65+
66+
const run_exe = b.addRunArtifact(exe);
67+
68+
run_exe.step.dependOn(&install.step);
69+
70+
const run_step = b.step("dev", "Run the cli");
71+
72+
run_step.dependOn(&run_exe.step);
73+
}
74+
}

build_12.zig

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
const msgpack = b.addModule("msgpack", .{
8+
.root_source_file = .{
9+
.path = "src/msgpack.zig",
10+
},
11+
});
12+
13+
const msgpack_rpc = b.addModule("msgpack_rpc", .{
14+
.root_source_file = .{
15+
.path = "src/msgpack_rpc.zig",
16+
},
17+
.imports = &.{
18+
.{
19+
.name = "msgpack",
20+
.module = msgpack,
21+
},
22+
},
23+
});
24+
25+
const test_step = b.step("test", "Run unit tests");
26+
27+
const msgpack_unit_tests = b.addTest(.{
28+
.root_source_file = .{ .path = "src/msgpack_uint_test.zig" },
29+
.target = target,
30+
.optimize = optimize,
31+
});
32+
msgpack_unit_tests.root_module.addImport("msgpack", msgpack);
33+
const run_msgpack_tests = b.addRunArtifact(msgpack_unit_tests);
34+
test_step.dependOn(&run_msgpack_tests.step);
35+
36+
const msgpack_rpc_unit_tests = b.addTest(.{
37+
.root_source_file = .{ .path = "src/msgpack_rpc_unit_test.zig" },
38+
.target = target,
39+
.optimize = optimize,
40+
});
41+
msgpack_rpc_unit_tests.root_module.addImport("msgpack_rpc", msgpack_rpc);
42+
const run_msgpack_rpc_tests = b.addRunArtifact(msgpack_rpc_unit_tests);
43+
test_step.dependOn(&run_msgpack_rpc_tests.step);
44+
45+
{
46+
//// build test cli
47+
const build_step = b.step("build_dev", "build cli test");
48+
const exe = b.addExecutable(.{
49+
.name = "msgpack_rpc",
50+
.root_source_file = .{ .path = "src/dev.zig" },
51+
.target = target,
52+
.optimize = optimize,
53+
});
54+
55+
exe.root_module.addImport("msgpack_rpc", msgpack_rpc);
56+
57+
const install = b.addInstallArtifact(exe, .{});
58+
59+
build_step.dependOn(&install.step);
60+
61+
const run_exe = b.addRunArtifact(exe);
62+
63+
run_exe.step.dependOn(&install.step);
64+
65+
const run_step = b.step("dev", "Run the cli");
66+
67+
run_step.dependOn(&run_exe.step);
68+
}
69+
}

src/msgpack.zig

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@
33

44
const std = @import("std");
55
const builtin = @import("builtin");
6+
7+
const current_zig = builtin.zig_version;
68
const Allocator = std.mem.Allocator;
79
const comptimePrint = std.fmt.comptimePrint;
810
const native_endian = builtin.cpu.arch.endian();
911

12+
const big_endian = switch (current_zig.minor) {
13+
11 => std.builtin.Endian.Big,
14+
12 => std.builtin.Endian.big,
15+
else => @compileError("not support current version zig"),
16+
};
17+
const little_endian = switch (current_zig.minor) {
18+
11 => std.builtin.Endian.Little,
19+
12 => std.builtin.Endian.little,
20+
else => @compileError("not support current version zig"),
21+
};
22+
1023
pub const Str = struct {
1124
str: []const u8,
1225
pub fn value(self: Str) []const u8 {
@@ -208,7 +221,7 @@ pub fn Pack(
208221

209222
fn write_u16_value(self: Self, val: u16) !void {
210223
var arr: [2]u8 = undefined;
211-
std.mem.writeInt(u16, &arr, val, .big);
224+
std.mem.writeInt(u16, &arr, val, big_endian);
212225

213226
try self.write_data(&arr);
214227
}
@@ -221,7 +234,7 @@ pub fn Pack(
221234

222235
fn write_u32_value(self: Self, val: u32) !void {
223236
var arr: [4]u8 = undefined;
224-
std.mem.writeInt(u32, &arr, val, .big);
237+
std.mem.writeInt(u32, &arr, val, big_endian);
225238

226239
try self.write_data(&arr);
227240
}
@@ -234,7 +247,7 @@ pub fn Pack(
234247

235248
fn write_u64_value(self: Self, val: u64) !void {
236249
var arr: [8]u8 = undefined;
237-
std.mem.writeInt(u64, &arr, val, .big);
250+
std.mem.writeInt(u64, &arr, val, big_endian);
238251

239252
try self.write_data(&arr);
240253
}
@@ -266,7 +279,7 @@ pub fn Pack(
266279

267280
fn write_i16_value(self: Self, val: i16) !void {
268281
var arr: [2]u8 = undefined;
269-
std.mem.writeInt(i16, &arr, val, .big);
282+
std.mem.writeInt(i16, &arr, val, big_endian);
270283

271284
try self.write_data(&arr);
272285
}
@@ -279,7 +292,7 @@ pub fn Pack(
279292

280293
fn write_i32_value(self: Self, val: i32) !void {
281294
var arr: [4]u8 = undefined;
282-
std.mem.writeInt(i32, &arr, val, .big);
295+
std.mem.writeInt(i32, &arr, val, big_endian);
283296

284297
try self.write_data(&arr);
285298
}
@@ -292,7 +305,7 @@ pub fn Pack(
292305

293306
fn write_i64_value(self: Self, val: i64) !void {
294307
var arr: [8]u8 = undefined;
295-
std.mem.writeInt(i64, &arr, val, .big);
308+
std.mem.writeInt(i64, &arr, val, big_endian);
296309

297310
try self.write_data(&arr);
298311
}
@@ -338,7 +351,7 @@ pub fn Pack(
338351
fn write_f32_value(self: Self, val: f32) !void {
339352
const int: u32 = @bitCast(val);
340353
var arr: [4]u8 = undefined;
341-
std.mem.writeInt(u32, &arr, int, .big);
354+
std.mem.writeInt(u32, &arr, int, big_endian);
342355

343356
try self.write_data(&arr);
344357
}
@@ -352,7 +365,7 @@ pub fn Pack(
352365
fn write_f64_value(self: Self, val: f64) !void {
353366
const int: u64 = @bitCast(val);
354367
var arr: [8]u8 = undefined;
355-
std.mem.writeInt(u64, &arr, int, .big);
368+
std.mem.writeInt(u64, &arr, int, big_endian);
356369

357370
try self.write_data(&arr);
358371
}
@@ -1205,7 +1218,7 @@ pub fn Pack(
12051218
if (len != 2) {
12061219
return MsGPackError.LENGTH_READING;
12071220
}
1208-
const val = std.mem.readInt(i16, &buffer, .big);
1221+
const val = std.mem.readInt(i16, &buffer, big_endian);
12091222
return val;
12101223
}
12111224

@@ -1215,7 +1228,7 @@ pub fn Pack(
12151228
if (len != 2) {
12161229
return MsGPackError.LENGTH_READING;
12171230
}
1218-
const val = std.mem.readInt(u16, &buffer, .big);
1231+
const val = std.mem.readInt(u16, &buffer, big_endian);
12191232
return val;
12201233
}
12211234

@@ -1225,7 +1238,7 @@ pub fn Pack(
12251238
if (len != 4) {
12261239
return MsGPackError.LENGTH_READING;
12271240
}
1228-
const val = std.mem.readInt(i32, &buffer, .big);
1241+
const val = std.mem.readInt(i32, &buffer, big_endian);
12291242
return val;
12301243
}
12311244

@@ -1235,7 +1248,7 @@ pub fn Pack(
12351248
if (len != 4) {
12361249
return MsGPackError.LENGTH_READING;
12371250
}
1238-
const val = std.mem.readInt(u32, &buffer, .big);
1251+
const val = std.mem.readInt(u32, &buffer, big_endian);
12391252
return val;
12401253
}
12411254

@@ -1245,7 +1258,7 @@ pub fn Pack(
12451258
if (len != 8) {
12461259
return MsGPackError.LENGTH_READING;
12471260
}
1248-
const val = std.mem.readInt(i64, &buffer, .big);
1261+
const val = std.mem.readInt(i64, &buffer, big_endian);
12491262
return val;
12501263
}
12511264

@@ -1255,7 +1268,7 @@ pub fn Pack(
12551268
if (len != 8) {
12561269
return MsGPackError.LENGTH_READING;
12571270
}
1258-
const val = std.mem.readInt(u64, &buffer, .big);
1271+
const val = std.mem.readInt(u64, &buffer, big_endian);
12591272
return val;
12601273
}
12611274

@@ -1573,7 +1586,7 @@ pub fn Pack(
15731586
if (len != 4) {
15741587
return MsGPackError.LENGTH_READING;
15751588
}
1576-
const val_int = std.mem.readInt(u32, &buffer, .big);
1589+
const val_int = std.mem.readInt(u32, &buffer, big_endian);
15771590
const val: f32 = @bitCast(val_int);
15781591
return val;
15791592
}
@@ -1584,7 +1597,7 @@ pub fn Pack(
15841597
if (len != 8) {
15851598
return MsGPackError.LENGTH_READING;
15861599
}
1587-
const val_int = std.mem.readInt(u64, &buffer, .big);
1600+
const val_int = std.mem.readInt(u64, &buffer, big_endian);
15881601
const val: f64 = @bitCast(val_int);
15891602
return val;
15901603
}

0 commit comments

Comments
 (0)