Ported to Linux!

This commit is contained in:
Krzosa Karol
2022-10-09 12:23:38 +02:00
parent 0637a32655
commit ee6f8114ee
5 changed files with 62 additions and 19 deletions

View File

@@ -1,8 +1,6 @@
@echo off @echo off
pushd %~dp0 pushd %~dp0
rem cl main.cpp -I.. user32.lib 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 popd

View File

@@ -794,9 +794,14 @@ compile_to_c_code(){
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
#ifndef Panic
#define Panic(...) (*(volatile int *)0 = 0)
#endif
#ifndef Assert #ifndef Assert
#define Assert(x) do{if(!(x))__debugbreak();}while(0) #define Assert(x) do{if(!(x))Panic();}while(0)
#endif #endif
#ifndef AssertMessage #ifndef AssertMessage

View File

@@ -309,7 +309,13 @@ compile_file(String filename, U32 compile_flags = COMPILE_NULL){
Scratch scratch; Scratch scratch;
F64 begin = os_time(); 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"); 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); system((const char *)compiler_call.str);
F64 end = os_time(); 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)){ if(is_flag_set(compile_flags, COMPILE_AND_RUN)){
String testing = compile_flags&COMPILE_TESTING ? "testing"_s : ""_s; String testing = compile_flags&COMPILE_TESTING ? "testing"_s : ""_s;
#if OS_WINDOWS
String sys = string_fmt(scratch, "a.exe %Q", testing); 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); int result = system((char *)sys.str);
assert(result != -1); assert(result != -1);
if(result == 0){ if(result == 0){

View File

@@ -229,8 +229,8 @@ For modules it's a bit different cause they should be distributed as valid.
#include "os.h" #include "os.h"
#if OS_WINDOWS #if OS_WINDOWS
#include "os_windows.cpp" #include "os_windows.cpp"
#elif OS_UNIX #elif OS_LINUX
#include "os_unix.cpp" #include "os_linux.cpp"
#else #else
#error Couldnt figure out OS using macros #error Couldnt figure out OS using macros
#endif #endif

View File

@@ -3,8 +3,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#include <string.h>
#include <linux/limits.h>
#define POSIX_PATH_MAX_KIND_OF 4096
#define POSIX_PAGE_SIZE 4096 #define POSIX_PAGE_SIZE 4096
function B32 function B32
@@ -37,33 +39,48 @@ os_read_file(Allocator *a, String name){
function String function String
os_get_exe_dir(Allocator *a){ 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 function String
os_get_absolute_path(Allocator *a, String path){ 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); assert(path.str[path.len] == 0);
realpath((char *)path.str, buffer_out); char buffer[PATH_MAX] = {};
// @memory @todo: If we allocated only 32 bytes here then deallocate rest realpath((char *)path.str, buffer);
String abs = string_from_cstring(buffer);
return string_from_cstring(buffer_out); String result = string_copy(a, abs);
return result;
} }
function B32 function B32
os_does_file_exist(String path){ 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 function String
os_get_working_dir(Allocator *allocator){ os_get_working_dir(Allocator *allocator){
char *buffer = exp_alloc_array(allocator, char, POSIX_PATH_MAX_KIND_OF); char *buffer = exp_alloc_array(allocator, char, PATH_MAX);
char *result = getcwd(buffer, POSIX_PATH_MAX_KIND_OF); char *result = getcwd(buffer, PATH_MAX);
return string_from_cstring(result); return string_from_cstring(result);
} }
function Array<OS_File_Info> function Array<OS_File_Info>
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); Scratch scratch(a);
Array<String> dirs_to_read = {scratch}; Array<String> dirs_to_read = {scratch};
dirs_to_read.add(dir); 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 = {}; 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); 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); closedir(d);
} }
else{
log_info("ERROR Failed to read dir: %Q, errno: %s\n", *it, strerror(errno));
}
} }
return result; return result;