From 96e4a09b66d07a9f4f5e9a4885bdbc5cec4b0d75 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 2 Feb 2024 23:35:19 +0200 Subject: [PATCH] pref: support file_notd_freestanding.v + file_d_freestanding.v, remove dependency to `os`, of $embed_file(), when compiling with -freestanding (#20712) --- vlib/v/embed_file/embed_file.v | 15 ++------------- vlib/v/embed_file/embed_file_d_freestanding.v | 5 +++++ .../embed_file/embed_file_notd_freestanding.v | 18 ++++++++++++++++++ vlib/v/gen/c/coutput_test.v | 12 ++++++++++++ .../freestanding_define/a_d_freestanding.c.v | 8 ++++++++ .../a_notd_freestanding.c.v | 8 ++++++++ .../testdata/freestanding_module_import_1.out | 2 ++ .../c/testdata/freestanding_module_import_1.vv | 4 ++++ .../testdata/freestanding_module_import_2.out | 2 ++ .../c/testdata/freestanding_module_import_2.vv | 4 ++++ vlib/v/pref/pref.c.v | 5 +++++ 11 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 vlib/v/embed_file/embed_file_d_freestanding.v create mode 100644 vlib/v/embed_file/embed_file_notd_freestanding.v create mode 100644 vlib/v/gen/c/testdata/freestanding_define/a_d_freestanding.c.v create mode 100644 vlib/v/gen/c/testdata/freestanding_define/a_notd_freestanding.c.v create mode 100644 vlib/v/gen/c/testdata/freestanding_module_import_1.out create mode 100644 vlib/v/gen/c/testdata/freestanding_module_import_1.vv create mode 100644 vlib/v/gen/c/testdata/freestanding_module_import_2.out create mode 100644 vlib/v/gen/c/testdata/freestanding_module_import_2.vv diff --git a/vlib/v/embed_file/embed_file.v b/vlib/v/embed_file/embed_file.v index 1f578018b..289e960d2 100644 --- a/vlib/v/embed_file/embed_file.v +++ b/vlib/v/embed_file/embed_file.v @@ -1,7 +1,5 @@ module embed_file -import os - pub const is_used = 1 // EmbedFileData encapsulates functionality for the `$embed_file()` compile time call. @@ -71,18 +69,9 @@ pub fn (mut ed EmbedFileData) data() &u8 { ed.uncompressed = &u8(memdup(decompressed.data, ed.len)) } } else { - mut path := os.resource_abs_path(ed.path) - if !os.is_file(path) { - path = ed.apath - if !os.is_file(path) { - panic('EmbedFileData error: files "${ed.path}" and "${ed.apath}" do not exist') - } - } - bytes := os.read_bytes(path) or { - panic('EmbedFileData error: "${path}" could not be read: ${err}') + $if !freestanding { + reload_from_file_at_runtime(mut ed) } - ed.uncompressed = bytes.data - ed.free_uncompressed = true } return ed.uncompressed } diff --git a/vlib/v/embed_file/embed_file_d_freestanding.v b/vlib/v/embed_file/embed_file_d_freestanding.v new file mode 100644 index 000000000..f4b31dc28 --- /dev/null +++ b/vlib/v/embed_file/embed_file_d_freestanding.v @@ -0,0 +1,5 @@ +module embed_file + +// do nothing, `os` is not available for that target +fn reload_from_file_at_runtime(mut ed EmbedFileData) { +} diff --git a/vlib/v/embed_file/embed_file_notd_freestanding.v b/vlib/v/embed_file/embed_file_notd_freestanding.v new file mode 100644 index 000000000..ffdb6ca46 --- /dev/null +++ b/vlib/v/embed_file/embed_file_notd_freestanding.v @@ -0,0 +1,18 @@ +module embed_file + +import os + +fn reload_from_file_at_runtime(mut ed EmbedFileData) { + mut path := os.resource_abs_path(ed.path) + if !os.is_file(path) { + path = ed.apath + if !os.is_file(path) { + panic('EmbedFileData error: files "${ed.path}" and "${ed.apath}" do not exist') + } + } + bytes := os.read_bytes(path) or { + panic('EmbedFileData error: "${path}" could not be read: ${err}') + } + ed.uncompressed = bytes.data + ed.free_uncompressed = true +} diff --git a/vlib/v/gen/c/coutput_test.v b/vlib/v/gen/c/coutput_test.v index 86e6ed20b..ec001c6cc 100644 --- a/vlib/v/gen/c/coutput_test.v +++ b/vlib/v/gen/c/coutput_test.v @@ -17,6 +17,8 @@ const show_compilation_output = os.getenv('VTEST_SHOW_COMPILATION_OUTPUT').int() const user_os = os.user_os() +const gcc_path = os.find_abs_path_of_executable('gcc') or { '' } + fn mm(s string) string { return term.colorize(term.magenta, s) } @@ -253,5 +255,15 @@ fn should_skip(relpath string) bool { return true } } + if relpath.contains('freestanding_module_import_') { + if user_os != 'linux' { + eprintln('> skipping ${relpath} on != linux') + return true + } + if gcc_path == '' { + eprintln('> skipping ${relpath} since it needs gcc, which is not detected') + return true + } + } return false } diff --git a/vlib/v/gen/c/testdata/freestanding_define/a_d_freestanding.c.v b/vlib/v/gen/c/testdata/freestanding_define/a_d_freestanding.c.v new file mode 100644 index 000000000..d2b837c35 --- /dev/null +++ b/vlib/v/gen/c/testdata/freestanding_define/a_d_freestanding.c.v @@ -0,0 +1,8 @@ +module freestanding_define + +const f = $embed_file(@FILE).len + +pub fn hi() { + println('hi from a_d_freestanding.c.v') + println('f.len: ${freestanding_define.f}') +} diff --git a/vlib/v/gen/c/testdata/freestanding_define/a_notd_freestanding.c.v b/vlib/v/gen/c/testdata/freestanding_define/a_notd_freestanding.c.v new file mode 100644 index 000000000..014ded57c --- /dev/null +++ b/vlib/v/gen/c/testdata/freestanding_define/a_notd_freestanding.c.v @@ -0,0 +1,8 @@ +module freestanding_define + +const f = $embed_file(@FILE).len + +pub fn hi() { + println('hi from a_notd_freestanding.c.v') + println('f.len: ${freestanding_define.f}') +} diff --git a/vlib/v/gen/c/testdata/freestanding_module_import_1.out b/vlib/v/gen/c/testdata/freestanding_module_import_1.out new file mode 100644 index 000000000..51c71e152 --- /dev/null +++ b/vlib/v/gen/c/testdata/freestanding_module_import_1.out @@ -0,0 +1,2 @@ +hi from a_notd_freestanding.c.v +f.len: 166 diff --git a/vlib/v/gen/c/testdata/freestanding_module_import_1.vv b/vlib/v/gen/c/testdata/freestanding_module_import_1.vv new file mode 100644 index 000000000..3b4e8edee --- /dev/null +++ b/vlib/v/gen/c/testdata/freestanding_module_import_1.vv @@ -0,0 +1,4 @@ +// vtest vflags: -cc gcc +import v.gen.c.testdata.freestanding_define + +freestanding_define.hi() diff --git a/vlib/v/gen/c/testdata/freestanding_module_import_2.out b/vlib/v/gen/c/testdata/freestanding_module_import_2.out new file mode 100644 index 000000000..c69fc5b61 --- /dev/null +++ b/vlib/v/gen/c/testdata/freestanding_module_import_2.out @@ -0,0 +1,2 @@ +hi from a_d_freestanding.c.v +f.len: 163 diff --git a/vlib/v/gen/c/testdata/freestanding_module_import_2.vv b/vlib/v/gen/c/testdata/freestanding_module_import_2.vv new file mode 100644 index 000000000..c19b1e17c --- /dev/null +++ b/vlib/v/gen/c/testdata/freestanding_module_import_2.vv @@ -0,0 +1,4 @@ +// vtest vflags: -cc gcc -freestanding +import v.gen.c.testdata.freestanding_define + +freestanding_define.hi() diff --git a/vlib/v/pref/pref.c.v b/vlib/v/pref/pref.c.v index 41203d68c..377767bff 100644 --- a/vlib/v/pref/pref.c.v +++ b/vlib/v/pref/pref.c.v @@ -1043,6 +1043,11 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin res.compile_defines << 'musl' res.compile_defines_all << 'musl' } + if res.is_bare { + // make `$if freestanding? {` + file_freestanding.v + file_notd_freestanding.v work: + res.compile_defines << 'freestanding' + res.compile_defines_all << 'freestanding' + } if 'callstack' in res.compile_defines_all { res.is_callstack = true } -- 2.39.5