From 8e62c45af1ac35f12622e23946d20ff1485a58d3 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 6 Jan 2024 14:37:48 +0100 Subject: [PATCH] Add command line arguments to build --- build.bat | 2 +- code/build_lib.cpp | 32 +++++++++++++++++++++++++++++++- code/build_main.cpp | 13 ++++++++++--- code/string.h | 1 + code/table.hpp | 29 +++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 5 deletions(-) diff --git a/build.bat b/build.bat index edca8de..c3cac64 100644 --- a/build.bat +++ b/build.bat @@ -5,5 +5,5 @@ mkdir build cd build cl -Fe:bld.exe ../code/build_main.cpp -WX -W3 -wd4200 -diagnostics:column -nologo -Zi -D_CRT_SECURE_NO_WARNINGS /MD cd .. -build\bld.exe +build\bld.exe asd=Memes diff --git a/code/build_lib.cpp b/code/build_lib.cpp index 97ded14..1349c67 100644 --- a/code/build_lib.cpp +++ b/code/build_lib.cpp @@ -61,6 +61,7 @@ S8_String SRC_CacheFilename; MA_Arena PernamentArena; MA_Arena *Perm = &PernamentArena; CL_SearchPaths SRC_SearchPaths = {}; // @todo; +Table CMDLine; void SRC_InitCache(MA_Arena *arena, S8_String cachefilename) { SRC_CacheFilename = cachefilename; @@ -260,6 +261,10 @@ Strs operator+(Str a, Str b) { return c; } +Strs operator+(Str a, char *b) { + return a + S8_MakeFromChar(b); +} + //@todo: split on any whitespace instead! Strs Split(char *str) { Str s = S8_MakeFromChar(str); @@ -321,9 +326,34 @@ Strs ListDir(char *dir) { return result; } +void ReportError(S8_String it) { + IO_FatalErrorf("Invalid command line argument syntax! Expected a key value pair!\n" + "Here is the wrong argument: %Q\n" + "Here is a good example: bld.exe profile=release platform=windows\n", + it); +} + #ifndef BUILD_MAIN int Main(); -int main() { +int main(int argc, char **argv) { + + if (argc > 1) IO_Printf("Command line arguments:\n"); + for (int i = 1; i < argc; i += 1) { + S8_String it = S8_MakeFromChar(argv[i]); + + int64_t idx = 0; + if (S8_Find(it, "="_s, 0, &idx)) { + S8_String key = S8_GetPrefix(it, idx); + S8_String value = S8_Skip(it, idx + 1); + if (key.len == 0) ReportError(it); + if (value.len == 0) ReportError(it); + IO_Printf("[%d] %Q = %Q\n", i, key, value); + + CMDLine.put(key, value); + } + else ReportError(it); + } + SRC_InitCache(Perm, S8_Lit("build_file.cache")); int result = Main(); if (result == 0) SRC_SaveCache(); diff --git a/code/build_main.cpp b/code/build_main.cpp index 76c1da3..52df966 100644 --- a/code/build_main.cpp +++ b/code/build_main.cpp @@ -7,13 +7,20 @@ int main(int argument_count, char **arguments) { IO_Printf("WORKING DIR: %Q\n", OS_GetWorkingDir(Perm)); S8_String cc = ON_WINDOWS("cl"_s) ON_MAC("clang"_s) ON_LINUX("gcc"_s); + S8_String cache_filename = "build_tool.cache"_s; + S8_String cmdline_args = S8_MakeEmpty(); for (int i = 1; i < argument_count; i += 1) { S8_String arg = S8_MakeFromChar(arguments[i]); - if (arg == "clear_cache"_s) OS_DeleteFile("build_tool.cache"_s); + if (arg == "clear_cache"_s) { + OS_DeleteFile(cache_filename); + break; + } + + cmdline_args = S8_Format(Perm, "%Q%Q ", cmdline_args, arg); } - SRC_InitCache(Perm, S8_Lit("build_tool.cache")); + SRC_InitCache(Perm, cache_filename); // Search for build file in the project directory S8_String build_file = {0}; @@ -62,7 +69,7 @@ int main(int argument_count, char **arguments) { // Run the build file double time = OS_GetTime(); if (build_file.str) { - int result = OS_SystemF("%s%Q", IF_WINDOWS_ELSE("", "./"), exe_name); + int result = OS_SystemF(IF_WINDOWS_ELSE("", "./") "%Q %Q", exe_name, cmdline_args); if (result != 0) { printf("FAILED execution of the build file!\n"); return result; diff --git a/code/string.h b/code/string.h index ffa23bf..ca3dd3c 100644 --- a/code/string.h +++ b/code/string.h @@ -1,4 +1,5 @@ #pragma once +#define S8_HEADER #include #include diff --git a/code/table.hpp b/code/table.hpp index 4ffa3c5..fc5c2a6 100644 --- a/code/table.hpp +++ b/code/table.hpp @@ -203,12 +203,41 @@ struct Table { return &v->value; } + Value get(uint64_t key, Value default_value) { + Entry *v = get_table_entry(key); + if (!v) return default_value; + return v->value; + } + Value *gets(char *str) { int len = TABLE_CStringLen(str); uint64_t hash = TABLE_HASH_BYTES(str, len); return get(hash); } + Value gets(char *str, Value default_value) { + int len = TABLE_CStringLen(str); + uint64_t hash = TABLE_HASH_BYTES(str, len); + return get(hash, default_value); + } + + #ifdef S8_HEADER + Value *get(S8_String s) { + uint64_t hash = TABLE_HASH_BYTES(s.str. (unsigned)s.len); + return get(hash); + } + + Value get(S8_String s, Value default_value) { + uint64_t hash = TABLE_HASH_BYTES(s.str. (unsigned)s.len); + return get(hash, default_value); + } + + void put(S8_String s, const Value &value) { + uint64_t hash = TABLE_HASH_BYTES(s.str, (unsigned)s.len); + insert(hash, value); + } + #endif + void puts(char *str, const Value &value) { int len = TABLE_CStringLen(str); uint64_t hash = TABLE_HASH_BYTES(str, len);