Porting bld to linux

This commit is contained in:
Krzosa Karol
2024-01-05 13:03:04 +01:00
parent 7c9c337194
commit 84f8cb1596
9 changed files with 96 additions and 33 deletions

View File

@@ -259,8 +259,8 @@ int Run(Strs s) {
} }
#ifndef BLD_MAIN #ifndef BLD_MAIN
int main() {
int Main(); int Main();
int main() {
SRC_InitCache(Perm, S8_Lit("buildfile.cache")); SRC_InitCache(Perm, S8_Lit("buildfile.cache"));
int result = Main(); int result = Main();
if (result == 0) SRC_SaveCache(); if (result == 0) SRC_SaveCache();

View File

@@ -6,6 +6,14 @@ int main(int argument_count, char **arguments) {
OS_SetWorkingDir(S8_Lit("build")); OS_SetWorkingDir(S8_Lit("build"));
IO_Printf("WORKING DIR: %Q\n", OS_GetWorkingDir(Perm)); 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
for (int i = 1; i < argument_count; i += 1) { for (int i = 1; i < argument_count; i += 1) {
S8_String arg = S8_MakeFromChar(arguments[i]); 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.cache"_s);
@@ -18,7 +26,7 @@ int main(int argument_count, char **arguments) {
{ {
S8_List files_current_dir = OS_ListDir(Perm, S8_Lit(".."), 0); S8_List files_current_dir = OS_ListDir(Perm, S8_Lit(".."), 0);
for (S8_Node *it = files_current_dir.first; it; it = it->next) { for (S8_Node *it = files_current_dir.first; it; it = it->next) {
if (S8_Find(it->string, S8_Lit("build.c"), S8_IGNORE_CASE, 0)) { if (S8_Find(it->string, S8_Lit("bld_file.c"), S8_IGNORE_CASE, 0)) {
build_file = it->string; build_file = it->string;
} }
} }
@@ -37,8 +45,18 @@ int main(int argument_count, char **arguments) {
// Compile the build file only if code changed // Compile the build file only if code changed
if (SRC_WasModified(build_file)) { if (SRC_WasModified(build_file)) {
double time = OS_GetTime(); double time = OS_GetTime();
S8_String f = S8_Lit("-WX -W3 -wd4200 -diagnostics:column -nologo -Zi"); int result = 0;
int result = OS_SystemF("cl %Q %Q -Fe:%Q ", build_file, f, exe_name); if (cc == "cl"_s) {
result = OS_SystemF("cl %Q -Fe:%Q -WX -W3 -wd4200 -diagnostics:column -nologo -Zi", build_file, exe_name);
}
else if (cc == "clang"_s) {
result = OS_SystemF("clang -Wno-writable-strings %Q -o %Q -g", build_file, exe_name);
}
else {
IO_Assert(cc == "gcc"_s);
result = OS_SystemF("gcc -Wno-write-strings %Q -o %Q -g", build_file, exe_name);
}
if (result != 0) { if (result != 0) {
IO_Printf("FAILED compilation of the build file!\n"); IO_Printf("FAILED compilation of the build file!\n");
return result; return result;
@@ -50,7 +68,11 @@ int main(int argument_count, char **arguments) {
// Run the build file // Run the build file
double time = OS_GetTime(); double time = OS_GetTime();
if (build_file.str) { if (build_file.str) {
#if OS_WINDOWS
int result = OS_SystemF("%.*s", S8_Expand(exe_name)); int result = OS_SystemF("%.*s", S8_Expand(exe_name));
#else
int result = OS_SystemF("./%.*s", S8_Expand(exe_name));
#endif
if (result != 0) { if (result != 0) {
printf("FAILED execution of the build file!\n"); printf("FAILED execution of the build file!\n");
return result; return result;

View File

@@ -2,15 +2,17 @@
mkdir build mkdir build
cd build cd build
set -e set -e
gcc -o bld ../bld_main.cpp -fno-exceptions -fno-rtti -Wno-write-strings
gcc -o test_filesystem ../test/test_filesystem.c -Wno-write-strings cd ..
./test_filesystem ./build/bld
gcc -o test_array ../test/test_array.cpp -fno-exceptions -fno-rtti -Wno-write-strings # gcc -o test_filesystem ../test/test_filesystem.c -Wno-write-strings
./test_array # ./test_filesystem
gcc -o test_table ../test/test_table.cpp -fno-exceptions -fno-rtti -Wno-write-strings # gcc -o test_array ../test/test_array.cpp -fno-exceptions -fno-rtti -Wno-write-strings
./test_table # ./test_array
gcc -o compile_as_c ../test/main.c # gcc -o test_table ../test/test_table.cpp -fno-exceptions -fno-rtti -Wno-write-strings
./compile_as_c # ./test_table
# gcc -o compile_as_c ../test/main.c
# ./compile_as_c
#-Wno-writable-strings #-Wno-writable-strings
cd .. cd ..

View File

@@ -414,44 +414,59 @@ OS_API S8_String OS_GetWorkingDir(MA_Arena *arena) {
} }
OS_API void OS_SetWorkingDir(S8_String path) { OS_API void OS_SetWorkingDir(S8_String path) {
IO_Assert(path.str[path.len] == 0); MA_Checkpoint scratch = MA_GetScratch();
chdir(path.str); S8_String copy = S8_Copy(scratch.arena, path);
chdir(copy.str);
MA_ReleaseScratch(scratch);
} }
OS_API S8_String OS_GetAbsolutePath(MA_Arena *arena, S8_String relative) { OS_API S8_String OS_GetAbsolutePath(MA_Arena *arena, S8_String relative) {
IO_Assert(relative.str[relative.len] == 0); MA_Checkpoint scratch = MA_GetScratch1(arena);
S8_String copy = S8_Copy(scratch.arena, relative);
char *buffer = (char *)MA_PushSizeNonZeroed(arena, PATH_MAX); char *buffer = (char *)MA_PushSizeNonZeroed(arena, PATH_MAX);
realpath((char *)relative.str, buffer); realpath((char *)copy.str, buffer);
S8_String result = S8_MakeFromChar(buffer); S8_String result = S8_MakeFromChar(buffer);
MA_ReleaseScratch(scratch);
return result; return result;
} }
OS_API bool OS_FileExists(S8_String path) { OS_API bool OS_FileExists(S8_String path) {
IO_Assert(path.str[path.len] == 0); MA_Checkpoint scratch = MA_GetScratch();
S8_String copy = S8_Copy(scratch.arena, path);
bool result = false; bool result = false;
if (access((char *)path.str, F_OK) == 0) { if (access((char *)copy.str, F_OK) == 0) {
result = true; result = true;
} }
MA_ReleaseScratch(scratch);
return result; return result;
} }
OS_API bool OS_IsDir(S8_String path) { OS_API bool OS_IsDir(S8_String path) {
IO_Assert(path.str[path.len] == 0); MA_Checkpoint scratch = MA_GetScratch();
S8_String copy = S8_Copy(scratch.arena, path);
struct stat s; struct stat s;
if (stat(path.str, &s) != 0) if (stat(copy.str, &s) != 0)
return false; return false;
bool result = S_ISDIR(s.st_mode); bool result = S_ISDIR(s.st_mode);
MA_ReleaseScratch(scratch);
return result; return result;
} }
OS_API bool OS_IsFile(S8_String path) { OS_API bool OS_IsFile(S8_String path) {
IO_Assert(path.str[path.len] == 0); MA_Checkpoint scratch = MA_GetScratch();
S8_String copy = S8_Copy(scratch.arena, path);
struct stat s; struct stat s;
if (stat(path.str, &s) != 0) if (stat(copy.str, &s) != 0)
return false; return false;
bool result = S_ISREG(s.st_mode); bool result = S_ISREG(s.st_mode);
MA_ReleaseScratch(scratch);
return result; return result;
} }
@@ -466,7 +481,8 @@ OS_API double OS_GetTime(void) {
OS_API S8_List OS_ListDir(MA_Arena *arena, S8_String path, unsigned flags) { OS_API S8_List OS_ListDir(MA_Arena *arena, S8_String path, unsigned flags) {
IO_Assert((flags & OS_RECURSIVE) == 0); IO_Assert((flags & OS_RECURSIVE) == 0);
IO_Assert(path.str[path.len] == 0); MA_Checkpoint scratch = MA_GetScratch1(arena);
path = S8_Copy(scratch.arena, path);
S8_List result = {0}; S8_List result = {0};
struct dirent *dir = 0; struct dirent *dir = 0;
@@ -483,25 +499,28 @@ OS_API S8_List OS_ListDir(MA_Arena *arena, S8_String path, unsigned flags) {
continue; continue;
} }
S8_String n = S8_Format(arena, "%Q/%s", path, dir->d_name); S8_String n = S8_Format(scratch.arena, "%Q/%s", path, dir->d_name);
if ((flags & OS_RELATIVE_PATHS) == 0) { if ((flags & OS_RELATIVE_PATHS) == 0) {
n = OS_GetAbsolutePath(arena, n); n = OS_GetAbsolutePath(scratch.arena, n);
} }
if (dir->d_type == DT_DIR) { if (dir->d_type == DT_DIR) {
n = S8_Format(arena, "%Q/", n); n = S8_Format(scratch.arena, "%Q/", n);
} }
S8_AddNode(arena, &result, n); S8_AddNode(arena, &result, S8_Copy(arena, n));
} }
closedir(d); closedir(d);
} }
MA_ReleaseScratch(scratch);
return result; return result;
} }
OS_API OS_Result OS_MakeDir(S8_String path) { OS_API OS_Result OS_MakeDir(S8_String path) {
IO_Assert(path.str[path.len]); MA_Checkpoint scratch = MA_GetScratch();
path = S8_Copy(scratch.arena, path);
int error = mkdir(path.str, 0755); int error = mkdir(path.str, 0755);
MA_ReleaseScratch(scratch);
return error == 0 ? OS_SUCCESS : OS_FAILURE; return error == 0 ? OS_SUCCESS : OS_FAILURE;
} }
@@ -523,12 +542,15 @@ OS_API OS_Result OS_DeleteDir(S8_String path, unsigned flags) {
} }
OS_API int64_t OS_GetFileModTime(S8_String file) { OS_API int64_t OS_GetFileModTime(S8_String file) {
IO_Assert(file.str[file.len] == 0); MA_Checkpoint scratch = MA_GetScratch();
file = S8_Copy(scratch.arena, file);
struct stat attrib = {}; struct stat attrib = {};
stat(file.str, &attrib); stat(file.str, &attrib);
struct timespec ts = attrib.st_mtim; struct timespec ts = attrib.st_mtim;
int64_t result = (((int64_t)ts.tv_sec) * 1000000ll) + ((int64_t)ts.tv_nsec) / 1000ll; int64_t result = (((int64_t)ts.tv_sec) * 1000000ll) + ((int64_t)ts.tv_nsec) / 1000ll;
MA_ReleaseScratch(scratch);
return result; return result;
} }
@@ -538,7 +560,8 @@ OS_API OS_Date OS_GetDate(void) {
} }
OS_API OS_Result OS_AppendFile(S8_String path, S8_String string) { OS_API OS_Result OS_AppendFile(S8_String path, S8_String string) {
IO_Assert(path.str[path.len] == 0); MA_Checkpoint scratch = MA_GetScratch();
path = S8_Copy(scratch.arena, path);
OS_Result result = OS_FAILURE; OS_Result result = OS_FAILURE;
FILE *f = fopen((const char *)path.str, "a"); FILE *f = fopen((const char *)path.str, "a");
@@ -555,11 +578,14 @@ OS_API OS_Result OS_AppendFile(S8_String path, S8_String string) {
result = OS_FAILURE; result = OS_FAILURE;
} }
} }
MA_ReleaseScratch(scratch);
return result; return result;
} }
OS_API S8_String OS_ReadFile(MA_Arena *arena, S8_String path) { OS_API S8_String OS_ReadFile(MA_Arena *arena, S8_String path) {
IO_Assert(path.str[path.len] == 0); MA_Checkpoint scratch = MA_GetScratch1(arena);
path = S8_Copy(scratch.arena, path);
S8_String result = {}; S8_String result = {};
FILE *f = fopen(path.str, "rb"); FILE *f = fopen(path.str, "rb");
@@ -574,6 +600,8 @@ OS_API S8_String OS_ReadFile(MA_Arena *arena, S8_String path) {
fclose(f); fclose(f);
} }
MA_ReleaseScratch(scratch);
return result; return result;
} }
@@ -598,7 +626,8 @@ OS_API OS_Result OS_WriteFile(S8_String path, S8_String string) {
} }
#else #else
OS_API OS_Result OS_WriteFile(S8_String path, S8_String string) { OS_API OS_Result OS_WriteFile(S8_String path, S8_String string) {
IO_Assert(path.str[path.len] == 0); MA_Checkpoint scratch = MA_GetScratch();
path = S8_Copy(scratch.arena, path);
OS_Result result = OS_FAILURE; OS_Result result = OS_FAILURE;
FILE *f = fopen((const char *)path.str, "w"); FILE *f = fopen((const char *)path.str, "w");
@@ -615,6 +644,8 @@ OS_API OS_Result OS_WriteFile(S8_String path, S8_String string) {
result = OS_FAILURE; result = OS_FAILURE;
} }
} }
MA_ReleaseScratch(scratch);
return result; return result;
} }
#endif #endif

View File

@@ -71,9 +71,11 @@ void TestBootstrapExclusive() {
} }
int main() { int main() {
IO_Printf("test_arena.cpp - ");
TestScratch(); TestScratch();
TestBuffer(); TestBuffer();
TestCreateAllocate(); TestCreateAllocate();
TestBootstrap(); TestBootstrap();
TestBootstrapExclusive(); TestBootstrapExclusive();
IO_Printf("DONE\n");
} }

View File

@@ -117,11 +117,13 @@ void TestCopy() {
} }
int main() { int main() {
IO_Printf("test_array.cpp - ");
TestExclusiveArenaBackedArray(); TestExclusiveArenaBackedArray();
TestRemoveForLoop(); TestRemoveForLoop();
TestBasic(); TestBasic();
TestReverseLoop(); TestReverseLoop();
TestCopy(); TestCopy();
IO_Printf("DONE\n");
return 0; return 0;
} }

View File

@@ -1,6 +1,7 @@
#include "../core.c" #include "../core.c"
int main() { int main() {
IO_Printf("test_filesystem.c - ");
MA_Arena arena = {}; MA_Arena arena = {};
S8_String read_file_path = S8_Lit("../test/data/read_file"); S8_String read_file_path = S8_Lit("../test/data/read_file");
@@ -102,4 +103,5 @@ int main() {
} }
} }
} }
IO_Printf("DONE\n");
} }

View File

@@ -37,7 +37,9 @@ void TestStrings() {
} }
int main() { int main() {
IO_Printf("test_table.cpp - ");
TestSimpleInsertAndIntegrity(); TestSimpleInsertAndIntegrity();
TestStrings(); TestStrings();
IO_Printf("DONE\n");
return 0; return 0;
} }