From 3bf48e29d2919b98ffdf0d939c89c640b96c0e96 Mon Sep 17 00:00:00 2001 From: A1029384756 Date: Sun, 14 Jun 2026 23:55:36 -0400 Subject: [PATCH 1/3] [subprocess] move away from `system` for `odin run` --- src/main.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6589efb54..a4acf1481 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -178,11 +178,41 @@ gb_internal i32 system_exec_command_line_app(char const *name, char const *fmt, return exit_code; } -gb_internal void system_must_exec_command_line_app(char const *name, char const *fmt, ...) { - va_list va; - va_start(va, fmt); - system_exec_command_line_app_internal(/* exit_on_err= */ true, name, fmt, va); - va_end(va); +#if defined(GB_SYSTEM_WINDOWS) +#include +#else +#include +extern char **environ; +#endif + +int run_subprocess(const char *name, const char **args) { +#if defined(GB_SYSTEM_WINDOWS) + return (int)_spawnvp(_P_WAIT, name, args); +#else + pid_t pid; + int status; + status = posix_spawn(&pid, name, NULL, NULL, (char *const *)args, environ); + if (status != 0) { + gb_printf_err("Could not spawn subprocess: %s\n", strerror(errno)); + return -1; + } + + for (;;) { + if (waitpid(pid, &status, WUNTRACED) < 0) { + gb_printf_err("Could not wait on subprocess: (pid: %d): %s\n", pid, strerror(errno)); + return -1; + } + + if (WIFEXITED(status)) { + return WEXITSTATUS(status); + } else if (WIFSIGNALED(status)) { + return -1; + } else if (WIFSTOPPED(status)) { + return -1; + } + } + GB_PANIC("Subprocess failure"); +#endif } #if defined(GB_SYSTEM_WINDOWS) @@ -3621,6 +3651,8 @@ int main(int arg_count, char const **arg_ptr) { init_build_context_error_pos_style(); Array args = setup_args(arg_count, arg_ptr); + Array run_args = array_make(heap_allocator(), 0, arg_count); + defer (array_free(&run_args)); String command = args[1]; String init_filename = {}; @@ -3648,9 +3680,6 @@ int main(int arg_count, char const **arg_ptr) { build_context.command_kind = Command_test; } - Array run_args = array_make(heap_allocator(), 0, arg_count); - defer (array_free(&run_args)); - isize run_args_start_idx = -1; for_array(i, args) { if (args[i] == "--") { @@ -4261,8 +4290,23 @@ end_of_code_gen:; String exe_name = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_Output]); defer (gb_free(heap_allocator(), exe_name.text)); - system_must_exec_command_line_app("odin run", "\"%.*s\" %.*s", LIT(exe_name), LIT(run_args_string)); + const char* exe_name_cstring = alloc_cstring(heap_allocator(), exe_name); + Array run_args_cstring = array_make(heap_allocator(), 0, run_args.count); + defer({ + for_array(i, run_args_cstring) { gb_free(heap_allocator(), (void*)run_args_cstring[i]); } + array_free(&run_args_cstring); + }); + array_add(&run_args_cstring, exe_name_cstring); + for_array(i, run_args) { + array_add(&run_args_cstring, alloc_cstring(heap_allocator(), run_args[i])); + } + array_add(&run_args_cstring, NULL); + + int subprocess_res = run_subprocess(exe_name_cstring, run_args_cstring.data); + if (subprocess_res) { + gb_exit(subprocess_res); + } if (!build_context.keep_executable) { char const *filename = cast(char const *)exe_name.text; gb_file_remove(filename); From 1b4e277f585072672006fc0c3dac923c83bb9f0e Mon Sep 17 00:00:00 2001 From: A1029384756 Date: Mon, 15 Jun 2026 22:59:58 -0400 Subject: [PATCH 2/3] [subprocess] removed unused variable --- src/main.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a4acf1481..631ace841 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3656,7 +3656,6 @@ int main(int arg_count, char const **arg_ptr) { String command = args[1]; String init_filename = {}; - String run_args_string = {}; isize last_non_run_arg = args.count; for_array(i, args) { @@ -3701,7 +3700,6 @@ int main(int arg_count, char const **arg_ptr) { } } args = array_slice(args, 0, last_non_run_arg); - run_args_string = string_join_and_quote(heap_allocator(), run_args); init_filename = args[2]; run_output = true; From 32db7d8d562e4e9846f1d781aef1cbcb2415b5c8 Mon Sep 17 00:00:00 2001 From: A1029384756 Date: Wed, 17 Jun 2026 00:12:36 -0400 Subject: [PATCH 3/3] [subprocess] remove `p` as `PATH` is not needed to find executable --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 631ace841..9b85fd3b7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -187,7 +187,7 @@ extern char **environ; int run_subprocess(const char *name, const char **args) { #if defined(GB_SYSTEM_WINDOWS) - return (int)_spawnvp(_P_WAIT, name, args); + return (int)_spawnv(_P_WAIT, name, args); #else pid_t pid; int status;