From 78ac60ce60de334c868c1e92df39ec03d6cac53d Mon Sep 17 00:00:00 2001 From: Andrew Rioux Date: Tue, 21 Jan 2025 03:11:19 -0500 Subject: [PATCH] feat: added loader and appropriate build scripts --- Cargo.lock | 12 ++++++++++ packages.nix | 45 +++++++++++++++++++---------------- sparse-installer/Cargo.toml | 6 +++++ sparse-installer/src/main.rs | 3 +++ unix-loader/build.zig | 37 ++++++++++++++++++++++++---- unix-loader/src/gzip.zig | 19 +++++++++++++++ unix-loader/src/libloader.zig | 11 +++++++++ unix-loader/src/loader.zig | 11 +++------ unix-loader/src/test_run.zig | 26 +++----------------- 9 files changed, 114 insertions(+), 56 deletions(-) create mode 100644 sparse-installer/Cargo.toml create mode 100644 sparse-installer/src/main.rs create mode 100644 unix-loader/src/gzip.zig diff --git a/Cargo.lock b/Cargo.lock index 0d0892d..3e9e2e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1894,6 +1894,10 @@ dependencies = [ "pcap-sys", ] +[[package]] +name = "sparse-installer" +version = "2.0.0" + [[package]] name = "sparse-server" version = "0.1.0" @@ -1912,6 +1916,14 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "sparse-unix-beacon" +version = "2.0.0" + +[[package]] +name = "sparse-windows-beacon" +version = "2.0.0" + [[package]] name = "spin" version = "0.9.8" diff --git a/packages.nix b/packages.nix index 0b824dd..34a9877 100644 --- a/packages.nix +++ b/packages.nix @@ -22,7 +22,7 @@ let freebsd-zig-libc = pkgs.writeText "freebsd-libc.txt" '' include_dir=${freebsd-libs}/usr/include sys_include_dir=${freebsd-libs}/usr/include - sys_include_dir=${freebsd-libs}/usr/lib + crt_dir=${freebsd-libs}/usr/lib msvc_lib_dir= kernel32_lib_dir= gcc_dir= @@ -51,6 +51,8 @@ let ./nl-sys/src/bridge.c (craneLib.fileset.commonCargoSources ./packets) (craneLib.fileset.commonCargoSources ./sparse-beacon) + (craneLib.fileset.commonCargoSources ./sparse-unix-beacon) + (craneLib.fileset.commonCargoSources ./sparse-windows-beacon) ]; }; @@ -89,7 +91,6 @@ let }; freebsdArgs = commonArgs // { - src = fileSetForBeaconCrate; # Sigh... # For some reason, crane and cargo don't run the build script for FreeBSD # It runs it with the dev shell for all 3 targets, and runs it in the @@ -130,6 +131,7 @@ let }); sparse-beacon-freebsd-sysv = craneLib.buildPackage (freebsdArgs // { + src = fileSetForBeaconCrate; cargoArtifacts = freebsdCargoArtifacts; name = "sparse-beacon-freebsd"; cargoExtraArgs = "-p sparse-unix-beacon"; @@ -160,16 +162,16 @@ let src = ./unix-loader; buildPhase = '' + mkdir $out + export XDG_CACHE_HOME=$(mktemp -d) zig build \ - -Dtarget=x86_64-linux-musl + --summary all \ + --prefix $out \ + --release=small \ + -Dbeacon=${sparse-beacon-linux}/bin/sparse-unix-beacon \ + -Dtarget=x86_64-linux-musl \ + --verbose ''; - - installPhase = '' - mkdir -p $out/lib - cp zig-out/lib/libunix-loader.so $out/lib/unix-loader.so - ''; - - SPARSE_BEACON = "${sparse-beacon-linux}/bin/sparse-beacon"; }; freebsd-loader = pkgs.stdenv.mkDerivation { @@ -180,18 +182,18 @@ let src = ./unix-loader; buildPhase = '' + mkdir $out + export XDG_CACHE_HOME=$(mktemp -d) zig build \ + --summary all \ + --prefix $out \ + --release=small \ -Dtarget=x86_64-freebsd \ + -Dbeacon=${sparse-beacon-freebsd}/bin/sparse-unix-beacon \ --sysroot ${freebsd-libs} \ - --libc ${freebsd-zig-libc} + --libc ${freebsd-zig-libc} \ + --verbose ''; - - installPhase = '' - mkdir -p $out/lib - cp zig-out/lib/libunix-loader.so $out/lib/unix-loader.so - ''; - - SPARSE_BEACON = "${sparse-beacon-freebsd}/bin/sparse-beacon"; }; sparse-installer-linux = craneLib.buildPackage (commonArgs // { @@ -203,7 +205,7 @@ let CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; - SPARSE_INSTALLER_LINUX = "${linux-loader}/lib/unix-loader.so"; + SPARSE_INSTALLER_LINUX = "${linux-loader}/lib/libunix-loader.so"; }); sparse-installer-freebsd-sysv = craneLib.buildPackage (commonArgs // { @@ -215,7 +217,7 @@ let CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; - SPARSE_INSTALLER_FREEBSD = "${freebsd-loader}/lib/unix-loader.so"; + SPARSE_INSTALLER_FREEBSD = "${freebsd-loader}/lib/libunix-loader.so"; }); sparse-installer-freebsd = @@ -248,7 +250,8 @@ let outputs = rec { packages = { inherit sparse-server sparse-beacon-linux sparse-beacon-windows - linux-loader freebsd-loader sparse-beacon-freebsd; + sparse-installer-linux sparse-installer-freebsd linux-loader + freebsd-loader sparse-beacon-freebsd; inherit freebsd-zig-libc; diff --git a/sparse-installer/Cargo.toml b/sparse-installer/Cargo.toml new file mode 100644 index 0000000..6ca54ba --- /dev/null +++ b/sparse-installer/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "sparse-installer" +edition = "2024" +version.workspace = true + +[dependencies] diff --git a/sparse-installer/src/main.rs b/sparse-installer/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/sparse-installer/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/unix-loader/build.zig b/unix-loader/build.zig index 615c072..75b64e5 100644 --- a/unix-loader/build.zig +++ b/unix-loader/build.zig @@ -1,26 +1,55 @@ const std = @import("std"); pub fn build(b: *std.Build) !void { + const beacon = b.option([]const u8, "beacon", "path to beacon to load") orelse std.debug.panic("Expected a path to a beacon to be provided with -Dbeacon", .{}); + const beacon_path = std.Build.LazyPath{ .cwd_relative = beacon }; + const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const tool = b.addExecutable(.{ + .name = "beacon-compress", + .root_source_file = b.path("src/gzip.zig"), + .target = b.host, + }); + + const tool_step = b.addRunArtifact(tool); + tool_step.addFileArg(beacon_path); + const compressed_beacon = tool_step.addOutputFileArg("beacon.gz"); + + const libloader = b.addObject(.{ + .name = "libloader", + .root_source_file = b.path("src/libloader.zig"), + .target = target, + .optimize = optimize, + .strip = true, + .pic = true, + }); + + libloader.root_module.addAnonymousImport("beacon", .{ .root_source_file = compressed_beacon }); + libloader.linkLibC(); + + libloader.step.dependOn(&tool_step.step); + const lib = b.addSharedLibrary(.{ .name = "unix-loader", .root_source_file = b.path("src/loader.zig"), .target = target, .optimize = optimize, + .strip = true, }); const exe = b.addExecutable(.{ - .name = "unix-loader", + .name = "test-loader", .root_source_file = b.path("src/test_run.zig"), .target = target, .optimize = optimize, + .strip = true, }); - lib.linkLibC(); - b.installArtifact(lib); + lib.addObject(libloader); + exe.addObject(libloader); - exe.linkLibC(); + b.installArtifact(lib); b.installArtifact(exe); } diff --git a/unix-loader/src/gzip.zig b/unix-loader/src/gzip.zig new file mode 100644 index 0000000..b1835d8 --- /dev/null +++ b/unix-loader/src/gzip.zig @@ -0,0 +1,19 @@ +const std = @import("std"); + +pub fn main() !void { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + const args = try std.process.argsAlloc(alloc); + const input_file_path = args[1]; + const output_file_path = args[2]; + + var input_file = try std.fs.cwd().openFile(input_file_path, .{}); + defer input_file.close(); + + var output_file = try std.fs.cwd().createFile(output_file_path, .{}); + defer output_file.close(); + + try std.compress.gzip.compress(input_file.reader(), output_file.writer(), .{ .level = .level_9 }); +} diff --git a/unix-loader/src/libloader.zig b/unix-loader/src/libloader.zig index e69de29..ba37085 100644 --- a/unix-loader/src/libloader.zig +++ b/unix-loader/src/libloader.zig @@ -0,0 +1,11 @@ +const std = @import("std"); + +const beacon = @embedFile("beacon"); + +fn use_beacon(message: []const u8) !void { + _ = try std.io.getStdOut().write(message); +} + +export fn hash_internals() void { + use_beacon(beacon) catch {}; +} diff --git a/unix-loader/src/loader.zig b/unix-loader/src/loader.zig index ecfeade..e50546f 100644 --- a/unix-loader/src/loader.zig +++ b/unix-loader/src/loader.zig @@ -1,10 +1,5 @@ -const std = @import("std"); -const testing = std.testing; +extern fn hash_internals() void; -export fn add(a: i32, b: i32) i32 { - return a + b; -} - -test "basic add functionality" { - try testing.expect(add(3, 7) == 10); +export fn calculate_hash() void { + hash_internals(); } diff --git a/unix-loader/src/test_run.zig b/unix-loader/src/test_run.zig index 42837a5..8905790 100644 --- a/unix-loader/src/test_run.zig +++ b/unix-loader/src/test_run.zig @@ -1,25 +1,5 @@ -const std = @import("std"); +extern fn hash_internals() void; -pub fn main() !void { - // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); - - // stdout is for the actual output of your application, for example if you - // are implementing gzip, then only the compressed bytes should be sent to - // stdout, not any debugging messages. - const stdout_file = std.io.getStdOut().writer(); - var bw = std.io.bufferedWriter(stdout_file); - const stdout = bw.writer(); - - try stdout.print("Run `zig build test` to run the tests.\n", .{}); - - try bw.flush(); // don't forget to flush! -} - -test "simple test" { - var list = std.ArrayList(i32).init(std.testing.allocator); - defer list.deinit(); // try commenting this out and see if zig detects the memory leak! - - try list.append(42); - try std.testing.expectEqual(@as(i32, 42), list.pop()); +pub fn main() void { + hash_internals(); }