summaryrefslogtreecommitdiff
path: root/build/addons.zig
blob: d22abc7a5d5d867f1de7686139ee95b4d20c91b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const std = @import("std");
const mupdf_build = @import("mupdf.zig");

/// The GPU addons over sketchbook's `gpu_scene`: the snake (addons/snake_shader)
/// and the frame corpus's translucent veil (test/veil.zig).
pub fn gpu(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, addon_mod: *std.Build.Module) ?*std.Build.Step.Compile {
    const snake_shader_step = b.step("snake-shader", "build the optional GPU snake addon into zig-out/lib");
    var snake_shader_lib: ?*std.Build.Step.Compile = null;
    const veil_step = b.step("veil", "build the frame corpus's translucent GPU test addon (test/veil.zig) into zig-out/lib");
    if (b.lazyDependency("sketchbook", .{ .@"gpu-only" = true })) |sb| {
        const files = b.addWriteFiles();
        for ([_][]const u8{ "vert", "frag", "post.frag" }) |stage| {
            const compile = b.addSystemCommand(&.{ "glslc", "-O" });
            compile.addFileArg(b.path(b.fmt("addons/snake_shader/snake.{s}", .{stage})));
            compile.addArg("-o");
            _ = files.addCopyFile(compile.addOutputFileArg(b.fmt("snake.{s}.spv", .{stage})), b.fmt("{s}.spv", .{stage}));
        }
        const shader_module = b.createModule(.{ .root_source_file = files.add("shaders.zig", "const v align(4) = @embedFile(\"vert.spv\").*;\npub const vert: []const u8 = &v;\nconst f align(4) = @embedFile(\"frag.spv\").*;\npub const frag: []const u8 = &f;\nconst p align(4) = @embedFile(\"post.frag.spv\").*;\npub const post: []const u8 = &p;\n") });
        const module = b.createModule(.{ .root_source_file = b.path("addons/snake_shader/snake_shader.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "addon", .module = addon_mod }, .{ .name = "gpu_scene", .module = sb.module("gpu_scene") }, .{ .name = "shaders", .module = shader_module } } });
        const lib = b.addLibrary(.{ .name = "snake_shader", .linkage = .dynamic, .root_module = module });
        snake_shader_lib = lib;
        snake_shader_step.dependOn(&b.addInstallArtifact(lib, .{}).step);
        snake_shader_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module })).step);
        // The frame corpus's translucent addon (test/veil.zig), into lib/ beside the snake.
        const veil = b.addLibrary(.{ .name = "veil", .linkage = .dynamic, .root_module = b.createModule(.{ .root_source_file = b.path("test/veil.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "addon", .module = addon_mod }, .{ .name = "gpu_scene", .module = sb.module("gpu_scene") } } }) });
        veil_step.dependOn(&b.addInstallArtifact(veil, .{}).step);
    }
    return snake_shader_lib;
}

pub const Pdf = struct {
    lib: ?*std.Build.Step.Compile,
    /// What a step that needs the addon depends on when it is not built.
    no_pdf: ?*std.Build.Step,
};

/// The PDF addon (addons/pdf/pdf.zig over MuPDF through pdf_bridge.c), built
/// and installed as the image addon is: compiled only for a step that
/// needs it (`pdf`, the tests). MuPDF is compiled against the build's
/// FreeType and zlib and links them into the library, which holds its
/// own copy of each beside a window's FreeType (docs/shared-libs.md); so
/// all three are position-independent, and the library exports its
/// `pardes_addon` alone.
pub fn pdf(b: *std.Build, c: struct {
    addon_mod: *std.Build.Module,
    builtin_mods: []const *std.Build.Module,
    c_optimize: std.lang.Optimize,
    core_module: *std.Build.Module,
    enable_jpx: bool,
    freetype: ?*std.Build.Step.Compile,
    freetype_dep: ?*std.Build.Dependency,
    io: std.Io,
    mupdf_check: *std.Build.Step,
    mupdf_dep: ?*std.Build.Dependency,
    optimize: std.lang.Optimize,
    pdf_pointer_bench_step: *std.Build.Step,
    pdf_scroll_bench_step: *std.Build.Step,
    target: std.Build.ResolvedTarget,
}) Pdf {
    var pdf_addon: ?*std.Build.Step.Compile = null;
    if (c.mupdf_dep) |md| if (c.freetype_dep) |fd| {
        // ...and one zlib, the one FreeType links for its gzip module. Asked
        // of FreeType's build with the options it asks with, the dependency
        // cache hands back that same artifact rather than a second copy.
        const zlib = fd.builder.dependency("zlib", .{ .target = c.target, .optimize = c.c_optimize }).artifact("z");
        c.freetype.?.root_module.pic = true;
        zlib.root_module.pic = true;
        const mupdf = mupdf_build.add(b, c.io, md, .{
            .target = c.target,
            .optimize = c.c_optimize,
            .jpx = c.enable_jpx,
            .freetype = c.freetype.?,
            .zlib = zlib,
            .pic = true,
        });
        // The bridge's header translated here for pdf.zig (Zig 0.17 has no
        // @cImport), as the kinds' header is for the core.
        const bridge_mod = b.addTranslateC(.{ .root_source_file = b.path("addons/pdf/pdf_bridge.h"), .target = c.target, .optimize = c.c_optimize }).createModule();
        const pdf_mod = b.createModule(.{ .root_source_file = b.path("addons/pdf/pdf.zig"), .target = c.target, .optimize = c.c_optimize, .link_libc = true, .imports = &.{ .{ .name = "addon", .module = c.addon_mod }, .{ .name = "pdf_bridge", .module = bridge_mod } } });
        mupdf.linkTo(pdf_mod);
        pdf_mod.addIncludePath(b.path("addons/pdf"));
        pdf_mod.addCSourceFile(.{ .file = b.path("addons/pdf/pdf_bridge.c"), .flags = &.{"-std=gnu11"} });
        const lib = b.addLibrary(.{ .name = "pdf", .linkage = .dynamic, .root_module = pdf_mod });
        lib.setVersionScript(b.addWriteFiles().add("pdf.map", "{ global: pardes_addon; local: *; };\n"));
        pdf_addon = lib;
        // ...and the same module compiled into a pardes, which exports
        // nothing there (addon.exportLib).
        for (c.builtin_mods) |m| m.addImport("pdf_addon", pdf_mod);
        c.mupdf_check.dependOn(mupdf_build.addProbe(b, mupdf, .{
            .target = c.target,
            .pdf = b.path("docs/design.pdf"),
        }));

        // The PDF benches: the core over the addon, a pane of it.
        const config = b.addOptions();
        config.addOption(bool, "release_fast_core", c.optimize == .fast);
        config.addOption([]const u8, "core_optimize", @tagName(c.optimize));
        config.addOptionPath("pdf_addon", lib.getEmittedBin());
        for ([_]struct { step: *std.Build.Step, exe: []const u8, file: []const u8, optimize: std.lang.Optimize }{
            .{ .step = c.pdf_pointer_bench_step, .exe = "pardes-pdf-pointer-bench", .file = "test/pdf_pointer_bench.zig", .optimize = .fast },
            .{ .step = c.pdf_scroll_bench_step, .exe = "pardes-pdf-scroll-bench", .file = "test/pdf_scroll_bench.zig", .optimize = c.optimize },
        }) |bench| {
            const exe = b.addExecutable(.{ .name = bench.exe, .root_module = b.createModule(.{ .target = c.target, .optimize = bench.optimize, .root_source_file = b.path(bench.file), .link_libc = true }) });
            exe.root_module.addImport("pardes", c.core_module);
            exe.root_module.addOptions("pdf_bench_config", config);
            const run = b.addRunArtifact(exe);
            run.addPassthruArgs();
            run.setCwd(b.path("."));
            bench.step.dependOn(&run.step);
        }
    };
    // Without it, what needs it says how to have it.
    const no_pdf: ?*std.Build.Step = if (pdf_addon == null) &b.addFail("this step needs the pdf addon, over MuPDF: add -Dpdf (it is fetched once, and on from then)").step else null;
    if (no_pdf) |s| for ([_]*std.Build.Step{ c.mupdf_check, c.pdf_pointer_bench_step, c.pdf_scroll_bench_step }) |step| step.dependOn(s);
    return .{ .lib = pdf_addon, .no_pdf = no_pdf };
}

/// The image addon (addons/image/image.zig over stb_image, stb.c),
/// installed by its step and handed by path to every test that opens a
/// picture.
pub fn image(b: *std.Build, target: std.Build.ResolvedTarget, c_optimize: std.lang.Optimize, addon_mod: *std.Build.Module, zstbi_dep: *std.Build.Dependency) *std.Build.Step.Compile {
    const image_mod = b.createModule(.{ .root_source_file = b.path("addons/image/image.zig"), .target = target, .optimize = c_optimize, .link_libc = true, .imports = &.{.{ .name = "addon", .module = addon_mod }} });
    image_mod.addIncludePath(zstbi_dep.path("libs/stbi"));
    image_mod.addCSourceFile(.{ .file = b.path("addons/image/stb.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
    const image_addon = b.addLibrary(.{ .name = "image", .linkage = .dynamic, .root_module = image_mod });
    return image_addon;
}