diff options
| author | ruki <[email protected]> | 2017-05-16 18:05:14 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-05-16 18:05:14 +0800 |
| commit | 6bd14e6b5269856ac468b03e7e820d9e952b42c1 (patch) | |
| tree | f5bf6412e3ecb39ef1247999b795c4140e5ade21 | |
| parent | cd09a4bc9a4a780cd857fff34444a00583de42ab (diff) | |
add find_program and impl find_ccache
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_program.lua | 124 | ||||
| -rw-r--r-- | xmake/modules/lib/detect/find_ccache.lua | 16 |
2 files changed, 129 insertions, 11 deletions
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_program.lua b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua new file mode 100644 index 000000000..2399c717b --- /dev/null +++ b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua @@ -0,0 +1,124 @@ +--!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_program.lua +-- + +-- define module +local sandbox_lib_detect_find_program = sandbox_lib_detect_find_program 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 sandbox = require("sandbox/sandbox") +local raise = require("sandbox/modules/raise") + +-- the cache info +local cacheinfo = cacheinfo or {} + +-- check program +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) + end + + -- check it + local ok, errors = sandbox.load(check, program) + if not ok then + utils.verror(errors) + end + + -- ok? + return ok +end + +-- find program +function sandbox_lib_detect_find_program._find(name, dirs, check) + + -- attempt to check it directly in current environment + if sandbox_lib_detect_find_program._check(name, check) then + return name + end + + -- attempt to check it from the given directories + if not path.is_absolute(name) then + for _, dir in ipairs(table.wrap(dirs)) do + + -- TODO + -- dir is registry path? + + -- the program path + local program_path = path.join(dir, name) + if os.isexec(program_path) then + + -- check it + if sandbox_lib_detect_find_program._check(program_path, check) then + return program_path + end + end + end + end +end + +-- find program +-- +-- @param name the program name +-- @param dirs the program directories +-- @param check the check script +-- +-- @return the program name or path +-- +function sandbox_lib_detect_find_program.main(name, dirs, check) + + -- attempt to get result from cache first + local result = cacheinfo[name] + if result then + return result + end + + -- find executable program + result = sandbox_lib_detect_find_program._find(name, dirs, check) + + -- cache result + if result then + cacheinfo[name] = result + end + + -- trace + if option.get("verbose") then + if result then + utils.cprint("checking for the %s ... ${green}%s", name, result) + else + utils.cprint("checking for the %s ... ${red}no", name) + end + end + + -- ok? + return result +end + +-- return module +return sandbox_lib_detect_find_program diff --git a/xmake/modules/lib/detect/find_ccache.lua b/xmake/modules/lib/detect/find_ccache.lua index 66338da2a..b90856c19 100644 --- a/xmake/modules/lib/detect/find_ccache.lua +++ b/xmake/modules/lib/detect/find_ccache.lua @@ -23,17 +23,11 @@ -- -- imports -import("lib.detect.find_file") +import("lib.detect.find_program") -- find ccache --- --- @param ... detected dirs, files, regs --- --- @return {shellname = ... , fullpath = ...} or nil --- -function main(...) - - -- TODO - print("ccache") - find_file() +function main() + return find_program("ccache", { "/usr/bin", + "/usr/local/bin"} + , function (program) os.run("%s -h", program) end) end |
