Skip to content

Commit

Permalink
Clean up build.zig files
Browse files Browse the repository at this point in the history
  • Loading branch information
mgord9518 committed Aug 2, 2023
1 parent 0b3a321 commit bab0c9f
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 77 deletions.
91 changes: 84 additions & 7 deletions zig/build.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,89 @@
const std = @import("std");
const squashfuse = @import("squashfuse-zig/build.zig");

pub const squashfuse = @import("squashfuse-zig/build.zig");

pub const LinkOptions = struct {
enable_zstd: bool = true,
enable_lz4: bool = true,
enable_lzo: bool = false,
enable_zlib: bool = true,
enable_xz: bool = true,

enable_fuse: bool = true,
use_system_fuse: bool = true,

use_libdeflate: bool = true,
};

// TODO: maybe this should just be named `link`?
pub fn linkVendored(exe: *std.Build.Step.Compile, opts: LinkOptions) void {
const prefix = thisDir();

exe.addIncludePath(.{ .path = prefix ++ "/../include" });

squashfuse.linkVendored(exe, .{
.enable_lz4 = opts.enable_lz4,
.enable_lzo = opts.enable_lzo,
.enable_zlib = opts.enable_zlib,
.enable_zstd = opts.enable_zstd,
.enable_xz = opts.enable_xz,

.use_libdeflate = opts.use_libdeflate,
});
}

/// Returns a build module for aisap with deps pre-wrapped
pub fn module(b: *std.Build) *std.Build.Module {
const prefix = thisDir();

const lib_options = b.addOptions();
lib_options.addOption(bool, "enable_xz", true);
lib_options.addOption(bool, "enable_zlib", true);
lib_options.addOption(bool, "use_libdeflate", true);
lib_options.addOption(bool, "enable_lzo", false);
lib_options.addOption(bool, "enable_lz4", true);
lib_options.addOption(bool, "enable_zstd", true);
lib_options.addOption(bool, "use_zig_zstd", false);

const squashfuse_module = b.addModule("squashfuse", .{
.source_file = .{ .path = prefix ++ "/squashfuse-zig/lib.zig" },
.dependencies = &.{
.{
.name = "build_options",
.module = lib_options.createModule(),
},
},
});

const known_folders_module = b.addModule("known-folders", .{
.source_file = .{ .path = prefix ++ "/known-folders/known-folders.zig" },
});

return b.addModule("aisap", .{
.source_file = .{ .path = prefix ++ "/lib.zig" },
.dependencies = &.{
.{
.name = "squashfuse",
.module = squashfuse_module,
},
.{
.name = "known-folders",
.module = known_folders_module,
},
},
});
}

pub inline fn thisDir() []const u8 {
return comptime std.fs.path.dirname(@src().file) orelse unreachable;
}

// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
const prefix = thisDir();

// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
Expand All @@ -29,13 +108,12 @@ pub fn build(b: *std.Build) void {
.name = "aisap",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "lib/c_api.zig" },
.root_source_file = .{ .path = prefix ++ "/lib/c_api.zig" },
.target = target,
.optimize = optimize,
});

const pie = b.option(bool, "pie", "build as a PIE (position independent executable)") orelse true;
lib.pie = pie;
lib.addIncludePath(.{ .path = prefix ++ "/../include" });

squashfuse.linkVendored(lib, .{
.enable_lz4 = true,
Expand All @@ -45,8 +123,6 @@ pub fn build(b: *std.Build) void {
.enable_xz = true,

.use_libdeflate = true,

.squashfuse_dir = "squashfuse-zig",
});

const known_folders_mod = b.addModule("known-folders", .{
Expand All @@ -66,7 +142,8 @@ pub fn build(b: *std.Build) void {
lib.addModule("squashfuse", squashfuse_mod);
lib.addModule("known-folders", known_folders_mod);

lib.addIncludePath(.{ .path = "../include" });
const pie = b.option(bool, "pie", "build as a PIE (position independent executable)") orelse true;
lib.pie = pie;

lib.linkLibC();

Expand Down
63 changes: 5 additions & 58 deletions zig/examples/build.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const squashfuse = @import("squashfuse-zig/build.zig");
const aisap = @import("aisap/zig/build.zig");

pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
Expand All @@ -19,64 +19,11 @@ pub fn build(b: *std.build.Builder) void {
.optimize = optimize,
});

const exe_options = b.addOptions();
exe_options.addOption(bool, "enable_xz", true);
exe_options.addOption(bool, "enable_zlib", true);
exe_options.addOption(bool, "use_libdeflate", true);
exe_options.addOption(bool, "enable_lzo", false);
exe_options.addOption(bool, "enable_lz4", true);
exe_options.addOption(bool, "enable_zstd", true);
exe_options.addOption(bool, "use_zig_zstd", false);
// TODO: find if there's some kind of convention here and follow it if so
const aisap_module = aisap.module(b);
exe.addModule("aisap", aisap_module);

const squashfuse_mod = b.addModule("squashfuse", .{
.source_file = .{ .path = "../squashfuse-zig/lib.zig" },
.dependencies = &.{
.{
.name = "build_options",
.module = exe_options.createModule(),
},
},
});

const known_folders_mod = b.addModule("known-folders", .{
.source_file = .{ .path = "../known-folders/known-folders.zig" },
});

const aisap_mod = b.addModule("aisap", .{
.source_file = .{ .path = "../lib.zig" },
.dependencies = &.{
// TODO: handle this in aisap
.{
.name = "squashfuse",
.module = squashfuse_mod,
},
.{
.name = "known-folders",
.module = known_folders_mod,
},
},
});

// exe.addModule("squashfuse", squashfuse_mod);
exe.addModule("aisap", aisap_mod);

exe.addIncludePath(.{ .path = "../../include" });
exe.addLibraryPath(.{ .path = "." });

squashfuse.linkVendored(exe, .{
.enable_lz4 = true,
.enable_lzo = false,
.enable_zlib = true,
.enable_zstd = true,
.enable_xz = true,

.use_libdeflate = true,
// .use_system_fuse = true,

.squashfuse_dir = "squashfuse-zig",
});

exe.linkSystemLibrary("fuse3");
aisap.linkVendored(exe, .{});

b.installArtifact(exe);

Expand Down
1 change: 0 additions & 1 deletion zig/examples/squashfuse-zig

This file was deleted.

1 change: 1 addition & 0 deletions zig/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn BWrapErrorFromInt(err: c_int) BWrapError!void {
extern fn bwrap_main(argc: c_int, argv: [*]const [*:0]const u8) c_int;
fn bwrap(allocator: *std.mem.Allocator, args: []const []const u8) !void {
var result = try allocator.alloc([*:0]const u8, args.len + 1);
//defer allocator.free(result);

// Set ARGV0 then iterate through the slice and convert it to a C char**
result[0] = "bwrap";
Expand Down
20 changes: 11 additions & 9 deletions zig/lib/AppImage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub const AppImage = struct {
// Parses the built-in JSON database into a HashMap
// TODO: do this comptime
fn initDatabase(allocator: std.mem.Allocator) !std.StringHashMap(Permissions) {
const json_database = @embedFile("../profile_database.json");
const json_database = @embedFile("profile_database.json");

const parsed = try std.json.parseFromSlice(
[]JsonPermissions,
Expand Down Expand Up @@ -508,17 +508,19 @@ pub const AppImage = struct {
var perms = try ai.permissions(allocator);
// defer perms.deinit();

std.debug.print("{}\n", .{perms});

if (perms.filesystem) |files| {
for (files) |*file| {
try list.appendSlice(
try file.toBwrapArgs(allocator),
);
if (perms) |prms| {
if (prms.filesystem) |files| {
for (files) |*file| {
try list.appendSlice(
try file.toBwrapArgs(allocator),
);
}
}

return list.toOwnedSlice();
}

return list.toOwnedSlice();
return &[_][]const u8{};
}

pub fn wrapArgsZ(ai: *AppImage, allocator: std.mem.Allocator) ![*:null]?[*:0]const u8 {
Expand Down
1 change: 1 addition & 0 deletions zig/lib/profile_database.json
1 change: 0 additions & 1 deletion zig/profile_database.json

This file was deleted.

2 changes: 1 addition & 1 deletion zig/squashfuse-zig
Submodule squashfuse-zig updated 1 files
+39 −47 build.zig

0 comments on commit bab0c9f

Please sign in to comment.