Add command line arguments to build

This commit is contained in:
Krzosa Karol
2024-01-06 14:37:48 +01:00
parent 37eec9346e
commit 8e62c45af1
5 changed files with 72 additions and 5 deletions

View File

@@ -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

View File

@@ -61,6 +61,7 @@ S8_String SRC_CacheFilename;
MA_Arena PernamentArena;
MA_Arena *Perm = &PernamentArena;
CL_SearchPaths SRC_SearchPaths = {}; // @todo;
Table<S8_String> 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();

View File

@@ -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;

View File

@@ -1,4 +1,5 @@
#pragma once
#define S8_HEADER
#include <stdint.h>
#include <stdbool.h>

View File

@@ -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);