From 632a8daeb6377bdfde47cf83dcffd116d88e999a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Feb 2026 14:51:17 -0300 Subject: updated to zig 0.16.0 and fixed memory leaks --- build.zig | 143 ++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 98 insertions(+), 45 deletions(-) (limited to 'build.zig') diff --git a/build.zig b/build.zig index beacc7b..dc5d8b5 100644 --- a/build.zig +++ b/build.zig @@ -1,46 +1,9 @@ const std = @import("std"); -fn capstone(b: *std.Build, options: struct { - target: std.Build.ResolvedTarget, - optimize: std.builtin.OptimizeMode, -}) struct { - dep: *std.Build.Dependency, - artifact: *std.Build.Step.Compile, - mod: *std.Build.Module, -} { - const capstone_dep = b.dependency("capstone", .{ - .target = options.target, - .optimize = options.optimize, - }); - - const compiled_capstone = capstone_dep.artifact("capstone"); - - // I don't really know what I'm doing here, but it works - const capstone_c = b.addTranslateC(.{ - .root_source_file = b.path("deps/tmp/capstone-6.0.0-Alpha4/include/capstone/capstone.h"), - // .root_source_file = capstone_dep.path("capstone.h"), - .target = options.target, - .optimize = options.optimize, - .link_libc = true, - }); - - const capstone_c_mod = capstone_c.createModule(); - return .{ - .dep = capstone_dep, - .artifact = compiled_capstone, - .mod = capstone_c_mod, - }; -} - pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const elfo_module = b.createModule(.{ - .root_source_file = b.path("src/elfo-pretty.zig"), - .target = target, - .optimize = optimize, - }); const gloves_module = b.createModule(.{ .root_source_file = b.path("src/gloves.zig"), .target = target, @@ -48,26 +11,81 @@ pub fn build(b: *std.Build) !void { .link_libc = true, }); - const cs = capstone(b, .{ + const capstone_dep = b.dependency("capstone", .{}); + const translate_c = b.addTranslateC(.{ + .root_source_file = capstone_dep.path("include/capstone/capstone.h"), .target = target, .optimize = optimize, + .link_libc = true, }); - elfo_module.addImport("capstone", cs.mod); - elfo_module.addLibraryPath(cs.artifact.getEmittedBin().dirname()); - elfo_module.linkLibrary(cs.artifact); - elfo_module.addIncludePath(cs.artifact.getEmittedIncludeTree()); + + // translate + + const c_mod = translate_c.createModule(); + + const capstone_lib = b.addLibrary(.{ + .name = "capstone", + .linkage = .static, + .root_module = c_mod, + }); + + capstone_lib.root_module.addIncludePath(capstone_dep.path("include")); + capstone_lib.root_module.addIncludePath(capstone_dep.path("include/capstone")); + capstone_lib.root_module.addIncludePath(capstone_dep.path("include/capstone/X86")); + + capstone_lib.installHeadersDirectory(capstone_dep.path("include/capstone"), "capstone", .{}); + capstone_lib.installHeader(capstone_dep.path("include/platform.h"), "capstone/platform.h"); + + if (optimize == .Debug) capstone_lib.root_module.addCMacro("CAPSTONE_DEBUG", ""); + capstone_lib.root_module.addCMacro("CAPSTONE_HAS_X86", ""); + capstone_lib.root_module.addCMacro("CAPSTONE_BUILD_CSTOOL", "OFF"); + capstone_lib.root_module.addCMacro("CAPSTONE_USE_SYS_DYN_MEM", "OFF"); + + capstone_lib.root_module.addCSourceFiles(.{ .root = capstone_dep.path(""), .files = common_sources }); + // capstone_lib.root_module.addCSourceFiles(.{ + // .root = capstone_dep.path("arch/AArch64"), + // .files = &.{ + // "AArch64BaseInfo.c", + // "AArch64Disassembler.c", + // "AArch64DisassemblerExtension.c", + // "AArch64InstPrinter.c", + // "AArch64Mapping.c", + // "AArch64Module.c", + // }, + // }); + capstone_lib.root_module.addCSourceFiles(.{ + .root = capstone_dep.path("arch/X86"), + .files = &.{ + "X86Disassembler.c", + "X86DisassemblerDecoder.c", + "X86IntelInstPrinter.c", + "X86ATTInstPrinter.c", + "X86InstPrinterCommon.c", + "X86Mapping.c", + "X86Module.c", + }, + }); + + b.installArtifact(capstone_lib); const elfo = b.addExecutable(.{ .name = "elfo-pretty", - .root_module = elfo_module, + .root_module = b.createModule(.{ + .root_source_file = b.path("src/elfo-pretty.zig"), + .target = target, + .optimize = optimize, + }), }); + 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, }); - elfo.linkLibrary(cs.artifact); + // elfo.linkLibrary(cs.artifact); b.installArtifact(elfo); b.installArtifact(gloves); @@ -79,5 +97,40 @@ pub fn build(b: *std.Build) !void { const run_gloves_step = b.step("gloves", "See the pretty gloves!"); run_gloves_step.dependOn(&run_gloves_cmd.step); - // TODO: add flag to run on pwndbg + // TODO: add flag to run on gdb } + +const common_sources: []const []const u8 = &.{ + "cs.c", + "Mapping.c", + "MCInst.c", + "MCInstrDesc.c", + "MCInstPrinter.c", + "MCRegisterInfo.c", + "SStream.c", + "utils.c", +}; + +// .AArch64 +// .ARC +// .ARM +// .Alpha +// .BPF +// .EVM +// .HPPA +// .LoongArch +// .M680X +// .M68K +// .MOS65XX +// .Mips +// .PowerPC +// .RISCV +// .SH +// .Sparc +// .SystemZ +// .TMS320C64x +// .TriCore +// .WASM +// .X86 +// .XCore +// .Xtensa -- cgit v1.3