summaryrefslogtreecommitdiff
path: root/build/addons.zig
blob: 38b02dff2846f3908b7817d7e419af3dee29824e (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
const std = @import("std");
const mupdf_build = @import("mupdf.zig");

/// The GPU addons over sketchbook's scene format (`gpu_scene`): the snake
/// (addons/snake_shader) and the frame corpus's translucent veil
/// (test/veil.zig). They are the window build's, from the sketchbook it
/// takes (`gui.Window.sketchbook`, null off the window): the terminal's
/// configures without sketchbook beside the checkout, and there their steps
/// say so.
pub fn gpu(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, addon_mod: *std.Build.Module, sketchbook: ?*std.Build.Dependency) ?*std.Build.Step.Compile {
    const snake_shader_step = b.step("snake-shader", "build the optional GPU snake addon into zig-out/lib");
    const veil_step = b.step("veil", "build the frame corpus's translucent GPU test addon (test/veil.zig) into zig-out/lib");
    const sb = sketchbook orelse {
        const fail = b.addFail("the GPU addons are the window's: -Dplatform=gui (sketchbook beside this checkout)");
        snake_shader_step.dependOn(&fail.step);
        veil_step.dependOn(&fail.step);
        return null;
    };
    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_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 lib;
}

pub const Pdf = struct {
    lib: ?*std.Build.Step.Compile,
    /// The same module, for a core with the addons compiled in, which
    /// exports nothing there (addon.exportLib).
    module: ?*std.Build.Module,
    /// 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,
    c_optimize: std.lang.Optimize,
    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,
    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;
    var pdf_module: ?*std.Build.Module = 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;
        pdf_module = pdf_mod;
        c.mupdf_check.dependOn(mupdf_build.addProbe(b, mupdf, .{
            .target = c.target,
            .pdf = b.path("docs/design.pdf"),
        }));
    };
    // 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, .module = pdf_module, .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;
}