aboutsummaryrefslogtreecommitdiff
path: root/build.zig
blob: c85791fa3e22be0535437df90e5f9d8973cf7b41 (plain)
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
const std = @import("std");

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

    // build typst module
    const build_typst = b.addSystemCommand(&[_][]const u8{
        "cargo", "build",
    });
    build_typst.setCwd(b.path("typst/"));
    if (optimize != .Debug) build_typst.addArg("--release");

    const mod = b.addModule("typdown", .{
        .root_source_file = b.path("src/root.zig"),
        .target = target,
        .optimize = optimize,
    });
    if (!target.result.isWasiLibC()) mod.link_libc = true;
    // link typst module during build
    mod.addIncludePath(b.path("typst"));
    mod.addLibraryPath(if (optimize == .Debug) b.path("typst/target/debug/") else b.path("typst/target/release/"));
    if (optimize != .Debug) mod.strip = true;

    const lib = b.addLibrary(.{
        .name = "typdown",
        .linkage = .static,
        .root_module = mod,
    });
    // link typst module to build
    lib.linkSystemLibrary("typdown_typst");

    const installed_lib = b.addInstallArtifact(lib, .{});
    installed_lib.step.dependOn(&build_typst.step);
    // when emitting headers will be fixed
    //installed_lib.emitted_h = lib.getEmittedH();

    const example = b.addExecutable(.{
        .name = "example",
        .root_module = b.createModule(.{
            .target = target,
            .optimize = optimize,
            .link_libc = true,
        }),
    });
    example.root_module.addCSourceFile(.{
        .file = b.path("examples/main.c"),
    });
    example.root_module.linkLibrary(lib);
    example.root_module.addIncludePath(b.path("include"));

    b.getInstallStep().dependOn(&installed_lib.step);

    const mod_tests = b.addTest(.{
        .root_module = mod,
        .use_llvm = true, // zig internal backend crashes during linking (for 0.15.2)
    });
    const run_mod_tests = b.addRunArtifact(mod_tests);
    generateSVG(b, &run_mod_tests.step) catch |err| run_mod_tests.step.addError("{}\n", .{err}) catch unreachable;
    run_mod_tests.step.dependOn(b.getInstallStep());

    const test_step = b.step("test", "Run tests");
    test_step.dependOn(&run_mod_tests.step);

    const examples_step = b.step("examples", "Run examples");
    const example_run = b.addRunArtifact(example);
    example_run.step.dependOn(b.getInstallStep());
    examples_step.dependOn(&example_run.step);

    const check = b.step("check", "Check if foo compiles");
    check.dependOn(&lib.step);
}

fn generateSVG(b: *std.Build, step: *std.Build.Step) !void {
    var dir = try std.fs.cwd().openDir("src/data/", .{ .iterate = true });
    defer dir.close();
    var iter = dir.iterate();
    while (try iter.next()) |it| {
        if (it.kind == .file and std.mem.endsWith(u8, it.name, ".typ")) {
            const cmd = b.addSystemCommand(&[_][]const u8{
                "typst", "c",
                "-f",    "svg",
                it.name,
            });
            cmd.setCwd(b.path("src/data/"));
            step.dependOn(&cmd.step);
        }
    }
}