Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
129 lines (114 loc) · 4.43 KB
/
Copy pathbuild.zig
File metadata and controls
129 lines (114 loc) · 4.43 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
const std = @import("std");
const builtin = @import("builtin");
const TargetQuery = std.Target.Query;
const release_targets = [_]TargetQuery{
.{
.cpu_arch = .x86_64,
.os_tag = .linux,
},
.{
.cpu_arch = .x86_64,
.os_tag = .windows,
},
.{
.cpu_arch = .x86_64,
.os_tag = .macos,
},
.{
.cpu_arch = .aarch64,
.os_tag = .macos,
},
};
const FileExtension = enum {
zip,
@"tar.gz",
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "love",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{},
}),
});
b.installArtifact(exe);
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
// Zig 0.17+ replaced b.args with addPassthruArgs().
// Zig 0.16 still uses the b.args field.
if (comptime builtin.zig_version.minor >= 17) {
run_cmd.addPassthruArgs();
} else {
if (b.args) |args| {
run_cmd.addArgs(args);
}
}
const exe_tests = b.addTest(.{
.root_module = exe.root_module,
});
const run_exe_tests = b.addRunArtifact(exe_tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_exe_tests.step);
{ // zig build release
const release_step = b.step("release", "Build release binaries");
const install_dir: std.Build.InstallDir = .{ .custom = "compressed" };
for (release_targets) |release_target| {
const resolved_target = b.resolveTargetQuery(release_target);
const exe_release = b.addExecutable(.{
.name = "love",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = resolved_target,
.optimize = .ReleaseSmall,
.imports = &.{},
}),
});
const is_windows = release_target.os_tag == .windows;
const exe_name = b.fmt("{s}{s}", .{ exe.name, resolved_target.result.exeFileExt() });
// An array has been created for possible extensions to new types of archives.
const extensions: []const FileExtension = if (is_windows) &.{.zip} else &.{.@"tar.gz"};
var file_path: std.Build.LazyPath = undefined;
for (extensions) |extension| {
// archive file name
const file_name = b.fmt("love-{t}-{t}.{t}", .{
resolved_target.result.cpu.arch,
resolved_target.result.os.tag,
extension,
});
// creating a command inside the project build step
const compress_cmd = std.Build.Step.Run.create(b, "compress artifact");
// init command properties
compress_cmd.clearEnvironment();
compress_cmd.step.max_rss = 16 * 1024 * 1024; // 16 MiB
// Building the compress command line
switch (extension) {
.zip => {
compress_cmd.addArgs(&.{ "7z", "a", "-mx=9" });
file_path = compress_cmd.addOutputFileArg(file_name);
compress_cmd.addArtifactArg(exe_release);
},
.@"tar.gz" => {
compress_cmd.addArgs(&.{ "tar", "caf" });
file_path = compress_cmd.addOutputFileArg(file_name);
compress_cmd.addPrefixedDirectoryArg("-C", exe_release.getEmittedBinDirectory());
compress_cmd.addArg(exe_name);
compress_cmd.addArgs(&.{
"--sort=name",
"--numeric-owner",
"--owner=0",
"--group=0",
});
},
}
// The artifact install to zig-out/compressed directory
const install_compressed = b.addInstallFileWithDir(file_path, install_dir, file_name);
release_step.dependOn(&install_compressed.step);
}
}
}
}