-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
145 lines (124 loc) · 4.91 KB
/
Copy pathbuild.zig
File metadata and controls
145 lines (124 loc) · 4.91 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
const std = @import("std");
const package_version = readPackageVersion(@embedFile("build.zig.zon"));
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const release_version = b.option([]const u8, "release-version", "Version label for release artifacts") orelse package_version;
const lib = b.addModule("aether", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
});
const exe = b.addExecutable(.{
.name = "aether",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "aether", .module = lib },
},
}),
});
b.installArtifact(exe);
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 lib_tests = b.addTest(.{
.root_module = lib,
});
const run_lib_tests = b.addRunArtifact(lib_tests);
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_lib_tests.step);
test_step.dependOn(&run_exe_tests.step);
const release_step = b.step("release", "Build release bundles and archives");
addReleaseBundle(
b,
release_step,
release_version,
"macos-aarch64",
.{ .cpu_arch = .aarch64, .os_tag = .macos },
);
addReleaseBundle(
b,
release_step,
release_version,
"macos-amd64",
.{ .cpu_arch = .x86_64, .os_tag = .macos },
);
addReleaseBundle(
b,
release_step,
release_version,
"linux-amd64",
.{ .cpu_arch = .x86_64, .os_tag = .linux },
);
}
fn addReleaseBundle(
b: *std.Build,
release_step: *std.Build.Step,
version: []const u8,
target_label: []const u8,
target_query: std.Target.Query,
) void {
const resolved_target = b.resolveTargetQuery(target_query);
const lib = b.addModule("aether", .{
.root_source_file = b.path("src/root.zig"),
.target = resolved_target,
});
const exe = b.addExecutable(.{
.name = "aether",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = resolved_target,
.optimize = .ReleaseFast,
.imports = &.{
.{ .name = "aether", .module = lib },
},
}),
});
const bundle_name = b.fmt("aether-{s}-v{s}", .{ target_label, version });
const release_root = b.fmt("release/{s}", .{version});
const bundle_dir = b.fmt("{s}/{s}", .{ release_root, bundle_name });
const examples_dir = b.fmt("{s}/examples/ecommerce", .{bundle_dir});
const archive_path = b.fmt("{s}/{s}.tar.gz", .{ release_root, bundle_name });
const ensure_release_root = b.addSystemCommand(&.{ "mkdir", "-p" });
ensure_release_root.addArg(release_root);
const ensure_examples_dir = b.addSystemCommand(&.{ "mkdir", "-p" });
ensure_examples_dir.addArg(examples_dir);
ensure_examples_dir.step.dependOn(&ensure_release_root.step);
const copy_binary = b.addSystemCommand(&.{ "cp" });
copy_binary.addFileArg(exe.getEmittedBin());
copy_binary.addArg(b.fmt("{s}/aether", .{bundle_dir}));
copy_binary.step.dependOn(&ensure_examples_dir.step);
const copy_readme = b.addSystemCommand(&.{ "cp" });
copy_readme.addFileArg(b.path("README.md"));
copy_readme.addArg(b.fmt("{s}/README.md", .{bundle_dir}));
copy_readme.step.dependOn(&ensure_examples_dir.step);
const copy_example = b.addSystemCommand(&.{ "cp" });
copy_example.addFileArg(b.path("examples/ecommerce/aether.json"));
copy_example.addArg(b.fmt("{s}/aether.json", .{examples_dir}));
copy_example.step.dependOn(&ensure_examples_dir.step);
const archive = b.addSystemCommand(&.{ "tar", "-czf" });
archive.addArg(archive_path);
archive.addArg("-C");
archive.addArg(release_root);
archive.addArg(bundle_name);
archive.step.dependOn(©_binary.step);
archive.step.dependOn(©_readme.step);
archive.step.dependOn(©_example.step);
release_step.dependOn(&archive.step);
}
fn readPackageVersion(comptime zon: []const u8) []const u8 {
const needle = ".version = \"";
const start = std.mem.indexOf(u8, zon, needle) orelse @compileError("build.zig.zon is missing .version");
const version_start = start + needle.len;
const version_end_rel = std.mem.indexOfScalar(u8, zon[version_start..], '"') orelse
@compileError("build.zig.zon has invalid .version");
return zon[version_start .. version_start + version_end_rel];
}