feat: added loader and appropriate build scripts

This commit is contained in:
Andrew Rioux 2025-01-21 03:11:19 -05:00
parent 489b1d380f
commit 78ac60ce60
Signed by: andrew.rioux
GPG Key ID: 9B8BAC47C17ABB94
9 changed files with 114 additions and 56 deletions

12
Cargo.lock generated
View File

@ -1894,6 +1894,10 @@ dependencies = [
"pcap-sys", "pcap-sys",
] ]
[[package]]
name = "sparse-installer"
version = "2.0.0"
[[package]] [[package]]
name = "sparse-server" name = "sparse-server"
version = "0.1.0" version = "0.1.0"
@ -1912,6 +1916,14 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "sparse-unix-beacon"
version = "2.0.0"
[[package]]
name = "sparse-windows-beacon"
version = "2.0.0"
[[package]] [[package]]
name = "spin" name = "spin"
version = "0.9.8" version = "0.9.8"

View File

@ -22,7 +22,7 @@ let
freebsd-zig-libc = pkgs.writeText "freebsd-libc.txt" '' freebsd-zig-libc = pkgs.writeText "freebsd-libc.txt" ''
include_dir=${freebsd-libs}/usr/include include_dir=${freebsd-libs}/usr/include
sys_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= msvc_lib_dir=
kernel32_lib_dir= kernel32_lib_dir=
gcc_dir= gcc_dir=
@ -51,6 +51,8 @@ let
./nl-sys/src/bridge.c ./nl-sys/src/bridge.c
(craneLib.fileset.commonCargoSources ./packets) (craneLib.fileset.commonCargoSources ./packets)
(craneLib.fileset.commonCargoSources ./sparse-beacon) (craneLib.fileset.commonCargoSources ./sparse-beacon)
(craneLib.fileset.commonCargoSources ./sparse-unix-beacon)
(craneLib.fileset.commonCargoSources ./sparse-windows-beacon)
]; ];
}; };
@ -89,7 +91,6 @@ let
}; };
freebsdArgs = commonArgs // { freebsdArgs = commonArgs // {
src = fileSetForBeaconCrate;
# Sigh... # Sigh...
# For some reason, crane and cargo don't run the build script for FreeBSD # 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 # 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 // { sparse-beacon-freebsd-sysv = craneLib.buildPackage (freebsdArgs // {
src = fileSetForBeaconCrate;
cargoArtifacts = freebsdCargoArtifacts; cargoArtifacts = freebsdCargoArtifacts;
name = "sparse-beacon-freebsd"; name = "sparse-beacon-freebsd";
cargoExtraArgs = "-p sparse-unix-beacon"; cargoExtraArgs = "-p sparse-unix-beacon";
@ -160,16 +162,16 @@ let
src = ./unix-loader; src = ./unix-loader;
buildPhase = '' buildPhase = ''
mkdir $out
export XDG_CACHE_HOME=$(mktemp -d)
zig build \ 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 { freebsd-loader = pkgs.stdenv.mkDerivation {
@ -180,18 +182,18 @@ let
src = ./unix-loader; src = ./unix-loader;
buildPhase = '' buildPhase = ''
mkdir $out
export XDG_CACHE_HOME=$(mktemp -d)
zig build \ zig build \
--summary all \
--prefix $out \
--release=small \
-Dtarget=x86_64-freebsd \ -Dtarget=x86_64-freebsd \
-Dbeacon=${sparse-beacon-freebsd}/bin/sparse-unix-beacon \
--sysroot ${freebsd-libs} \ --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 // { sparse-installer-linux = craneLib.buildPackage (commonArgs // {
@ -203,7 +205,7 @@ let
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; 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 // { sparse-installer-freebsd-sysv = craneLib.buildPackage (commonArgs // {
@ -215,7 +217,7 @@ let
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; 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 = sparse-installer-freebsd =
@ -248,7 +250,8 @@ let
outputs = rec { outputs = rec {
packages = { packages = {
inherit sparse-server sparse-beacon-linux sparse-beacon-windows 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; inherit freebsd-zig-libc;

View File

@ -0,0 +1,6 @@
[package]
name = "sparse-installer"
edition = "2024"
version.workspace = true
[dependencies]

View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View File

@ -1,26 +1,55 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.Build) !void { 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 target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); 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(.{ const lib = b.addSharedLibrary(.{
.name = "unix-loader", .name = "unix-loader",
.root_source_file = b.path("src/loader.zig"), .root_source_file = b.path("src/loader.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.strip = true,
}); });
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "unix-loader", .name = "test-loader",
.root_source_file = b.path("src/test_run.zig"), .root_source_file = b.path("src/test_run.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.strip = true,
}); });
lib.linkLibC(); lib.addObject(libloader);
b.installArtifact(lib); exe.addObject(libloader);
exe.linkLibC(); b.installArtifact(lib);
b.installArtifact(exe); b.installArtifact(exe);
} }

19
unix-loader/src/gzip.zig Normal file
View File

@ -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 });
}

View File

@ -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 {};
}

View File

@ -1,10 +1,5 @@
const std = @import("std"); extern fn hash_internals() void;
const testing = std.testing;
export fn add(a: i32, b: i32) i32 { export fn calculate_hash() void {
return a + b; hash_internals();
}
test "basic add functionality" {
try testing.expect(add(3, 7) == 10);
} }

View File

@ -1,25 +1,5 @@
const std = @import("std"); extern fn hash_internals() void;
pub fn main() !void { pub fn main() void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) hash_internals();
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());
} }