-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
153 lines (127 loc) · 4.64 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
const std = @import("std");
const Date = @import("src/date.zig");
pub fn build(b: *std.Build) !void {
try writeVersion(b.allocator);
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const raylib_dep = b.dependency("raylib-zig", .{
.target = target,
.optimize = optimize,
});
const raylib = raylib_dep.module("raylib");
const raygui = raylib_dep.module("raygui");
const raylib_artifact = raylib_dep.artifact("raylib");
raylib_artifact.defineCMacro("SUPPORT_FILEFORMAT_JPG", null);
//includeHeader(&raylib_artifact.root_module, "src/memory.h");
const exe = b.addExecutable(.{
.name = "projectboat",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
if (target.result.os.tag == .windows) switch (optimize) {
.Debug => exe.subsystem = .Console,
else => exe.subsystem = .Windows
};
exe.linkLibrary(raylib_artifact);
exe.root_module.addImport("raylib", raylib);
exe.root_module.addImport("raygui", raygui);
b.installArtifact(exe);
const configTool = b.addExecutable(.{
.name = "configTool",
.root_source_file = b.path("src/configTool.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(configTool);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const config_cmd = b.addRunArtifact(configTool);
config_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
config_cmd.addArgs(args);
}
const run_config = b.step("config", "Add ConVars to config file");
run_config.dependOn(&config_cmd.step);
}
fn gitHash(allocator: std.mem.Allocator, buffer: *[7]u8) !void {
//Find current branch(?)
const head = try std.fs.cwd().openFile(".git/HEAD", .{});
defer head.close();
//Read data from HEAD
const stat = try head.stat();
const data = try head.readToEndAlloc(allocator, stat.size);
defer allocator.free(data);
//Get path to current HEAD's ref Just like me fr
const index = std.mem.indexOf(u8, data, " ") orelse return error.no_space_in_head;
const ref_path = try std.fs.path.join(
allocator,
&.{ ".git/", data[index + 1 .. data.len - 1] },
);
defer allocator.free(ref_path);
_ = std.mem.replace(
u8,
ref_path,
std.fs.path.sep_str_posix,
std.fs.path.sep_str,
ref_path,
);
const ref = try std.fs.cwd().openFile(ref_path, .{});
defer ref.close();
//Read git hash and copy it to the buffer
const ref_stat = try ref.stat();
const ref_data = try ref.readToEndAlloc(allocator, ref_stat.size);
@memcpy(buffer, ref_data[0..7]);
}
fn writeVersion(allocator: std.mem.Allocator) !void {
var cwd = std.fs.cwd();
cwd.deleteFile("src/version") catch {};
var timestamp = Date.now();
const formatted = try timestamp.format(allocator);
defer allocator.free(formatted);
var buffer: [7]u8 = undefined;
try gitHash(allocator, &buffer);
const file = try cwd.createFile("src/version", .{});
defer file.close();
try file.writeAll(formatted);
try file.writeAll("-");
try file.writeAll(&buffer);
}
fn includeHeader(m: *std.Build.Module, sub_path: []const u8) void {
const b = m.owner;
const path = std.fs.cwd().realpathAlloc(b.allocator, sub_path) catch @panic("OOM");
const string = b.fmt("-include{s}", .{path});
for (m.link_objects.items) |lobj| {
switch (lobj) {
.c_source_file => updateFlags(
std.Build.Module.CSourceFile,
b,
lobj.c_source_file,
string,
),
.c_source_files => updateFlags(
std.Build.Module.CSourceFiles,
b,
lobj.c_source_files,
string,
),
else => {},
}
}
}
fn updateFlags(T: type, b: *std.Build, c_source_file: *T, path: []u8) void {
if (T != std.Build.Module.CSourceFile and T != std.Build.Module.CSourceFiles) {
@compileError("Needs to be CSourceFile or CSourceFiles");
}
const new_flags = b.allocator.alloc([]u8, c_source_file.flags.len + 1) catch @panic("OOM");
for (new_flags[0..c_source_file.flags.len], c_source_file.flags) |*new_flag, old_flag| {
new_flag.* = b.dupe(old_flag);
}
new_flags[new_flags.len - 1] = path;
c_source_file.flags = new_flags;
}