forked from MoAlyousef/zfltk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_utils.zig
More file actions
355 lines (346 loc) · 15.7 KB
/
Copy pathbuild_utils.zig
File metadata and controls
355 lines (346 loc) · 15.7 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const std = @import("std");
const Build = std.Build;
const CompileStep = Build.Step.Compile;
pub const FinalOpts = struct {
use_wayland: bool = false,
system_jpeg: bool = false,
system_png: bool = false,
system_zlib: bool = false,
build_examples: bool = false,
use_fltk_config: bool = false,
};
pub inline fn thisDir() []const u8 {
return comptime std.fs.path.dirname(@src().file) orelse @panic("error");
}
const Example = struct {
description: ?[]const u8,
output: []const u8,
input: []const u8,
pub fn init(output: []const u8, input: []const u8, desc: ?[]const u8) Example {
return Example{
.description = desc,
.output = output,
.input = input,
};
}
};
pub const examples = &[_]Example{
Example.init("simple", "examples/simple.zig", "A simple hello world app"),
Example.init("capi", "examples/capi.zig", "Using the C-api directly"),
Example.init("customwidget", "examples/customwidget.zig", "Custom widget example"),
Example.init("image", "examples/image.zig", "Simple image example"),
Example.init("input", "examples/input.zig", "Simple input example"),
Example.init("mixed", "examples/mixed.zig", "Mixing both c and zig apis"),
Example.init("editor", "examples/editor.zig", "More complex example"),
Example.init("layout", "examples/layout.zig", "Layout example"),
Example.init("valuators", "examples/valuators.zig", "valuators example"),
Example.init("channels", "examples/channels.zig", "Use messages to handle events"),
Example.init("editormsgs", "examples/editormsgs.zig", "Use messages in the editor example"),
Example.init("browser", "examples/browser.zig", "Browser example"),
Example.init("flex", "examples/flex.zig", "Flex example"),
Example.init("threadawake", "examples/threadawake.zig", "Thread awake example"),
Example.init("handle", "examples/handle.zig", "Handle example"),
Example.init("flutterlike", "examples/flutterlike.zig", "Flutter-like example"),
Example.init("glwin", "examples/glwin.zig", "OpenGL window example"),
Example.init("tile", "examples/tile.zig", "Tile group example"),
};
pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_prefix: []const u8, opts: FinalOpts) !void {
const target = try std.zig.system.resolveTargetQuery(.{});
var buf: [1024]u8 = undefined;
const sdk_lib_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/lib", .{install_prefix});
_ = std.fs.cwd().openDir(sdk_lib_dir, .{}) catch |err| {
std.debug.print("Warning: {!}. The cfltk library will be rebuilt from source!\n", .{err});
var bin_buf: [250]u8 = undefined;
var src_buf: [250]u8 = undefined;
var inst_buf: [250]u8 = undefined;
const cmake_bin_path = try std.fmt.bufPrint(bin_buf[0..], "{s}/cfltk/bin", .{install_prefix});
const cmake_src_path = try std.fmt.bufPrint(src_buf[0..], "{s}/cfltk", .{install_prefix});
const cmake_inst_path = try std.fmt.bufPrint(inst_buf[0..], "-DCMAKE_INSTALL_PREFIX={s}/cfltk/lib", .{install_prefix});
var zfltk_config: *std.Build.Step.Run = undefined;
const which_png = switch (opts.system_png) {
false => "-DFLTK_USE_SYSTEM_LIBPNG=OFF",
true => "-DFLTK_USE_SYSTEM_LIBPNG=ON",
};
const which_jpeg = switch (opts.system_jpeg) {
false => "-DFLTK_USE_SYSTEM_LIBJPEG=OFF",
true => "-DFLTK_USE_SYSTEM_LIBJPEG=ON",
};
const which_zlib = switch (opts.system_zlib) {
false => "-DFLTK_USE_SYSTEM_ZLIB=OFF",
true => "-DFLTK_USE_SYSTEM_ZLIB=ON",
};
if (target.os.tag == .windows) {
zfltk_config = b.addSystemCommand(&[_][]const u8{
"cmake",
"-B",
cmake_bin_path,
"-S",
cmake_src_path,
"-GNinja",
"-DCMAKE_BUILD_TYPE=Release",
cmake_inst_path,
"-DFLTK_BUILD_TEST=OFF",
which_png,
which_jpeg,
which_zlib,
"-DFLTK_BUILD_GL=ON",
"-DCFLTK_USE_OPENGL=ON",
"-DFLTK_BUILD_FLUID=OFF",
"-DFLTK_BUILD_FLTK_OPTIONS=OFF",
});
} else if (target.isDarwin()) {
zfltk_config = b.addSystemCommand(&[_][]const u8{
"cmake",
"-B",
cmake_bin_path,
"-S",
cmake_src_path,
"-DCMAKE_BUILD_TYPE=Release",
cmake_inst_path,
"-DFLTK_BUILD_TEST=OFF",
which_png,
which_jpeg,
which_zlib,
"-DFLTK_BUILD_GL=ON",
"-DCFLTK_USE_OPENGL=ON",
"-DFLTK_BUILD_FLUID=OFF",
"-DFLTK_BUILD_FLTK_OPTIONS=OFF",
});
} else {
if (opts.use_wayland) {
zfltk_config = b.addSystemCommand(&[_][]const u8{
"cmake",
"-B",
cmake_bin_path,
"-S",
cmake_src_path,
"-DCMAKE_BUILD_TYPE=Release",
cmake_inst_path,
"-DFLTK_BUILD_TEST=OFF",
which_png,
which_jpeg,
which_zlib,
"-DFLTK_BUILD_GL=ON",
"-DCFLTK_USE_OPENGL=ON",
"-DFLTK_BACKEND_WAYLAND=ON",
"-DFLTK_BUILD_FLUID=OFF",
"-DFLTK_BUILD_FLTK_OPTIONS=OFF",
"-DFLTK_USE_LIBDECOR_GTK=OFF",
});
} else {
zfltk_config = b.addSystemCommand(&[_][]const u8{
"cmake",
"-B",
cmake_bin_path,
"-S",
cmake_src_path,
"-DCMAKE_BUILD_TYPE=Release",
cmake_inst_path,
"-DFLTK_BUILD_TEST=OFF",
which_png,
which_jpeg,
which_zlib,
"-DFLTK_USE_PANGO=ON", // enable if rtl/cjk font support is needed
"-DFLTK_BUILD_GL=ON",
"-DCFLTK_USE_OPENGL=ON",
"-DFLTK_BACKEND_WAYLAND=OFF",
"-DFLTK_GRAPHICS_CAIRO=ON",
"-DFLTK_BUILD_FLUID=OFF",
"-DFLTK_BUILD_FLTK_OPTIONS=OFF",
});
}
}
zfltk_config.setCwd(b.path("zig-out/cfltk"));
_ = std.fs.cwd().openDir(cmake_src_path, .{}) catch |git_err| {
std.debug.print("Warning: {!}. The cfltk library will be grabbed!\n", .{git_err});
const cfltk_fetch = b.addSystemCommand(&[_][]const u8{ "git", "clone", "https://github.com/MoAlyousef/cfltk", cmake_src_path, "--depth=1", "--recurse-submodules" });
zfltk_config.step.dependOn(&cfltk_fetch.step);
};
const cpu_count = try std.Thread.getCpuCount();
const jobs = try std.fmt.allocPrint(b.allocator, "{d}", .{cpu_count});
defer b.allocator.free(jobs);
const zfltk_build = b.addSystemCommand(&[_][]const u8{
"cmake",
"--build",
cmake_bin_path,
"--config",
"Release",
"--parallel",
jobs,
});
zfltk_build.setCwd(b.path("zig-out/cfltk"));
zfltk_build.step.dependOn(&zfltk_config.step);
// This only needs to run once!
const zfltk_install = b.addSystemCommand(&[_][]const u8{
"cmake",
"--install",
cmake_bin_path,
});
zfltk_install.setCwd(b.path("zig-out/cfltk"));
zfltk_install.step.dependOn(&zfltk_build.step);
finalize_cfltk.dependOn(&zfltk_install.step);
};
}
pub fn cfltk_link(exe: *CompileStep, install_prefix: []const u8, opts: FinalOpts) !void {
var buf: [1024]u8 = undefined;
const target = exe.root_module.resolved_target.?.result;
const inc_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/include", .{install_prefix});
exe.addIncludePath(.{ .cwd_relative = inc_dir });
const lib_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/lib/lib", .{install_prefix});
exe.addLibraryPath(.{ .cwd_relative = lib_dir });
exe.linkSystemLibrary("cfltk");
exe.linkSystemLibrary("fltk");
exe.linkSystemLibrary("fltk_images");
if (opts.system_png) {
exe.linkSystemLibrary("png");
} else {
exe.linkSystemLibrary("fltk_png");
}
if (opts.system_jpeg) {
exe.linkSystemLibrary("jpeg");
} else {
exe.linkSystemLibrary("fltk_jpeg");
}
if (opts.system_zlib) {
exe.linkSystemLibrary("z");
} else {
exe.linkSystemLibrary("fltk_z");
}
exe.linkSystemLibrary("fltk_gl");
exe.linkLibC();
exe.linkLibCpp();
if (target.os.tag == .windows) {
exe.linkSystemLibrary("ws2_32");
exe.linkSystemLibrary("comctl32");
exe.linkSystemLibrary("gdi32");
exe.linkSystemLibrary("oleaut32");
exe.linkSystemLibrary("ole32");
exe.linkSystemLibrary("uuid");
exe.linkSystemLibrary("shell32");
exe.linkSystemLibrary("advapi32");
exe.linkSystemLibrary("comdlg32");
exe.linkSystemLibrary("winspool");
exe.linkSystemLibrary("user32");
exe.linkSystemLibrary("kernel32");
exe.linkSystemLibrary("odbc32");
exe.linkSystemLibrary("gdiplus");
exe.linkSystemLibrary("opengl32");
exe.linkSystemLibrary("glu32");
} else if (target.isDarwin()) {
exe.linkFramework("Carbon");
exe.linkFramework("Cocoa");
exe.linkFramework("ApplicationServices");
exe.linkFramework("OpenGL");
exe.linkFramework("UniformTypeIdentifiers");
} else {
if (opts.use_wayland) {
exe.linkSystemLibrary("wayland-client");
exe.linkSystemLibrary("wayland-cursor");
exe.linkSystemLibrary("xkbcommon");
exe.linkSystemLibrary("dbus-1");
exe.linkSystemLibrary("EGL");
exe.linkSystemLibrary("wayland-egl");
}
exe.linkSystemLibrary("GL");
exe.linkSystemLibrary("GLU");
exe.linkSystemLibrary("pthread");
exe.linkSystemLibrary("X11");
exe.linkSystemLibrary("Xext");
exe.linkSystemLibrary("Xinerama");
exe.linkSystemLibrary("Xcursor");
exe.linkSystemLibrary("Xrender");
exe.linkSystemLibrary("Xfixes");
exe.linkSystemLibrary("Xft");
exe.linkSystemLibrary("fontconfig");
exe.linkSystemLibrary("pango-1.0");
exe.linkSystemLibrary("pangoxft-1.0");
exe.linkSystemLibrary("gobject-2.0");
exe.linkSystemLibrary("cairo");
exe.linkSystemLibrary("pangocairo-1.0");
}
}
pub fn link_using_fltk_config(b: *Build, exe: *CompileStep, finalize_cfltk: *Build.Step, install_prefix: []const u8) !void {
const target = exe.root_module.resolved_target.?.result;
exe.linkLibC();
exe.linkLibCpp();
var buf: [1024]u8 = undefined;
const inc_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/include", .{install_prefix});
exe.addIncludePath(b.path(inc_dir));
const cmake_src_path = try std.fmt.allocPrint(b.allocator, "{s}/cfltk", .{install_prefix});
_ = std.fs.cwd().openDir(cmake_src_path, .{}) catch |git_err| {
std.debug.print("Warning: {!}. The cfltk library will be grabbed!\n", .{git_err});
const cfltk_fetch = b.addSystemCommand(&[_][]const u8{ "git", "clone", "https://github.com/MoAlyousef/cfltk", cmake_src_path, "--depth=1" });
finalize_cfltk.dependOn(&cfltk_fetch.step);
};
var lib = b.addStaticLibrary(.{
.name = "cfltk",
.target = exe.root_module.resolved_target.?,
.optimize = exe.root_module.optimize.?,
.use_llvm = false,
});
const proc = try std.process.Child.run(.{
.allocator = b.allocator,
.argv = &[_][]const u8{ "fltk-config", "--use-images", "--use-gl", "--cflags" },
});
const out = proc.stdout;
var cflags = std.ArrayList([]const u8).init(b.allocator);
var it = std.mem.tokenizeAny(u8, out, " ");
while (it.next()) |x| {
try cflags.append(x);
}
cflags.clearAndFree(); // why does the above not work?!
try cflags.append("-I/usr/local/include");
try cflags.append(try std.fmt.allocPrint(b.allocator, "-I{s}", .{inc_dir}));
try cflags.append("-DCFLTK_USE_GL");
lib.addCSourceFiles(.{ .files = &[_][]const u8{
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_new.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_lock.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_window.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_button.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_widget.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_group.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_text.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_box.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_input.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_menu.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_dialog.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_valuator.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_browser.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_misc.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_image.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_draw.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_table.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_tree.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_surface.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_font.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_utils.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_printer.cpp", .{install_prefix}),
}, .flags = cflags.items });
if (target.isDarwin()) {
lib.addCSourceFile(.{ .file = b.path(try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_nswindow.m", .{install_prefix})), .flags = cflags.items });
}
const proc2 = try std.process.Child.run(.{
.allocator = b.allocator,
.argv = &[_][]const u8{ "fltk-config", "--use-images", "--use-gl", "--ldflags" },
});
const out2 = proc2.stdout;
var lflags = std.ArrayList([]const u8).init(b.allocator);
var Lflags = std.ArrayList([]const u8).init(b.allocator);
var it2 = std.mem.tokenizeAny(u8, out2, " ");
while (it2.next()) |x| {
if (std.mem.startsWith(u8, x, "-l") and !std.mem.startsWith(u8, x, "-ldl")) try lflags.append(x[2..]);
if (std.mem.startsWith(u8, x, "-L")) try Lflags.append(x[2..]);
}
for (Lflags.items) |f| {
lib.addLibraryPath(b.path(f));
}
for (lflags.items) |f| {
lib.linkSystemLibrary(f);
}
lib.linkLibC();
lib.linkLibCpp();
lib.step.dependOn(finalize_cfltk);
exe.step.dependOn(&lib.step);
exe.linkLibrary(lib);
}