diff options
| author | ruki <[email protected]> | 2017-06-14 21:14:29 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-06-14 21:14:29 +0800 |
| commit | ac365d846275293099f0f4dc76c1988995be2b26 (patch) | |
| tree | f6a844376fe2e9e401ae5a6f43d94da3d51e2a4e /xmake/modules/lib/detect/find_tool.lua | |
| parent | 75a12f5e305adfa4fd7d3bb8abb13ec8460a4df8 (diff) | |
add find_tool
Diffstat (limited to 'xmake/modules/lib/detect/find_tool.lua')
| -rw-r--r-- | xmake/modules/lib/detect/find_tool.lua | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/xmake/modules/lib/detect/find_tool.lua b/xmake/modules/lib/detect/find_tool.lua new file mode 100644 index 000000000..9d4a54635 --- /dev/null +++ b/xmake/modules/lib/detect/find_tool.lua @@ -0,0 +1,82 @@ +--!The Make-like Build Utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file find_tool.lua +-- + +-- imports +import("lib.detect.find_program") +import("lib.detect.find_programver") + +-- find tool from modules +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}) + if find_tool then + return find_tool(opt) + end + end +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} +-- +-- @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, version = find_tool("ccache", {version = true}) +-- +-- @endcode +-- +function main(name, opt) + + -- init options + opt = opt or {} + + -- attempt to find tool from modules first + local tool, version = _find_from_modules(name, opt) + if tool then + return tool, version + end + + -- find tool + tool = find_program(name, opt.pathes, opt.check) + + -- find tool version + local version = nil + if tool and opt.version then + version = find_programver(tool) + end + + -- ok? + return tool, version +end |
