Skip to content

Commit

Permalink
pref,builder: support -use-os-system-to-run to workaround segfaults u…
Browse files Browse the repository at this point in the history
…sing not fully updated xcode command line tools
  • Loading branch information
spytheman committed Sep 22, 2023
1 parent 3fb1230 commit ed42341
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
46 changes: 27 additions & 19 deletions vlib/v/builder/compile.v
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,34 @@ fn (mut b Builder) run_compiled_executable_and_exit() {
run_args << ['run', compiled_file]
}
run_args << b.pref.run_args
mut run_process := os.new_process(run_file)
run_process.set_args(run_args)

if b.pref.is_verbose {
println('running ${run_process.filename} with arguments ${run_process.args}')
}
// Ignore sigint and sigquit while running the compiled file,
// so ^C doesn't prevent v from deleting the compiled file.
// See also https://git.musl-libc.org/cgit/musl/tree/src/process/system.c
prev_int_handler := os.signal_opt(.int, eshcb) or { serror('set .int', err) }
mut prev_quit_handler := os.SignalHandler(eshcb)
$if !windows { // There's no sigquit on windows
prev_quit_handler = os.signal_opt(.quit, eshcb) or { serror('set .quit', err) }
}
run_process.wait()
os.signal_opt(.int, prev_int_handler) or { serror('restore .int', err) }
$if !windows {
os.signal_opt(.quit, prev_quit_handler) or { serror('restore .quit', err) }
}
ret := run_process.code
run_process.close()
println('running ${run_file} with arguments ${run_args.join(' ')}')
}
mut ret := 0
if b.pref.use_os_system_to_run {
command_to_run := run_file + ' ' + run_args.join(' ')
ret = os.system(command_to_run)
// eprintln('> ret: ${ret:5} | command_to_run: ${command_to_run}')
} else {
mut run_process := os.new_process(run_file)
run_process.set_args(run_args)
// Ignore sigint and sigquit while running the compiled file,
// so ^C doesn't prevent v from deleting the compiled file.
// See also https://git.musl-libc.org/cgit/musl/tree/src/process/system.c
prev_int_handler := os.signal_opt(.int, eshcb) or { serror('set .int', err) }
mut prev_quit_handler := os.SignalHandler(eshcb)
$if !windows { // There's no sigquit on windows
prev_quit_handler = os.signal_opt(.quit, eshcb) or { serror('set .quit', err) }
}
run_process.wait()
os.signal_opt(.int, prev_int_handler) or { serror('restore .int', err) }
$if !windows {
os.signal_opt(.quit, prev_quit_handler) or { serror('restore .quit', err) }
}
ret = run_process.code
run_process.close()
}
b.cleanup_run_executable_after_exit(compiled_file)
exit(ret)
}
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/help/common/run.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ The exit status of run will be:
* `1` if the compilation failed.
* The exit code of the compiled executable otherwise.

Flags specific to `v run`:
-use-os-system-to-run
Use os.system() to run the produced executable, instead of os.new_process;
this is useful as a temporary workaround for segfaults, when running code
on macos, that may happen when xcode is updated after 2023/08/30, and for
comparing os.system with os.new_process on other systems. Usually used with:
`export VFLAGS='-use-os-system-to-run'`.

For more about build flags, see `v help build`.
4 changes: 4 additions & 0 deletions vlib/v/pref/pref.v
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pub mut:
dump_files string // `-dump-files files.txt` - let V store all V or .template file paths, that were used by the compiled program in `files.txt`, one path per line.
use_cache bool // when set, use cached modules to speed up subsequent compilations, at the cost of slower initial ones (while the modules are cached)
retry_compilation bool = true // retry the compilation with another C compiler, if tcc fails.
use_os_system_to_run bool // when set, use os.system() to run the produced executable, instead of os.new_process; works around segfaults on macos, that may happen when xcode is updated
// TODO Convert this into a []string
cflags string // Additional options which will be passed to the C compiler *before* other options.
ldflags string // Additional options which will be passed to the C compiler *after* everything else.
Expand Down Expand Up @@ -660,6 +661,9 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
'-usecache' {
res.use_cache = true
}
'-use-os-system-to-run' {
res.use_os_system_to_run = true
}
'-nocache' {
res.use_cache = false
}
Expand Down

0 comments on commit ed42341

Please sign in to comment.