cgen: do not #include signal.h, on -os wasm32 and -d no_segfault_handler

master
Delyan Angelov 2022-06-01 13:21:22 +03:00
parent 2fa64f1471
commit 5a2c271bd4
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 12 additions and 2 deletions

View File

@ -212,6 +212,7 @@ mut:
autofree_methods map[int]bool autofree_methods map[int]bool
generated_free_methods map[int]bool generated_free_methods map[int]bool
autofree_scope_stmts []string autofree_scope_stmts []string
use_segfault_handler bool = true
} }
pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) string { pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) string {
@ -267,6 +268,7 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) string {
field_data_type: ast.Type(table.find_type_idx('FieldData')) field_data_type: ast.Type(table.find_type_idx('FieldData'))
init: strings.new_builder(100) init: strings.new_builder(100)
is_cc_msvc: pref.ccompiler == 'msvc' is_cc_msvc: pref.ccompiler == 'msvc'
use_segfault_handler: !('no_segfault_handler' in pref.compile_defines || pref.os == .wasm32)
} }
// anon fn may include assert and thus this needs // anon fn may include assert and thus this needs
// to be included before any test contents are written // to be included before any test contents are written
@ -429,6 +431,9 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) string {
mut b := strings.new_builder(640000) mut b := strings.new_builder(640000)
b.write_string(g.hashes()) b.write_string(g.hashes())
if g.use_segfault_handler {
b.writeln('\n#define V_USE_SIGNAL_H')
}
b.writeln('\n// V comptime_definitions:') b.writeln('\n// V comptime_definitions:')
b.write_string(g.comptime_definitions.str()) b.write_string(g.comptime_definitions.str())
b.writeln('\n// V typedefs:') b.writeln('\n// V typedefs:')
@ -575,6 +580,7 @@ fn cgen_process_one_file_cb(p &pool.PoolProcessor, idx int, wid int) &Gen {
is_autofree: global_g.pref.autofree is_autofree: global_g.pref.autofree
referenced_fns: global_g.referenced_fns referenced_fns: global_g.referenced_fns
is_cc_msvc: global_g.is_cc_msvc is_cc_msvc: global_g.is_cc_msvc
use_segfault_handler: global_g.use_segfault_handler
} }
g.gen_file() g.gen_file()
return g return g
@ -781,7 +787,9 @@ pub fn (mut g Gen) init() {
// we know that this is being called before the multi-threading starts // we know that this is being called before the multi-threading starts
// and this is being called in the main thread, so we can mutate the table // and this is being called in the main thread, so we can mutate the table
mut muttable := unsafe { &ast.Table(g.table) } mut muttable := unsafe { &ast.Table(g.table) }
if g.use_segfault_handler {
muttable.used_fns['v_segmentation_fault_handler'] = true muttable.used_fns['v_segmentation_fault_handler'] = true
}
muttable.used_fns['eprintln'] = true muttable.used_fns['eprintln'] = true
muttable.used_fns['print_backtrace'] = true muttable.used_fns['print_backtrace'] = true
muttable.used_fns['exit'] = true muttable.used_fns['exit'] = true
@ -4734,7 +4742,7 @@ fn (mut g Gen) write_init_function() {
// ___argv is declared as voidptr here, because that unifies the windows/unix logic // ___argv is declared as voidptr here, because that unifies the windows/unix logic
g.writeln('void _vinit(int ___argc, voidptr ___argv) {') g.writeln('void _vinit(int ___argc, voidptr ___argv) {')
if 'no_segfault_handler' !in g.pref.compile_defines || g.pref.os == .wasm32 { if g.use_segfault_handler {
// 11 is SIGSEGV. It is hardcoded here, to avoid FreeBSD compilation errors for trivial examples. // 11 is SIGSEGV. It is hardcoded here, to avoid FreeBSD compilation errors for trivial examples.
g.writeln('#if __STDC_HOSTED__ == 1\n\tsignal(11, v_segmentation_fault_handler);\n#endif') g.writeln('#if __STDC_HOSTED__ == 1\n\tsignal(11, v_segmentation_fault_handler);\n#endif')
} }

View File

@ -554,7 +554,9 @@ voidptr memdup(voidptr src, int sz);
#include <io.h> // _waccess #include <io.h> // _waccess
#include <direct.h> // _wgetcwd #include <direct.h> // _wgetcwd
#ifdef V_USE_SIGNAL_H
#include <signal.h> // signal and SIGSEGV for segmentation fault handler #include <signal.h> // signal and SIGSEGV for segmentation fault handler
#endif
#ifdef _MSC_VER #ifdef _MSC_VER
// On MSVC these are the same (as long as /volatile:ms is passed) // On MSVC these are the same (as long as /volatile:ms is passed)