Skip to content

Commit 71ef2e6

Browse files
author
theseyan
committed
Use zfetch instead of cURL, fix runtime bugs
1 parent 5ce9f3f commit 71ef2e6

File tree

11 files changed

+160
-3465
lines changed

11 files changed

+160
-3465
lines changed
File renamed without changes.

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
[submodule "deps/zip"]
1414
path = deps/zip
1515
url = https://github.com/kuba--/zip
16+
[submodule "deps/zfetch"]
17+
path = deps/zfetch
18+
url = https://github.com/truemedian/zfetch

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ bkg and pkg (Node) have a number of differences arising either from a design dec
4040
bkg is written in Zig and compilation is fairly straightforward. The prerequisites are:
4141
- Zig version [0.10.0-dev.3554+bfe8a4d9f](https://ziglang.org/builds/zig-0.10.0-dev.3554+bfe8a4d9f.tar.xz)
4242

43-
```properties
43+
```bash
4444
# Clone the repository and update submodules
4545
git clone https://github.com/theseyan/bkg && cd bkg
4646
git submodule update --init --recursive
4747

4848
# Build for x86_64-linux
49-
zig build -Drelease-fast -target x86_64-linux
49+
zig build -Drelease-fast -Dtarget=x86_64-linux
5050

5151
# [Optional] Build runtime for x86_64-linux
5252
zig build-exe -target x86_64-linux src/bkg_runtime.zig -lc deps/lz4/lib/lz4.c deps/microtar/src/microtar.c --pkg-begin known-folders deps/known-folders/known-folders.zig --pkg-end
@@ -59,9 +59,11 @@ zig build-exe -target x86_64-linux src/bkg_runtime.zig -lc deps/lz4/lib/lz4.c de
5959

6060
- Compiler: Stream archive directly to `lz4_compress_default` instead of through the filesystem
6161
- Runtime: Stream decompressed buffer directly to microtar instead of through the filesystem. This will greatly improve startup time.
62+
- Pass CLI args to javascript
63+
- Use [uuid](https://github.com/dmgk/zig-uuid) temporary directory naming to prevent possible naming conflicts
6264
- Bundle sources (and possibly node_modules) into a single file before packaging
6365
- JSON build script, advanced options to include external assets, make compression optional
6466
- Bun CLI flags
65-
- Use [zfetch](https://github.com/truemedian/zfetch) instead of cURL
67+
- :white_check_mark: ~~Use [zfetch](https://github.com/truemedian/zfetch) instead of cURL~~
6668
- Fork a custom build of Bun with only the JS runtime and use that instead of the official binaries
6769
- If size of `bun` can be brought down under 50 MB, consider executing directly from memory

build.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const zfetch = @import("deps/zfetch/build.zig");
23

34
pub fn build(b: *std.build.Builder) !void {
45
const target = b.standardTargetOptions(.{});
@@ -19,8 +20,8 @@ pub fn build(b: *std.build.Builder) !void {
1920
// Link zig-clap library
2021
exe.addPackagePath("clap", "deps/zig-clap/clap.zig");
2122

22-
// Link cURL
23-
exe.linkSystemLibrary("curl");
23+
// Link zfetch
24+
exe.addPackage(try zfetch.getPackage(b));
2425

2526
// Link known-folders
2627
exe.addPackagePath("known-folders", "deps/known-folders/known-folders.zig");

deps/zfetch

Submodule zfetch added at 12b1374

src/bkg_runtime.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// It should only deal with running the packaged app
33

44
// To build the runtime
5-
// zig build-exe src/bkg_runtime.zig -lc deps/lz4/lib/lz4.c deps/microtar/src/microtar.c --pkg-begin known-folders deps/known-folders/known-folders.zig --pkg-end
5+
// zig build-exe -Drelease-fast src/bkg_runtime.zig -lc deps/lz4/lib/lz4.c deps/microtar/src/microtar.c --pkg-begin known-folders deps/known-folders/known-folders.zig --pkg-end
66
// Strip debug symbols:
77
// strip bkg_runtime
88

src/cli.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub fn init(allocator: std.mem.Allocator) anyerror!void {
7979

8080
// Initialize version manager
8181
try versionManager.init(allocator);
82+
defer versionManager.deinit();
8283

8384
// Make sure we have the latest Bun and bkg runtime for the target
8485
var runtimePath = try versionManager.downloadBun(try versionManager.getLatestBunVersion(), target);

src/compiler.zig

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ pub fn build(allocator: std.mem.Allocator, bunPath: []const u8, bkgPath: []const
1919
// Rename executable
2020
try std.fs.renameAbsolute("/tmp/__bkg_build_runtime", out);
2121

22+
// Give executable permissions
23+
var file: ?std.fs.File = std.fs.openFileAbsolute(out, .{}) catch |e| switch(e) {
24+
else => null
25+
};
26+
if(file != null) {
27+
try file.?.chmod(755);
28+
file.?.close();
29+
}else {
30+
std.debug.print("Could not mark binary as executable. Run `chmod +x {s}` to do it manually.\n", .{std.fs.path.basename(out)});
31+
}
32+
2233
_ = target;
2334
return out;
2435

@@ -68,10 +79,10 @@ pub fn buildArchive(allocator: std.mem.Allocator, bun_path: []const u8, root: []
6879

6980
// Add Bun binary to archive
7081
{
82+
std.debug.print("Adding Bun binary...\n", .{});
7183
var bun = try std.fs.openFileAbsolute(bun_path, .{});
7284
const bunBuf: []u8 = try bun.readToEndAlloc(allocator, 256 * 1024 * 1024);
7385

74-
std.debug.print("Adding Bun binary...\n", .{});
7586
_ = mtar.mtar_write_file_header(&tar, "bkg_bun", @intCast(c_uint, bunBuf.len));
7687
_ = mtar.mtar_write_data(&tar, bunBuf.ptr, @intCast(c_uint, bunBuf.len));
7788

src/runtime.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub fn execProcess(allocator: std.mem.Allocator, root: []const u8) anyerror!void
120120
// Give executable permissions to bun
121121
var file: ?std.fs.File = std.fs.openFileAbsolute(try std.mem.concat(allocator, u8, &.{root, "/", "bkg_bun"}), .{}) catch |e| switch(e) {
122122
error.AccessDenied => null,
123-
else => return error.FailedToOpenExecutable
123+
else => return e
124124
};
125125

126126
// We get an error.AccessDenied if the file is already executable

0 commit comments

Comments
 (0)