forked from burrito-elixir/burrito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
102 lines (73 loc) · 3.11 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
////
// DO NOT EDIT THIS FILE
////
const std = @import("std");
const foilz = @import("src/archiver.zig");
const log = std.log;
const Builder = std.build.Builder;
const CrossTarget = std.zig.CrossTarget;
const Mode = std.builtin.Mode;
const LibExeObjStep = std.build.LibExeObjStep;
var builder: *Builder = undefined;
var target: *const CrossTarget = undefined;
var wrapper_exe: *LibExeObjStep = undefined;
pub fn build(b: *Builder) !void {
log.info("Zig is building an Elixir binary... ⚡", .{});
builder = b;
target = &builder.standardTargetOptions(.{});
// Run build steps!
_ = try run_archiver();
_ = try build_wrapper();
log.info("DONE 🚀", .{});
}
pub fn run_archiver() !void {
log.info("Generating and compressing release payload... 📦", .{});
const release_path = std.os.getenv("__BURRITO_RELEASE_PATH");
try foilz.pack_directory(release_path.?, "./payload.foilz");
const compress_cmd = builder.addSystemCommand(&[_][]const u8{ "/bin/bash", "-c", "xz -9ez --check=crc32 --stdout --keep payload.foilz > src/payload.foilz.xz" });
try compress_cmd.step.make();
}
pub fn build_wrapper() !void {
log.info("Building wrapper and embedding payload... 🌯", .{});
const release_name = std.os.getenv("__BURRITO_RELEASE_NAME");
const plugin_path = std.os.getenv("__BURRITO_PLUGIN_PATH");
const is_prod = std.os.getenv("__BURRITO_IS_PROD");
var file = try std.fs.cwd().openFile("payload.foilz", .{});
defer file.close();
const uncompressed_size = try file.getEndPos();
wrapper_exe = builder.addExecutable(release_name.?, "src/wrapper.zig");
const exe_options = builder.addOptions();
wrapper_exe.addOptions("build_options", exe_options);
exe_options.addOption([]const u8, "RELEASE_NAME", release_name.?);
exe_options.addOption(u64, "UNCOMPRESSED_SIZE", uncompressed_size);
if (std.mem.eql(u8, is_prod.?, "1")) {
exe_options.addOption(bool, "IS_PROD", true);
wrapper_exe.setBuildMode(Mode.ReleaseSmall);
// https://github.com/ziglang/zig/issues/13405
wrapper_exe.strip = true;
} else {
exe_options.addOption(bool, "IS_PROD", false);
wrapper_exe.setBuildMode(Mode.Debug);
}
wrapper_exe.setTarget(target.*);
if (target.isWindows()) {
wrapper_exe.addIncludePath("src/");
}
// Link standard C libary to the wrapper
wrapper_exe.linkSystemLibrary("c");
if (plugin_path) |plugin| {
log.info("Plugin found! {s} 🔌", .{plugin});
wrapper_exe.addPackagePath("burrito_plugin", plugin);
} else {
wrapper_exe.addPackagePath("burrito_plugin", "./_dummy_plugin.zig");
}
wrapper_exe.addIncludePath("src/xz");
wrapper_exe.addCSourceFile("src/xz/xz_crc32.c", &[0][]const u8{});
wrapper_exe.addCSourceFile("src/xz/xz_dec_lzma2.c", &[0][]const u8{});
wrapper_exe.addCSourceFile("src/xz/xz_dec_stream.c", &[0][]const u8{});
wrapper_exe.install();
const run_cmd = wrapper_exe.run();
run_cmd.step.dependOn(builder.getInstallStep());
const run_step = builder.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}