Skip to content

Commit

Permalink
native: support -no-builtin (generate executables < 1KB Linux with …
Browse files Browse the repository at this point in the history
…`v -no-builtin -b native examples/hello_world.v`)
  • Loading branch information
spytheman committed Oct 2, 2023
1 parent f43be7a commit c0a7397
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
36 changes: 36 additions & 0 deletions cmd/tools/vtest-all.v
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const vargs = args_string.all_before('test-all')

const vtest_nocleanup = os.getenv('VTEST_NOCLEANUP').bool()

const hw_native_no_builtin_size_limit = 300

fn main() {
mut commands := get_all_commands()
// summary
Expand Down Expand Up @@ -57,6 +59,8 @@ const ends_with_nothing = '<nothing>'

const contains_nothing = '<nothing>'

type FnCheck = fn () !

struct Command {
mut:
line string
Expand All @@ -71,6 +75,8 @@ mut:
ends_with string = ends_with_nothing
contains string = contains_nothing
output string
before_cb FnCheck = unsafe { nil }
after_cb FnCheck = unsafe { nil }
}

fn get_all_commands() []Command {
Expand Down Expand Up @@ -156,6 +162,20 @@ fn get_all_commands() []Command {
line: '${vexe} -experimental -b native run examples/native/hello_world.v > /dev/null'
okmsg: 'V compiles and runs examples/native/hello_world.v on the native backend for linux'
}
res << Command{
line: '${vexe} -no-builtin -experimental -b native examples/hello_world.v > /dev/null'
okmsg: 'V compiles examples/hello_world.v on the native backend for linux with `-no-builtin` & the executable is <= ${hw_native_no_builtin_size_limit} bytes'
rmfile: 'examples/hello_world'
after_cb: fn () ! {
file := 'examples/hello_world'
if !os.exists(file) {
return error('>> file ${file} does not exist')
}
if os.file_size(file) > hw_native_no_builtin_size_limit {
return error('>> file ${file} bigger than ${hw_native_no_builtin_size_limit} bytes')
}
}
}
}
// only compilation:
res << Command{
Expand Down Expand Up @@ -329,6 +349,14 @@ fn (mut cmd Command) run() {
if cmd.label != '' {
println(term.header_left(cmd.label, '*'))
}
if cmd.before_cb != unsafe { nil } {
cmd.before_cb() or {
cmd.ecode = -1
cmd.errmsg = '> before_cb callback for "${cmd.line}" ${term.failed('FAILED')}\n${err}'
println(cmd.errmsg)
return
}
}
sw := time.new_stopwatch()
if cmd.runcmd == .system {
cmd.ecode = os.system(cmd.line)
Expand All @@ -340,6 +368,14 @@ fn (mut cmd Command) run() {
cmd.output = res.output
}
spent := sw.elapsed().milliseconds()
if cmd.after_cb != unsafe { nil } {
cmd.after_cb() or {
cmd.ecode = -1
cmd.errmsg = '> after_cb callback for "${cmd.line}" ${term.failed('FAILED')}\n${err}'
println(cmd.errmsg)
return
}
}
//
mut is_failed := false
mut is_failed_expected := false
Expand Down
5 changes: 4 additions & 1 deletion vlib/v/builder/nativebuilder/nativebuilder.v
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ pub fn compile_native(mut b builder.Builder) {
if b.pref.is_verbose {
println('all .v files before:')
}
mut files := b.get_builtin_files()
mut files := []string{}
if !b.pref.no_builtin {
files << b.get_builtin_files()
}
files << b.get_user_files()
b.set_module_lookup_paths()
if b.pref.is_verbose {
Expand Down

0 comments on commit c0a7397

Please sign in to comment.