1
0

replace Makefile with build.zig

This commit is contained in:
Rokas Puzonas 2024-02-29 22:30:50 +02:00
parent 8473de5f13
commit d275d207a7
14 changed files with 111 additions and 42 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
build
zig-cache
zig-out

View File

@ -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

83
build.zig Normal file
View File

@ -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);
}
}

1
compile_flags.txt Normal file
View File

@ -0,0 +1 @@
-Isrc

View File

@ -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;

View File

@ -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 <filename>\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;

View File

@ -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 <filename>\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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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));

View File

@ -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));

View File

@ -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;

View File

@ -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));