diff options
| author | ruki <[email protected]> | 2017-06-14 23:04:41 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-06-14 23:04:41 +0800 |
| commit | d8f66856927cb9575be311a2102c8a6ad1deea51 (patch) | |
| tree | 33b0421fe6ecb67229ab50e0655d539655cdef1c | |
| parent | ac365d846275293099f0f4dc76c1988995be2b26 (diff) | |
improve find_tool and os.execv
| -rw-r--r-- | xmake/core/base/os.lua | 49 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_package.lua | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/os.lua | 50 | ||||
| -rw-r--r-- | xmake/modules/lib/detect/find_tool.lua | 19 | ||||
| -rw-r--r-- | xmake/modules/privilege/sudo.lua | 52 |
5 files changed, 96 insertions, 76 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 84af2dbed..93f910c48 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -442,7 +442,7 @@ function os.tmpfile() return path.join(os.tmpdir(), "_" .. (os.uuid():gsub("-", ""))) end --- run shell +-- run command function os.run(cmd) -- parse arguments @@ -455,23 +455,23 @@ function os.run(cmd) return os.runv(argv[1], table.slice(argv, 2)) end --- run shell with arguments list -function os.runv(shellname, argv) +-- run command with arguments list +function os.runv(program, argv) -- make temporary log file local log = os.tmpfile() -- execute it - local ok = os.execv(shellname, argv, log, log) + local ok = os.execv(program, argv, log, log) if ok ~= 0 then -- make errors local errors = io.readfile(log) if not errors or #errors == 0 then if argv ~= nil then - errors = string.format("runv(%s %s) failed(%d)!", shellname, table.concat(argv, ' '), ok) + errors = string.format("runv(%s %s) failed(%d)!", program, table.concat(argv, ' '), ok) else - errors = string.format("runv(%s) failed(%d)!", shellname, ok) + errors = string.format("runv(%s) failed(%d)!", program, ok) end end @@ -489,7 +489,7 @@ function os.runv(shellname, argv) return true end --- execute shell +-- execute command function os.exec(cmd, outfile, errfile) -- parse arguments @@ -502,12 +502,31 @@ function os.exec(cmd, outfile, errfile) return os.execv(argv[1], table.slice(argv, 2), outfile, errfile) end --- execute shell with arguments list -function os.execv(shellname, argv, outfile, errfile) +-- execute command with arguments list +-- +-- program: "clang", "xcrun -sdk macosx clang", "~/dir/test\ xxx/clang" +-- filename: "clang", "xcrun"", "~/dir/test\ xxx/clang" +-- +function os.execv(program, argv, outfile, errfile) + + -- init arguments + local args = os.argw(argv) + + -- is not executable program file? + local filename = program + if not os.isexec(program) then + + -- parse the filename and arguments, .e.g "xcrun -sdk macosx clang" + local splitinfo = program:split("%s") + filename = splitinfo[1] + if #splitinfo > 1 then + args = table.join(table.slice(splitinfo, 2), args) + end + end -- open command local ok = -1 - local proc = process.openv(shellname, os.argw(argv), outfile, errfile) + local proc = process.openv(filename, args, outfile, errfile) if proc ~= nil then -- wait process @@ -546,7 +565,7 @@ function os.execv(shellname, argv, outfile, errfile) return ok end --- run shell and return output and error data +-- run command and return output and error data function os.iorun(cmd) -- parse arguments @@ -559,15 +578,15 @@ function os.iorun(cmd) return os.iorunv(argv[1], table.slice(argv, 2)) end --- run shell with arguments and return output and error data -function os.iorunv(shellname, argv) +-- run command with arguments and return output and error data +function os.iorunv(program, argv) -- make temporary output and error file local outfile = os.tmpfile() local errfile = os.tmpfile() -- run command - local ok = os.execv(shellname, argv, outfile, errfile) + local ok = os.execv(program, argv, outfile, errfile) -- get output and error data local outdata = io.readfile(outfile) @@ -595,7 +614,7 @@ function os.raise(msg, ...) end end --- is executable program? +-- is executable program file? function os.isexec(filepath) -- check diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua index 0bc4bcd26..a2f1cc24b 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua @@ -57,7 +57,7 @@ function sandbox_lib_detect_find_package._find_from_modules(name, opt) -- "detect.package.find_xxx" exists? if os.isfile(path.join(os.programdir(), "modules", "detect", "package", "find_" .. name .. ".lua")) then - local find_package = import("detect.package.find_" .. name, {anonymous = true}) + local find_package = import("detect.package.find_" .. name) if find_package then return find_package(opt) end diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index 90312ae2a..657364cb2 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -246,7 +246,7 @@ function sandbox_os.scriptdir() return rootdir end --- quietly run shell +-- quietly run command function sandbox_os.run(cmd, ...) -- make command @@ -259,20 +259,20 @@ function sandbox_os.run(cmd, ...) end end --- quietly run shell with arguments list -function sandbox_os.runv(shellname, argv) +-- quietly run command with arguments list +function sandbox_os.runv(program, argv) - -- make shellname - shellname = vformat(shellname) + -- make program + program = vformat(program) -- run it - local ok, errors = os.runv(shellname, argv) + local ok, errors = os.runv(program, argv) if not ok then os.raise(errors) end end --- quietly run shell and echo verbose info if [-v|--verbose] option is enabled +-- quietly run command and echo verbose info if [-v|--verbose] option is enabled function sandbox_os.vrun(cmd, ...) -- echo command @@ -284,19 +284,19 @@ function sandbox_os.vrun(cmd, ...) utils.ifelse(option.get("verbose"), sandbox_os.exec, sandbox_os.run)(cmd, ...) end --- quietly run shell with arguments list and echo verbose info if [-v|--verbose] option is enabled -function sandbox_os.vrunv(shellname, argv) +-- quietly run command with arguments list and echo verbose info if [-v|--verbose] option is enabled +function sandbox_os.vrunv(program, argv) -- echo command if option.get("verbose") then - print(vformat(shellname), table.concat(argv, " ")) + print(vformat(program), table.concat(argv, " ")) end -- run it - utils.ifelse(option.get("verbose"), sandbox_os.execv, sandbox_os.runv)(shellname, argv) + utils.ifelse(option.get("verbose"), sandbox_os.execv, sandbox_os.runv)(program, argv) end --- run shell and return output and error data +-- run command and return output and error data function sandbox_os.iorun(cmd, ...) -- make command @@ -312,14 +312,14 @@ function sandbox_os.iorun(cmd, ...) return outdata, errdata end --- run shell and return output and error data -function sandbox_os.iorunv(shellname, argv) +-- run command and return output and error data +function sandbox_os.iorunv(program, argv) - -- make shellname - shellname = vformat(shellname) + -- make program + program = vformat(program) -- run it - local ok, outdata, errdata = os.iorunv(shellname, argv) + local ok, outdata, errdata = os.iorunv(program, argv) if not ok then os.raise(errdata) end @@ -328,7 +328,7 @@ function sandbox_os.iorunv(shellname, argv) return outdata, errdata end --- execute shell +-- execute command function sandbox_os.exec(cmd, ...) -- make command @@ -341,19 +341,19 @@ function sandbox_os.exec(cmd, ...) end end --- execute shell with arguments list -function sandbox_os.execv(shellname, argv) +-- execute command with arguments list +function sandbox_os.execv(program, argv) - -- make shellname - shellname = vformat(shellname) + -- make program + program = vformat(program) -- run it - local ok = os.execv(shellname, argv) + local ok = os.execv(program, argv) if ok ~= 0 then if argv ~= nil then - os.raise("execv(%s %s) failed(%d)!", shellname, table.concat(argv, ' '), ok) + os.raise("execv(%s %s) failed(%d)!", program, table.concat(argv, ' '), ok) else - os.raise("execv(%s) failed(%d)!", shellname, ok) + os.raise("execv(%s) failed(%d)!", program, ok) end end end diff --git a/xmake/modules/lib/detect/find_tool.lua b/xmake/modules/lib/detect/find_tool.lua index 9d4a54635..f04b06602 100644 --- a/xmake/modules/lib/detect/find_tool.lua +++ b/xmake/modules/lib/detect/find_tool.lua @@ -31,7 +31,7 @@ function _find_from_modules(name, opt) -- "detect.tool.find_xxx" exists? if os.isfile(path.join(os.programdir(), "modules", "detect", "tool", "find_" .. name .. ".lua")) then - local find_tool = import("detect.tool.find_" .. name, {anonymous = true}) + local find_tool = import("detect.tool.find_" .. name) if find_tool then return find_tool(opt) end @@ -41,18 +41,19 @@ end -- find tool -- -- @param name the tool name --- @param opt the options, .e.g {pathes = {"/usr/bin"}, check = function (tool) os.run("%s -h", tool) end, version = true} +-- @param opt the options, .e.g {program = "xcrun -sdk macosx clang", pathes = {"/usr/bin"}, check = function (tool) os.run("%s -h", tool) end, version = true} -- -- @return the tool name or path, version -- -- @code -- --- local tool = find_tool("ccache") --- local tool = find_tool("ccache", {pathes = {"/usr/bin", "/usr/local/bin"}}) --- local tool = find_tool("ccache", {check= "--help"}) -- simple check command: ccache --help --- local tool = find_tool("ccache", {check = function (tool) os.run("%s -h", tool) end}) --- local tool = find_tool("ccache", {pathes = {"$(env PATH)", "$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug;Debugger)"}}) --- local tool = find_tool("ccache", {pathes = {"$(env PATH)", function () return "/usr/bin"end}}) +-- local tool = find_tool("clang") +-- local tool = find_tool("clang", {program = "xcrun -sdk macosx clang"}) +-- local tool = find_tool("clang", {pathes = {"/usr/bin", "/usr/local/bin"}}) +-- local tool = find_tool("clang", {check= "--help"}) -- simple check command: ccache --help +-- local tool = find_tool("clang", {check = function (tool) os.run("%s -h", tool) end}) +-- local tool = find_tool("clang", {pathes = {"$(env PATH)", "$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug;Debugger)"}}) +-- local tool = find_tool("clang", {pathes = {"$(env PATH)", function () return "/usr/bin"end}}) -- local tool, version = find_tool("ccache", {version = true}) -- -- @endcode @@ -69,7 +70,7 @@ function main(name, opt) end -- find tool - tool = find_program(name, opt.pathes, opt.check) + tool = find_program(opt.program or name, opt.pathes, opt.check) -- find tool version local version = nil diff --git a/xmake/modules/privilege/sudo.lua b/xmake/modules/privilege/sudo.lua index 4eb5f6538..5116fa29d 100644 --- a/xmake/modules/privilege/sudo.lua +++ b/xmake/modules/privilege/sudo.lua @@ -26,7 +26,7 @@ import("core.base.option") import("detect.tool.find_sudo") --- sudo run shell with administrator permission +-- sudo run command with administrator permission -- -- .e.g -- _sudo(os.run, "echo", "hello xmake!") @@ -34,8 +34,8 @@ import("detect.tool.find_sudo") function _sudo(runner, cmd, ...) -- find sudo - local program = find_sudo() - assert(program, "sudo not found!") + local sudo = find_sudo() + assert(sudo, "sudo not found!") -- get current path environment local pathenv = os.getenv("PATH") @@ -45,26 +45,26 @@ function _sudo(runner, cmd, ...) pathenv = pathenv:gsub("\"", "\\\"") -- run it with administrator permission and preserve parent environment - runner(program .. " env PATH=\"" .. pathenv .. "\" " .. cmd, ...) + runner(sudo .. " env PATH=\"" .. pathenv .. "\" " .. cmd, ...) else -- run it with administrator permission - runner(program .. " " .. cmd, ...) + runner(sudo .. " " .. cmd, ...) end end --- sudo run shell with administrator permission and arguments list +-- sudo run command with administrator permission and arguments list -- -- .e.g -- _sudov(os.runv, {"echo", "hello xmake!"}) -- -function _sudov(runner, shellname, argv) +function _sudov(runner, program, argv) -- find sudo - local program = find_sudo() - assert(program, "sudo not found!") + local sudo = find_sudo() + assert(sudo, "sudo not found!") -- run it with administrator permission and preserve parent environment - runner(program, table.join("env", "PATH=" .. os.getenv("PATH"), shellname, argv)) + runner(sudo, table.join("env", "PATH=" .. os.getenv("PATH"), program, argv)) end -- sudo run lua script with administrator permission and arguments list @@ -94,44 +94,44 @@ function has() return find_sudo() ~= nil end --- sudo run shell +-- sudo run command function run(cmd, ...) return _sudo(os.run, cmd, ...) end --- sudo run shell with arguments list -function runv(shellname, argv) - return _sudov(os.run, shellname, argv) +-- sudo run command with arguments list +function runv(program, argv) + return _sudov(os.run, program, argv) end --- sudo quietly run shell and echo verbose info if [-v|--verbose] option is enabled +-- sudo quietly run command and echo verbose info if [-v|--verbose] option is enabled function vrun(cmd, ...) return _sudo(os.vrun, cmd, ...) end --- sudo quietly run shell with arguments list and echo verbose info if [-v|--verbose] option is enabled -function vrunv(shellname, argv) - return _sudov(os.vrunv, shellname, argv) +-- sudo quietly run command with arguments list and echo verbose info if [-v|--verbose] option is enabled +function vrunv(program, argv) + return _sudov(os.vrunv, program, argv) end --- sudo run shell and return output and error data +-- sudo run command and return output and error data function iorun(cmd, ...) return _sudo(os.iorun, cmd, ...) end --- sudo run shell and return output and error data -function iorunv(shellname, argv) - return _sudov(os.iorunv, shellname, argv) +-- sudo run command and return output and error data +function iorunv(program, argv) + return _sudov(os.iorunv, program, argv) end --- sudo execute shell +-- sudo execute command function exec(cmd, ...) return _sudo(os.exec, cmd, ...) end --- sudo execute shell with arguments list -function execv(shellname, argv) - return _sudov(os.execv, shellname, argv) +-- sudo execute command with arguments list +function execv(program, argv) + return _sudov(os.execv, program, argv) end -- sudo run lua script |
