Skip to content

Commit

Permalink
pref: support file_notd_freestanding.v + file_d_freestanding.v, remov…
Browse files Browse the repository at this point in the history
…e dependency to `os`, of $embed_file(), when compiling with -freestanding (#20712)
  • Loading branch information
spytheman authored Feb 2, 2024
1 parent 07016fb commit 96e4a09
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 13 deletions.
15 changes: 2 additions & 13 deletions vlib/v/embed_file/embed_file.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module embed_file

import os

pub const is_used = 1

// EmbedFileData encapsulates functionality for the `$embed_file()` compile time call.
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/embed_file/embed_file_d_freestanding.v
Original file line number Diff line number Diff line change
@@ -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) {
}
18 changes: 18 additions & 0 deletions vlib/v/embed_file/embed_file_notd_freestanding.v
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions vlib/v/gen/c/coutput_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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}')
}
Original file line number Diff line number Diff line change
@@ -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}')
}
2 changes: 2 additions & 0 deletions vlib/v/gen/c/testdata/freestanding_module_import_1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hi from a_notd_freestanding.c.v
f.len: 166
4 changes: 4 additions & 0 deletions vlib/v/gen/c/testdata/freestanding_module_import_1.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// vtest vflags: -cc gcc
import v.gen.c.testdata.freestanding_define

freestanding_define.hi()
2 changes: 2 additions & 0 deletions vlib/v/gen/c/testdata/freestanding_module_import_2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hi from a_d_freestanding.c.v
f.len: 163
4 changes: 4 additions & 0 deletions vlib/v/gen/c/testdata/freestanding_module_import_2.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// vtest vflags: -cc gcc -freestanding
import v.gen.c.testdata.freestanding_define

freestanding_define.hi()
5 changes: 5 additions & 0 deletions vlib/v/pref/pref.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 96e4a09

Please sign in to comment.