forked from branc116/brplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
133 lines (117 loc) · 5.26 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
const std = @import("std");
const rlplot_flags = &[_][]const u8{
"-DZIG",
"-std=gnu99",
"-D_GNU_SOURCE",
"-DGL_SILENCE_DEPRECATION=199309L",
"-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/189
};
fn rlf(comptime name: []const u8) []const u8 {
return "raylib/src/" ++ name;
}
pub fn addRayLib(b: *std.build.Builder, target: std.zig.CrossTarget, opt: std.builtin.Mode) *std.Build.CompileStep {
const raylib = b.addStaticLibrary(.{ .name = "raylib", .optimize = opt, .target = target });
raylib.linkLibC();
const sources = .{ rlf("rcore.c"), rlf("rtext.c"), rlf("rmodels.c"), rlf("rtextures.c"), rlf("utils.c"), rlf("rshapes.c") };
raylib.addCSourceFiles(&sources, rlplot_flags);
switch (target.getOsTag()) {
.linux => {
raylib.defineCMacro("PLATFORM_DESKTOP", null);
raylib.linkSystemLibrary("glfw");
},
.windows => {
raylib.defineCMacro("PLATFORM_DESKTOP", null);
raylib.defineCMacro("_WIN32", "1");
raylib.addIncludePath(.{ .path = "./raylib/src/external/glfw/include" });
raylib.addCSourceFile(.{ .file = .{ .path = rlf("/rglfw.c") }, .flags = rlplot_flags });
raylib.linkSystemLibrary("winmm");
raylib.linkSystemLibrary("gdi32");
raylib.linkSystemLibrary("opengl32");
raylib.addIncludePath(.{ .path = "./raylib/src/external/glfw/deps/mingw" });
},
.wasi => {
//-DGRAPHICS_API_OPENGL_ES2 -DPLATFORM_WEB --memory-init-file 1 --closure 1 -s WASM_BIGINT -s ENVIRONMENT=web -sALLOW_MEMORY_GROWTH -s USE_GLFW=3 -s ASYNCIFY $(CCFLAGS)
raylib.defineCMacro("PLATFORM_WEB", null);
raylib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
},
else => {
@panic("Unsupported OS");
},
}
return raylib;
}
const print = std.debug.print;
fn one_shader(f: std.fs.File, comptime name: []const u8, comptime macroName: []const u8) !void {
const file = @embedFile(name);
_ = try f.write("\n#define " ++ macroName ++ "\\\n \"");
for (file) |v| {
if (v == '\n') {
_ = try f.write("\\n\" \\\n \""); // `\n"<newline> "`
} else {
_ = try f.write(&[1]u8{v});
}
}
_ = try f.write("\"\n");
}
fn genShaderhFile() !void {
const shader_file_name = "src/shaders.h";
std.fs.cwd().deleteFile(shader_file_name) catch |e| print("{}", .{e});
const file = try std.fs.cwd().createFile(shader_file_name, .{ .read = true });
_ = try one_shader(file, "src/desktop/shaders/grid.fs", "SHADER_GRID_FS");
_ = try one_shader(file, "src/desktop/shaders/line.fs", "SHADER_LINE_FS");
_ = try one_shader(file, "src/desktop/shaders/line.vs", "SHADER_LINE_VS");
_ = try one_shader(file, "src/desktop/shaders/quad.fs", "SHADER_QUAD_FS");
_ = try one_shader(file, "src/desktop/shaders/quad.vs", "SHADER_QUAD_VS");
}
// This has been tested to work with zig master branch as of commit 87de821 or May 14 2023
pub fn addRlPlot(b: *std.build.Builder, opt: std.builtin.OptimizeMode, target: std.zig.CrossTarget) !*std.Build.CompileStep {
const rlplot = b.addExecutable(.{ .name = "rlplot", .optimize = opt, .target = target });
const raylib = addRayLib(b, target, opt);
rlplot.linkLibrary(raylib);
rlplot.linkLibC();
rlplot.addIncludePath(.{ .path = "raylib/src" });
rlplot.addCSourceFiles(&.{ "src/graph.c", "src/main.c", "src/points_group.c", "src/read_input.c", "src/smol_mesh.c", "src/q.c", "src/resampling.c", "src/ui.c", "src/help.c" }, rlplot_flags);
rlplot.addObjectFile(.{ .path = "src/print_stacktrace.zig" });
if (opt != .Debug) {
try genShaderhFile();
rlplot.defineCMacro("RELEASE", null);
rlplot.want_lto = true;
raylib.want_lto = true;
} else {
rlplot.addCSourceFile(.{ .file = .{ .path = "src/desktop/linux/refresh_shaders.c" }, .flags = rlplot_flags });
}
switch (target.getOsTag()) {
.linux => {
rlplot.defineCMacro("PLATFORM_DESKTOP", null);
rlplot.addCSourceFile(.{ .file = .{ .path = "./src/desktop/linux/read_input.c" }, .flags = rlplot_flags });
},
.windows => {
rlplot.defineCMacro("PLATFORM_DESKTOP", null);
rlplot.defineCMacro("_WIN32", "1");
rlplot.addCSourceFile(.{ .file = .{ .path = "./src/desktop/win/read_input.c" }, .flags = rlplot_flags });
},
.wasi => {
rlplot.defineCMacro("PLATFORM_WEB", null);
rlplot.defineCMacroRaw("sUSE_GLFW=3");
},
else => {
@panic("Unsupported OS");
},
}
return rlplot;
}
pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const lib = try addRlPlot(b, optimize, target);
b.installArtifact(lib);
}
const srcdir = struct {
fn getSrcDir() []const u8 {
return std.fs.path.dirname(@src().file).?;
}
}.getSrcDir();