-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
173 lines (147 loc) · 5.22 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
166
167
168
169
170
171
172
173
const std = @import ("std");
const toolbox = @import ("toolbox");
fn update (builder: *std.Build,
dependencies: *const toolbox.Dependencies) !void
{
const glfw_path = try builder.build_root.join (builder.allocator,
&.{ "glfw", });
std.fs.deleteTreeAbsolute (glfw_path) catch |err|
{
switch (err)
{
error.FileNotFound => {},
else => return err,
}
};
try dependencies.clone (builder, "glfw", glfw_path);
var glfw_dir = try std.fs.openDirAbsolute (glfw_path, .{ .iterate = true, });
defer glfw_dir.close ();
var it = glfw_dir.iterate ();
while (try it.next ()) |*entry|
{
if (!std.mem.eql (u8, entry.name, "src") and
!std.mem.eql (u8, entry.name, "include"))
try std.fs.deleteTreeAbsolute (try std.fs.path.join (
builder.allocator, &.{ glfw_path, entry.name, }));
}
try toolbox.clean (builder, &.{ "glfw", }, &.{});
}
pub fn build (builder: *std.Build) !void
{
const target = builder.standardTargetOptions (.{});
const optimize = builder.standardOptimizeOption (.{});
const dependencies = try toolbox.Dependencies.init (builder, "glfw.zig",
&.{ "glfw", },
.{
.toolbox = .{
.name = "tiawl/toolbox",
.host = toolbox.Repository.Host.github,
.ref = toolbox.Repository.Reference.tag,
},
.vulkan = .{
.name = "tiawl/vulkan.zig",
.host = toolbox.Repository.Host.github,
.ref = toolbox.Repository.Reference.tag,
},
.wayland = .{
.name = "tiawl/wayland.zig",
.host = toolbox.Repository.Host.github,
.ref = toolbox.Repository.Reference.tag,
},
.X11 = .{
.name = "tiawl/X11.zig",
.host = toolbox.Repository.Host.github,
.ref = toolbox.Repository.Reference.tag,
},
}, .{
.glfw = .{
.name = "glfw/glfw",
.host = toolbox.Repository.Host.github,
.ref = toolbox.Repository.Reference.tag,
},
});
if (builder.option (bool, "update", "Update binding") orelse false)
try update (builder, &dependencies);
const lib = builder.addStaticLibrary (.{
.name = "glfw",
.root_source_file = builder.addWriteFiles ().add ("empty.c", ""),
.target = target,
.optimize = optimize,
});
lib.linkLibC ();
var root_dir = try builder.build_root.handle.openDir (".",
.{ .iterate = true, });
defer root_dir.close ();
var walk = try root_dir.walk (builder.allocator);
while (try walk.next ()) |*entry|
{
if (std.mem.startsWith (u8, entry.path, "glfw") and
entry.kind == .directory) toolbox.addInclude (lib, entry.path);
}
toolbox.addHeader (lib, try builder.build_root.join (builder.allocator,
&.{ "glfw", "include", "GLFW", }), "GLFW", &.{ ".h", });
const vulkan_dep = builder.dependency ("vulkan", .{
.target = target,
.optimize = optimize,
});
lib.installLibraryHeaders (vulkan_dep.artifact ("vulkan"));
const src_path = try builder.build_root.join (builder.allocator,
&.{ "glfw", "src", });
var src_dir = try std.fs.openDirAbsolute (src_path, .{ .iterate = true, });
defer src_dir.close ();
switch (target.result.os.tag)
{
.windows => {
lib.linkSystemLibrary ("gdi32");
lib.linkSystemLibrary ("user32");
lib.linkSystemLibrary ("shell32");
const flags = [_][] const u8 { "-D_GLFW_WIN32", "-Isrc", };
var it = src_dir.iterate ();
while (try it.next ()) |*entry|
{
if ((!std.mem.startsWith (u8, entry.name, "linux_") and
!std.mem.startsWith (u8, entry.name, "posix_") and
!std.mem.startsWith (u8, entry.name, "xkb_") and
!std.mem.startsWith (u8, entry.name, "glx_") and
!std.mem.startsWith (u8, entry.name, "x11_") and
!std.mem.startsWith (u8, entry.name, "cocoa_") and
!std.mem.startsWith (u8, entry.name, "nsgl_") and
!std.mem.startsWith (u8, entry.name, "wl_")) and
toolbox.isCSource (entry.name) and entry.kind == .file)
try toolbox.addSource (lib, src_path, entry.name, &flags);
}
},
.macos => return error.MacOSUnsupported,
else => {
const X11_dep = builder.dependency ("X11", .{
.target = target,
.optimize = optimize,
});
const wayland_dep = builder.dependency ("wayland", .{
.target = target,
.optimize = optimize,
});
lib.linkLibrary (X11_dep.artifact ("X11"));
lib.linkLibrary (wayland_dep.artifact ("wayland"));
lib.installLibraryHeaders (X11_dep.artifact ("X11"));
lib.installLibraryHeaders (wayland_dep.artifact ("wayland"));
const flags = [_][] const u8
{
"-D_GLFW_X11", "-D_GLFW_WAYLAND",
"-Wno-implicit-function-declaration", "-Isrc",
};
var it = src_dir.iterate ();
while (try it.next ()) |*entry|
{
if ((!std.mem.startsWith (u8, entry.name, "wgl_") and
!std.mem.startsWith (u8, entry.name, "win32_") and
!std.mem.startsWith (u8, entry.name, "cocoa_") and
!std.mem.startsWith (u8, entry.name, "nsgl_")) and
toolbox.isCSource (entry.name) and entry.kind == .file)
try toolbox.addSource (lib, src_path, entry.name, &flags);
}
lib.root_module.addCMacro ("WL_MARSHAL_FLAG_DESTROY", "1");
},
}
builder.installArtifact (lib);
}