From 4e8db8dc6e96fac0e01a63d50f0f07a6066a8fc3 Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Sun, 4 Aug 2024 10:56:16 +0200 Subject: [PATCH] More fixes and improvements for the build program --- bootstrap/build.c | 64 +++++++++++++++++++++++++++++++++++++++++------ bootstrap/lib.h | 17 ++++++++++--- bootstrap/main.c | 12 --------- 3 files changed, 70 insertions(+), 23 deletions(-) diff --git a/bootstrap/build.c b/bootstrap/build.c index bf10c12..7cd8b2d 100644 --- a/bootstrap/build.c +++ b/bootstrap/build.c @@ -1,6 +1,7 @@ #include "lib.h" #define build_dir "build" +#define nest_dir "nest" typedef enum OptimizationMode : u8 { @@ -282,9 +283,10 @@ fn void run_tests(Arena* arena, TestOptions const * const test_options, char** e print("===========================\n\n"); String compiler_path = arena_join_string(arena, ((Slice(String)) array_to_slice(((String[]){ - strlit(build_dir "/"), - strlit("nest"), + strlit(build_dir "/" "nest"), + strlit("_"), optimization_string, + strlit("_"), linkage_string, })))); compile_options.out_path = string_to_c(compiler_path); @@ -299,6 +301,8 @@ fn void run_tests(Arena* arena, TestOptions const * const test_options, char** e { String test_path = test_options->test_paths.pointer[test_i]; char* test_path_c = string_to_c(test_path); + auto test_dir = string_no_extension(test_path); + auto test_name = string_base(test_dir); for (u32 engine_i = 0; engine_i < test_options->execution_engines.length; engine_i += 1) { @@ -323,12 +327,17 @@ fn void run_tests(Arena* arena, TestOptions const * const test_options, char** e 0, }; + run_command((CStringSlice) array_to_slice(arguments), envp); if (execution_engine != EXECUTION_ENGINE_INTERPRETER) { + String out_program = arena_join_string(arena, ((Slice(String)) array_to_slice(((String[]){ + strlit(nest_dir "/"), + test_name, + })))); char* run_arguments[] = { - compile_options.out_path, + string_to_c(out_program), 0, }; run_command((CStringSlice) array_to_slice(run_arguments), envp); @@ -350,6 +359,7 @@ int main(int argc, char* argv[], char** envp) ExecutionEngine preferred_execution_engine = EXECUTION_ENGINE_COUNT; Command command = COMMAND_COUNT; u8 test_every_config = 0; + String source_file_path = {}; for (int i = 1; i < argc; i += 1) { @@ -377,6 +387,20 @@ int main(int argc, char* argv[], char** envp) } } + if (argc > 2) + { + auto* c_argument = argv[2]; + auto argument = cstr(c_argument); + auto expected_start = strlit("tests/"); + if (expected_start.length < argument.length) + { + if (strncmp(c_argument, "tests/", sizeof("tests/") - 1) == 0) + { + source_file_path = argument; + } + } + } + if (command == COMMAND_COUNT) { print("Expected a command\n"); @@ -391,15 +415,20 @@ int main(int argc, char* argv[], char** envp) switch (command) { case COMMAND_DEBUG: + if (!source_file_path.pointer) + { + fail(); + } + compile_and_run(&(CompileOptions) { .in_path = compiler_source_path, - .out_path = build_dir "/" "nest", + .out_path = build_dir "/" "nest_O0_static", .compiler = default_compiler, .debug_info = 1, .error_on_warning = 0, .optimization_mode = O0, .linkage = LINKAGE_STATIC, - }, envp, EXECUTION_ENGINE_INTERPRETER, 1, "tests/first.nat"); + }, envp, EXECUTION_ENGINE_INTERPRETER, 1, string_to_c(source_file_path)); break; case COMMAND_RUN_TESTS: { @@ -408,13 +437,24 @@ int main(int argc, char* argv[], char** envp) static_assert(array_length(all_linkages) == LINKAGE_COUNT); OptimizationMode all_optimization_modes[] = { O0, O1, O2, O3, Os, Oz }; static_assert(array_length(all_optimization_modes) == OPTIMIZATION_COUNT); - String all_test_paths[] = { + String every_single_test[] = { strlit("tests/first.nat"), + strlit("tests/add_sub.nat"), + strlit("tests/mul.nat"), + strlit("tests/div.nat"), + strlit("tests/and.nat"), + strlit("tests/or.nat"), + strlit("tests/xor.nat"), + strlit("tests/return_var.nat"), + strlit("tests/return_mod_scope.nat"), + strlit("tests/shift_left.nat"), + strlit("tests/shift_right.nat"), + strlit("tests/thousand_simple_functions.nat"), + strlit("tests/simple_arg.nat"), }; ExecutionEngine all_execution_engines[] = { EXECUTION_ENGINE_INTERPRETER, EXECUTION_ENGINE_C }; static_assert(array_length(all_execution_engines) == EXECUTION_ENGINE_COUNT); - Slice(String) test_selection = (Slice(String)) array_to_slice(all_test_paths); Slice(Linkage) linkage_selection; Slice(OptimizationMode) optimization_selection; Slice(ExecutionEngine) execution_engine_selection; @@ -436,6 +476,16 @@ int main(int argc, char* argv[], char** envp) execution_engine_selection = (Slice(ExecutionEngine)) { .pointer = &preferred_execution_engine, .length = 1 }; } + Slice(String) test_selection; + if (source_file_path.pointer) + { + test_selection = (Slice(String)) { .pointer = &source_file_path, .length = 1 }; + } + else + { + test_selection = (Slice(String)) array_to_slice(every_single_test); + } + run_tests(arena, &(TestOptions) { .linkages = linkage_selection, .optimization_modes = optimization_selection, diff --git a/bootstrap/lib.h b/bootstrap/lib.h index 4afa343..aa6951e 100644 --- a/bootstrap/lib.h +++ b/bootstrap/lib.h @@ -1377,19 +1377,28 @@ fn void run_command(CStringSlice arguments, char* envp[]) int status = 0; int options = 0; pid_t result = syscall_waitpid(pid, &status, options); + int success = 0; if (result == pid) { - WIFEXITED(status); - auto exit_code = WEXITSTATUS(status); - if (exit_code != 0) + if (WIFEXITED(status)) { - trap(); + auto exit_code = WEXITSTATUS(status); + if (exit_code == 0) + { + success = 1; + } } } else { trap(); } + + if (!success) + { + print("Program failed to run!\n"); + fail(); + } } } diff --git a/bootstrap/main.c b/bootstrap/main.c index a60b06b..35f9e5b 100644 --- a/bootstrap/main.c +++ b/bootstrap/main.c @@ -4906,18 +4906,6 @@ void entry_point(int argc, const char* argv[]) (char*)c_source_path.pointer, 0, }; - print("Argument count: {u32}\n", (u32)array_length(command)); - - for (u32 i = 0; i < array_length(command); i += 1) - { - auto* arg = command[i]; - if (arg) - { - print("{cstr} ", arg); - } - } - - print("\n"); run_command((CStringSlice) array_to_slice(command), envp); } break;