summaryrefslogtreecommitdiff
path: root/xmake/modules/lib/detect/find_tool.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2021-01-06 00:52:40 +0800
committerruki <[email protected]>2021-01-06 00:52:40 +0800
commit80e2c546e0e900b23cd3fc945d2f53cb33297253 (patch)
tree19c93bd5fe8ff5ab458977774886e5f4fae41f66 /xmake/modules/lib/detect/find_tool.lua
parent40ca0361e3d0bb08218bc984e1d3eef35f16e5d4 (diff)
fix find_tool for package
Diffstat (limited to 'xmake/modules/lib/detect/find_tool.lua')
-rw-r--r--xmake/modules/lib/detect/find_tool.lua67
1 files changed, 42 insertions, 25 deletions
diff --git a/xmake/modules/lib/detect/find_tool.lua b/xmake/modules/lib/detect/find_tool.lua
index 9d494d68f..02c9c03d1 100644
--- a/xmake/modules/lib/detect/find_tool.lua
+++ b/xmake/modules/lib/detect/find_tool.lua
@@ -22,6 +22,7 @@
import("lib.detect.find_program")
import("lib.detect.find_programver")
import("lib.detect.find_toolname")
+import("core.base.semver")
-- find tool from modules
function _find_from_modules(name, opt)
@@ -37,31 +38,7 @@ function _find_from_modules(name, opt)
end
-- find tool
---
--- @param name the tool name
--- @param opt the options, e.g. {program = "xcrun -sdk macosx clang", paths = {"/usr/bin"},
--- check = function (tool) os.run("%s -h", tool) end, version = true
--- force = true, cachekey = "xxx", envs = {PATH = "xxx"}}
---
--- @return {name = "", program = "", version = ""} or nil
---
--- @code
---
--- local tool = find_tool("clang")
--- local tool = find_tool("clang", {program = "xcrun -sdk macosx clang"})
--- local tool = find_tool("clang", {paths = {"/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", {paths = {"$(env PATH)", "$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug;Debugger)"}})
--- local tool = find_tool("clang", {paths = {"$(env PATH)", function () return "/usr/bin"end}})
--- local tool = find_tool("ccache", {version = true})
---
--- @endcode
---
-function main(name, opt)
-
- -- init options
- opt = opt or {}
+function _find_tool(name, opt)
-- find tool name
local toolname = find_toolname(name or opt.program)
@@ -90,3 +67,43 @@ function main(name, opt)
end
return {name = toolname, program = program, version = version}
end
+
+-- find tool
+--
+-- @param name the tool name
+-- @param opt the options, e.g. {program = "xcrun -sdk macosx clang", paths = {"/usr/bin"},
+-- check = function (tool) os.run("%s -h", tool) end, version = true
+-- force = true, cachekey = "xxx", envs = {PATH = "xxx"}}
+--
+-- @return {name = "", program = "", version = ""} or nil
+--
+-- @code
+--
+-- local tool = find_tool("clang")
+-- local tool = find_tool("clang", {program = "xcrun -sdk macosx clang"})
+-- local tool = find_tool("clang", {paths = {"/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", {paths = {"$(env PATH)", "$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug;Debugger)"}})
+-- local tool = find_tool("clang", {paths = {"$(env PATH)", function () return "/usr/bin"end}})
+-- local tool = find_tool("ccache", {version = true})
+--
+-- @endcode
+--
+function main(name, opt)
+
+ -- do find
+ opt = opt or {}
+ if opt.require_version then
+ opt.version = true
+ end
+ local result = _find_tool(name, opt)
+
+ -- match version?
+ if opt.require_version and opt.require_version:find('.', 1, true) and result then
+ if not (result.version and (result.version == opt.require_version or semver.satisfies(result.version, opt.require_version))) then
+ result = nil
+ end
+ end
+ return result
+end