summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-02-21 23:03:56 +0800
committerruki <[email protected]>2020-02-21 12:23:38 +0800
commit2a0f498a465f382a3f595b2bdf87f6d2975b7874 (patch)
tree6ced85afd3113971053c2182c3f7c2d740c98e7d
parenteef01795e14276dac1f904217585b9c9208441dd (diff)
improve os.execv errors
-rw-r--r--xmake/core/base/os.lua33
-rw-r--r--xmake/core/sandbox/modules/os.lua48
2 files changed, 54 insertions, 27 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 68273b919..9faa61d59 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -631,17 +631,23 @@ function os.runv(program, argv, opt)
local logfile = os.tmpfile()
-- execute it
- local ok = os.execv(program, argv, table.join(opt, {stdout = logfile, stderr = logfile}))
+ local ok, errors = os.execv(program, argv, table.join(opt, {stdout = logfile, stderr = logfile}))
if ok ~= 0 then
- -- make errors
- local errors = io.readfile(logfile)
- if not errors or #errors == 0 then
- if argv ~= nil then
- errors = string.format("runv(%s %s) failed(%d)!", program, table.concat(argv, ' '), ok)
- else
- errors = string.format("runv(%s) failed(%d)!", program, ok)
+ -- get command
+ local cmd = program
+ if argv then
+ cmd = cmd .. " " .. os.args(argv)
+ end
+
+ -- get subprocess errors
+ if ok ~= nil then
+ errors = io.readfile(logfile)
+ if not errors or #errors == 0 then
+ errors = string.format("runv(%s) failed(%d)", cmd, ok)
end
+ else
+ errors = string.format("cannot runv(%s), error: %s", cmd, errors and errors or "unknown")
end
-- remove the temporary log file
@@ -659,7 +665,7 @@ function os.runv(program, argv, opt)
end
-- execute command
-function os.exec(cmd, outfile, errfile)
+function os.exec(cmd)
-- parse arguments
local argv = os.argv(cmd)
@@ -668,7 +674,7 @@ function os.exec(cmd, outfile, errfile)
end
-- run it
- return os.execv(argv[1], table.slice(argv, 2), {stdout = outfile, stderr = errfile})
+ return os.execv(argv[1], table.slice(argv, 2))
end
-- execute command with arguments list
@@ -734,6 +740,9 @@ function os.execv(program, argv, opt)
-- close process
proc:close()
+ else
+ -- cannot execute process
+ return nil, os.strerror()
end
-- ok?
@@ -764,7 +773,7 @@ function os.iorunv(program, argv, opt)
local errfile = os.tmpfile()
-- run command
- local ok = os.execv(program, argv, table.join(opt, {stdout = outfile, stderr = errfile}))
+ local ok, errors = os.execv(program, argv, table.join(opt, {stdout = outfile, stderr = errfile}))
-- get output and error data
local outdata = io.readfile(outfile)
@@ -775,7 +784,7 @@ function os.iorunv(program, argv, opt)
os.rm(errfile)
-- ok?
- return ok == 0, outdata, errdata
+ return ok == 0, outdata, errdata, errors
end
-- raise an exception and abort the current script
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 48f18bd52..010318473 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -353,11 +353,13 @@ function sandbox_os.iorun(cmd, ...)
cmd = vformat(cmd, ...)
-- run it
- local ok, outdata, errdata = os.iorun(cmd)
+ local ok, outdata, errdata, errors = os.iorun(cmd)
if not ok then
- local errors = errdata or ""
- if #errors:trim() == 0 then
- errors = outdata or ""
+ if not errors then
+ errors = errdata or ""
+ if #errors:trim() == 0 then
+ errors = outdata or ""
+ end
end
os.raise({errors = errors, stderr = errdata, stdout = outdata})
end
@@ -373,11 +375,13 @@ function sandbox_os.iorunv(program, argv, opt)
program = vformat(program)
-- run it
- local ok, outdata, errdata = os.iorunv(program, argv, opt)
+ local ok, outdata, errdata, errors = os.iorunv(program, argv, opt)
if not ok then
- local errors = errdata or ""
- if #errors:trim() == 0 then
- errors = outdata or ""
+ if not errors then
+ errors = errdata or ""
+ if #errors:trim() == 0 then
+ errors = outdata or ""
+ end
end
os.raise({errors = errors, stderr = errdata, stdout = outdata})
end
@@ -393,9 +397,14 @@ function sandbox_os.exec(cmd, ...)
cmd = vformat(cmd, ...)
-- run it
- local ok = os.exec(cmd)
- if ok ~= 0 then
- os.raise("exec(%s) failed(%d)!", cmd, ok)
+ local ok, errors = os.exec(cmd)
+ if ok ~= 0 and errors then
+ if ok ~= nil then
+ errors = string.format("exec(%s) failed(%d)", cmd, ok)
+ else
+ errors = string.format("cannot exec(%s), error: %s", cmd, errors and errors or "unknown")
+ end
+ os.raise(errors)
end
end
@@ -421,13 +430,22 @@ function sandbox_os.execv(program, argv, opt)
-- run it
opt = opt or {}
- local ok = os.execv(program, argv, opt)
+ local ok, errors = os.execv(program, argv, opt)
if ok ~= 0 and not opt.try then
- if argv ~= nil then
- os.raise("execv(%s %s) failed(%d)!", program, table.concat(argv, ' '), ok)
+
+ -- get command
+ local cmd = program
+ if argv then
+ cmd = cmd .. " " .. os.args(argv)
+ end
+
+ -- get errors
+ if ok ~= nil then
+ errors = string.format("execv(%s) failed(%d)", cmd, ok)
else
- os.raise("execv(%s) failed(%d)!", program, ok)
+ errors = string.format("cannot execv(%s), error: %s", cmd, errors and errors or "unknown")
end
+ os.raise(errors)
end
return ok
end