Skip to content

Commit ed42341

Browse files
committed
pref,builder: support -use-os-system-to-run to workaround segfaults using not fully updated xcode command line tools
1 parent 3fb1230 commit ed42341

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

vlib/v/builder/compile.v

Lines changed: 27 additions & 19 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -134,26 +134,34 @@ fn (mut b Builder) run_compiled_executable_and_exit() {
134
run_args << ['run', compiled_file]
134
run_args << ['run', compiled_file]
135
}
135
}
136
run_args << b.pref.run_args
136
run_args << b.pref.run_args
137-
mut run_process := os.new_process(run_file)
137+
138-
run_process.set_args(run_args)
139
if b.pref.is_verbose {
138
if b.pref.is_verbose {
140-
println('running ${run_process.filename} with arguments ${run_process.args}')
139+
println('running ${run_file} with arguments ${run_args.join(' ')}')
141-
}
140+
}
142-
// Ignore sigint and sigquit while running the compiled file,
141+
mut ret := 0
143-
// so ^C doesn't prevent v from deleting the compiled file.
142+
if b.pref.use_os_system_to_run {
144-
// See also https://git.musl-libc.org/cgit/musl/tree/src/process/system.c
143+
command_to_run := run_file + ' ' + run_args.join(' ')
145-
prev_int_handler := os.signal_opt(.int, eshcb) or { serror('set .int', err) }
144+
ret = os.system(command_to_run)
146-
mut prev_quit_handler := os.SignalHandler(eshcb)
145+
// eprintln('> ret: ${ret:5} | command_to_run: ${command_to_run}')
147-
$if !windows { // There's no sigquit on windows
146+
} else {
148-
prev_quit_handler = os.signal_opt(.quit, eshcb) or { serror('set .quit', err) }
147+
mut run_process := os.new_process(run_file)
149-
}
148+
run_process.set_args(run_args)
150-
run_process.wait()
149+
// Ignore sigint and sigquit while running the compiled file,
151-
os.signal_opt(.int, prev_int_handler) or { serror('restore .int', err) }
150+
// so ^C doesn't prevent v from deleting the compiled file.
152-
$if !windows {
151+
// See also https://git.musl-libc.org/cgit/musl/tree/src/process/system.c
153-
os.signal_opt(.quit, prev_quit_handler) or { serror('restore .quit', err) }
152+
prev_int_handler := os.signal_opt(.int, eshcb) or { serror('set .int', err) }
154-
}
153+
mut prev_quit_handler := os.SignalHandler(eshcb)
155-
ret := run_process.code
154+
$if !windows { // There's no sigquit on windows
156-
run_process.close()
155+
prev_quit_handler = os.signal_opt(.quit, eshcb) or { serror('set .quit', err) }
156+
}
157+
run_process.wait()
158+
os.signal_opt(.int, prev_int_handler) or { serror('restore .int', err) }
159+
$if !windows {
160+
os.signal_opt(.quit, prev_quit_handler) or { serror('restore .quit', err) }
161+
}
162+
ret = run_process.code
163+
run_process.close()
164+
}
157
b.cleanup_run_executable_after_exit(compiled_file)
165
b.cleanup_run_executable_after_exit(compiled_file)
158
exit(ret)
166
exit(ret)
159
}
167
}

vlib/v/help/common/run.txt

Lines changed: 8 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -15,4 +15,12 @@ The exit status of run will be:
15
* `1` if the compilation failed.
15
* `1` if the compilation failed.
16
* The exit code of the compiled executable otherwise.
16
* The exit code of the compiled executable otherwise.
17

17

18+
Flags specific to `v run`:
19+
-use-os-system-to-run
20+
Use os.system() to run the produced executable, instead of os.new_process;
21+
this is useful as a temporary workaround for segfaults, when running code
22+
on macos, that may happen when xcode is updated after 2023/08/30, and for
23+
comparing os.system with os.new_process on other systems. Usually used with:
24+
`export VFLAGS='-use-os-system-to-run'`.
25+
18
For more about build flags, see `v help build`.
26
For more about build flags, see `v help build`.

vlib/v/pref/pref.v

Lines changed: 4 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -154,6 +154,7 @@ pub mut:
154
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.
154
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.
155
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)
155
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)
156
retry_compilation bool = true // retry the compilation with another C compiler, if tcc fails.
156
retry_compilation bool = true // retry the compilation with another C compiler, if tcc fails.
157+
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
157
// TODO Convert this into a []string
158
// TODO Convert this into a []string
158
cflags string // Additional options which will be passed to the C compiler *before* other options.
159
cflags string // Additional options which will be passed to the C compiler *before* other options.
159
ldflags string // Additional options which will be passed to the C compiler *after* everything else.
160
ldflags string // Additional options which will be passed to the C compiler *after* everything else.
@@ -660,6 +661,9 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
660
'-usecache' {
661
'-usecache' {
661
res.use_cache = true
662
res.use_cache = true
662
}
663
}
664+
'-use-os-system-to-run' {
665+
res.use_os_system_to_run = true
666+
}
663
'-nocache' {
667
'-nocache' {
664
res.use_cache = false
668
res.use_cache = false
665
}
669
}

0 commit comments

Comments
 (0)