From c0a7397637992a5894b712c80fef3a7acc302a23 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 2 Oct 2023 19:04:25 +0300 Subject: [PATCH] native: support `-no-builtin` (generate executables < 1KB Linux with `v -no-builtin -b native examples/hello_world.v`) --- cmd/tools/vtest-all.v | 36 ++++++++++++++++++++ vlib/v/builder/nativebuilder/nativebuilder.v | 5 ++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/cmd/tools/vtest-all.v b/cmd/tools/vtest-all.v index 13d368480a..13eddab668 100644 --- a/cmd/tools/vtest-all.v +++ b/cmd/tools/vtest-all.v @@ -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 @@ -57,6 +59,8 @@ const ends_with_nothing = '' const contains_nothing = '' +type FnCheck = fn () ! + struct Command { mut: line string @@ -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 { @@ -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{ @@ -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) @@ -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 diff --git a/vlib/v/builder/nativebuilder/nativebuilder.v b/vlib/v/builder/nativebuilder/nativebuilder.v index e1807c5c34..790eea454d 100644 --- a/vlib/v/builder/nativebuilder/nativebuilder.v +++ b/vlib/v/builder/nativebuilder/nativebuilder.v @@ -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 {