Improve build tool api, cleanup pass, add lexer tests

This commit is contained in:
Krzosa Karol
2024-01-25 22:21:58 +01:00
parent 738d27db9d
commit e39cd78546
12 changed files with 94 additions and 94 deletions

View File

@@ -1,5 +1,5 @@
#define SRC_CACHE_ENTRY_COUNT 1024
struct SRC_CacheEntry {
uint64_t filepath_hash;
uint64_t file_hash;
@@ -19,6 +19,11 @@ SRC_Cache *SRC_FromFileCache;
S8_String SRC_CacheFilename;
CL_SearchPaths SRC_SearchPaths = {}; // @todo;
#define SRC_CacheScope(cache_filename) \
SRC_InitCache(Perm, cache_filename); \
defer { SRC_SaveCache(); };
void SRC_InitCache(MA_Arena *arena, S8_String cachefilename) {
SRC_CacheFilename = cachefilename;

View File

@@ -75,17 +75,6 @@ S8_String Fmt(const char *str, ...) {
return str_fmt;
}
bool CodeWasModified(S8_String str, S8_String artifact = {}) {
return SRC_WasModified(str, artifact);
}
S8_String IfCodeWasModified(S8_String cfile, S8_String objfile) {
Array<S8_String> result = {};
if (SRC_WasModified(cfile, objfile))
return cfile;
return objfile;
}
int Run(Array<S8_String> s) {
S8_String cmd = Merge(s);
return OS_SystemF("%.*s", S8_Expand(cmd));
@@ -98,3 +87,36 @@ Array<S8_String> ListDir(char *dir) {
}
return result;
}
Array<S8_String> CMD_Make(char **argv, int argc) {
Array<S8_String> result = {Perm};
IO_Printf("Command line arguments:\n");
for (int i = 1; i < argc; i += 1) {
S8_String it = S8_MakeFromChar(argv[i]);
result.add(it);
IO_Printf("[%d] %.*s\n", i, S8_Expand(it));
}
return result;
}
S8_String CMD_Get(Array<S8_String> &cmd, S8_String name, S8_String default_value = "") {
For(cmd) {
int64_t idx = 0;
if (S8_Seek(it, "="_s, 0, &idx)) {
S8_String key = S8_GetPrefix(it, idx);
S8_String value = S8_Skip(it, idx + 1);
if (key == name) {
return value;
}
}
}
return default_value;
}
bool CMD_Match(Array<S8_String> &cmd, S8_String name) {
For(cmd) {
if (it == name) return true;
}
return false;
}

View File

@@ -54,38 +54,3 @@ S8_String GCC_Flags = "-Wno-write-strings";
S8_String GCC_NoStd = "-fno-exceptions";
S8_String GCC_Debug = "-fsanitize=address -g";
#ifndef BUILD_MAIN
int Main();
void BUILD_ReportError(S8_String it) {
IO_FatalErrorf("Invalid command line argument syntax! Expected a key value pair!\n"
"Here is the wrong argument: %.*s\n"
"Here is a good example: bld.exe profile=release platform=windows\n",
S8_Expand(it));
}
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_Seek(it, "="_s, 0, &idx)) {
S8_String key = S8_GetPrefix(it, idx);
S8_String value = S8_Skip(it, idx + 1);
if (key.len == 0) BUILD_ReportError(it);
if (value.len == 0) BUILD_ReportError(it);
IO_Printf("[%d] %.*s = %.*s\n", i, S8_Expand(key), S8_Expand(value));
CMDLine.put(key, value);
}
else BUILD_ReportError(it);
}
SRC_InitCache(Perm, S8_Lit("build_file.cache"));
int result = Main();
if (result == 0) SRC_SaveCache();
return result;
}
#endif

View File

@@ -1,4 +1,3 @@
#define BUILD_MAIN
#include "library.cpp"
int main(int argument_count, char **arguments) {
@@ -7,21 +6,17 @@ int main(int argument_count, char **arguments) {
S8_String working_dir = OS_GetWorkingDir(Perm);
IO_Printf("WORKING DIR: %.*s\n", S8_Expand(working_dir));
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(cache_filename);
break;
Array<S8_String> cmd = CMD_Make(arguments, argument_count);
if (CMD_Match(cmd, "clear_cache")) {
for (OS_FileIter it = OS_IterateFiles(Perm, "./"); OS_IsValid(it); OS_Advance(&it)) {
if (!it.is_directory && S8_EndsWith(it.filename, ".cache", true)) {
OS_DeleteFile(it.absolute_path);
}
}
cmdline_args = S8_Format(Perm, "%.*s%.*s ", S8_Expand(cmdline_args), S8_Expand(arg));
return 0;
}
SRC_InitCache(Perm, cache_filename);
S8_String cc = CMD_Get(cmd, "cc", IF_WINDOWS("cl") IF_MAC("clang") IF_LINUX("gcc"));
// Search for build file in the project directory
S8_String build_file = {};
@@ -38,6 +33,7 @@ int main(int argument_count, char **arguments) {
}
}
SRC_CacheScope("build_tool.cache");
S8_String name_no_ext = S8_GetNameNoExt(build_file);
S8_String exe_name = S8_Format(Perm, "%.*s.exe", S8_Expand(name_no_ext));
@@ -45,14 +41,14 @@ int main(int argument_count, char **arguments) {
if (SRC_WasModified(build_file, exe_name)) {
double time = OS_GetTime();
int result = 0;
if (cc == "cl"_s) {
if (cc == "cl") {
result = OS_SystemF("cl %.*s -Fe:%.*s -WX -W3 -wd4200 -diagnostics:column -nologo -Zi", S8_Expand(build_file), S8_Expand(exe_name));
}
else if (cc == "clang"_s) {
else if (cc == "clang") {
result = OS_SystemF("clang++ -std=c++11 -fdiagnostics-absolute-paths -Wno-writable-strings %.*s -o %.*s -g", S8_Expand(build_file), S8_Expand(exe_name));
}
else {
IO_Assert(cc == "gcc"_s);
IO_Assert(cc == "gcc");
result = OS_SystemF("gcc -Wno-write-strings %.*s -o %.*s -g", S8_Expand(build_file), S8_Expand(exe_name));
}
@@ -64,6 +60,8 @@ int main(int argument_count, char **arguments) {
IO_Printf("TIME Build file compilation: %f\n", time);
}
S8_String cmdline_args = Merge(cmd);
// Run the build file
double time = OS_GetTime();
if (build_file.str) {
@@ -75,7 +73,4 @@ int main(int argument_count, char **arguments) {
}
time = OS_GetTime() - time;
IO_Printf("TIME total build file execution: %f\n", time);
SRC_SaveCache();
// OS_SetWorkingDir(S8_Lit(".."));
}