re-write `$if os {` and fix GCC segfaults on compilation

pull/1049/head
Alexander Medvednikov 2019-06-24 13:51:11 +02:00
parent e604d5e2c8
commit b13f233521
4 changed files with 58 additions and 43 deletions

View File

@ -25,7 +25,7 @@ enum BuildMode {
} }
fn vtmp_path() string { fn vtmp_path() string {
return os.home_dir() + '/.vlang$Version/' return os.home_dir() + '/.vlang/'
} }
const ( const (
@ -159,19 +159,6 @@ fn (c mut V) compile() {
} }
// Main pass // Main pass
cgen.run = RUN_MAIN cgen.run = RUN_MAIN
if c.os == MAC {
cgen.genln('#define mac (1) ')
// cgen.genln('#include <pthread.h>')
}
if c.os == LINUX {
cgen.genln('#define linux (1) ')
cgen.genln('#include <pthread.h>')
}
if c.os == WINDOWS {
cgen.genln('#define windows (1) ')
// cgen.genln('#include <WinSock2.h>')
cgen.genln('#include <windows.h> ')
}
if c.is_play { if c.is_play {
cgen.genln('#define VPLAY (1) ') cgen.genln('#define VPLAY (1) ')
} }
@ -182,6 +169,22 @@ fn (c mut V) compile() {
#include <stdarg.h> // for va_list #include <stdarg.h> // for va_list
#include <inttypes.h> // int64_t etc #include <inttypes.h> // int64_t etc
#ifdef __linux__
#include <pthread.h>
#endif
#ifdef __APPLE__
#endif
#ifdef _WIN32
#include <windows.h>
//#include <WinSock2.h>
#endif
//================================== TYPEDEFS ================================*/ //================================== TYPEDEFS ================================*/
typedef unsigned char byte; typedef unsigned char byte;

View File

@ -2494,6 +2494,16 @@ fn (p mut Parser) get_tmp_counter() int {
return p.tmp_cnt 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() { fn (p mut Parser) comp_time() {
p.next() p.next()
if p.tok == IF { if p.tok == IF {
@ -2504,11 +2514,12 @@ fn (p mut Parser) comp_time() {
} }
name := p.check_name() name := p.check_name()
if name in SupportedPlatforms { if name in SupportedPlatforms {
ifdef_name := os_name_to_ifdef(name)
if not { if not {
p.genln('#ifndef $name') p.genln('#ifndef $ifdef_name')
} }
else { else {
p.genln('#ifdef $name') p.genln('#ifdef $ifdef_name')
} }
p.check(LCBR) p.check(LCBR)
p.statements_no_curly_end() p.statements_no_curly_end()

View File

@ -239,9 +239,9 @@ fn (ctx &GG) draw_rect2(x, y, w, h float, c gx.Color) {
ctx.shader.set_int('has_texture', 0) ctx.shader.set_int('has_texture', 0)
// 4--1 // 4--1
// 3--2 // 3--2
#ifdef linux $if linux {
// y += h // y += h
#endif }
vertices := [ vertices := [
x + w, y, 0, x + w, y, 0,
x + w, y + h, 0, x + w, y + h, 0,

29
os/os.v
View File

@ -288,15 +288,15 @@ pub fn system(cmd string) string {
fn system_into_lines(s string) []string { fn system_into_lines(s string) []string {
mut res := []string mut res := []string
cmd := '$s 2>&1' cmd := '$s 2>&1'
#ifdef windows max := 5000
$if windows {
# FILE* f = _popen(cmd.str, "r"); # FILE* f = _popen(cmd.str, "r");
#else }
$else {
# FILE* f = popen(cmd.str, "r"); # FILE* f = popen(cmd.str, "r");
#endif }
#define MAX 5000 # char * buf = malloc(sizeof(char) * max);
// # char buf[MAX]; # while (fgets(buf, max, f) != NULL)
# char * buf = malloc(sizeof(char) * MAX);
# while (fgets(buf, MAX, f) != NULL)
{ {
val := '' val := ''
# buf[strlen(buf) - 1] = '\0'; // eat the newline fgets() stores # 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 { pub fn file_exists(path string) bool {
// # return access( path.str, F_OK ) != -1 ; // # return access( path.str, F_OK ) != -1 ;
res := false res := false
#ifdef windows $if windows {
# res = _access( path.str, 0 ) != -1 ; # res = _access( path.str, 0 ) != -1 ;
#else }
$else {
# res = access( path.str, 0 ) != -1 ; # res = access( path.str, 0 ) != -1 ;
#endif }
return res return res
} }
@ -435,16 +436,16 @@ pub fn write_file(path, text string) {
} }
fn on_segfault(f voidptr) { fn on_segfault(f voidptr) {
#ifdef windows $if windows {
return return
#endif }
#ifdef mac $if mac {
# struct sigaction sa; # struct sigaction sa;
# memset(&sa, 0, sizeof(struct sigaction)); # memset(&sa, 0, sizeof(struct sigaction));
# sigemptyset(&sa.sa_mask); # sigemptyset(&sa.sa_mask);
# sa.sa_sigaction = f; # sa.sa_sigaction = f;
# sa.sa_flags = SA_SIGINFO; # sa.sa_flags = SA_SIGINFO;
# sigaction(SIGSEGV, &sa, 0); # sigaction(SIGSEGV, &sa, 0);
#endif }
} }