-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.zig
165 lines (143 loc) · 4.87 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
162
163
164
165
const std = @import("std");
const builtin = @import("builtin");
const Builder = std.build.Builder;
const build_path = "build";
const build_path_c = "ref/build";
const default_cflags = "-pipe -Wall -O3 -fomit-frame-pointer -march=native";
const Target = struct {
name: []const u8,
libs: ?[]const u8,
cflags: []const u8,
};
const targets = [_]Target{
Target{
.name = "k-nucleotide",
.libs = "c",
.cflags = default_cflags ++ " -std=c99 -fopenmp",
},
Target{
.name = "binary-trees",
.libs = "c",
.cflags = default_cflags,
},
Target{
.name = "fannkuch-redux",
.libs = null,
.cflags = default_cflags,
},
Target{
.name = "fasta",
.libs = null,
.cflags = default_cflags ++ " -std=c99 -fopenmp -mfpmath=sse -msse3",
},
Target{
.name = "mandelbrot",
.libs = null,
.cflags = default_cflags ++ " -fopenmp -mno-fma -fno-finite-math-only -mfpmath=sse",
},
Target{
.name = "n-body",
.libs = null,
.cflags = default_cflags ++ " -mfpmath=sse -msse3",
},
Target{
.name = "pidigits",
.libs = "c gmp",
.cflags = default_cflags,
},
Target{
.name = "reverse-complement",
.libs = "c",
.cflags = default_cflags ++ " -funroll-loops -fopenmp",
},
Target{
.name = "spectral-norm",
.libs = null,
.cflags = default_cflags ++ " -fopenmp -mfpmath=sse -msse3",
},
Target{
.name = "regex-redux",
.libs = "c pcre omp",
.cflags = default_cflags ++ " -fopenmp",
},
};
const CreateDirStep = struct {
const Step = std.build.Step;
step: Step,
builder: *Builder,
dir_path: []const u8,
allow_existing: bool,
pub fn init(builder: *Builder, dir_path: []const u8, allow_existing: bool) CreateDirStep {
return CreateDirStep{
.builder = builder,
.step = Step.init(.custom, builder.fmt("CreateDir {s}", .{dir_path}), builder.allocator, make),
.dir_path = dir_path,
.allow_existing = allow_existing,
};
}
fn make(step: *Step) !void {
const self = @fieldParentPtr(CreateDirStep, "step", step);
const full_path = self.builder.pathFromRoot(self.dir_path);
std.fs.makeDirAbsolute(full_path) catch |err| {
if (self.allow_existing and err == error.PathAlreadyExists) {
return;
}
std.debug.warn("Unable to create {s}: {s}\n", .{ full_path, @errorName(err) });
return err;
};
}
};
fn addCreateDirStep(self: *Builder, dir_path: []const u8, allow_existing: bool) *CreateDirStep {
const create_dir_step = self.allocator.create(CreateDirStep) catch unreachable;
create_dir_step.* = CreateDirStep.init(self, dir_path, allow_existing);
return create_dir_step;
}
pub fn build(b: *Builder) !void {
const create_build_dir = addCreateDirStep(b, build_path, true);
const create_build_c_dir = addCreateDirStep(b, build_path_c, true);
inline for (targets) |target| {
// Zig Target
{
const exe = b.addExecutable(target.name, "src/" ++ target.name ++ ".zig");
exe.setBuildMode(.ReleaseFast);
exe.step.dependOn(&create_build_dir.step);
exe.setOutputDir(build_path);
if (target.libs) |libs| {
var it = std.mem.split(u8, libs, " ");
while (it.next()) |lib| {
exe.linkSystemLibrary(lib);
}
}
b.default_step.dependOn(&exe.step);
}
// C Target
{
const exe = b.addExecutable(target.name, null);
var cflags = std.ArrayList([]const u8).init(b.allocator);
defer cflags.deinit();
var cflag_it = std.mem.split(u8, target.cflags, " ");
while (cflag_it.next()) |flag| {
try cflags.append(flag);
}
exe.addCSourceFile("ref/" ++ target.name ++ ".c", cflags.items);
exe.addIncludeDir("ref/include");
exe.step.dependOn(&create_build_c_dir.step);
exe.setOutputDir(build_path_c);
if (target.libs) |libs| {
var it = std.mem.split(u8, libs, " ");
while (it.next()) |lib| {
exe.linkSystemLibrary(lib);
}
} else {
// Building c files so we always link libc
exe.linkSystemLibrary("c");
}
b.default_step.dependOn(&exe.step);
}
}
const clean_build_path = b.addRemoveDirTree(build_path);
const clean_build_path_c = b.addRemoveDirTree(build_path_c);
const clean_step = b.step("clean", "Clean build directories");
clean_step.dependOn(&clean_build_path.step);
clean_step.dependOn(&clean_build_path_c.step);
}