Skip to content

Commit

Permalink
Fix c macro
Browse files Browse the repository at this point in the history
  • Loading branch information
imbillow committed Jun 11, 2024
1 parent 976217f commit eafe61f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 55 deletions.
38 changes: 13 additions & 25 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ pub fn build(b: *std.Build) void {
"arch/Sparc/SparcDisassembler.c",
"arch/Sparc/SparcMapping.c",
} });
var arch_iter = std.mem.splitSequence(u8, "ARM AARCH64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM MOS65XX WASM BPF RISCV SH TRICORE ALPHA HPPA XTENSA", " ");
var buf: [128]u8 = undefined;
while (arch_iter.next()) |entry| {
const _support = std.fmt.bufPrint(&buf, "CAPSTONE_{s}_SUPPORT", .{entry}) catch unreachable;
std.debug.print("Enable {s}\n", .{_support});
libcapstone.root_module.addCMacro(_support, "1");

const has_arch = std.fmt.bufPrint(&buf, "CAPSTONE_HAS_{s}", .{entry}) catch unreachable;
libcapstone.root_module.addCMacro(has_arch, "1");
}
libcapstone.addIncludePath(b.path("include"));
libcapstone.linkLibC();

Expand Down Expand Up @@ -166,20 +176,6 @@ pub fn build(b: *std.Build) void {
cstool.linkLibC();
cstool.linkLibrary(libcsprint);

const cstest = b.addExecutable(.{
.name = "cstest",
.target = target,
.optimize = optimize,
});
cstest.addCSourceFiles(.{ .files = &.{
"suite/cstest//src/capstone_test.c",
"suite/cstest//src/helper.c",
"suite/cstest//src/main.c",
} });
cstest.addIncludePath(b.path("suite/cstest//include"));
cstest.linkLibC();
cstest.linkLibrary(libcsprint);

// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
Expand All @@ -188,8 +184,6 @@ pub fn build(b: *std.Build) void {
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(cstool);

b.installArtifact(cstest);
b.installArtifact(libcsprint);

// This *creates* a Run step in the build graph, to be executed when another
Expand Down Expand Up @@ -222,21 +216,15 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
});
lib_unit_tests.linkLibrary(libcapstone);
lib_unit_tests.linkLibrary(libcsprint);
// lib_unit_tests.addIncludePath() ∏

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);

// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_exe_unit_tests.step);
}
24 changes: 0 additions & 24 deletions src/main.zig

This file was deleted.

15 changes: 9 additions & 6 deletions src/root.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const std = @import("std");
const cs = @cImport({
@cInclude("capstone/capstone.h");
});
const testing = std.testing;

export fn add(a: i32, b: i32) i32 {
return a + b;
}

test "basic add functionality" {
try testing.expect(add(3, 7) == 10);
test "basic open functionality" {
const handle: cs.csh = 0;
for (0..cs.CS_ARCH_MAX) |arch| {
try testing.expect(cs.cs_open(@intCast(arch), cs.CS_MODE_LITTLE_ENDIAN, @constCast(&handle)) == cs.CS_ERR_OK);
}
defer _ = cs.cs_close(@constCast(&handle));
}

0 comments on commit eafe61f

Please sign in to comment.