diff options
| author | ruki <[email protected]> | 2016-04-13 10:13:37 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-04-13 10:13:37 +0800 |
| commit | 097a0d5eb612d898fb8de01d6db7760aed947b4b (patch) | |
| tree | 9c2ad3ffe67da1663953f9b7097721e96156cb18 /xmake/core/tool/tool.lua | |
| parent | 833bfb207b3f924eaa81feb496cf74e6223d0041 (diff) | |
throw os.run errors
Diffstat (limited to 'xmake/core/tool/tool.lua')
| -rw-r--r-- | xmake/core/tool/tool.lua | 172 |
1 files changed, 154 insertions, 18 deletions
diff --git a/xmake/core/tool/tool.lua b/xmake/core/tool/tool.lua index 4a4a1db26..fcfc28b1e 100644 --- a/xmake/core/tool/tool.lua +++ b/xmake/core/tool/tool.lua @@ -20,19 +20,146 @@ -- @file tool.lua -- --- define module: tool -local tool = tool or {} +-- define module +local tool = tool or {} +local _module = _module or {} -- load modules local os = require("base/os") local path = require("base/path") local utils = require("base/utils") +local table = require("base/table") local string = require("base/string") local filter = require("base/filter") local config = require("project/config") local global = require("project/global") +local sandbox = require("sandbox/sandbox") local platform = require("platform/platform") +-- get the property +function _module:get(name) + + -- the interface + local interface = self._INTERFACE + assert(interface) + + -- check + if not interface.get then + return + end + + -- get it + return interface.get(name) +end + +-- make the define flag +function _module:define(macro) + + -- the interface + local interface = self._INTERFACE + assert(interface) + + -- check + if not interface.define then + return + end + + -- make it + return interface.define(macro) +end + +-- make the undefine flag +function _module:undefine(macro) + + -- the interface + local interface = self._INTERFACE + assert(interface) + + -- check + if not interface.undefine then + return + end + + -- make it + return interface.undefine(macro) +end + +-- make the includedir flag +function _module:includedir(dir) + + -- the interface + local interface = self._INTERFACE + assert(interface) + + -- check + if not interface.includedir then + return + end + + -- make it + return interface.includedir(dir) +end + +-- make the command +function _module:command(srcfile, objfile, flags, logfile) + + -- the interface + local interface = self._INTERFACE + assert(interface and interface.command) + + -- make it + return interface.command(srcfile, objfile, flags, logfile) +end + +-- check the given flags +function _module:check(flags) + + -- the interface + local interface = self._INTERFACE + assert(interface) + + -- no check? + if not interface.check then + return true + end + + -- have been checked? return it directly + interface.checked = interface.checked or {} + if interface.checked[flags] ~= nil then + return interface.checked[flags] + end + + -- check it + local ok, results = sandbox.load(interface.check, flags) + if not ok then + os.raise(results) + end + + -- save the checked result + interface.checked[flags] = results + + -- ok? + return ok +end + +-- run the command +function _module:run(cmd, ...) + + -- the private + local interface = self._INTERFACE + assert(interface) + + -- no run interface? + if not interface.run then + + -- run it + return os.run(cmd, ...) + end + + -- run it + return sandbox.load(interface.run, cmd, ...) +end + -- the directories of tools function tool._directories(name) @@ -146,25 +273,30 @@ function tool._filter() end --- load the given tool from the given name(.e.g cc, ar, ld, sh, ..) -function tool.load(name) +-- load the given tool from the given kind +-- +-- the kinds: +-- +-- .e.g cc, cxx, mm, mxx, as, ar, ld, sh, .. +-- +function tool.load(kind) -- get the shell name - local shellname = platform.tool(name) + local shellname = platform.tool(kind) if not shellname then - return nil, string.format("cannot get tool for %s", name) + return nil, string.format("cannot get tool for %s", kind) end -- get it directly from cache dirst tool._TOOLS = tool._TOOLS or {} - if tool._TOOLS[name] then - return tool._TOOLS[name] + if tool._TOOLS[kind] then + return tool._TOOLS[kind] end -- find the tool script path local toolpath = nil local toolname = path.basename(shellname) - for _, dir in ipairs(tool._directories(name)) do + for _, dir in ipairs(tool._directories(kind)) do -- find this directory toolpath = tool._find(dir, toolname) @@ -196,15 +328,19 @@ function tool.load(name) end -- init the tool module - if module and module.init then + if module.init then module.init(shellname) end + + -- wrap this module + local result = table.inherit(_module) + result._INTERFACE = module -- save tool to the cache - tool._TOOLS[name] = module + tool._TOOLS[kind] = result -- ok? - return module + return result end -- failed @@ -212,26 +348,26 @@ function tool.load(name) end -- check the tool and return the absolute path if exists -function tool.check(name, dirs) +function tool.check(shellname, dirs) -- check - assert(name) + assert(shellname) -- attempt to run it directly first - if os.execute(string.format("%s > %s 2>&1", name, xmake._NULDEV)) ~= 0x7f00 then - return name + if os.execute(string.format("%s > %s 2>&1", shellname, xmake._NULDEV)) ~= 0x7f00 then + return shellname end -- attempt to get it from the given directories for _, dir in ipairs(table.wrap(dirs)) do -- check it - local toolpath = path.translate(string.format("%s/%s", dir, name)) + local toolpath = path.translate(string.format("%s/%s", dir, shellname)) if os.isfile(toolpath) and os.execute(string.format("%s > %s 2>&1", toolpath, xmake._NULDEV)) ~= 0x7f00 then return toolpath end end end --- return module: tool +-- return module return tool |
