From b13f2335212c279ab1e0136f232224a0cca8fa28 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 24 Jun 2019 13:51:11 +0200 Subject: [PATCH] re-write `$if os {` and fix GCC segfaults on compilation --- compiler/main.v | 31 +++++++++++++++------------- compiler/parser.v | 15 ++++++++++++-- gg/gg.v | 4 ++-- os/os.v | 51 ++++++++++++++++++++++++----------------------- 4 files changed, 58 insertions(+), 43 deletions(-) diff --git a/compiler/main.v b/compiler/main.v index 6a9f8d4726..97add2044e 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -25,7 +25,7 @@ enum BuildMode { } fn vtmp_path() string { - return os.home_dir() + '/.vlang$Version/' + return os.home_dir() + '/.vlang/' } const ( @@ -159,19 +159,6 @@ fn (c mut V) compile() { } // Main pass cgen.run = RUN_MAIN - if c.os == MAC { - cgen.genln('#define mac (1) ') - // cgen.genln('#include ') - } - if c.os == LINUX { - cgen.genln('#define linux (1) ') - cgen.genln('#include ') - } - if c.os == WINDOWS { - cgen.genln('#define windows (1) ') - // cgen.genln('#include ') - cgen.genln('#include ') - } if c.is_play { cgen.genln('#define VPLAY (1) ') } @@ -182,6 +169,22 @@ fn (c mut V) compile() { #include // for va_list #include // int64_t etc + +#ifdef __linux__ +#include +#endif + + +#ifdef __APPLE__ + +#endif + + +#ifdef _WIN32 +#include +//#include +#endif + //================================== TYPEDEFS ================================*/ typedef unsigned char byte; diff --git a/compiler/parser.v b/compiler/parser.v index fa9924f05c..6d9011cc5a 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -2494,6 +2494,16 @@ fn (p mut Parser) get_tmp_counter() int { return p.tmp_cnt } +fn os_name_to_ifdef(name string) string { + switch name { + case 'windows': return '_WIN32' + case 'mac': return '__APPLE__' + case 'linux': return '__linux__' + } + panic('bad os ifdef name "$name"') + return '' +} + fn (p mut Parser) comp_time() { p.next() if p.tok == IF { @@ -2504,11 +2514,12 @@ fn (p mut Parser) comp_time() { } name := p.check_name() if name in SupportedPlatforms { + ifdef_name := os_name_to_ifdef(name) if not { - p.genln('#ifndef $name') + p.genln('#ifndef $ifdef_name') } else { - p.genln('#ifdef $name') + p.genln('#ifdef $ifdef_name') } p.check(LCBR) p.statements_no_curly_end() diff --git a/gg/gg.v b/gg/gg.v index 788ede3c5f..ccd9d6f21b 100644 --- a/gg/gg.v +++ b/gg/gg.v @@ -239,9 +239,9 @@ fn (ctx &GG) draw_rect2(x, y, w, h float, c gx.Color) { ctx.shader.set_int('has_texture', 0) // 4--1 // 3--2 -#ifdef linux + $if linux { // y += h -#endif + } vertices := [ x + w, y, 0, x + w, y + h, 0, diff --git a/os/os.v b/os/os.v index 8d0d77b5aa..e4dfa7fb01 100644 --- a/os/os.v +++ b/os/os.v @@ -288,15 +288,15 @@ pub fn system(cmd string) string { fn system_into_lines(s string) []string { mut res := []string cmd := '$s 2>&1' -#ifdef windows - # FILE* f = _popen(cmd.str, "r"); -#else - # FILE* f = popen(cmd.str, "r"); -#endif - #define MAX 5000 - // # char buf[MAX]; - # char * buf = malloc(sizeof(char) * MAX); - # while (fgets(buf, MAX, f) != NULL) + max := 5000 + $if windows { + # FILE* f = _popen(cmd.str, "r"); + } + $else { + # FILE* f = popen(cmd.str, "r"); + } + # char * buf = malloc(sizeof(char) * max); + # while (fgets(buf, max, f) != NULL) { val := '' # buf[strlen(buf) - 1] = '\0'; // eat the newline fgets() stores @@ -323,11 +323,12 @@ fn exit(code int) { pub fn file_exists(path string) bool { // # return access( path.str, F_OK ) != -1 ; res := false -#ifdef windows - # res = _access( path.str, 0 ) != -1 ; -#else - # res = access( path.str, 0 ) != -1 ; -#endif + $if windows { + # res = _access( path.str, 0 ) != -1 ; + } + $else { + # res = access( path.str, 0 ) != -1 ; + } return res } @@ -435,16 +436,16 @@ pub fn write_file(path, text string) { } fn on_segfault(f voidptr) { -#ifdef windows - return -#endif -#ifdef mac - # struct sigaction sa; - # memset(&sa, 0, sizeof(struct sigaction)); - # sigemptyset(&sa.sa_mask); - # sa.sa_sigaction = f; - # sa.sa_flags = SA_SIGINFO; - # sigaction(SIGSEGV, &sa, 0); -#endif + $if windows { + return + } + $if mac { + # struct sigaction sa; + # memset(&sa, 0, sizeof(struct sigaction)); + # sigemptyset(&sa.sa_mask); + # sa.sa_sigaction = f; + # sa.sa_flags = SA_SIGINFO; + # sigaction(SIGSEGV, &sa, 0); + } }