diff --git a/releasenotes.md b/releasenotes.md index aeb135668..bf6c886d5 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -21,6 +21,7 @@ - && doesn't work correctly with lambdas #1279. - Fix incorrect override of optimization levels when using projects. - Add experimental `@noalias` attribute. +- Add a `--run-once` option to delete the output file after running it. ### Fixes diff --git a/src/build/build.h b/src/build/build.h index 6b0907e28..4a585ac3f 100644 --- a/src/build/build.h +++ b/src/build/build.h @@ -410,6 +410,7 @@ typedef struct BuildOptions_ bool read_stdin; bool print_output; bool print_input; + bool run_once; const char *panicfn; const char *benchfn; const char *testfn; @@ -507,6 +508,7 @@ typedef struct const char *asm_file_dir; const char *script_dir; bool run_after_compile; + bool delete_after_run; bool generate_benchmark_runner; bool generate_test_runner; bool benchmark_output; @@ -651,4 +653,4 @@ bool command_accepts_files(CompilerCommand command); void update_build_target_with_opt_level(BuildTarget *target, OptimizationSetting level); void create_project(BuildOptions *build_options); void create_library(BuildOptions *build_options); -void resolve_libraries(BuildTarget *build_target); \ No newline at end of file +void resolve_libraries(BuildTarget *build_target); diff --git a/src/build/build_options.c b/src/build/build_options.c index 673488ddc..061433f83 100644 --- a/src/build/build_options.c +++ b/src/build/build_options.c @@ -90,6 +90,7 @@ static void usage(void) PRINTF(" --about - Prints a short description of C3."); PRINTF(" --symtab - Sets the preferred symtab size."); PRINTF(" --max-mem - Sets the preferred max memory size."); + PRINTF(" --run-once - After running the output file, delete it immediately."); PRINTF(" -V --version - Print version information."); PRINTF(" -E - Lex only."); PRINTF(" -P - Only parse and output the AST as JSON."); @@ -130,7 +131,6 @@ static void usage(void) PRINTF(" -g - Emit debug info."); PRINTF(" -g0 - Emit no debug info."); PRINTF(""); - PRINTF(""); PRINTF(" -l - Link with the library provided."); PRINTF(" -L - Append the directory to the linker search paths."); PRINTF(" -z - Send the as a parameter to the linker."); @@ -675,6 +675,10 @@ static void parse_option(BuildOptions *options) print_version(); exit_compiler(COMPILER_SUCCESS_EXIT); } + if (match_longopt("run-once")) { + options->run_once = true; + return; + } if ((argopt = match_argopt("fp-math"))) { options->fp_math = (FpOpt)parse_multi_option(argopt, 3, fp_math); diff --git a/src/build/builder.c b/src/build/builder.c index 2379ca5a7..971e89e3d 100644 --- a/src/build/builder.c +++ b/src/build/builder.c @@ -212,6 +212,7 @@ static void update_build_target_from_options(BuildTarget *target, BuildOptions * case COMMAND_COMPILE_RUN: case COMMAND_CLEAN_RUN: target->run_after_compile = true; + target->delete_after_run = options->run_once; break; case COMMAND_COMPILE_ONLY: target->type = TARGET_TYPE_OBJECT_FILES; @@ -447,4 +448,4 @@ void init_build_target(BuildTarget *target, BuildOptions *options) if (!file_is_dir(target->build_dir)) error_exit("Expected '%s' to be a directory.", target->build_dir); } load_library_files(); -} \ No newline at end of file +} diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index f5a5d8374..ee7612a87 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -652,6 +652,12 @@ void compiler_compile(void) printf("Launching %s...\n", name); int ret = system(name); + + if (active_target.delete_after_run) + { + file_delete_file(name); + } + #if PLATFORM_POSIX if (WIFEXITED(ret)) { @@ -1311,4 +1317,4 @@ int find_padding_length(const char** arr, const int count) } width += 2; return width; -} \ No newline at end of file +}