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

os.exec: return ?string instead of string

This commit is contained in:
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
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)
}

View File

@ -137,11 +137,17 @@ fn main() {
println('Building vget...')
os.chdir(vroot + '/tools')
vexec := os.args[0]
os.exec('$vexec vget.v')
_ := os.exec('$vexec vget.v') or {
panic(err)
return // TODO remove return
}
println('Done.')
}
println('Installing module ${mod}...')
os.exec('$vget $mod')
_ := os.exec('$vget $mod') or {
panic(err)
return // TODO remove return
}
return
}
// TODO quit if the compiler is too old
@ -871,7 +877,19 @@ mut args := ''
}
// Run
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
// Print the C command
if v.pref.show_c_cmd || v.pref.is_verbose {
@ -880,18 +898,6 @@ mut args := ''
println('cc took $diff ms')
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
if v.os == .linux && !linux_host && v.pref.build_mode != .build {
v.out_name = v.out_name.replace('.o', '')
@ -905,11 +911,11 @@ mut args := ''
'/usr/lib/x86_64-linux-gnu/crti.o ' +
obj_file +
' /usr/lib/x86_64-linux-gnu/libc.so ' +
'/usr/lib/x86_64-linux-gnu/crtn.o')
println(ress)
if ress.contains('error:') {
exit(1)
'/usr/lib/x86_64-linux-gnu/crtn.o') or {
panic(err)
return // TODO remove return
}
println(ress)
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' {
@ -1315,24 +1321,15 @@ fn run_repl() []string {
if line.starts_with('print') {
source_code := lines.join('\n') + '\n' + line
os.write_file(file, source_code)
s := os.exec('$vexe run $file -repl')
mut vals := s.split('\n')
if s.contains('panic: ') {
if !s.contains('declared and not used') {
for i:=1; i<vals.len; i++ {
println(vals[i])
s := os.exec('$vexe run $file -repl') or {
panic(err)
break // TODO remove break
}
}
else {
println(s)
}
}
else {
vals := s.split('\n')
for i:=0; i < vals.len; i++ {
println(vals[i])
}
}
}
else {
mut temp_line := line
mut temp_flag := false
@ -1342,19 +1339,10 @@ fn run_repl() []string {
}
temp_source_code := lines.join('\n') + '\n' + temp_line
os.write_file(temp_file, temp_source_code)
s := os.exec('$vexe run $temp_file -repl')
if s.contains('panic: ') {
if !s.contains('declared and not used') {
mut vals := s.split('\n')
for i:=0; i < vals.len; i++ {
println(vals[i])
s := os.exec('$vexe run $temp_file -repl') or {
panic(err)
break // TODO remove break
}
}
else {
lines << line
}
}
else {
lines << line
vals := s.split('\n')
for i:=0; i<vals.len-1; i++ {
@ -1362,7 +1350,6 @@ fn run_repl() []string {
}
}
}
}
return lines
}
@ -1418,15 +1405,24 @@ fn env_vflags_and_os_args() []string {
fn update_v() {
println('Updating V...')
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)
$if windows {
os.mv('$vroot/v.exe', '$vroot/v_old.exe')
s = os.exec('$vroot/make.bat')
println(s)
s2 := os.exec('$vroot/make.bat') or {
panic(err)
return // TODO remove return
}
println(s2)
} $else {
s = os.exec('make -C "$vroot"')
println(s)
s2 := os.exec('make -C "$vroot"') or {
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
// If its not there then end user needs to update their visual studio
// 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"')
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')
res := os.exec(cmd)
_ := os.exec(cmd) or {
println(err)
panic('msvc error')
return // TODO remove return
}
// println(res)
// 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' {
os.rm('.$v.out_name_c')
@ -386,7 +389,10 @@ fn build_thirdparty_obj_file_with_msvc(flag string) {
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)
}

View File

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

View File

@ -36,6 +36,9 @@ fn main() {
os.mkdir('.vmodules')
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)
}

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.
pub fn exec(_cmd string) string {
pub fn exec(_cmd string) ?string {
cmd := '$_cmd 2>&1'
f := popen(cmd)
if isnil(f) {
// TODO optional or error code
println('popen $cmd failed')
return ''
return error('popen $cmd failed')
}
buf := [1000]byte
mut res := ''
for C.fgets(buf, 1000, f) != 0 {
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.