diff --git a/.gitignore b/.gitignore index 378eac2..4c82b07 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -build +zig-cache +zig-out diff --git a/Makefile b/Makefile deleted file mode 100644 index c2f2412..0000000 --- a/Makefile +++ /dev/null @@ -1,31 +0,0 @@ -CFLAGS=-lm -g -Wall -O1 - -# TODO: Move this to a build.zig. This is becoming a mess, I don't want to deal with Makefiles anymore - -build/main: src/main.c src/repetition_tester.c build/multi_nop_loop.o build/write_loops.o build/load_uop.o build/store_uop.o build/short_load_uop.o src/rprof.h build/read_widths.o - mkdir -p build - gcc -o build/main src/main.c build/multi_nop_loop.o build/write_loops.o build/load_uop.o build/store_uop.o build/short_load_uop.o build/read_widths.o $(CFLAGS) - -build/short_load_uop.o: src/short_load_uop.asm - nasm -g -f elf64 -o build/short_load_uop.o src/short_load_uop.asm - -build/load_uop.o: src/load_uop.asm - nasm -g -f elf64 -o build/load_uop.o src/load_uop.asm - -build/write_loops.o: src/write_loops.asm - nasm -g -f elf64 -o build/write_loops.o src/write_loops.asm - -build/multi_nop_loop.o: src/multi_nop_loop.asm - nasm -g -f elf64 -o build/multi_nop_loop.o src/multi_nop_loop.asm - -build/store_uop.o: src/store_uop.asm - nasm -g -f elf64 -o build/store_uop.o src/store_uop.asm - -build/read_widths.o: src/read_widths.asm - nasm -g -f elf64 -o build/read_widths.o src/read_widths.asm - -run: ./build/main - ./build/main $(TEST_NAME) - -clean: - rm -r build diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..f570872 --- /dev/null +++ b/build.zig @@ -0,0 +1,83 @@ +const std = @import("std"); +const Builder = std.build.Builder; + +fn addLinuxAssembly(b: *Builder, filename: []const u8) !std.Build.LazyPath { + const obj_basename = try std.mem.concat(b.allocator, u8, &.{ + std.fs.path.stem(filename), + ".o" + }); + + const obj = b.addSystemCommand(&.{ "nasm", "-g", "-f", "elf64", "-o" }); + const output_obj = obj.addOutputFileArg(obj_basename); + obj.addFileArg(.{ .path = filename }); + + return output_obj; +} + +fn addAllLinuxAssmeblies(b: *Builder) !std.ArrayList(std.Build.LazyPath) { + var linux_assemblies = std.ArrayList(std.Build.LazyPath).init(b.allocator); + errdefer linux_assemblies.deinit(); + + var dir = try std.fs.cwd().openIterableDir("src", .{ }); + var it = dir.iterate(); + while (try it.next()) |file| { + if (file.kind != .file) continue; + + const ext = std.fs.path.extension(file.name); + if (!std.mem.eql(u8, ext, ".asm")) continue; + + const assembly_path = try std.mem.concat(b.allocator, u8, &.{ "src/", file.name }); + defer b.allocator.free(assembly_path); + + try linux_assemblies.append(try addLinuxAssembly(b, assembly_path)); + } + + return linux_assemblies; +} + +pub fn build(b: *Builder) !void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + var linux_assemblies = try addAllLinuxAssmeblies(b); + defer linux_assemblies.deinit(); + + var dir = try std.fs.cwd().openIterableDir("src/tests", .{ }); + var it = dir.iterate(); + while (try it.next()) |file| { + if (file.kind != .file) continue; + + const ext = std.fs.path.extension(file.name); + if (!std.mem.eql(u8, ext, ".c")) continue; + + const source_file_path = try std.mem.concat(b.allocator, u8, &.{ "src/tests/", file.name }); + defer b.allocator.free(source_file_path); + + const executable_name = std.fs.path.stem(file.name); + + const exe = b.addExecutable(.{ + .name = executable_name, + .root_source_file = .{ .path = source_file_path }, + .optimize = optimize, + .target = target + }); + exe.addIncludePath(.{ .path = "src" }); + exe.linkLibC(); + + for (linux_assemblies.items) |obj| { + exe.addObjectFile(obj); + } + + b.installArtifact(exe); + + const run_exe = b.addRunArtifact(exe); + if (b.args) |args| { + run_exe.addArgs(args); + } + + const step_description = try std.fmt.allocPrint(b.allocator, "Run '{s}' test", .{source_file_path}); + defer b.allocator.free(step_description); + const run_step = b.step(executable_name, step_description); + run_step.dependOn(&run_exe.step); + } +} diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..d3e4bc1 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1 @@ +-Isrc diff --git a/src/main_load_uop.c b/src/tests/load_uop.c similarity index 97% rename from src/main_load_uop.c rename to src/tests/load_uop.c index 84b7a49..4d7d149 100644 --- a/src/main_load_uop.c +++ b/src/tests/load_uop.c @@ -1,7 +1,7 @@ #include "repetition_tester.c" #include "load_uop.h" -int main_test_load_uop() { +int main() { typedef void (*test_cb)(uint8_t *buffer, uint64_t byte_count); struct testcase { char *name; diff --git a/src/main_malloc_read.c b/src/tests/malloc_read.c similarity index 95% rename from src/main_malloc_read.c rename to src/tests/malloc_read.c index 4f107e7..3d7fbd6 100644 --- a/src/main_malloc_read.c +++ b/src/tests/malloc_read.c @@ -54,7 +54,14 @@ static void read_malloc_file_with_read(bool should_alloc, struct repetitor *repe free_buffer(should_alloc, buffer); } -int main_test_malloc_read(char *filename) { +int main(int argc, char **argv) { + if (argc < 2) { + printf("Usage: %s \n", argv[0]); + return -1; + } + + char *filename = argv[1]; + typedef void (*read_file_b)(bool should_alloc, struct repetitor *repetitor, uint8_t *buffer, uint64_t buffer_size, char *filename); struct testcase { char *name; diff --git a/src/main_read_file.c b/src/tests/read_file.c similarity index 93% rename from src/main_read_file.c rename to src/tests/read_file.c index 3b254f5..8880c83 100644 --- a/src/main_read_file.c +++ b/src/tests/read_file.c @@ -46,7 +46,15 @@ void read_file_with_read(struct repetitor *repetitor, uint8_t *buffer, uint64_t close(file); } -int main_test_read_file(char *filename) { +int main(int argc, char **argv) { + if (argc < 2) { + printf("Usage: %s \n", argv[0]); + return -1; + } + + char *filename = argv[1]; + + typedef void (*read_file_b)(struct repetitor *repetitor, uint8_t *buffer, uint64_t buffer_size, char *filename); struct testcase { char *name; diff --git a/src/main_read_widths.c b/src/tests/read_widths.c similarity index 97% rename from src/main_read_widths.c rename to src/tests/read_widths.c index f08b168..317aa23 100644 --- a/src/main_read_widths.c +++ b/src/tests/read_widths.c @@ -1,7 +1,7 @@ #include "repetition_tester.c" #include "read_widths.h" -int main_test_read_widths() { +int main() { typedef void (*test_cb)(uint8_t *buffer, uint64_t byte_count); struct testcase { char *name; diff --git a/src/main_short_load_uop.c b/src/tests/short_load_uop.c similarity index 97% rename from src/main_short_load_uop.c rename to src/tests/short_load_uop.c index 961fb06..a8bfeed 100644 --- a/src/main_short_load_uop.c +++ b/src/tests/short_load_uop.c @@ -1,7 +1,7 @@ #include "repetition_tester.c" #include "short_load_uop.h" -int main_test_short_load_uop() { +int main() { typedef void (*test_cb)(uint8_t *buffer, uint64_t byte_count); struct testcase { char *name; diff --git a/src/main_store_uop.c b/src/tests/store_uop.c similarity index 97% rename from src/main_store_uop.c rename to src/tests/store_uop.c index 1312ff6..c5aaf2b 100644 --- a/src/main_store_uop.c +++ b/src/tests/store_uop.c @@ -1,7 +1,7 @@ #include "repetition_tester.c" #include "store_uop.h" -int main_test_store_uop() { +int main() { typedef void (*test_cb)(uint8_t *buffer, uint64_t byte_count); struct testcase { char *name; diff --git a/src/main_write_all_bytes.c b/src/tests/write_all_bytes.c similarity index 97% rename from src/main_write_all_bytes.c rename to src/tests/write_all_bytes.c index 0a13861..4204449 100644 --- a/src/main_write_all_bytes.c +++ b/src/tests/write_all_bytes.c @@ -19,7 +19,7 @@ static void test_write_to_all_bytes(struct repetitor *repetitor, uint8_t *buffer } } -int main_test_write_all_bytes() { +int main() { struct repetitor repetitor = {}; repetitor_init(&repetitor); printf("CPU Frequency: %ldHz (~%.2fGHz)\n", repetitor.cpu_freq, (float)repetitor.cpu_freq/(1000*1000*1000)); diff --git a/src/main_write_backward.c b/src/tests/write_backward.c similarity index 97% rename from src/main_write_backward.c rename to src/tests/write_backward.c index cdca37e..c4e5cf6 100644 --- a/src/main_write_backward.c +++ b/src/tests/write_backward.c @@ -1,6 +1,6 @@ #include "repetition_tester.c" -int main_test_write_backward() { +int main() { struct repetitor repetitor = {}; repetitor_init(&repetitor); printf("CPU Frequency: %ldHz (~%.2fGHz)\n", repetitor.cpu_freq, (float)repetitor.cpu_freq/(1000*1000*1000)); diff --git a/src/main_write_bytes_asm.c b/src/tests/write_bytes_asm.c similarity index 97% rename from src/main_write_bytes_asm.c rename to src/tests/write_bytes_asm.c index 09a9e44..eb2ec38 100644 --- a/src/main_write_bytes_asm.c +++ b/src/tests/write_bytes_asm.c @@ -1,7 +1,7 @@ #include "repetition_tester.c" #include "multi_nop_loop.h" -int main_test_write_bytes_asm() { +int main() { typedef void (*write_bytes_cb)(uint8_t *buffer, uint64_t byte_count); struct testcase { char *name; diff --git a/src/main_write_loop.c b/src/tests/write_loop.c similarity index 98% rename from src/main_write_loop.c rename to src/tests/write_loop.c index 6d65bcd..e5d0d35 100644 --- a/src/main_write_loop.c +++ b/src/tests/write_loop.c @@ -1,7 +1,7 @@ #include "repetition_tester.c" #include "write_loops.h" -int main_test_write_loop() { +int main() { struct repetitor repetitor = {}; repetitor_init(&repetitor); printf("CPU Frequency: %ldHz (~%.2fGHz)\n", repetitor.cpu_freq, (float)repetitor.cpu_freq/(1000*1000*1000));