Skip to content

Commit

Permalink
fix: use homebrew's mupdf if deps directory is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
botantony committed Feb 28, 2025
1 parent fdb1352 commit ff60c36
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,47 @@ fn addMupdfDeps(exe: *std.Build.Step.Compile, b: *std.Build, prefix: []const u8)
exe.linkLibC();
}

fn addMupdfHomebrew(exe: *std.Build.Step.Compile, target: std.Target) void {
if (target.os.tag == .macos and target.cpu.arch == .aarch64) {
exe.addIncludePath(.{ .cwd_relative = "/opt/homebrew/include" });
exe.addLibraryPath(.{ .cwd_relative = "/opt/homebrew/lib" });
} else if (target.os.tag == .macos and target.cpu.arch == .x86_64) {
exe.addIncludePath(.{ .cwd_relative = "/usr/local/include" });
exe.addLibraryPath(.{ .cwd_relative = "/usr/local/lib" });
} else if (target.os.tag == .linux) {
exe.addIncludePath(.{ .cwd_relative = "/home/linuxbrew/.linuxbrew/include" });
exe.addLibraryPath(.{ .cwd_relative = "/home/linuxbrew/.linuxbrew/lib" });

const linux_libs = [_][]const u8{
"mupdf-third", "harfbuzz",
"freetype", "jbig2dec",
"jpeg", "openjp2",
"gumbo", "mujs",
};
for (linux_libs) |lib| exe.linkSystemLibrary(lib);
}
exe.linkSystemLibrary("mupdf");
exe.linkSystemLibrary("z");
exe.linkLibC();
}

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

var useVendorMupdf = true;
const prefix = "./local";
const location = "./deps/mupdf/local";

std.fs.cwd().access("./deps/mupdf/Makefile", .{}) catch |err| {
if (err == error.FileNotFound) {
useVendorMupdf = false;
} else {
std.debug.print("Error: {s}\n", .{@errorName(err)});
return;
}
};

var make_args = std.ArrayList([]const u8).init(b.allocator);
defer make_args.deinit();

Expand Down Expand Up @@ -64,12 +98,15 @@ pub fn build(b: *std.Build) void {
exe.root_module.addImport("vaxis", deps.vaxis.module("vaxis"));
exe.root_module.addImport("fzwatch", deps.fzwatch.module("fzwatch"));

exe.step.dependOn(&mupdf_build_step.step);

addMupdfDeps(exe, b, location);

b.installArtifact(exe);
b.getInstallStep().dependOn(&mupdf_build_step.step);
if (useVendorMupdf) {
exe.step.dependOn(&mupdf_build_step.step);
addMupdfDeps(exe, b, location);
b.installArtifact(exe);
b.getInstallStep().dependOn(&mupdf_build_step.step);
} else {
addMupdfHomebrew(exe, target.result);
b.installArtifact(exe);
}

const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| run_cmd.addArgs(args);
Expand Down

0 comments on commit ff60c36

Please sign in to comment.