From 472f0acc544660088248ea27cfc9c2105b4ca665 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 29 Jun 2026 01:51:50 +0300 Subject: [PATCH] v3: fix self host --- vlib/v3/gen/c/cleanc.v | 6 +++ vlib/v3/gen/c/fn.v | 52 +++++++++++++++++++ .../v3/tests/c_directive_order_codegen_test.v | 16 +++++- .../v3/tests/posix_wait_header_codegen_test.v | 1 + 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/vlib/v3/gen/c/cleanc.v b/vlib/v3/gen/c/cleanc.v index ec7c3e005..4d03431ab 100644 --- a/vlib/v3/gen/c/cleanc.v +++ b/vlib/v3/gen/c/cleanc.v @@ -766,6 +766,9 @@ fn c_preserved_system_include_declared_fns(include_arg string) []string { '' { return ['dlopen', 'dlsym', 'dlclose', 'dlerror'] } + '' { + return ['mach_absolute_time', 'mach_timebase_info'] + } else { return []string{} } @@ -774,6 +777,9 @@ fn c_preserved_system_include_declared_fns(include_arg string) []string { fn c_preserved_system_include_struct_names(include_arg string) []string { match include_arg.trim_space() { + '' { + return ['mach_timebase_info_data_t'] + } '', '', '', '', '', '' { return c_x11_preserved_struct_names.clone() diff --git a/vlib/v3/gen/c/fn.v b/vlib/v3/gen/c/fn.v index 95294023b..6b7f1b436 100644 --- a/vlib/v3/gen/c/fn.v +++ b/vlib/v3/gen/c/fn.v @@ -3907,6 +3907,9 @@ fn (g &FlatGen) should_emit_c_extern_decl(cfn string) bool { if cfn.contains('.') { return false } + if cfn in c_preamble_declared_extern_symbols { + return false + } if cfn in c_static_helper_symbols { return false } @@ -3919,6 +3922,55 @@ fn (g &FlatGen) should_emit_c_extern_decl(cfn string) bool { return true } +const c_preamble_declared_extern_symbols = { + '_exit': true + '__errno': true + '__errno_location': true + '__error': true + '_errno': true + 'abort': true + 'access': true + 'atexit': true + 'ceil': true + 'ceilf': true + 'close': true + 'cos': true + 'dup2': true + 'execlp': true + 'execvp': true + 'fabs': true + 'fcntl': true + 'floor': true + 'floorf': true + 'fmod': true + 'fork': true + 'getenv': true + 'ldexp': true + 'memcmp': true + 'memcpy': true + 'memmove': true + 'memset': true + 'open': true + 'pipe': true + 'pow': true + 'pthread_mutex_destroy': true + 'pthread_mutex_init': true + 'pthread_mutex_lock': true + 'pthread_mutex_unlock': true + 'read': true + 'realpath': true + 'setenv': true + 'signal': true + 'snprintf': true + 'sqrt': true + 'strcmp': true + 'strlen': true + 'strncmp': true + 'strncpy': true + 'strrchr': true + 'strstr': true +} + const c_static_helper_symbols = { 'EV_SET': true 'FD_ISSET': true diff --git a/vlib/v3/tests/c_directive_order_codegen_test.v b/vlib/v3/tests/c_directive_order_codegen_test.v index 34feb2ba7..367101a68 100644 --- a/vlib/v3/tests/c_directive_order_codegen_test.v +++ b/vlib/v3/tests/c_directive_order_codegen_test.v @@ -677,7 +677,18 @@ fn directive_order_gen_c_preserved_mach_headers(v3_bin string) string { #include #include -fn main() {} +@[typedef] +struct C.mach_timebase_info_data_t { + numer u32 + denom u32 +} + +fn C.mach_timebase_info(&C.mach_timebase_info_data_t) + +fn main() { + mut tb := C.mach_timebase_info_data_t{} + C.mach_timebase_info(&tb) +} ') c_out := os.join_path(os.temp_dir(), 'v3_c_directive_order_mach.c') os.rm(c_out) or {} @@ -1013,6 +1024,9 @@ fn test_preserved_mach_headers_are_wrapped_with_panic_alias() { assert c_code.contains('#define panic mach_panic\n#include \n#undef panic'), c_code assert c_code.contains('#define panic mach_panic\n#include \n#undef panic'), c_code assert directive_order_index(c_code, '#undef panic') < preamble_idx, c_code + assert !c_code.contains('typedef struct mach_timebase_info_data_t mach_timebase_info_data_t;'), c_code + + assert directive_order_count(c_code, 'mach_timebase_info(') == 1, c_code } fn test_stdarg_in_inlined_header_uses_headerless_va_defs() { diff --git a/vlib/v3/tests/posix_wait_header_codegen_test.v b/vlib/v3/tests/posix_wait_header_codegen_test.v index 44f6935da..04504609c 100644 --- a/vlib/v3/tests/posix_wait_header_codegen_test.v +++ b/vlib/v3/tests/posix_wait_header_codegen_test.v @@ -95,6 +95,7 @@ fn main() { assert with_os.c_code.contains('void* memmove(void* dest, const void* src, size_t n);'), with_os.c_code assert with_os.c_code.contains('int memcmp(const void* s1, const void* s2, size_t n);'), with_os.c_code assert with_os.c_code.contains('size_t strlen(const char* s);'), with_os.c_code + assert !with_os.c_code.contains('i32 strlen(char* s);'), with_os.c_code assert with_os.c_code.contains('int strcmp(const char* s1, const char* s2);'), with_os.c_code assert with_os.c_code.contains('int strncmp(const char* s1, const char* s2, size_t n);'), with_os.c_code assert with_os.c_code.contains('char* strncpy(char* dest, const char* src, size_t n);'), with_os.c_code -- 2.39.5