From 881f3b8c1b8e5fae14b2a94212d8be110cd7ca74 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 1 May 2026 04:45:37 +0300 Subject: [PATCH] all: vinix fixes --- vlib/builtin/builtin_nix.c.v | 4 +- vlib/builtin/printing.c.v | 4 +- vlib/db/sqlite/install_thirdparty_sqlite.vsh | 1 - vlib/v/gen/c/cheaders.v | 16 +++++++ .../gen/c/cheaders_manual_stdlib_decls_test.v | 19 ++++++++ vlib/v/gen/c/coutput_test.v | 46 +++++++++++++++++++ vlib/v/gen/c/fn.v | 3 ++ 7 files changed, 90 insertions(+), 3 deletions(-) diff --git a/vlib/builtin/builtin_nix.c.v b/vlib/builtin/builtin_nix.c.v index 17d40dca3..c0fba08bf 100644 --- a/vlib/builtin/builtin_nix.c.v +++ b/vlib/builtin/builtin_nix.c.v @@ -10,7 +10,9 @@ fn builtin_init() { gc_set_warn_proc(internal_gc_warn_proc_none) } } - unbuffer_stdout() + $if !vinix { + unbuffer_stdout() + } } fn break_if_debugger_attached() { diff --git a/vlib/builtin/printing.c.v b/vlib/builtin/printing.c.v index 5acfd5aa8..7cdae90ec 100644 --- a/vlib/builtin/printing.c.v +++ b/vlib/builtin/printing.c.v @@ -103,7 +103,9 @@ pub fn flush_stderr() { // See https://www.gnu.org/software/libc/manual/html_node/Buffering-Concepts.html . // See https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_05 . pub fn unbuffer_stdout() { - $if freestanding { + $if vinix { + return + } $else $if freestanding { not_implemented := 'unbuffer_stdout is not implemented\n' bare_eprint(not_implemented.str, u64(not_implemented.len)) } $else $if builtin_write_buf_to_fd_should_use_c_write ? { diff --git a/vlib/db/sqlite/install_thirdparty_sqlite.vsh b/vlib/db/sqlite/install_thirdparty_sqlite.vsh index 1d0296959..3527fb509 100755 --- a/vlib/db/sqlite/install_thirdparty_sqlite.vsh +++ b/vlib/db/sqlite/install_thirdparty_sqlite.vsh @@ -10,7 +10,6 @@ fn should_be_ok(http_status_code int, msg string) { } fn main() { - unbuffer_stdout() os.chdir(@VEXEROOT)! download_page_url := 'https://sqlite.org/download.html' diff --git a/vlib/v/gen/c/cheaders.v b/vlib/v/gen/c/cheaders.v index 99fff2d67..e3ca0747b 100644 --- a/vlib/v/gen/c/cheaders.v +++ b/vlib/v/gen/c/cheaders.v @@ -477,6 +477,22 @@ extern struct __sFstub __stderr[]; #define stdin ((struct __sFILE *)__stdin) #define stdout ((struct __sFILE *)__stdout) #define stderr ((struct __sFILE *)__stderr) + #elif defined(__BIONIC__) +struct __sFILE; +typedef struct __sFILE FILE; + #if __ANDROID_API__ >= 23 +extern FILE* stdin; +extern FILE* stdout; +extern FILE* stderr; + #define stdin stdin + #define stdout stdout + #define stderr stderr + #else +extern FILE __sF[]; + #define stdin (&__sF[0]) + #define stdout (&__sF[1]) + #define stderr (&__sF[2]) + #endif #elif defined(__linux__) && !defined(__GLIBC__) && !defined(__GNU_LIBRARY__) && !defined(__BIONIC__) && !defined(__UCLIBC__) typedef struct _IO_FILE FILE; // musl exposes the stdio streams as `FILE *const`, so match that to stay diff --git a/vlib/v/gen/c/cheaders_manual_stdlib_decls_test.v b/vlib/v/gen/c/cheaders_manual_stdlib_decls_test.v index 23ef77e56..5419db2b4 100644 --- a/vlib/v/gen/c/cheaders_manual_stdlib_decls_test.v +++ b/vlib/v/gen/c/cheaders_manual_stdlib_decls_test.v @@ -62,6 +62,24 @@ fn test_default_c_prelude_uses_manual_stdio_stdlib_string_and_stdarg_decls() { assert generated_c.contains('#if defined(__vinix__)\nV_CRT_LINKAGE int V_CRT_CALL strcmp(char *left, char *right);\nV_CRT_LINKAGE int V_CRT_CALL strncmp(char *left, char *right, size_t n);\n#else\nV_CRT_LINKAGE int V_CRT_CALL strcmp(const char *left, const char *right);'), generated_c } +fn test_android_prelude_uses_bionic_file_decls() { + tmp_dir := os.join_path(os.vtmp_dir(), 'cheaders_android_${os.getpid()}') + os.mkdir_all(tmp_dir)! + defer { + os.rmdir_all(tmp_dir) or {} + } + source_path := os.join_path(tmp_dir, 'android.v') + output_path := os.join_path(tmp_dir, 'android.c') + os.write_file(source_path, 'fn main() {\n\tprintln("hi")\n}\n')! + cmd := '${os.quoted_path(cheaders_manual_stdlib_vexe)} -os android -apk -o ${os.quoted_path(output_path)} ${os.quoted_path(source_path)}' + res := os.execute(cmd) + assert res.exit_code == 0, '${cmd}\n${res.output}' + generated_c := os.read_file(output_path)!.replace('\r\n', '\n') + assert generated_c.contains('#elif defined(__BIONIC__)\nstruct __sFILE;\ntypedef struct __sFILE FILE;'), generated_c + assert generated_c.contains('#if __ANDROID_API__ >= 23\nextern FILE* stdin;\nextern FILE* stdout;\nextern FILE* stderr;'), generated_c + assert !generated_c.contains('#elif defined(__BIONIC__)\ntypedef struct _IO_FILE FILE;'), generated_c +} + fn test_vinix_prelude_leaves_stdio_and_stdlib_to_vinix_stubs() { tmp_dir := os.join_path(os.vtmp_dir(), 'cheaders_vinix_${os.getpid()}') os.mkdir_all(tmp_dir)! @@ -83,6 +101,7 @@ fn test_vinix_prelude_leaves_stdio_and_stdlib_to_vinix_stubs() { assert !generated_c.contains('extern void printf_panic(charptr _d1, Array_voidptr _d2);'), generated_c assert !generated_c.contains('extern void text_start();'), generated_c assert !generated_c.contains('extern voidptr __builtin_return_address(int _d1);'), generated_c + assert !generated_c.contains('builtin__unbuffer_stdout();'), generated_c } fn test_msvc_windows_prelude_uses_msvc_crt_headers() { diff --git a/vlib/v/gen/c/coutput_test.v b/vlib/v/gen/c/coutput_test.v index cb65e1f69..cd8b48a71 100644 --- a/vlib/v/gen/c/coutput_test.v +++ b/vlib/v/gen/c/coutput_test.v @@ -290,6 +290,52 @@ fn main() { assert !compilation.output.contains('extern int c_header_decl(') } +fn test_c_fallback_decl_uses_c_helper_submodule_includes() { + test_source := os.join_path(os.vtmp_dir(), 'coutput_c_helper_include') + module_path := os.join_path(test_source, 'sdl') + c_module_path := os.join_path(module_path, 'c') + os.rmdir_all(test_source) or {} + os.mkdir_all(c_module_path)! + defer { + os.rmdir_all(test_source) or {} + } + header_path := os.join_path(c_module_path, 'c_helper_decl.h') + os.write_file(header_path, + ['#include ', 'typedef enum { false_value, true_value } foreign_bool;', 'foreign_bool c_helper_decl(void);'].join('\n') + + '\n')! + header_include_path := header_path.replace('\\', '/') + os.write_file(os.join_path(c_module_path, 'c.c.v'), 'module c + +pub const used_import = 1 + +#include "${header_include_path}" +')! + os.write_file(os.join_path(module_path, 'sdl.v'), 'module sdl + +import sdl.c + +pub const used_import = c.used_import +')! + os.write_file(os.join_path(module_path, 'atomic.c.v'), 'module sdl + +fn C.c_helper_decl() bool + +pub fn call() { + _ = C.c_helper_decl() +} +')! + old_wd := os.getwd() + os.chdir(test_source) or {} + defer { + os.chdir(old_wd) or {} + } + cmd := '${os.quoted_path(vexe)} -shared -o - sdl' + compilation := os.execute(cmd) + ensure_compilation_succeeded(compilation, cmd) + assert compilation.output.contains('#include "${header_include_path}"') + assert !compilation.output.contains('extern bool c_helper_decl(') +} + fn test_user_defined_windows_dllmain_disables_generated_entrypoint() { os.chdir(vroot) or {} test_source := os.join_path(os.vtmp_dir(), 'coutput_user_defined_windows_dllmain.vv') diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 6ce6373aa..c856f8114 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -832,6 +832,9 @@ fn modules_with_c_includes(files []&ast.File) map[string]bool { for file in files { if file_has_c_includes(file) { mods[file.mod.name] = true + if file.mod.name.ends_with('.c') { + mods[file.mod.name.all_before_last('.')] = true + } } } return mods -- 2.39.5