1
0

restructure project layout

This commit is contained in:
Rokas Puzonas 2024-04-07 12:50:22 +03:00
parent cef071d28f
commit adcb0891fb
27 changed files with 28 additions and 85 deletions

View File

@ -14,11 +14,13 @@ fn addLinuxAssembly(b: *Builder, filename: []const u8) !std.Build.LazyPath {
return output_obj;
}
fn addAllLinuxAssmeblies(b: *Builder) !std.ArrayList(std.Build.LazyPath) {
var linux_assemblies = std.ArrayList(std.Build.LazyPath).init(b.allocator);
fn addAllLinuxAssmeblies(b: *Builder, path: []const u8) !std.ArrayList(std.Build.LazyPath) {
const allocator = b.allocator;
var linux_assemblies = std.ArrayList(std.Build.LazyPath).init(allocator);
errdefer linux_assemblies.deinit();
var dir = try std.fs.cwd().openIterableDir("src", .{ });
var dir = try std.fs.cwd().openIterableDir(path, .{ });
var it = dir.iterate();
while (try it.next()) |file| {
if (file.kind != .file) continue;
@ -26,8 +28,8 @@ fn addAllLinuxAssmeblies(b: *Builder) !std.ArrayList(std.Build.LazyPath) {
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);
const assembly_path = try std.fs.path.join(allocator, &.{ path, file.name });
defer allocator.free(assembly_path);
try linux_assemblies.append(try addLinuxAssembly(b, assembly_path));
}
@ -39,32 +41,32 @@ pub fn build(b: *Builder) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
var linux_assemblies = try addAllLinuxAssmeblies(b);
defer linux_assemblies.deinit();
const allocator = b.allocator;
var dir = try std.fs.cwd().openIterableDir("src/tests", .{ });
var dir = try std.fs.cwd().openIterableDir("src", .{ });
var it = dir.iterate();
while (try it.next()) |file| {
if (file.kind != .file) continue;
while (try it.next()) |entry| {
if (entry.kind != .directory) continue;
const ext = std.fs.path.extension(file.name);
if (!std.mem.eql(u8, ext, ".c")) continue;
const program_dir = try std.fs.path.join(allocator, &.{ "src", entry.name });
defer allocator.free(program_dir);
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 main_c = try std.fs.path.join(allocator, &.{ program_dir, "main.c" });
defer allocator.free(main_c);
const exe = b.addExecutable(.{
.name = executable_name,
.root_source_file = .{ .path = source_file_path },
.name = entry.name,
.root_source_file = .{ .path = main_c },
.optimize = optimize,
.target = target
});
exe.addIncludePath(.{ .path = program_dir });
exe.addIncludePath(.{ .path = "src" });
exe.linkLibC();
for (linux_assemblies.items) |obj| {
var assemblies = try addAllLinuxAssmeblies(b, program_dir);
defer assemblies.deinit();
for (assemblies.items) |obj| {
exe.addObjectFile(obj);
}
@ -75,9 +77,9 @@ pub fn build(b: *Builder) !void {
run_exe.addArgs(args);
}
const step_description = try std.fmt.allocPrint(b.allocator, "Run '{s}' test", .{source_file_path});
const step_description = try std.fmt.allocPrint(b.allocator, "Run '{s}' test", .{ entry.name });
defer b.allocator.free(step_description);
const run_step = b.step(executable_name, step_description);
const run_step = b.step(entry.name, step_description);
run_step.dependOn(&run_exe.step);
}
}

View File

@ -119,3 +119,4 @@ int main(int argc, char **argv) {
return 0;
}

View File

@ -54,3 +54,4 @@ int main() {
return 0;
}

View File

@ -38,3 +38,4 @@ int main() {
return 0;
}

View File

@ -38,3 +38,4 @@ int main() {
return 0;
}

View File

@ -37,3 +37,4 @@ int main() {
return 0;
}

View File

@ -1,64 +0,0 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include "repetition_tester.c"
#define RPROF_IMPLEMENTATION
#include "rprof.h"
#include "main_read_file.c"
#include "main_write_bytes_asm.c"
#include "main_write_all_bytes.c"
#include "main_malloc_read.c"
#include "main_write_backward.c"
#include "main_write_loop.c"
#include "main_load_uop.c"
#include "main_store_uop.c"
#include "main_short_load_uop.c"
#include "main_read_widths.c"
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage: %s <test-name>\n", argv[0]);
return -1;
}
char *test_name = argv[1];
if (!strncmp(test_name, "write_bytes_asm", sizeof("write_bytes_asm"))) {
return main_test_write_bytes_asm();
} else if (!strncmp(test_name, "read_widths", sizeof("read_widths"))) {
return main_test_read_widths();
} else if (!strncmp(test_name, "write_all_bytes", sizeof("write_bytes"))) {
return main_test_write_all_bytes();
} else if (!strncmp(test_name, "load_uop", sizeof("load_uop"))) {
return main_test_load_uop();
} else if (!strncmp(test_name, "store_uop", sizeof("store_uop"))) {
return main_test_store_uop();
} else if (!strncmp(test_name, "short_load_uop", sizeof("short_load_uop"))) {
return main_test_short_load_uop();
} else if (!strncmp(test_name, "write_loop", sizeof("write_loop"))) {
return main_test_write_loop();
} else if (!strncmp(test_name, "write_backward", sizeof("write_backward"))) {
return main_test_write_backward();
} else if (!strncmp(test_name, "read_file", sizeof("read_file"))) {
if (argc < 3) {
printf("Usage: %s read_file <filename>\n", argv[0]);
return -1;
}
return main_test_read_file(argv[2]);
} else if (!strncmp(test_name, "malloc_read", sizeof("malloc_read"))) {
if (argc < 3) {
printf("Usage: %s malloc_read <filename>\n", argv[0]);
return -1;
}
return main_test_malloc_read(argv[2]);
} else {
printf("ERROR: Unknown test case '%s'\n", test_name);
return -1;
}
}