1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: make exec() return ?Result with exit code and output

This commit is contained in:
Alexander Medvednikov 2019-08-17 20:21:59 +03:00
parent 60bf668281
commit 8a1324c141
5 changed files with 48 additions and 35 deletions

View File

@ -261,7 +261,7 @@ fn build_thirdparty_obj_file(flag string) {
res := os.exec('$cc -fPIC -c -o $obj_path $cfiles') or { res := os.exec('$cc -fPIC -c -o $obj_path $cfiles') or {
panic(err) panic(err)
} }
println(res) println(res.output)
} }
fn os_name_to_ifdef(name string) string { fn os_name_to_ifdef(name string) string {

View File

@ -924,7 +924,7 @@ mut args := ''
'/usr/lib/x86_64-linux-gnu/crtn.o') or { '/usr/lib/x86_64-linux-gnu/crtn.o') or {
panic(err) panic(err)
} }
println(ress) println(ress.output)
println('linux cross compilation done. resulting binary: "$v.out_name"') println('linux cross compilation done. resulting binary: "$v.out_name"')
} }
if !v.pref.is_debug && v.out_name_c != 'v.c' && v.out_name_c != 'v_macos.c' { if !v.pref.is_debug && v.out_name_c != 'v.c' && v.out_name_c != 'v_macos.c' {
@ -1336,7 +1336,7 @@ fn run_repl() []string {
s := os.exec('$vexe run $file -repl') or { s := os.exec('$vexe run $file -repl') or {
panic(err) panic(err)
} }
vals := s.split('\n') vals := s.output.split('\n')
for i:=0; i < vals.len; i++ { for i:=0; i < vals.len; i++ {
println(vals[i]) println(vals[i])
} }
@ -1354,7 +1354,7 @@ fn run_repl() []string {
panic(err) panic(err)
} }
lines << line lines << line
vals := s.split('\n') vals := s.output.split('\n')
for i:=0; i<vals.len; i++ { for i:=0; i<vals.len; i++ {
println(vals[i]) println(vals[i])
} }
@ -1418,18 +1418,18 @@ fn update_v() {
s := os.exec('git -C "$vroot" pull --rebase origin master') or { s := os.exec('git -C "$vroot" pull --rebase origin master') or {
panic(err) panic(err)
} }
println(s) println(s.output)
$if windows { $if windows {
os.mv('$vroot/v.exe', '$vroot/v_old.exe') os.mv('$vroot/v.exe', '$vroot/v_old.exe')
s2 := os.exec('$vroot/make.bat') or { s2 := os.exec('$vroot/make.bat') or {
panic(err) panic(err)
} }
println(s2) println(s2.output)
} $else { } $else {
s2 := os.exec('make -C "$vroot"') or { s2 := os.exec('make -C "$vroot"') or {
panic(err) panic(err)
} }
println(s2) println(s2.output)
} }
} }

View File

@ -145,7 +145,7 @@ fn find_vs() ?VsInstallation {
} }
// println('res: "$res"') // println('res: "$res"')
version := os.read_file('$res\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt') or { version := os.read_file('$res.output\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt') or {
println('Unable to find msvc version') println('Unable to find msvc version')
return error('Unable to find vs installation') return error('Unable to find vs installation')
} }
@ -158,11 +158,11 @@ fn find_vs() ?VsInstallation {
version version
} }
lib_path := '$res\\VC\\Tools\\MSVC\\$v\\lib\\x64' lib_path := '$res.output\\VC\\Tools\\MSVC\\$v\\lib\\x64'
include_path := '$res\\VC\\Tools\\MSVC\\$v\\include' include_path := '$res.output\\VC\\Tools\\MSVC\\$v\\include'
if os.file_exists('$lib_path\\vcruntime.lib') { if os.file_exists('$lib_path\\vcruntime.lib') {
p := '$res\\VC\\Tools\\MSVC\\$v\\bin\\Hostx64\\x64' p := '$res.output\\VC\\Tools\\MSVC\\$v\\bin\\Hostx64\\x64'
// println('$lib_path $include_path') // println('$lib_path $include_path')
@ -406,6 +406,6 @@ fn build_thirdparty_obj_file_with_msvc(flag string) {
res := os.exec('""$msvc.exe_path\\cl.exe" /volatile:ms /Z7 $include_string /c $cfiles /Fo"$obj_path" /D_UNICODE /DUNICODE"') or { res := os.exec('""$msvc.exe_path\\cl.exe" /volatile:ms /Z7 $include_string /c $cfiles /Fo"$obj_path" /D_UNICODE /DUNICODE"') or {
panic(err) panic(err)
} }
println(res) println(res.output)
} }

View File

@ -2385,9 +2385,11 @@ fn (p mut Parser) factor() string {
typ = p.if_st(true, 0) typ = p.if_st(true, 0)
return typ return typ
default: default:
if p.pref.is_verbose || p.pref.is_debug {
next := p.peek() next := p.peek()
println('PREV=${p.prev_tok.str()}') println('prev=${p.prev_tok.str()}')
println('.neXT=${next.str()}') println('next=${next.str()}')
}
p.error('unexpected token: `${p.tok.str()}`') p.error('unexpected token: `${p.tok.str()}`')
} }
p.next()// TODO everything should next() p.next()// TODO everything should next()

View File

@ -307,18 +307,6 @@ pub fn (f File) close() {
} }
// system starts the specified command, waits for it to complete, and returns its code. // system starts the specified command, waits for it to complete, and returns its code.
pub fn system(cmd string) int {
mut ret := int(0)
$if windows {
ret = C._wsystem(cmd.to_wide())
} $else {
ret = C.system(cmd.str)
}
if ret == -1 {
os.print_c_errno()
}
return ret
}
fn popen(path string) *FILE { fn popen(path string) *FILE {
$if windows { $if windows {
@ -341,12 +329,19 @@ fn pclose(f *FILE) int {
} }
} }
struct Result {
pub:
exit_code int
output string
//stderr string // TODO
}
// exec starts the specified command, waits for it to complete, and returns its output. // exec starts the specified command, waits for it to complete, and returns its output.
pub fn exec(_cmd string) ?string { pub fn exec(cmd string) ?Result {
cmd := '$_cmd 2>&1' pcmd := '$cmd 2>&1'
f := popen(cmd) f := popen(pcmd)
if isnil(f) { if isnil(f) {
return error('popen $cmd failed') return error('exec("$cmd") failed')
} }
buf := [1000]byte buf := [1000]byte
mut res := '' mut res := ''
@ -354,11 +349,27 @@ pub fn exec(_cmd string) ?string {
res += tos(buf, strlen(buf)) res += tos(buf, strlen(buf))
} }
res = res.trim_space() res = res.trim_space()
status_code := pclose(f)/256 exit_code := pclose(f)/256
if status_code != 0 { //if exit_code != 0 {
return error(res) //return error(res)
//}
return Result {
output: res
exit_code: exit_code
} }
return res }
pub fn system(cmd string) int {
mut ret := int(0)
$if windows {
ret = C._wsystem(cmd.to_wide())
} $else {
ret = C.system(cmd.str)
}
if ret == -1 {
os.print_c_errno()
}
return ret
} }
// `getenv` returns the value of the environment variable named by the key. // `getenv` returns the value of the environment variable named by the key.