From 0fb8074353c0b7df8742dfc8f0f42ee9cc5e4b2e Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 31 Jul 2020 21:25:37 +0300 Subject: [PATCH] cgen: add support for `$if gcc {}` too --- vlib/v/gen/cgen.v | 3 +++ vlib/v/gen/cheaders.v | 17 +++++++++++++++++ vlib/v/parser/comptime.v | 19 +++++++++++++------ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index e91300ff51..c0a6050ccf 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -4026,6 +4026,9 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string { return '_VJS' } // compilers: + 'gcc' { + return '__V_GCC__' + } 'tinyc' { return '__TINYC__' } diff --git a/vlib/v/gen/cheaders.v b/vlib/v/gen/cheaders.v index 638d6856a6..ab2204beb7 100644 --- a/vlib/v/gen/cheaders.v +++ b/vlib/v/gen/cheaders.v @@ -28,6 +28,23 @@ const ( #define __NOINLINE __attribute__((noinline)) #define __IRQHANDLER __attribute__((interrupt)) +// Using just __GNUC__ for detecting gcc, is not reliable because other compilers define it too: +#ifdef __GNUC__ + #define __V_GCC__ +#endif +#ifdef __TINYC__ + #undef __V_GCC__ +#endif +#ifdef __cplusplus + #undef __V_GCC__ +#endif +#ifdef __clang__ + #undef __V_GCC__ +#endif +#ifdef _MSC_VER + #undef __V_GCC__ +#endif + #ifdef __TINYC__ #undef EMPTY_STRUCT_DECLARATION #undef EMPTY_STRUCT_INITIALIZATION diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index f8c931b375..be580adc0c 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -225,19 +225,26 @@ fn (mut p Parser) comp_if() ast.Stmt { name_pos := name_pos_start.extend(p.tok.position()) // mut stmts := []ast.Stmt{} - mut skip := false + mut skip_os := false + mut skip_cc := false if val in supported_platforms { os := os_from_string(val) if (!is_not && os != p.pref.os) || (is_not && os == p.pref.os) { - skip = true + skip_os = true } } else if val in supported_ccompilers { - cc := cc_from_string(val) - user_cc := cc_from_string(p.pref.ccompiler) - if (!is_not && cc != user_cc) || (is_not && cc == user_cc) { - skip = true + if p.pref.ccompiler.len == 2 && p.pref.ccompiler == 'cc' { + // we just do not know, so we can not skip: + skip_cc = false + }else { + cc := cc_from_string(val) + user_cc := cc_from_string(p.pref.ccompiler) + if (!is_not && cc != user_cc) || (is_not && cc == user_cc) { + skip_cc = true + } } } + mut skip := skip_os || skip_cc // `$if os {` or `$if compiler {` for a different target, skip everything inside // to avoid compilation errors (like including or calling WinAPI fns // on non-Windows systems)