summaryrefslogtreecommitdiff
path: root/xmake/scripts
diff options
context:
space:
mode:
authorruki <[email protected]>2015-06-04 12:01:02 +0800
committerruki <[email protected]>2015-06-04 12:01:02 +0800
commit9f1d3468c22adabab1ad06cdb0014c4f6ef2caa6 (patch)
tree38aa06eba8a633d91646e6cefbd7f942350bb0ce /xmake/scripts
parent5bb9c509ed7d76918eb54d8500d6f981747d7e0e (diff)
find the given tool from name
Diffstat (limited to 'xmake/scripts')
-rw-r--r--xmake/scripts/action/_lua.lua5
-rw-r--r--xmake/scripts/platform/platform.lua35
-rw-r--r--xmake/scripts/tools/tools.lua112
3 files changed, 141 insertions, 11 deletions
diff --git a/xmake/scripts/action/_lua.lua b/xmake/scripts/action/_lua.lua
index 1018b1722..2f7597b03 100644
--- a/xmake/scripts/action/_lua.lua
+++ b/xmake/scripts/action/_lua.lua
@@ -29,6 +29,7 @@ local path = require("base/path")
local utils = require("base/utils")
local config = require("base/config")
local string = require("base/string")
+local tools = require("tools/tools")
-- done the given config
function _lua.done()
@@ -75,9 +76,9 @@ function _lua.done()
file = path.absolute(file)
end
- -- attempt to load script from the xmake tool directory
+ -- attempt to load script from the tools directory
if not os.isfile(file) then
- file = xmake._SCRIPTS_DIR .. "/tools/" .. options.script .. ".lua"
+ file = tools.find(options.script, xmake._SCRIPTS_DIR .. "/tools")
end
-- load and run the script file
diff --git a/xmake/scripts/platform/platform.lua b/xmake/scripts/platform/platform.lua
index 120a03f84..718587c84 100644
--- a/xmake/scripts/platform/platform.lua
+++ b/xmake/scripts/platform/platform.lua
@@ -36,11 +36,11 @@ local compiler = require("compiler/compiler")
function platform._load_prober(root)
-- the platform file path
- local file = string.format("%s/prober.lua", root)
- if os.isfile(file) then
+ local filepath = string.format("%s/prober.lua", root)
+ if os.isfile(filepath) then
-- load script
- local script = loadfile(file)
+ local script = loadfile(filepath)
if script then
-- load prober
@@ -58,11 +58,11 @@ end
function platform._load_from(root, plat)
-- the platform file path
- local file = string.format("%s/platform/%s/%s.lua", root, plat, plat)
- if os.isfile(file) then
+ local filepath = string.format("%s/platform/%s/%s.lua", root, plat, plat)
+ if os.isfile(filepath) then
-- load script
- local script = loadfile(file)
+ local script = loadfile(filepath)
if script then
-- load module
@@ -70,7 +70,7 @@ function platform._load_from(root, plat)
if module then
-- save directory
- module._DIRECTORY = path.directory(file)
+ module._DIRECTORY = path.directory(filepath)
assert(module._DIRECTORY)
-- attempt to load prober
@@ -139,6 +139,23 @@ function platform._configs(plat)
return configs
end
+-- get the current platform module
+function platform.module()
+
+ -- load it
+ return platform._load(config.get("plat"))
+end
+
+-- get the current platform module directory
+function platform.directory()
+
+ -- load it
+ local module = platform.module()
+ if module then
+ return module._DIRECTORY
+ end
+end
+
-- make the current platform configure
function platform.make()
@@ -374,7 +391,7 @@ function platform.probe(configs, is_global)
-- probe config
else
-- probe it
- local module = platform._load(config.get("plat"))
+ local module = platform.module()
if module and module._PROBER and module._PROBER.done then
module._PROBER.done(configs, is_global)
end
@@ -385,7 +402,7 @@ end
function platform.build(mkfile, target)
-- attempt to done the platform special make first
- local module = platform._load(config.get("plat"))
+ local module = platform.module()
if module and module._MAKER and module._MAKER.done then
return module._MAKER.done(mkfile, target)
end
diff --git a/xmake/scripts/tools/tools.lua b/xmake/scripts/tools/tools.lua
new file mode 100644
index 000000000..089f425e9
--- /dev/null
+++ b/xmake/scripts/tools/tools.lua
@@ -0,0 +1,112 @@
+--!The Automatic Cross-platform Build Tool
+--
+-- XMake is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU Lesser General Public License as published by
+-- the Free Software Foundation; either version 2.1 of the License, or
+-- (at your option) any later version.
+--
+-- XMake is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU Lesser General Public License for more details.
+--
+-- You should have received a copy of the GNU Lesser General Public License
+-- along with XMake;
+-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
+--
+-- Copyright (C) 2009 - 2015, ruki All rights reserved.
+--
+-- @author ruki
+-- @file tools.lua
+--
+
+-- define module: tools
+local tools = tools or {}
+
+-- load modules
+local os = require("base/os")
+local path = require("base/path")
+local utils = require("base/utils")
+local string = require("base/string")
+local platform = require("platform/platform")
+
+-- match the tool name
+function tools._match(name, toolname)
+
+ -- match full? ok
+ if name == toolname then return true end
+
+ -- match the full word? ok
+ if name:find("^" .. toolname .. "$") then return true end
+
+ -- contains it? ok
+ if name:find(toolname, true) then return true end
+
+ -- not matched
+ return false
+end
+
+-- find tool from the given root directory and name
+function tools._find_from(root, name)
+
+ -- attempt to get it directly first
+ local filepath = string.format("%s/%s.lua", root, name)
+ if os.isfile(filepath) then
+ return filepath
+ end
+
+ -- make the lower name
+ name = name:lower()
+
+ -- get all tool files
+ local files = os.match(string.format("%s/*.lua", root))
+ for _, file in ipairs(files) do
+
+ -- the tool name
+ local toolname = path.basename(file)
+
+ -- found it?
+ if toolname and toolname ~= "tools" and tools._match(name, toolname:lower()) then
+ return file
+ end
+ end
+
+end
+
+-- find tool from the given name and directory (optional)
+function tools.find(name, root)
+
+ -- check
+ assert(name)
+
+ -- init filename
+ local filepath = nil
+
+ -- only find it from this directory if the given directory exists
+ if root then return tools._find_from(root, name) end
+
+ -- attempt to find it from the current platform directory first
+ if not filepath then filepath = tools._find_from(platform.directory() .. "/tools", name) end
+
+ -- attempt to find it from the script directory
+ if not filepath then filepath = tools._find_from(xmake._SCRIPTS_DIR .. "/tools", name) end
+
+ -- ok?
+ return filepath
+end
+
+-- load tool from the given name and directory (optional)
+function tools.load(name, root)
+
+ -- find the tool file path
+ local toolpath = tools.find(name, root)
+
+ -- not exists?
+ if not toolpath or not os.isfile(toolpath) then
+ return
+ end
+
+end
+
+-- return module: tools
+return tools