os.exec: return ?string instead of string

pull/1515/head
Julian Schurhammer 2019-08-08 17:30:05 +12:00 committed by Alexander Medvednikov
parent 1470b3da11
commit 28147c0930
6 changed files with 91 additions and 70 deletions

View File

@ -261,7 +261,10 @@ fn build_thirdparty_obj_file(flag string) {
} }
} }
cc := if os.user_os() == 'windows' { 'gcc' } else { 'cc' } // TODO clang support on Windows cc := if os.user_os() == 'windows' { 'gcc' } else { 'cc' } // TODO clang support on Windows
res := os.exec('$cc -fPIC -c -o $obj_path $cfiles') res := os.exec('$cc -fPIC -c -o $obj_path $cfiles') or {
panic(err)
return // TODO remove return
}
println(res) println(res)
} }

View File

@ -137,11 +137,17 @@ fn main() {
println('Building vget...') println('Building vget...')
os.chdir(vroot + '/tools') os.chdir(vroot + '/tools')
vexec := os.args[0] vexec := os.args[0]
os.exec('$vexec vget.v') _ := os.exec('$vexec vget.v') or {
panic(err)
return // TODO remove return
}
println('Done.') println('Done.')
} }
println('Installing module ${mod}...') println('Installing module ${mod}...')
os.exec('$vget $mod') _ := os.exec('$vget $mod') or {
panic(err)
return // TODO remove return
}
return return
} }
// TODO quit if the compiler is too old // TODO quit if the compiler is too old
@ -871,7 +877,19 @@ mut args := ''
} }
// Run // Run
ticks := time.ticks() ticks := time.ticks()
res := os.exec(cmd) _ := os.exec(cmd) or {
if v.pref.is_debug {
println(err)
} else {
print(err.limit(200))
if err.len > 200 {
println('...\n(Use `v -debug` to print the entire error message)\n')
}
}
panic('C error. This should never happen. ' +
'Please create a GitHub issue: https://github.com/vlang/v/issues/new/choose')
return // TODO remove return
}
diff := time.ticks() - ticks diff := time.ticks() - ticks
// Print the C command // Print the C command
if v.pref.show_c_cmd || v.pref.is_verbose { if v.pref.show_c_cmd || v.pref.is_verbose {
@ -880,18 +898,6 @@ mut args := ''
println('cc took $diff ms') println('cc took $diff ms')
println('=========\n') println('=========\n')
} }
if res.contains('error: ') {
if v.pref.is_debug {
println(res)
} else {
print(res.limit(200))
if res.len > 200 {
println('...\n(Use `v -debug` to print the entire error message)\n')
}
}
panic('C error. This should never happen. ' +
'Please create a GitHub issue: https://github.com/vlang/v/issues/new/choose')
}
// Link it if we are cross compiling and need an executable // Link it if we are cross compiling and need an executable
if v.os == .linux && !linux_host && v.pref.build_mode != .build { if v.os == .linux && !linux_host && v.pref.build_mode != .build {
v.out_name = v.out_name.replace('.o', '') v.out_name = v.out_name.replace('.o', '')
@ -905,11 +911,11 @@ mut args := ''
'/usr/lib/x86_64-linux-gnu/crti.o ' + '/usr/lib/x86_64-linux-gnu/crti.o ' +
obj_file + obj_file +
' /usr/lib/x86_64-linux-gnu/libc.so ' + ' /usr/lib/x86_64-linux-gnu/libc.so ' +
'/usr/lib/x86_64-linux-gnu/crtn.o') '/usr/lib/x86_64-linux-gnu/crtn.o') or {
println(ress) panic(err)
if ress.contains('error:') { return // TODO remove return
exit(1)
} }
println(ress)
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' {
@ -1315,22 +1321,13 @@ fn run_repl() []string {
if line.starts_with('print') { if line.starts_with('print') {
source_code := lines.join('\n') + '\n' + line source_code := lines.join('\n') + '\n' + line
os.write_file(file, source_code) os.write_file(file, source_code)
s := os.exec('$vexe run $file -repl') s := os.exec('$vexe run $file -repl') or {
mut vals := s.split('\n') panic(err)
if s.contains('panic: ') { break // TODO remove break
if !s.contains('declared and not used') {
for i:=1; i<vals.len; i++ {
println(vals[i])
}
}
else {
println(s)
}
} }
else { vals := s.split('\n')
for i:=0; i < vals.len; i++ { for i:=0; i < vals.len; i++ {
println(vals[i]) println(vals[i])
}
} }
} }
else { else {
@ -1342,24 +1339,14 @@ fn run_repl() []string {
} }
temp_source_code := lines.join('\n') + '\n' + temp_line temp_source_code := lines.join('\n') + '\n' + temp_line
os.write_file(temp_file, temp_source_code) os.write_file(temp_file, temp_source_code)
s := os.exec('$vexe run $temp_file -repl') s := os.exec('$vexe run $temp_file -repl') or {
if s.contains('panic: ') { panic(err)
if !s.contains('declared and not used') { break // TODO remove break
mut vals := s.split('\n')
for i:=0; i < vals.len; i++ {
println(vals[i])
}
}
else {
lines << line
}
} }
else { lines << line
lines << line vals := s.split('\n')
vals := s.split('\n') for i:=0; i<vals.len-1; i++ {
for i:=0; i<vals.len-1; i++ { println(vals[i])
println(vals[i])
}
} }
} }
} }
@ -1418,15 +1405,24 @@ fn env_vflags_and_os_args() []string {
fn update_v() { fn update_v() {
println('Updating V...') println('Updating V...')
vroot := os.dir(os.executable()) vroot := os.dir(os.executable())
mut s := os.exec('git -C "$vroot" pull --rebase origin master') s := os.exec('git -C "$vroot" pull --rebase origin master') or {
panic(err)
return // TODO remove return
}
println(s) println(s)
$if windows { $if windows {
os.mv('$vroot/v.exe', '$vroot/v_old.exe') os.mv('$vroot/v.exe', '$vroot/v_old.exe')
s = os.exec('$vroot/make.bat') s2 := os.exec('$vroot/make.bat') or {
println(s) panic(err)
return // TODO remove return
}
println(s2)
} $else { } $else {
s = os.exec('make -C "$vroot"') s2 := os.exec('make -C "$vroot"') or {
println(s) panic(err)
return // TODO remove return
}
println(s2)
} }
} }

View File

@ -134,7 +134,10 @@ fn find_vs() ?VsInstallation {
// VSWhere is guaranteed to be installed at this location now // VSWhere is guaranteed to be installed at this location now
// If its not there then end user needs to update their visual studio // If its not there then end user needs to update their visual studio
// installation! // installation!
res := os.exec('""%ProgramFiles(x86)%\\Microsoft Visual Studio\\Installer\\vswhere.exe" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath"') res := os.exec('""%ProgramFiles(x86)%\\Microsoft Visual Studio\\Installer\\vswhere.exe" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath"') or {
panic(err)
return error(err)// TODO remove return
}
// println('res: "$res"') // println('res: "$res"')
version := os.read_file('$res\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt') or { version := os.read_file('$res\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt') or {
@ -341,13 +344,13 @@ pub fn (v mut V) cc_msvc() {
// println('$cmd') // println('$cmd')
res := os.exec(cmd) _ := os.exec(cmd) or {
println(err)
panic('msvc error')
return // TODO remove return
}
// println(res) // println(res)
// println('C OUTPUT:') // println('C OUTPUT:')
if res.contains('error') {
println(res)
panic('msvc error')
}
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' {
os.rm('.$v.out_name_c') os.rm('.$v.out_name_c')
@ -386,7 +389,10 @@ fn build_thirdparty_obj_file_with_msvc(flag string) {
println('$cfiles') println('$cfiles')
res := os.exec('""$msvc.exe_path\\cl.exe" /volatile:ms /Z7 $include_string /c $cfiles /Fo"$obj_path" /D_UNICODE /DUNICODE"') res := os.exec('""$msvc.exe_path\\cl.exe" /volatile:ms /Z7 $include_string /c $cfiles /Fo"$obj_path" /D_UNICODE /DUNICODE"') or {
panic(err)
return // TODO remove return
}
println(res) println(res)
} }

View File

@ -31,6 +31,7 @@ for /r . %%x in (*_test.v) do (
goto :done goto :done
:fail :fail
echo fail
exit /b 1 exit /b 1
:done :done

View File

@ -36,6 +36,9 @@ fn main() {
os.mkdir('.vmodules') os.mkdir('.vmodules')
println('Done.') println('Done.')
} }
os.exec('git -C "$home/.vmodules" clone --depth=1 $mod.url ' + mod.name.replace('.', '/')) _ := os.exec('git -C "$home/.vmodules" clone --depth=1 $mod.url ' + mod.name.replace('.', '/')) or {
panic(err)
return // TODO remove return
}
println(s) println(s)
} }

View File

@ -336,21 +336,33 @@ fn popen(path string) *FILE {
} }
} }
fn pclose(f *FILE) int {
$if windows {
return C._pclose(f)
}
$else {
return C.pclose(f)
}
}
// 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) ?string {
cmd := '$_cmd 2>&1' cmd := '$_cmd 2>&1'
f := popen(cmd) f := popen(cmd)
if isnil(f) { if isnil(f) {
// TODO optional or error code return error('popen $cmd failed')
println('popen $cmd failed')
return ''
} }
buf := [1000]byte buf := [1000]byte
mut res := '' mut res := ''
for C.fgets(buf, 1000, f) != 0 { for C.fgets(buf, 1000, f) != 0 {
res += tos(buf, strlen(buf)) res += tos(buf, strlen(buf))
} }
return res.trim_space() res = res.trim_space()
status_code := pclose(f)/256
if status_code != 0 {
return error(res)
}
return res
} }
// `getenv` returns the value of the environment variable named by the key. // `getenv` returns the value of the environment variable named by the key.