-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.zig
161 lines (147 loc) · 4.23 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const std = @import("std");
pub fn build(b: *std.Build) void {
if (comptime !checkVersion())
@compileError("Please! Update zig toolchain >= 0.11!");
const target = b.standardTargetOptions(.{
.whitelist = permissive_targets,
.default_target = .{
.cpu_arch = .x86_64,
.os_tag = .windows,
.abi = .gnu,
},
});
const optimize = b.standardOptimizeOption(.{});
const shared = b.option(bool, "Shared", "Build Winpthreads Shared Library [default: false]") orelse false;
const tests = b.option(bool, "Tests", "Build Tests [default: false]") orelse false;
const lib = if (shared)
b.addSharedLibrary(.{
.name = "winpthreads",
.optimize = optimize,
.target = target,
.version = .{
.major = 10,
.minor = 0,
.patch = 0,
},
})
else
b.addStaticLibrary(.{
.name = "winpthreads",
.optimize = optimize,
.target = target,
});
lib.want_lto = false;
lib.root_module.sanitize_c = false;
if (optimize == .Debug or optimize == .ReleaseSafe)
lib.bundle_compiler_rt = true
else
lib.root_module.strip = true;
lib.addCSourceFiles(.{ .files = src, .flags = &.{
"-Wall",
"-Wextra",
} });
lib.defineCMacro("__USE_MINGW_ANSI_STDIO", "1");
lib.addIncludePath(b.path("include"));
lib.addIncludePath(b.path("src"));
lib.linkLibC();
b.installArtifact(lib);
lib.installHeadersDirectory(b.path("include"), "", .{});
if (tests) {
buildExe(b, lib, .{
.name = "test_nanosleep",
.file = "tests/t_nanosleep.c",
});
buildExe(b, lib, .{
.name = "test_clock_gettime",
.file = "tests/t_clock_gettime.c",
});
buildExe(b, lib, .{
.name = "test_clock_getres",
.file = "tests/t_clock_getres.c",
});
}
}
fn buildExe(b: *std.Build, pthread: *std.Build.Step.Compile, binfo: BuildInfo) void {
const target = pthread.root_module.resolved_target.?;
const optimize = pthread.root_module.optimize.?;
const exe = b.addExecutable(.{
.name = binfo.name,
.target = target,
.optimize = optimize,
});
if (optimize != .Debug)
exe.root_module.strip = true;
if (target.result.os.tag == .windows)
exe.want_lto = false;
exe.addIncludePath(b.path("include"));
exe.addIncludePath(b.path("src"));
exe.linkLibrary(pthread);
exe.addCSourceFile(.{
.file = b.path(binfo.file),
.flags = &.{
"-Wall",
"-Wextra",
"-Wpedantic",
},
});
exe.linkLibC();
if (!std.mem.startsWith(u8, binfo.name, "test"))
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step(binfo.name, b.fmt("Run the {s}", .{binfo.name}));
run_step.dependOn(&run_cmd.step);
}
const BuildInfo = struct {
name: []const u8,
file: []const u8,
};
fn checkVersion() bool {
const builtin = @import("builtin");
if (!@hasDecl(builtin, "zig_version")) {
return false;
}
const needed_version = std.SemanticVersion.parse("0.11.0") catch unreachable;
const version = builtin.zig_version;
const order = version.order(needed_version);
return order != .lt;
}
const src: []const []const u8 = &.{
"src/nanosleep.c",
"src/cond.c",
"src/barrier.c",
"src/misc.c",
"src/clock.c",
"src/libgcc/dll_math.c",
"src/spinlock.c",
"src/thread.c",
"src/mutex.c",
"src/sem.c",
"src/sched.c",
"src/ref.c",
"src/rwlock.c",
};
const permissive_targets: []const std.Target.Query = &.{
.{}, // native
.{
.cpu_arch = .aarch64,
.cpu_model = .baseline,
.os_tag = .windows,
.abi = .gnu,
},
.{
.cpu_arch = .x86,
.cpu_model = .baseline,
.os_tag = .windows,
.abi = .gnu,
},
.{
.cpu_arch = .x86_64,
.cpu_model = .baseline,
.os_tag = .windows,
.abi = .gnu,
},
};