-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuild.zig
104 lines (87 loc) · 3.49 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
const std = @import("std");
const builtin = @import("builtin");
/// Windows only: use the LIBRARY_PATH environment variable to add library paths
/// TODO: is there an actual environment variable that works?
fn addLibraryPathEnvVar(exe: *std.Build.Step.Compile, alloc: std.mem.Allocator) !void {
// This is ugly because of utf16...
const env_name = try std.unicode.utf8ToUtf16LeAllocZ(alloc, "LIBRARY_PATH");
defer alloc.free(env_name);
if (std.process.getenvW(env_name)) |env| {
var it = std.mem.tokenize(u16, env, &[_]u16{';'});
while (it.next()) |path| {
const path_u8 = try std.unicode.utf16LeToUtf8Alloc(alloc, path);
defer alloc.free(path_u8);
exe.addLibraryPath(.{ .cwd_relative = path_u8 });
}
}
}
// Call that from your own build.zig as a helper!
pub fn link(exe: *std.Build.Step.Compile) void {
exe.linkLibC();
if (builtin.os.tag == .windows) {
const alloc = std.heap.page_allocator;
// So that the library paths are available
addLibraryPathEnvVar(exe, alloc) catch @panic("Failed to use allocator in addLibraryPathEnvVar");
}
exe.linkSystemLibrary("csfml-graphics");
exe.linkSystemLibrary("csfml-system");
exe.linkSystemLibrary("csfml-window");
exe.linkSystemLibrary("csfml-audio");
exe.linkSystemLibrary("csfml-network");
}
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const mode = b.standardOptimizeOption(.{});
// Register modules
const module = b.addModule("sfml", .{
.root_source_file = b.path("src/root.zig"),
});
// Register test runner
const test_runner = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = mode,
});
link(test_runner);
//Build step to generate docs:
const docs = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = mode,
});
link(docs);
docs.root_module.addImport("sfml", module);
const emitted_docs = docs.getEmittedDocs();
const docs_step = b.step("docs", "Generate docs");
docs_step.dependOn(&b.addInstallDirectory(.{
.source_dir = emitted_docs,
.install_dir = .prefix,
.install_subdir = "docs",
}).step);
const all_step = b.step("all", "Compile all examples");
// Register the examples
example(b, module, target, mode, all_step, "sfml_example");
example(b, module, target, mode, all_step, "green_circle");
example(b, module, target, mode, all_step, "heat_haze");
// Register the test step
const test_step = b.step("test", "Runs the test suite.");
var run = b.addRunArtifact(test_runner);
test_step.dependOn(&run.step);
}
fn example(b: *std.Build, module: *std.Build.Module, target: anytype, mode: anytype, all: *std.Build.Step, comptime name: []const u8) void {
const exe = b.addExecutable(.{
.name = name,
.root_source_file = b.path("src/examples/" ++ name ++ ".zig"),
.target = target,
.optimize = mode,
});
link(exe);
exe.root_module.addImport("sfml", module);
const install = b.addInstallArtifact(exe, .{});
const install_step = b.step(name, "Get the compiled " ++ name ++ " example in the bin folder");
install_step.dependOn(&install.step);
all.dependOn(&install.step);
const run = b.addRunArtifact(exe);
const run_step = b.step("run-" ++ name, "Run the " ++ name ++ " test");
run_step.dependOn(&run.step);
}