summaryrefslogtreecommitdiff
path: root/build.zig
blob: 08789c7e050729241b48edf02a6aa9048355f37f (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
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 shock = b.addExecutable(.{
        .name = "shock",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/shock.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 opt_gdb = b.option(bool, "gdb", "Run executable under gdb") orelse false;
    const comp_opts = b.addOptions();
    comp_opts.addOption(bool, "gdb", opt_gdb);

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

    // Looooopy
    const gcc_test = b.addSystemCommand(&.{"gcc"});
    gcc_test.addFileArg(b.path("tests/c/hello-world.c"));
    gcc_test.addArg("-o");
    const gcc_hello_file = gcc_test.addOutputFileArg("hello-world");

    const runner = b.addRunArtifact(shock);
    runner.addFileArg(gcc_hello_file);
    runner.addFileArg(gcc_hello_file);

    const loop_step = b.step("loop", "Interative Programming Loop! you should run it with watch: i.e. zig build loop --watch");
    loop_step.dependOn(&runner.step);
    // ---

    const test_step = b.step("test", "Run unit tests");
    const run_elfo_step = b.step("elfo", "See the pretty elfo!");
    const run_shock_step = b.step("shock", "SHOCK something!");
    const run_gloves_step = b.step("gloves", "See the pretty gloves!");

    const zls_check = b.step("check", "Check if the program compiles");
    zls_check.dependOn(&elfo.step);
    zls_check.dependOn(&shock.step);
    zls_check.dependOn(&unit_tests.step);

    const run_steps = [_]struct { *std.Build.Step, *std.Build.Step.Compile }{
        .{ test_step, unit_tests },
        .{ run_elfo_step, elfo },
        .{ run_gloves_step, gloves },
        .{ run_shock_step, shock },
    };

    const gdbscript = b.addWriteFile("gdbscript",
        \\r
    );

    for (run_steps) |run| {
        run.@"1".root_module.addOptions("meta", comp_opts);
        if (opt_gdb) {
            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);
        }
    }
}