diff --git a/build_file.cpp b/build_file.cpp index 81f88ef..97a8cee 100644 --- a/build_file.cpp +++ b/build_file.cpp @@ -1,66 +1,116 @@ #include "build_tool/library.cpp" -void Compile(S8_String cc, S8_String files); -int ReturnValue = 0; - int main(int argc, char **argv) { - Array cmd = CMD_Make(argv, argc); - S8_String cc = CMD_Get(cmd, "cc", IF_WINDOWS("cl") IF_MAC("clang") IF_LINUX("gcc")); + MA_Scratch scratch; + S8_String working_dir = OS_GetWorkingDir(scratch); - Compile(cc, "../tests/test_thread.cpp"); - // Compile(cc, "../tests/test_main.cpp"); - // Compile(cc, "../tests/test_filesystem.c"); + Array files = {scratch}; + files.add("../../tests/test_main.cpp"); + files.add("../../tests/test_filesystem.c"); - return ReturnValue; -} + Array exes = {scratch}; + Array processes = {scratch}; -void Compile(S8_String cc, S8_String file) { + For2(file, files) { + S8_String name_no_ext = S8_GetNameNoExt(file); + S8_String exe = Fmt("%.*s.exe", S8_Expand(name_no_ext)); + bool is_cpp = S8_EndsWith(file, ".cpp"); + +#if OS_WINDOWS + { + S8_String cc = "cl"; + + Array flags = {scratch}; + flags += "/MP /Zi -D_CRT_SECURE_NO_WARNINGS"; + flags += "/FC /WX /W3 /wd4200 /diagnostics:column /nologo"; + flags += "/GF /Gm- /Oi"; + flags += "/GR- /EHa-"; + flags += Fmt("/Fe:%.*s", S8_Expand(exe)); + + Array debug = {scratch}; + debug += "/D_DEBUG -RTC1 -Od"; + + Array release = {scratch}; + release += "-O2 -MT -DNDEBUG -GL"; + + Array link = {scratch}; + link += "/link /incremental:no"; + + S8_String dir_debug = Fmt("%.*s/%.*s_cl_debug_" OS_NAME, S8_Expand(working_dir), S8_Expand(name_no_ext)); + S8_String dir_release = Fmt("%.*s/%.*s_cl_release_" OS_NAME, S8_Expand(working_dir), S8_Expand(name_no_ext)); + + Array cmd_debug = cc + file + flags + debug + link; + Array cmd_release = cc + file + flags + release + link; + + Process p1 = RunEx(cmd_debug, dir_debug); + Process p2 = RunEx(cmd_release, dir_release); + + processes.add(p1); + processes.add(p2); + + exes.add(Fmt("%.*s/%.*s", S8_Expand(dir_debug), S8_Expand(exe))); + exes.add(Fmt("%.*s/%.*s", S8_Expand(dir_release), S8_Expand(exe))); + } +#endif + { + S8_String cc = is_cpp ? "clang++" : "clang"; + + Array flags = {scratch}; + flags += "-g -Wno-write-strings"; + flags += "-fdiagnostics-absolute-paths"; + flags += "-fsanitize=address"; + if (is_cpp) flags += "-std=c++11"; + flags += Fmt("-o %.*s", S8_Expand(exe)); + + S8_String dir = Fmt("%.*s/%.*s_debug_clang_" OS_NAME, S8_Expand(working_dir), S8_Expand(name_no_ext)); + Process p = RunEx(cc + file + flags, dir); + processes.add(p); + exes.add(Fmt("%.*s/%.*s", S8_Expand(dir), S8_Expand(exe))); + } +#if OS_LINUX + { + S8_String cc = is_cpp ? "g++" : "gcc"; + Array flags = {scratch}; + flags += "-g -Wno-write-strings"; + flags += "-fsanitize=address"; + if (is_cpp) flags += "-std=c++11"; + flags += Fmt("-o %.*s", S8_Expand(exe)); + + S8_String dir = Fmt("%.*s/%.*s_debug_gcc_" OS_NAME, S8_Expand(working_dir), S8_Expand(name_no_ext)); + Process p = RunEx(cc + file + flags, dir); + processes.add(p); + exes.add(Fmt("%.*s/%.*s", S8_Expand(dir), S8_Expand(exe))); + } +#endif + } + + // + // Wait for all compilation to finalize + // int result = 0; - - S8_String name_no_ext = S8_GetNameNoExt(file); - S8_String exe = Fmt("%.*s.exe", S8_Expand(name_no_ext)); - bool is_cpp = S8_EndsWith(file, ".cpp"); - - if (cc == "cl") { - - Array flags = {Perm}; - flags += "/MP /Zi -D_CRT_SECURE_NO_WARNINGS"; - flags += "/FC /WX /W3 /wd4200 /diagnostics:column /nologo"; - flags += "/GF /Gm- /Oi"; - flags += "/GR- /EHa-"; - flags += "/D_DEBUG -RTC1 -Od"; - flags += Fmt("/Fe:%.*s", S8_Expand(exe)); - - Array link = {Perm}; - link += "/link /incremental:no"; - - result = Run(cc + file + flags + link); - } - else if (cc == "clang") { - if (is_cpp) cc = "clang++"; - - Array flags = {Perm}; - flags += "-g -Wno-write-strings"; - flags += "-fdiagnostics-absolute-paths"; - flags += "-fsanitize=address"; - if (is_cpp) flags += "-std=c++11"; - flags += Fmt("-o %.*s", S8_Expand(exe)); - - result = Run(cc + file + flags); - } - else { - IO_Assert(cc == "gcc"); - if (is_cpp) cc = "g++"; - - Array flags = {Perm}; - flags += "-g -Wno-write-strings"; - flags += "-fsanitize=address"; - if (is_cpp) flags += "-std=c++11"; - flags += Fmt("-o %.*s", S8_Expand(exe)); - - result = Run(cc + file + flags); + For(processes) { + int exit_code = Wait(&it); + if (exit_code != 0) result = exit_code; } - if (result == 0) result = OS_SystemF(IF_WINDOWS_ELSE("", "./") "%.*s", S8_Expand(exe)); - else ReturnValue = result; + // + // Run exes if there were no errors + // + if (result == 0) { + processes.reset(); + For(exes) { + Process p = RunEx(it); + processes.add(p); + } + + int i = 0; + For(processes) { + int exit_code = Wait(&it); + S8_String name = exes[i++]; + IO_Printf("%.*s - %d\n", S8_Expand(name), exit_code); + if (exit_code != 0) result = exit_code; + } + } + + return result; } diff --git a/build_tool/easy_strings.cpp b/build_tool/easy_strings.cpp index 6e582d1..59e9de5 100644 --- a/build_tool/easy_strings.cpp +++ b/build_tool/easy_strings.cpp @@ -79,11 +79,6 @@ S8_String Fmt(const char *str, ...) { return str_fmt; } -int Run(Array s) { - S8_String cmd = Merge(s); - return OS_SystemF("%.*s", S8_Expand(cmd)); -} - Array ListDir(char *dir) { Array result = {}; for (OS_FileIter it = OS_IterateFiles(Perm, S8_MakeFromChar(dir)); OS_IsValid(it); OS_Advance(&it)) { @@ -94,7 +89,7 @@ Array ListDir(char *dir) { Array CMD_Make(char **argv, int argc) { Array result = {Perm}; - IO_Printf("Command line arguments:\n"); + if (argc > 1) IO_Printf("Command line arguments:\n"); for (int i = 1; i < argc; i += 1) { S8_String it = S8_MakeFromChar(argv[i]); result.add(it); diff --git a/build_tool/library.cpp b/build_tool/library.cpp index 1306caa..f11548c 100644 --- a/build_tool/library.cpp +++ b/build_tool/library.cpp @@ -18,6 +18,7 @@ Table CMDLine; #include "cache.cpp" #include "easy_strings.cpp" +#include "process.cpp" S8_String CL_Flags = "/MP /Zi /FC /WX /W3 /wd4200 /diagnostics:column /nologo -D_CRT_SECURE_NO_WARNINGS /GF /Gm- /Oi"; S8_String CL_Link = "/link /incremental:no"; diff --git a/build_tool/main.cpp b/build_tool/main.cpp index cdcc903..9b515ec 100644 --- a/build_tool/main.cpp +++ b/build_tool/main.cpp @@ -49,7 +49,7 @@ int main(int argument_count, char **arguments) { } else { IO_Assert(cc == "gcc"); - result = OS_SystemF("gcc -Wno-write-strings %.*s -o %.*s -g", S8_Expand(build_file), S8_Expand(exe_name)); + result = OS_SystemF("g++ -Wno-write-strings %.*s -o %.*s -g", S8_Expand(build_file), S8_Expand(exe_name)); } if (result != 0) { diff --git a/build_tool/process.cpp b/build_tool/process.cpp index 83563a5..400064a 100644 --- a/build_tool/process.cpp +++ b/build_tool/process.cpp @@ -1,24 +1,20 @@ -struct TH_Process { +struct Process { bool is_valid; char platform[32]; }; #if OS_WINDOWS -TH_Process TH_CreateProcess(S8_String in_cmd, S8_String in_working_dir = "") { +Process RunEx(S8_String in_cmd) { MA_Scratch scratch; - if (in_working_dir != "" && !OS_IsAbsolute(in_working_dir)) { - in_working_dir = OS_GetAbsolutePath(scratch, in_working_dir); - } - wchar_t *application_name = NULL; wchar_t *cmd = S8_ToWidechar(scratch, in_cmd); BOOL inherit_handles = FALSE; DWORD creation_flags = 0; void *enviroment = NULL; - wchar_t *working_dir = in_working_dir == "" ? NULL : S8_ToWidechar(scratch, in_working_dir); + wchar_t *working_dir = NULL; STARTUPINFOW startup_info = {}; startup_info.cb = sizeof(STARTUPINFOW); - TH_Process result = {}; + Process result = {}; IO_Assert(sizeof(result.platform) >= sizeof(PROCESS_INFORMATION)); PROCESS_INFORMATION *process_info = (PROCESS_INFORMATION *)result.platform; BOOL success = CreateProcessW(application_name, cmd, NULL, NULL, inherit_handles, creation_flags, enviroment, working_dir, &startup_info, process_info); @@ -32,12 +28,12 @@ TH_Process TH_CreateProcess(S8_String in_cmd, S8_String in_working_dir = "") { NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); LocalFree(lpMsgBuf); - IO_FatalErrorf("Failed to create process \nworking_dir: %.*s\ncmd: %.*s\nwindows_message: %s", S8_Expand(in_working_dir), S8_Expand(in_cmd), lpMsgBuf); + IO_FatalErrorf("Failed to create process \ncmd: %.*s\nwindows_message: %s", S8_Expand(in_cmd), lpMsgBuf); } return result; } -int TH_WaitForProcessExit(TH_Process *process) { +int Wait(Process *process) { IO_Assert(process->is_valid); PROCESS_INFORMATION *pi = (PROCESS_INFORMATION *)process->platform; WaitForSingleObject(pi->hProcess, INFINITE); @@ -61,13 +57,9 @@ struct TH_UnixProcess { extern char **environ; -TH_Process TH_CreateProcess(S8_String cmd, S8_String working_dir = "") { +Process RunEx(S8_String cmd) { MA_Scratch scratch; - if (working_dir != "" && !OS_IsAbsolute(working_dir)) { - working_dir = OS_GetAbsolutePath(scratch, working_dir); - } - - TH_Process result = {}; + Process result = {}; IO_Assert(sizeof(result.platform) >= sizeof(TH_UnixProcess)); TH_UnixProcess *u = (TH_UnixProcess *)result.platform; @@ -101,23 +93,19 @@ TH_Process TH_CreateProcess(S8_String cmd, S8_String working_dir = "") { args.add(NULL); } - S8_String prev_dir = {}; - if (working_dir != "") prev_dir = OS_GetWorkingDir(scratch); - if (working_dir != "") OS_SetWorkingDir(working_dir); int err = posix_spawnp(&u->pid, exec_file.str, NULL, NULL, args.data, environ); if (err == 0) { result.is_valid = true; } else { - perror("Failed to create process"); - IO_FatalErrorf("Failed to create process \nworking_dir: %.*s\ncmd: %.*s", S8_Expand(working_dir), S8_Expand(cmd)); + perror("posix_spawnp error"); + IO_FatalErrorf("Failed to create process, cmd: %.*s", S8_Expand(cmd)); } - if (working_dir != "") OS_SetWorkingDir(prev_dir); return result; } -int TH_WaitForProcessExit(TH_Process *process) { +int Wait(Process *process) { if (!process->is_valid) return 1; TH_UnixProcess *u = (TH_UnixProcess *)process->platform; @@ -137,3 +125,31 @@ int TH_WaitForProcessExit(TH_Process *process) { return result; } #endif + +Process RunEx(Array s) { + S8_String cmd = Merge(s); + Process proc = RunEx(cmd); + return proc; +} + +Process RunEx(Array s, S8_String process_start_dir) { + OS_MakeDir(process_start_dir); + S8_String working_dir = OS_GetWorkingDir(Perm); + OS_SetWorkingDir(process_start_dir); + S8_String cmd = Merge(s); + Process proc = RunEx(cmd); + OS_SetWorkingDir(working_dir); + return proc; +} + +int Run(S8_String cmd) { + Process process = RunEx(cmd); + int result = Wait(&process); + return result; +} + +int Run(Array cmd) { + S8_String cmds = Merge(cmd); + int result = Run(cmds); + return result; +} diff --git a/tests/test_thread.cpp b/tests/test_thread.cpp index faf2d8f..bd7576a 100644 --- a/tests/test_thread.cpp +++ b/tests/test_thread.cpp @@ -1,128 +1 @@ #include "../build_tool/library.cpp" -#include "../build_tool/process.cpp" - -int main() { - MA_Scratch scratch; - S8_String working_dir = OS_GetWorkingDir(scratch); - - Array exes = {scratch}; - Array proc = {scratch}; - Array files = {scratch}; - files.add("../../tests/test_main.cpp"); - files.add("../../tests/test_filesystem.c"); - - For2(file, files) { - S8_String name_no_ext = S8_GetNameNoExt(file); - S8_String exe = Fmt("%.*s.exe", S8_Expand(name_no_ext)); - bool is_cpp = S8_EndsWith(file, ".cpp"); - -#if OS_WINDOWS - // Setup CL Debug - { - S8_String cc = "cl"; - - Array flags = {scratch}; - flags += "/MP /Zi -D_CRT_SECURE_NO_WARNINGS"; - flags += "/FC /WX /W3 /wd4200 /diagnostics:column /nologo"; - flags += "/GF /Gm- /Oi"; - flags += "/GR- /EHa-"; - flags += Fmt("/Fe:%.*s", S8_Expand(exe)); - - Array debug = {scratch}; - debug += "/D_DEBUG -RTC1 -Od"; - - Array release = {scratch}; - release += "-O2 -MT -DNDEBUG -GL"; - - Array link = {scratch}; - link += "/link /incremental:no"; - - S8_String dir_debug = Fmt("%.*s/%.*s_cl_debug_" OS_NAME, S8_Expand(working_dir), S8_Expand(name_no_ext)); - S8_String dir_release = Fmt("%.*s/%.*s_cl_release_" OS_NAME, S8_Expand(working_dir), S8_Expand(name_no_ext)); - - OS_MakeDir(dir_debug); - OS_MakeDir(dir_release); - - Array cmd_debug = cc + file + flags + debug + link; - Array cmd_release = cc + file + flags + release + link; - - S8_String cmd_debug_m = Merge(cmd_debug); - S8_String cmd_release_m = Merge(cmd_release); - - TH_Process p1 = TH_CreateProcess(cmd_debug_m, dir_debug); - TH_Process p2 = TH_CreateProcess(cmd_release_m, dir_release); - - proc.add(p1); - proc.add(p2); - - exes.add(Fmt("%.*s/%.*s", S8_Expand(dir_debug), S8_Expand(exe))); - exes.add(Fmt("%.*s/%.*s", S8_Expand(dir_release), S8_Expand(exe))); - } -#endif - - { - S8_String cc = is_cpp ? "clang++" : "clang"; - - Array flags = {scratch}; - flags += "-g -Wno-write-strings"; - flags += "-fdiagnostics-absolute-paths"; - flags += "-fsanitize=address"; - if (is_cpp) flags += "-std=c++11"; - flags += Fmt("-o %.*s", S8_Expand(exe)); - - S8_String dir = Fmt("%.*s/%.*s_debug_clang_" OS_NAME, S8_Expand(working_dir), S8_Expand(name_no_ext)); - OS_MakeDir(dir); - - S8_String cmd = Merge(cc + file + flags); - TH_Process p = TH_CreateProcess(cmd, dir); - proc.add(p); - exes.add(Fmt("%.*s/%.*s", S8_Expand(dir), S8_Expand(exe))); - } - -#if OS_LINUX - { - S8_String cc = is_cpp ? "g++" : "gcc"; - Array flags = {scratch}; - flags += "-g -Wno-write-strings"; - flags += "-fsanitize=address"; - if (is_cpp) flags += "-std=c++11"; - flags += Fmt("-o %.*s", S8_Expand(exe)); - - S8_String dir = Fmt("%.*s/%.*s_debug_gcc_" OS_NAME, S8_Expand(working_dir), S8_Expand(name_no_ext)); - OS_MakeDir(dir); - - S8_String cmd = Merge(cc + file + flags); - TH_Process p = TH_CreateProcess(cmd, dir); - proc.add(p); - exes.add(Fmt("%.*s/%.*s", S8_Expand(dir), S8_Expand(exe))); - } -#endif - } - - // - // Wait for all compilation to finalize - // - int result = 0; - For(proc) { - int exit_code = TH_WaitForProcessExit(&it); - if (exit_code != 0) result = exit_code; - } - - // - // Run exes if there were no errors - // - if (result == 0) { - proc.reset(); - For(exes) { - TH_Process p = TH_CreateProcess(it); - proc.add(p); - } - - For(proc) { - int exit_code = TH_WaitForProcessExit(&it); - if (exit_code != 0) result = exit_code; - } - } - - return result; -} \ No newline at end of file