summaryrefslogtreecommitdiff
path: root/build.zig
blob: 36e5b53e460499d2472c3c7cd3453018fa636ffb (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
const std = @import("std");
const frida = @import("build/frida-gum.zig");
const capstone = @import("build/capstone.zig").capstone;

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

    const gloves_module = b.createModule(.{
        .root_source_file = b.path("src/gloves.zig"),
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });

    const elfo = b.addExecutable(.{
        .name = "elfo",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/elfo.zig"),
            .target = target,
            .optimize = optimize,
            .link_libc = true,
        }),
    });

    const capstone_lib = capstone(b, .{
        .optimize = optimize,
        .target = target,
    });

    const frida_lib = frida.gum_stalker(b, .{
        .optimize = optimize,
        .target = target,
    });

    frida_lib.step.dependOn(&capstone_lib.step);
    frida_lib.root_module.linkLibrary(capstone_lib);

    b.installArtifact(capstone_lib);
    b.installArtifact(frida_lib);

    elfo.root_module.linkLibrary(capstone_lib);
    elfo.root_module.addImport("capstone", capstone_lib.root_module);

    const gloves = b.addExecutable(.{
        .name = "gloves",
        .root_module = gloves_module,
    });

    b.installArtifact(elfo);
    b.installArtifact(gloves);

    const gdb_opt = b.option(bool, "gdb", "Run executable under gdb") orelse false;
    const comp_opts = b.addOptions();
    comp_opts.addOption(bool, "gdb", gdb_opt);

    const unit_tests = b.addTest(.{
        .root_module = b.createModule(.{
            .optimize = optimize,
            .target = target,
            .root_source_file = b.path("tests/test.zig"),
        }),
    });

    const test_step = b.step("test", "Test the library");

    const run_elfo_step = b.step("elfo", "See the pretty elfo!");
    const run_gloves_step = b.step("gloves", "See the pretty gloves!");

    const run_steps = [_]struct { *std.Build.Step, *std.Build.Step.Compile }{
        .{ test_step, unit_tests },
        .{ run_elfo_step, elfo },
        .{ run_gloves_step, gloves },
    };
    const gdbscript = b.addWriteFile("gdbscript",
        \\r
    );

    for (run_steps) |run| {
        run.@"1".root_module.addOptions("meta", comp_opts);
        if (gdb_opt) {
            const run_gdb = b.addSystemCommand(&.{"gdb"});
            run_gdb.addArg("-x");
            run_gdb.addFileArg(gdbscript.getDirectory().path(b, "gdbscript"));
            run_gdb.addArtifactArg(run.@"1");
            run.@"0".dependOn(&run_gdb.step);
        } else {
            const run_unit = b.addRunArtifact(run.@"1");
            run.@"0".dependOn(&run_unit.step);
        }
    }
}