From 5c9eb264e85846788db1e1e9a29953855eaa2062 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Tue, 13 Jun 2023 14:54:11 +0200 Subject: [PATCH] Delete object files after linking. --- releasenotes.md | 2 ++ src/compiler/compiler.c | 15 +++++++++++++++ src/utils/file_utils.c | 34 +++++++++++++++++++++++++--------- src/utils/lib.h | 1 + 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/releasenotes.md b/releasenotes.md index b8d904698..af7c0d902 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -3,6 +3,8 @@ ## 0.5.0 Change List ### Changes / improvements +- Temporary objects may now invoke methods using ref parameters. +- Delete object files after successful linking. - `@if` introduced, other top level conditional compilation removed. - `@dynamic` and `@interface` for dynamic dispatch. - `$if` now uses `$if :` syntax. diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 426b58237..a7350d440 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -281,6 +281,14 @@ static void compiler_print_bench(void) } } +void delete_object_files(const char **files, size_t count) +{ + for (size_t i = 0; i < count; i++) + { + file_delete_file(files[i]); + } +} + void compiler_compile(void) { sema_analysis_run(); @@ -464,6 +472,7 @@ void compiler_compile(void) platform_linker(output_exe, obj_files, output_file_count); compiler_link_time = bench_mark(); compiler_print_bench(); + delete_object_files(obj_files, output_file_count); } else { @@ -474,6 +483,10 @@ void compiler_compile(void) printf("No linking is performed due to missing linker support.\n"); active_target.run_after_compile = false; } + else + { + delete_object_files(obj_files, output_file_count); + } } if (active_target.run_after_compile) @@ -490,6 +503,7 @@ void compiler_compile(void) { error_exit("Failed to produce static library '%s'.", output_static); } + delete_object_files(obj_files, output_file_count); compiler_link_time = bench_mark(); compiler_print_bench(); printf("Static library '%s' created.", output_static); @@ -500,6 +514,7 @@ void compiler_compile(void) { error_exit("Failed to produce static library '%s'.", output_dynamic); } + delete_object_files(obj_files, output_file_count); printf("Dynamic library '%s' created.", output_dynamic); compiler_link_time = bench_mark(); compiler_print_bench(); diff --git a/src/utils/file_utils.c b/src/utils/file_utils.c index c6585813d..365e158ab 100644 --- a/src/utils/file_utils.c +++ b/src/utils/file_utils.c @@ -419,10 +419,20 @@ extern int _getdrive(void); extern int _chdrive(int drive); #endif +bool file_delete_file(const char *path) +{ + assert(path); +#if (_MSC_VER) + return DeleteFileW(win_utf8to16(path)); +#else + return !unlink(path); +#endif +} + bool file_delete_all_files_in_dir_with_suffix(const char *path, const char *suffix) { assert(path); -#if PLATFORM_WINDOWS +#if (_MSC_VER) const char *cmd = "del /q"; #else const char *cmd = "rm -f"; @@ -508,30 +518,37 @@ void file_add_wildcard_files(const char ***files, const char *path, bool recursi #endif -#if PLATFORM_WINDOWS -const char *execute_cmd(const char *cmd) -{ - FATAL_ERROR("Not implemented"); -} -#else #define BUFSIZE 1024 const char *execute_cmd(const char *cmd) { char buffer[BUFSIZE]; char *output = ""; FILE *process = NULL; +#if (_MSC_VER) + if (!(process = _wpopen(win_utf8to16(cmd), L"r"))) + { + error_exit("Failed to open a pipe for command '%s'.", cmd); + } +#else if (!(process = popen(cmd, "r"))) { error_exit("Failed to open a pipe for command '%s'.", cmd); } +#endif while (fgets(buffer, BUFSIZE - 1, process)) { output = str_cat(output, buffer); } - if (pclose(process)) +#if PLATFORM_WINDOWS + int err = _pclose(process); +#else + int err = pclose(process); +#endif + if (err) { error_exit("Failed to execute '%s'.", cmd); } + while (output[0] != 0) { switch (output[0]) @@ -549,7 +566,6 @@ const char *execute_cmd(const char *cmd) } return str_trim(output); } -#endif #if PLATFORM_WINDOWS diff --git a/src/utils/lib.h b/src/utils/lib.h index 9e88d7c4b..d748bbab0 100644 --- a/src/utils/lib.h +++ b/src/utils/lib.h @@ -70,6 +70,7 @@ const char* file_expand_path(const char* path); const char* find_lib_dir(void); const char *find_rel_exe_dir(const char *dir); bool file_delete_all_files_in_dir_with_suffix(const char *dir, const char *suffix); +bool file_delete_file(const char *path); bool file_is_dir(const char *file); bool file_exists(const char *path); FILE *file_open_read(const char *path);