From ee6f8114eeb01841a2c3391bac9987ea7fc23d84 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 9 Oct 2022 12:23:38 +0200 Subject: [PATCH] Ported to Linux! --- build.bat | 4 +-- core_codegen_c_language.cpp | 7 ++++- core_compiler.cpp | 10 +++++++ core_main.cpp | 4 +-- os_unix.cpp => os_linux.cpp | 56 ++++++++++++++++++++++++++++--------- 5 files changed, 62 insertions(+), 19 deletions(-) rename os_unix.cpp => os_linux.cpp (71%) diff --git a/build.bat b/build.bat index 09b7810..6e13611 100644 --- a/build.bat +++ b/build.bat @@ -1,8 +1,6 @@ @echo off - - pushd %~dp0 rem cl main.cpp -I.. user32.lib -clang core_main.cpp -O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib +clang core_main.cpp -O2 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib popd diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index 8a588e7..fd7b0a0 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -794,9 +794,14 @@ compile_to_c_code(){ #include #include +#include + +#ifndef Panic + #define Panic(...) (*(volatile int *)0 = 0) +#endif #ifndef Assert -#define Assert(x) do{if(!(x))__debugbreak();}while(0) +#define Assert(x) do{if(!(x))Panic();}while(0) #endif #ifndef AssertMessage diff --git a/core_compiler.cpp b/core_compiler.cpp index 9e7174c..d67584f 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -309,7 +309,13 @@ compile_file(String filename, U32 compile_flags = COMPILE_NULL){ Scratch scratch; F64 begin = os_time(); +#if OS_WINDOWS String compiler_call = string_fmt(scratch, "clang.exe program.c -Wall -Wno-unused-function -Wno-parentheses-equality -g -o a.exe -lgdi32 -luser32 -lwinmm"); +#else + String compiler_call = string_fmt(scratch, "clang program.c -std=c99 -Wall -Wno-unused-function -g -o a.out"); + // String compiler_call = string_fmt(scratch, "tcc program.c -Wall -g -o a.out"); +#endif + log_trace("%Q", compiler_call); system((const char *)compiler_call.str); F64 end = os_time(); @@ -332,7 +338,11 @@ compile_file(String filename, U32 compile_flags = COMPILE_NULL){ if(is_flag_set(compile_flags, COMPILE_AND_RUN)){ String testing = compile_flags&COMPILE_TESTING ? "testing"_s : ""_s; + #if OS_WINDOWS String sys = string_fmt(scratch, "a.exe %Q", testing); + #else + String sys = string_fmt(scratch, "./a.out %Q", testing); + #endif int result = system((char *)sys.str); assert(result != -1); if(result == 0){ diff --git a/core_main.cpp b/core_main.cpp index 202a47d..ebd5ba8 100644 --- a/core_main.cpp +++ b/core_main.cpp @@ -229,8 +229,8 @@ For modules it's a bit different cause they should be distributed as valid. #include "os.h" #if OS_WINDOWS #include "os_windows.cpp" -#elif OS_UNIX -#include "os_unix.cpp" +#elif OS_LINUX +#include "os_linux.cpp" #else #error Couldnt figure out OS using macros #endif diff --git a/os_unix.cpp b/os_linux.cpp similarity index 71% rename from os_unix.cpp rename to os_linux.cpp index 2b4b8f7..be12391 100644 --- a/os_unix.cpp +++ b/os_linux.cpp @@ -3,8 +3,10 @@ #include #include #include +#include +#include +#include -#define POSIX_PATH_MAX_KIND_OF 4096 #define POSIX_PAGE_SIZE 4096 function B32 @@ -37,33 +39,48 @@ os_read_file(Allocator *a, String name){ function String os_get_exe_dir(Allocator *a){ - return "/mnt/c/AProgramming/cparse/compiler"_s; + char buffer[PATH_MAX] = {}; + if (readlink("/proc/self/exe", buffer, PATH_MAX) == -1) { + log_info("Failed to retrieve the path of the executable, the method used is fetching /proc/self/exe, very likely you are using an OS that doesn't follow this convention and as such it is currently not supported"); + } + String exe = string_from_cstring(buffer); + exe = string_chop_last_slash(exe); + String result = string_copy(a, exe); + + log_trace("Retrieved executable path %Q", result); + return result; } function String os_get_absolute_path(Allocator *a, String path){ - char *buffer_out = exp_alloc_array(a, char, POSIX_PATH_MAX_KIND_OF); assert(path.str[path.len] == 0); - realpath((char *)path.str, buffer_out); - // @memory @todo: If we allocated only 32 bytes here then deallocate rest - - return string_from_cstring(buffer_out); + char buffer[PATH_MAX] = {}; + realpath((char *)path.str, buffer); + String abs = string_from_cstring(buffer); + String result = string_copy(a, abs); + return result; } function B32 os_does_file_exist(String path){ - return true; + B32 result = false; + assert(path.str[path.len] == 0); + if (access((char *)path.str, F_OK) == 0) { + result = true; + } + log_trace("Does file exist? %Q %d", path, result); + return result; } function String os_get_working_dir(Allocator *allocator){ - char *buffer = exp_alloc_array(allocator, char, POSIX_PATH_MAX_KIND_OF); - char *result = getcwd(buffer, POSIX_PATH_MAX_KIND_OF); + char *buffer = exp_alloc_array(allocator, char, PATH_MAX); + char *result = getcwd(buffer, PATH_MAX); return string_from_cstring(result); } function Array -os_list_dir(Allocator *a, String dir, U32 flags = LIST_NO_FLAGS){ +os_list_dir(Allocator *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){ Scratch scratch(a); Array dirs_to_read = {scratch}; dirs_to_read.add(dir); @@ -86,12 +103,25 @@ os_list_dir(Allocator *a, String dir, U32 flags = LIST_NO_FLAGS){ } OS_File_Info entry = {}; - entry.relative_path = string_from_cstring(dir->d_name); + entry.relative_path = string_fmt(a, "%Q/%s", *it, dir->d_name); entry.absolute_path = os_get_absolute_path(a, entry.relative_path); - log_info("%Q\n", entry.absolute_path); + + if(dir->d_type == DT_DIR){ + entry.is_directory = true; + + if(flags & LIST_RECURSE_INTO_DIRS){ + dirs_to_read.add(entry.absolute_path); + } + } + + result.add(entry); + log_trace("%Q %d", entry.absolute_path, entry.is_directory); } closedir(d); } + else{ + log_info("ERROR Failed to read dir: %Q, errno: %s\n", *it, strerror(errno)); + } } return result;