Skip to content

Commit

Permalink
Merge branch 'master' into feature/zig-master
Browse files Browse the repository at this point in the history
  • Loading branch information
freref authored Feb 28, 2025
2 parents 983a9ba + 4302afb commit 792c7a3
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");

fn addMupdfDeps(exe: *std.Build.Step.Compile, b: *std.Build, prefix: []const u8) void {
fn addMupdfStatic(exe: *std.Build.Step.Compile, b: *std.Build, prefix: []const u8) void {
exe.addIncludePath(.{ .cwd_relative = b.fmt("{s}/include", .{prefix}) });
exe.addLibraryPath(.{ .cwd_relative = b.fmt("{s}/lib", .{prefix}) });

Expand All @@ -10,13 +10,47 @@ fn addMupdfDeps(exe: *std.Build.Step.Compile, b: *std.Build, prefix: []const u8)
exe.linkLibC();
}

fn addMupdfDynamic(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 @@ -66,12 +100,15 @@ pub fn build(b: *std.Build) void {

exe.root_module.addAnonymousImport("metadata", .{ .root_source_file = b.path("build.zig.zon") });

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);
addMupdfStatic(exe, b, location);
b.installArtifact(exe);
b.getInstallStep().dependOn(&mupdf_build_step.step);
} else {
addMupdfDynamic(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 792c7a3

Please sign in to comment.