From 6e38cb160e885d896bda8d72d7b072a11d0de17b Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 5 Jan 2024 13:41:29 +0100 Subject: [PATCH] Port bld cache to linux successfuly --- bld_file.cpp | 37 ++++++++++++++++++++++++++++++++++--- bld_lib.cpp | 19 ++++++++++++++++++- bld_main.cpp | 11 +++-------- build.sh | 2 +- preproc_env.h | 20 ++++++++++++++++++++ 5 files changed, 76 insertions(+), 13 deletions(-) diff --git a/bld_file.cpp b/bld_file.cpp index 8e15d1d..303aa9b 100644 --- a/bld_file.cpp +++ b/bld_file.cpp @@ -1,5 +1,35 @@ #include "bld_lib.cpp" +Strs cc = ON_WINDOWS("cl") ON_MAC("clang") ON_LINUX("gcc"); + +int CompileFiles(Strs files) { + int result = 0; + Str exe = FilenameWithoutExt(files[0]); + Str filestr = Merge(files); + if (cc == "gcc") { + result = OS_SystemF("g++ -o %Q.exe %Q -g -Wno-write-strings", exe, filestr); + } + else if (cc == "clang") { + result = OS_SystemF("clang++ -o %Q.exe %Q -g -Wno-writable-strings", exe, filestr); + } + else { + result = OS_SystemF("cl -Fe:%Q.exe %Q -Zi -WX -W3 -wd4200 -diagnostics:column -nologo -D_CRT_SECURE_NO_WARNINGS", exe, filestr); + } + return result; +} + +int Main() { + Strs files = ListDir("../test"); + For(files) { + if (S8_Find(it, "test_"_s, 0, 0)) { + CompileFiles(it); + } + } + + return 0; +} + +#if 0 // msvc flag reference int Main() { bool debug = true; bool release = !debug; @@ -9,7 +39,7 @@ int Main() { Strs flags = Split("-WX -W3 -wd4200 -diagnostics:column -nologo -Z7 -FC -GF -Gm- -Oi -Zo -D_CRT_SECURE_NO_WARNINGS"); Strs link = Split("-link -incremental:no"); Strs files = Split("../test/main_core_as_header.cpp ../core.c"); - // files += IfCodeWasModified("../core.c", "../core.obj"); + // files += IfCodeWasModified("../core.c", "../core.obj"); // .o for linux! if (use_std) { flags += Split("-GR- -EHa-"); } @@ -32,8 +62,9 @@ int Main() { if (error != 0) return error; } - objs += S8_Format(Perm, "../build/%Q.obj", FilenameWithoutExt(it)); + objs += S8_Format(Perm, "../build/%Q.obj", FilenameWithoutExt(it)); // .o for linux! } int error = Run(cc + objs + flags + link); return error; -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/bld_lib.cpp b/bld_lib.cpp index 1079e36..f7f403e 100644 --- a/bld_lib.cpp +++ b/bld_lib.cpp @@ -121,7 +121,7 @@ bool SRC_WasModified(S8_String file) { S8_String without_ext = S8_ChopLastPeriod(file); S8_String name_only = S8_SkipToLastSlash(without_ext); - S8_String obj = S8_Format(Perm, "%.*s.obj", S8_Expand(name_only)); + S8_String obj = S8_Format(Perm, "%.*s.%s", S8_Expand(name_only), IF_WINDOWS_ELSE("obj", "o")); bool modified = OS_FileExists(obj) == false; SRC_CacheEntry *in_memory = SRC_HashFile(file, 0); @@ -150,9 +150,19 @@ struct Strs : Array { *this = {}; this->add(S8_MakeFromChar(str)); } + Strs(Str a) { + *this = {}; + this->add(a); + } Strs(Array a) { MA_MemoryCopy(this, &a, sizeof(a)); } }; +bool operator==(Strs a, char *b) { + if (a.len != 1) return false; + bool result = a[0] == S8_MakeFromChar(b); + return result; +} + Strs operator+(Strs a, Strs b) { Strs c = {a.copy(*Perm)}; c.add_array(b); @@ -258,6 +268,13 @@ int Run(Strs s) { return OS_SystemF("%Q", cmd); } +Strs ListDir(char *dir) { + Strs result = {}; + S8_List files = OS_ListDir(Perm, S8_MakeFromChar(dir), 0); + S8_For(it, files) result.add(it->string); + return result; +} + #ifndef BLD_MAIN int Main(); int main() { diff --git a/bld_main.cpp b/bld_main.cpp index 798d31d..c785aaa 100644 --- a/bld_main.cpp +++ b/bld_main.cpp @@ -6,13 +6,7 @@ int main(int argument_count, char **arguments) { OS_SetWorkingDir(S8_Lit("build")); IO_Printf("WORKING DIR: %Q\n", OS_GetWorkingDir(Perm)); -#if OS_LINUX - S8_String cc = "gcc"_s; -#elif OS_WINDOWS - S8_String cc = "cl"_s; -#else - S8_String cc = "clang"_s; -#endif + S8_String cc = ON_WINDOWS("cl"_s) ON_MAC("clang"_s) ON_LINUX("gcc"_s); for (int i = 1; i < argument_count; i += 1) { S8_String arg = S8_MakeFromChar(arguments[i]); @@ -54,7 +48,8 @@ int main(int argument_count, char **arguments) { } else { IO_Assert(cc == "gcc"_s); - result = OS_SystemF("gcc -Wno-write-strings %Q -o %Q -g", build_file, exe_name); + if (result == 0) result = OS_SystemF("gcc -c -Wno-write-strings %Q -o %Q.o -g", build_file, b); + if (result == 0) result = OS_SystemF("gcc -Wno-write-strings %Q.o -o %Q -g", b, exe_name); } if (result != 0) { diff --git a/build.sh b/build.sh index 5f29ccf..1384491 100644 --- a/build.sh +++ b/build.sh @@ -2,7 +2,7 @@ mkdir build cd build set -e -gcc -o bld ../bld_main.cpp -fno-exceptions -fno-rtti -Wno-write-strings +gcc -o bld ../bld_main.cpp -g -fno-exceptions -fno-rtti -Wno-write-strings cd .. ./build/bld # gcc -o test_filesystem ../test/test_filesystem.c -Wno-write-strings diff --git a/preproc_env.h b/preproc_env.h index 5253ba0..53f4e85 100644 --- a/preproc_env.h +++ b/preproc_env.h @@ -80,6 +80,26 @@ #define FORCE_INLINE inline #endif +#if OS_MAC + #define ON_MAC(x) x +#else + #define ON_MAC(x) +#endif + +#if OS_WINDOWS + #define ON_WINDOWS(x) x + #define IF_WINDOWS_ELSE(x, y) x +#else + #define ON_WINDOWS(x) + #define IF_WINDOWS_ELSE(x, y) y +#endif + +#if OS_LINUX + #define ON_LINUX(x) x +#else + #define ON_LINUX(x) +#endif + // #if COMPILER_CLANG // #pragma clang diagnostic push // #pragma clang diagnostic ignored "-Wmicrosoft-enum-forward-reference"