diff options
| author | ruki <[email protected]> | 2017-05-17 11:51:34 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-05-17 11:51:34 +0800 |
| commit | cfb1dd7f7b3ece03825c686651c2f88ac65b33d5 (patch) | |
| tree | 5f25b6568f076d9b21120865934c8ef656e86cae | |
| parent | beb2fe1f4981307bf75a1552acd744771ad750d8 (diff) | |
parse program version
| -rwxr-xr-x | scripts/get.sh | 5 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_program.lua | 31 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_programver.lua | 92 | ||||
| -rw-r--r-- | xmake/modules/lib/detect/find_ccache.lua | 30 |
4 files changed, 144 insertions, 14 deletions
diff --git a/scripts/get.sh b/scripts/get.sh index 6279cfdd3..895147b6a 100755 --- a/scripts/get.sh +++ b/scripts/get.sh @@ -87,10 +87,11 @@ fi if [ 'x__install_only__' != "x$2" ] then make -C /tmp/$$xmake_getter --no-print-directory build - if [ $? -ne 0 ] + rv=$? + if [ $rv -ne 0 ] then make -C /tmp/$$xmake_getter/core --no-print-directory error - my_exit 'Build Fail' + my_exit 'Build Fail' $rv fi fi PATHclone=$PATH diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_program.lua b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua index 41c185198..0c811a8de 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_program.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua @@ -41,11 +41,19 @@ function sandbox_lib_detect_find_program._check(program, check) -- no check script? attempt to run it directly if not check then - return 0 == os.exec(shellname, xmake._NULDEV, xmake._NULDEV) + return 0 == os.execv(program, {"--version"}, xmake._NULDEV, xmake._NULDEV) end -- check it - local ok, errors = sandbox.load(check, program) + local ok = false + local errors = nil + if type(check) == "string" then + ok, errors = os.runv(program, {check}) + else + ok, errors = sandbox.load(check, program) + end + + -- check failed? print verbose error info if not ok then utils.verror(errors) end @@ -86,13 +94,22 @@ end -- -- @param name the program name -- @param dirs the program directories --- @param check the check script +-- @param check the check script or command -- -- @return the program name or path -- +-- @code +-- +-- local program = find_program("ccache") +-- local program = find_program("ccache", { "/usr/bin", "/usr/local/bin"}) +-- local program = find_program("ccache", { "/usr/bin", "/usr/local/bin"}, "--help") -- simple check command: ccache --help +-- local program = find_program("ccache", { "/usr/bin", "/usr/local/bin"}, function (program) os.run("%s -h", program) end) +-- +-- @endcode +-- function sandbox_lib_detect_find_program.main(name, dirs, check) - -- TODO get version info, which name + -- TODO which name -- get detect cache local detectcache = cache(utils.ifelse(os.isfile(project.file()), "local.detect", "memory.detect")) @@ -110,6 +127,8 @@ function sandbox_lib_detect_find_program.main(name, dirs, check) -- cache result if result then cacheinfo[name] = result + detectcache:set("find_program", cacheinfo) + detectcache:flush() end -- trace @@ -121,10 +140,6 @@ function sandbox_lib_detect_find_program.main(name, dirs, check) end end - -- save cache info - detectcache:set("find_program", cacheinfo) - detectcache:flush() - -- ok? return result end diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_programver.lua b/xmake/core/sandbox/modules/import/lib/detect/find_programver.lua new file mode 100644 index 000000000..fb00f7365 --- /dev/null +++ b/xmake/core/sandbox/modules/import/lib/detect/find_programver.lua @@ -0,0 +1,92 @@ +--!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_programver.lua +-- + +-- define module +local sandbox_lib_detect_find_programver = sandbox_lib_detect_find_programver or {} + +-- load modules +local os = require("base/os") +local path = require("base/path") +local table = require("base/table") +local utils = require("base/utils") +local option = require("base/option") +local cache = require("project/cache") +local project = require("project/project") +local sandbox = require("sandbox/sandbox") +local raise = require("sandbox/modules/raise") + +-- find program version +-- +-- @param program the program +-- @param parse the version parse script or lua match pattern +-- +-- @return the version string +-- +-- @code +-- local version = find_programver("ccache") +-- local version = find_programver("ccache", "(%d+%.?%d*%.?%d*.-)%s") +-- local version = find_programver("ccache", function (output) return output:match("(%d+%.?%d*%.?%d*.-)%s") end) +-- @endcode +-- +function sandbox_lib_detect_find_programver.main(program, parse) + + -- get detect cache + local detectcache = cache(utils.ifelse(os.isfile(project.file()), "local.detect", "memory.detect")) + + -- attempt to get result from cache first + local cacheinfo = detectcache:get("find_programver") or {} + local result = cacheinfo[program] + if result then + return result + end + + -- attempt to get version output info + local ok, outdata = os.iorunv(program, {"--version"}) + + -- find version info + if ok and outdata and #outdata > 0 then + if type(parse) == "function" then + ok, result = sandbox.load(parse, outdata) + if not ok then + utils.verror(result) + result = nil + end + elseif parse == nil or type(parse) == "string" then + result = outdata:match(parse or "(%d+%.?%d*%.?%d*.-)%s") + end + end + + -- cache result + if result then + cacheinfo[program] = result + detectcache:set("find_program", cacheinfo) + detectcache:flush() + end + + -- ok? + return result +end + +-- return module +return sandbox_lib_detect_find_programver diff --git a/xmake/modules/lib/detect/find_ccache.lua b/xmake/modules/lib/detect/find_ccache.lua index b90856c19..55f0d5a95 100644 --- a/xmake/modules/lib/detect/find_ccache.lua +++ b/xmake/modules/lib/detect/find_ccache.lua @@ -24,10 +24,32 @@ -- imports import("lib.detect.find_program") +import("lib.detect.find_programver") -- find ccache -function main() - return find_program("ccache", { "/usr/bin", - "/usr/local/bin"} - , function (program) os.run("%s -h", program) end) +-- +-- @param argv the arguments, .e.g {version = true} +-- +-- @return program, version +-- +-- @code +-- +-- local ccache = find_ccache() +-- local ccache, version = find_ccache({version = true}) +-- +-- @endcode +-- +function main(argv) + + -- find program + local program = find_program("ccache", { "/usr/bin", "/usr/local/bin"}) + + -- find program version + local version = nil + if argv and argv.version then + version = find_programver(program) + end + + -- ok? + return program, version end |
