From cc1a90e96bf6d95a4f52c8cf4f7069df960cc456 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 5 Jan 2024 14:12:32 +0100 Subject: [PATCH] using bld to build on both linux and windows --- bld_file.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++ bld_lib.cpp | 27 +++++++++++++++++------ bld_main.cpp | 11 +++++----- build.bat | 44 ++----------------------------------- build.sh | 2 +- clexer.c | 14 ++++++------ clexer.h | 2 +- test/main.c | 5 ----- test/main.cpp | 5 ----- test/test_filesystem.c | 2 +- 10 files changed, 86 insertions(+), 75 deletions(-) delete mode 100644 test/main.c delete mode 100644 test/main.cpp diff --git a/bld_file.cpp b/bld_file.cpp index 303aa9b..d6d887d 100644 --- a/bld_file.cpp +++ b/bld_file.cpp @@ -20,6 +20,7 @@ int CompileFiles(Strs files) { int Main() { Strs files = ListDir("../test"); + CompileFiles({"../test/main_core_as_header.cpp", "../core.c"}); For(files) { if (S8_Find(it, "test_"_s, 0, 0)) { CompileFiles(it); @@ -67,4 +68,52 @@ int Main() { int error = Run(cc + objs + flags + link); return error; } + +set DEBUG=-Od -RTC1 -D_DEBUG -MTd -fsanitize=address +set RELEASE=-O2 -MT -DNDEBUG -GL +set COMMON=-MP -FC -Z7 -GF -Gm- -Oi -Zo -EHa- -GR- +set WRN=-WX -W3 -wd4200 -diagnostics:column -nologo -D_CRT_SECURE_NO_WARNINGS +set LINK_DEBUG=-NODEFAULTLIB:LIBCMT +set LINK_RELEASE=-opt:ref -opt:icf -ltcg + +set DEBUG_LINE=%DEBUG% %WRN% %COMMON% -link -incremental:no %LINK_DEBUG% +set RELEASE_LINE=%RELEASE% %WRN% %COMMON% -link -incremental:no %LINK_RELEASE% + +mkdir build +cd build + +cl.exe -Fe:bld.exe ../bld_main.cpp %DEBUG_LINE% +if %errorlevel% neq 0 exit /b %errorlevel% +bld.exe +if %errorlevel% neq 0 exit /b %errorlevel% + +cl.exe -Fe:test_arena.exe ../test/test_arena.cpp %DEBUG_LINE% +if %errorlevel% neq 0 exit /b %errorlevel% +test_arena.exe +if %errorlevel% neq 0 exit /b %errorlevel% + +cl.exe -Fe:test_table.exe ../test/test_table.cpp %DEBUG_LINE% +if %errorlevel% neq 0 exit /b %errorlevel% +test_table.exe +if %errorlevel% neq 0 exit /b %errorlevel% + +cl.exe -Fe:test_array.exe ../test/test_array.cpp %DEBUG_LINE% +if %errorlevel% neq 0 exit /b %errorlevel% +test_array.exe +if %errorlevel% neq 0 exit /b %errorlevel% + +cl.exe -Fe:cpp_debug.exe ../test/main.cpp %DEBUG_LINE% +if %errorlevel% neq 0 exit /b %errorlevel% + +cl.exe -Fe:cpp_release.exe ../test/main.cpp %RELEASE_LINE% +if %errorlevel% neq 0 exit /b %errorlevel% +cl.exe -Fe:c_debug.exe ../test/main.c %DEBUG_LINE% +if %errorlevel% neq 0 exit /b %errorlevel% +cl.exe -Fe:main_core_as_header.exe ../core.c ../test/main_core_as_header.cpp %DEBUG_LINE% +if %errorlevel% neq 0 exit /b %errorlevel% +cd .. + +rem rtc1 - runtime error checks +rem gl - whole program optimizations + #endif \ No newline at end of file diff --git a/bld_lib.cpp b/bld_lib.cpp index f7f403e..134e419 100644 --- a/bld_lib.cpp +++ b/bld_lib.cpp @@ -108,7 +108,7 @@ SRC_CacheEntry *SRC_HashFile(S8_String file, char *parent_file) { return result; } -bool SRC_WasModified(S8_String file) { +bool SRC_WasModified(S8_String file, S8_String artifact_path) { double time_start = OS_GetTime(); if (OS_FileExists(file) == false) { @@ -121,8 +121,9 @@ 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.%s", S8_Expand(name_only), IF_WINDOWS_ELSE("obj", "o")); - bool modified = OS_FileExists(obj) == false; + + if (artifact_path.len == 0) artifact_path = S8_Format(Perm, "%.*s.%s", S8_Expand(name_only), IF_WINDOWS_ELSE("obj", "o")); + bool modified = OS_FileExists(artifact_path) == false; SRC_CacheEntry *in_memory = SRC_HashFile(file, 0); IO_Assert(in_memory); @@ -150,6 +151,17 @@ struct Strs : Array { *this = {}; this->add(S8_MakeFromChar(str)); } + Strs(char *a, char *b) { + *this = {}; + this->add(S8_MakeFromChar(a)); + this->add(S8_MakeFromChar(b)); + } + Strs(char *a, char *b, char *c) { + *this = {}; + this->add(S8_MakeFromChar(a)); + this->add(S8_MakeFromChar(b)); + this->add(S8_MakeFromChar(c)); + } Strs(Str a) { *this = {}; this->add(a); @@ -250,13 +262,14 @@ Str Merge(Strs list, Str separator = " "_s) { return string; } -bool CodeWasModified(char *str) { return SRC_WasModified(S8_MakeFromChar(str)); } -bool CodeWasModified(S8_String str) { return SRC_WasModified(str); } S8_String FilenameWithoutExt(S8_String it) { return S8_SkipToLastSlash(S8_ChopLastPeriod(it)); } +bool CodeWasModified(char *str, char *artifact = 0) { return SRC_WasModified(S8_MakeFromChar(str), S8_MakeFromChar(artifact)); } +bool CodeWasModified(S8_String str, S8_String artifact = {}) { return SRC_WasModified(str, artifact); } Strs IfCodeWasModified(char *cfile, char *objfile) { Strs result = {}; S8_String s = S8_MakeFromChar(cfile); - if (SRC_WasModified(s)) { + S8_String o = S8_MakeFromChar(objfile); + if (SRC_WasModified(s, o)) { return cfile; } return objfile; @@ -278,7 +291,7 @@ Strs ListDir(char *dir) { #ifndef BLD_MAIN int Main(); int main() { - SRC_InitCache(Perm, S8_Lit("buildfile.cache")); + SRC_InitCache(Perm, S8_Lit("bld_file.cache")); int result = Main(); if (result == 0) SRC_SaveCache(); } diff --git a/bld_main.cpp b/bld_main.cpp index c785aaa..d5dbccb 100644 --- a/bld_main.cpp +++ b/bld_main.cpp @@ -10,7 +10,7 @@ int main(int argument_count, char **arguments) { for (int i = 1; i < argument_count; i += 1) { S8_String arg = S8_MakeFromChar(arguments[i]); - if (arg == "clear_cache"_s) OS_DeleteFile("bld.cache"_s); + if (arg == "clear_cache"_s) OS_DeleteFile("bld_tool.cache"_s); } SRC_InitCache(Perm, S8_Lit("bld.cache")); @@ -37,7 +37,7 @@ int main(int argument_count, char **arguments) { // @todo: add search path? // Compile the build file only if code changed - if (SRC_WasModified(build_file)) { + if (SRC_WasModified(build_file, exe_name)) { double time = OS_GetTime(); int result = 0; if (cc == "cl"_s) { @@ -48,8 +48,7 @@ int main(int argument_count, char **arguments) { } else { IO_Assert(cc == "gcc"_s); - 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); + result = OS_SystemF("gcc -Wno-write-strings %Q -o %Q -g", build_file, exe_name); } if (result != 0) { @@ -64,9 +63,9 @@ int main(int argument_count, char **arguments) { double time = OS_GetTime(); if (build_file.str) { #if OS_WINDOWS - int result = OS_SystemF("%.*s", S8_Expand(exe_name)); + int result = OS_SystemF("%Q", exe_name); #else - int result = OS_SystemF("./%.*s", S8_Expand(exe_name)); + int result = OS_SystemF("./%Q", exe_name); #endif if (result != 0) { printf("FAILED execution of the build file!\n"); diff --git a/build.bat b/build.bat index c435587..ce02722 100644 --- a/build.bat +++ b/build.bat @@ -1,49 +1,9 @@ @echo off call ../misc/compile_setup.bat -set DEBUG=-Od -RTC1 -D_DEBUG -MTd -fsanitize=address -set RELEASE=-O2 -MT -DNDEBUG -GL -set COMMON=-MP -FC -Z7 -GF -Gm- -Oi -Zo -EHa- -GR- -set WRN=-WX -W3 -wd4200 -diagnostics:column -nologo -D_CRT_SECURE_NO_WARNINGS -set LINK_DEBUG=-NODEFAULTLIB:LIBCMT -set LINK_RELEASE=-opt:ref -opt:icf -ltcg - -set DEBUG_LINE=%DEBUG% %WRN% %COMMON% -link -incremental:no %LINK_DEBUG% -set RELEASE_LINE=%RELEASE% %WRN% %COMMON% -link -incremental:no %LINK_RELEASE% - mkdir build cd build - -cl.exe -Fe:bld.exe ../bld_main.cpp %DEBUG_LINE% -if %errorlevel% neq 0 exit /b %errorlevel% -rem bld.exe -if %errorlevel% neq 0 exit /b %errorlevel% - -cl.exe -Fe:test_arena.exe ../test/test_arena.cpp %DEBUG_LINE% -if %errorlevel% neq 0 exit /b %errorlevel% -test_arena.exe -if %errorlevel% neq 0 exit /b %errorlevel% - -cl.exe -Fe:test_table.exe ../test/test_table.cpp %DEBUG_LINE% -if %errorlevel% neq 0 exit /b %errorlevel% -test_table.exe -if %errorlevel% neq 0 exit /b %errorlevel% - -cl.exe -Fe:test_array.exe ../test/test_array.cpp %DEBUG_LINE% -if %errorlevel% neq 0 exit /b %errorlevel% -test_array.exe -if %errorlevel% neq 0 exit /b %errorlevel% - -cl.exe -Fe:cpp_debug.exe ../test/main.cpp %DEBUG_LINE% -if %errorlevel% neq 0 exit /b %errorlevel% - -cl.exe -Fe:cpp_release.exe ../test/main.cpp %RELEASE_LINE% -if %errorlevel% neq 0 exit /b %errorlevel% -cl.exe -Fe:c_debug.exe ../test/main.c %DEBUG_LINE% -if %errorlevel% neq 0 exit /b %errorlevel% -cl.exe -Fe:main_core_as_header.exe ../core.c ../test/main_core_as_header.cpp %DEBUG_LINE% -if %errorlevel% neq 0 exit /b %errorlevel% +cl -Fe:bld.exe ../bld_main.cpp -WX -W3 -wd4200 -diagnostics:column -nologo -Zi -D_CRT_SECURE_NO_WARNINGS cd .. +build\bld.exe -rem rtc1 - runtime error checks -rem gl - whole program optimizations diff --git a/build.sh b/build.sh index 1384491..a2b458c 100644 --- a/build.sh +++ b/build.sh @@ -2,7 +2,7 @@ mkdir build cd build set -e -gcc -o bld ../bld_main.cpp -g -fno-exceptions -fno-rtti -Wno-write-strings +g++ -o bld ../bld_main.cpp -g cd .. ./build/bld # gcc -o test_filesystem ../test/test_filesystem.c -Wno-write-strings diff --git a/clexer.c b/clexer.c index dbc824b..8e9a83d 100644 --- a/clexer.c +++ b/clexer.c @@ -107,7 +107,7 @@ CL_PRIVATE_FUNCTION void *CL__PushSizeZeroed(CL_Arena *arena, int size) { return result; } -char *CL_FixString[] = { +const char *CL_FixString[] = { "", "SUFFIX_U", "SUFFIX_UL", @@ -122,7 +122,7 @@ char *CL_FixString[] = { "PREFIX_L", }; -char *CL_KindString[] = { +const char *CL_KindString[] = { "EOF", "*", "/", @@ -242,7 +242,7 @@ char *CL_KindString[] = { "KEYWORD__GENERIC", }; -char *CL_MessageKindString[] = { +const char *CL_MessageKindString[] = { "ERROR", "WARNING", "TRACE", @@ -317,12 +317,12 @@ CL_PRIVATE_FUNCTION CL_Token *CL_CopyToken(CL_Arena *arena, CL_Token *token) { } CL_API_FUNCTION void CL_StringifyMessage(char *buff, int buff_size, CL_Message *msg) { - char *kind = CL_MessageKindString[msg->kind]; + const char *kind = CL_MessageKindString[msg->kind]; CL_SNPRINTF(buff, buff_size, "%s:%d %15s %15s", msg->token.file, msg->token.line, kind, msg->string); } CL_API_FUNCTION void CL_Stringify(char *buff, int buff_size, CL_Token *token) { - char *token_kind = "UNKNOWN"; + const char *token_kind = "UNKNOWN"; if (token->kind < CL_COUNT) token_kind = CL_KindString[token->kind]; CL_SNPRINTF(buff, buff_size, "%s:%d %15s %15.*s", token->file, token->line, token_kind, token->len, token->str); } @@ -1499,7 +1499,7 @@ CL_API_FUNCTION void CL_DefaultTokenize(CL_LexResult *T, CL_Token *token) { default: { CL_Message *result = CL_PushStruct(T->arena->other, CL_Message); result->kind = CLM_WARNING; - result->string = "Unhandled character, skipping ..."; + result->string = (char *)"Unhandled character, skipping ..."; CL_SLL_QUEUE_ADD(T->first_message, T->last_message, result); result->token = *token; token->kind = CL_COMMENT; @@ -1749,7 +1749,7 @@ CL_PRIVATE_FUNCTION char *CL_ChopLastSlash(CL_Arena *arena, char *str) { result = CL_PushStringCopy(arena, str, slash_pos); } else { - result = "./"; + result = (char *)"./"; } return result; } diff --git a/clexer.h b/clexer.h index 718a232..86bd945 100644 --- a/clexer.h +++ b/clexer.h @@ -410,7 +410,7 @@ CL_INLINE int CL_StringLength(char *string) { return len; } -CL_INLINE bool CL_StringsAreEqual(char *a, int64_t alen, char *b, int64_t blen) { +CL_INLINE bool CL_StringsAreEqual(char *a, int64_t alen, const char *b, int64_t blen) { if (alen != blen) return false; for (int i = 0; i < alen; i += 1) { if (a[i] != b[i]) return false; diff --git a/test/main.c b/test/main.c deleted file mode 100644 index fc26919..0000000 --- a/test/main.c +++ /dev/null @@ -1,5 +0,0 @@ -#include "../core.c" -int main() { - MA_Arena arena = {0}; - return 0; -} \ No newline at end of file diff --git a/test/main.cpp b/test/main.cpp deleted file mode 100644 index 5f6face..0000000 --- a/test/main.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "../core.c" - -int main() { - MA_Arena arena = {}; -} \ No newline at end of file diff --git a/test/test_filesystem.c b/test/test_filesystem.c index fbbc9e0..ecf3363 100644 --- a/test/test_filesystem.c +++ b/test/test_filesystem.c @@ -2,7 +2,7 @@ int main() { IO_Printf("test_filesystem.c - "); - MA_Arena arena = {}; + MA_Arena arena = {0}; S8_String read_file_path = S8_Lit("../test/data/read_file"); // Read file test