diff options
| author | ruki <[email protected]> | 2017-07-05 21:49:06 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-07-05 21:49:06 +0800 |
| commit | 5a1c8b7a4938745c40f7ad14de4823b09e979b7c (patch) | |
| tree | d7a0b17eee2efdcadbfe544c916735b779715ce5 | |
| parent | 6e0271d2e38f9b4d48ec271af225f71a0faa4f49 (diff) | |
add detect.features and improve has_flags
| -rw-r--r-- | xmake/modules/lib/detect/features.lua | 95 | ||||
| -rw-r--r-- | xmake/modules/lib/detect/has_features.lua | 62 | ||||
| -rw-r--r-- | xmake/modules/lib/detect/has_flags.lua | 18 |
3 files changed, 167 insertions, 8 deletions
diff --git a/xmake/modules/lib/detect/features.lua b/xmake/modules/lib/detect/features.lua new file mode 100644 index 000000000..e0c11b35f --- /dev/null +++ b/xmake/modules/lib/detect/features.lua @@ -0,0 +1,95 @@ +--!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 features.lua +-- + +-- imports +import("lib.detect.find_tool") + +-- get all features of the current tool +-- +-- @param name the tool name +-- @param opt the argument options, .e.g {program = "", flags = {}, toolkind = "[cc|cxx|ld|ar|sh|gc|rc|dc|mm|mxx]"} +-- +-- @return the features dictionary +-- +-- @code +-- local features = features("clang") +-- local features = features("clang", {flags = "-O0", program = "xcrun -sdk macosx clang"}) +-- local features = features("clang", {flags = {"-g", "-O0"}, toolkind = "cxx"}) +-- @endcode +-- +function main(name, opt) + + -- init options + opt = opt or {} + + -- find tool program and version first + opt.version = true + local tool = find_tool(name, opt) + if not tool then + return {} + end + + -- init tool + opt.toolname = tool.name + opt.program = tool.program + opt.programver = tool.version + + -- init cache and key + local key = tool.program .. "_" .. (tool.version or "") .. "_" .. (opt.toolkind or "") .. table.concat(table.wrap(opt.flags), ",") + _g._RESULTS = _g._RESULTS or {} + local results = _g._RESULTS + + -- @note avoid detect the same program in the same time if running in the coroutine (.e.g ccache) + local coroutine_running = coroutine.running() + if coroutine_running then + while _g._checking ~= nil and _g._checking == key do + coroutine.yield() + end + end + + -- get result from the cache first + local result = results[key] + if result ~= nil then + return result + end + + -- detect.tools.xxx.has_flag(flag, opt)? + _g._checking = ifelse(coroutine_running, key, nil) + if os.isfile(path.join(os.programdir(), "modules", "detect", "tools", tool.name, "features.lua")) then + local features = import("detect.tools." .. tool.name .. ".features") + if features then + result = features(opt) + end + end + _g._checking = nil + + -- no features? + result = result or {} + + -- save result to cache + results[key] = result + + -- ok? + return result +end diff --git a/xmake/modules/lib/detect/has_features.lua b/xmake/modules/lib/detect/has_features.lua new file mode 100644 index 000000000..dc2950a71 --- /dev/null +++ b/xmake/modules/lib/detect/has_features.lua @@ -0,0 +1,62 @@ +--!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 has_features.lua +-- + +-- imports +import("core.base.option") +import("lib.detect.features", {alias = "get_features"}) + +-- has the given features for the current tool? +-- +-- @param name the tool name +-- @param features the features +-- @param opt the argument options, .e.g {verbose = false, flags = {}, program = "", toolkind = "[cc|cxx|ld|ar|sh|gc|rc|dc|mm|mxx]"} +-- +-- @return the supported features or nil +-- +-- @code +-- local features = has_features("clang", "cxx_constexpr") +-- local features = has_features("clang", {"cxx_constexpr", "c_static_assert"}, {flags = {"-g", "-O0"}, program = "xcrun -sdk macosx clang"}) +-- local features = has_features("clang", {"cxx_constexpr", "c_static_assert"}, {flags = "-g", toolkind = "cxx"}) +-- @endcode +-- +function main(name, features, opt) + + -- init options + opt = opt or {} + + -- get all features + local all = get_features(name, opt) or {} + + -- get results + local results = nil + for _, feature in ipairs(features) do + if all[feature] then + results = results or {} + table.insert(results, feature) + end + end + + -- ok? + return results +end diff --git a/xmake/modules/lib/detect/has_flags.lua b/xmake/modules/lib/detect/has_flags.lua index f307fc40f..ef6d1b1d1 100644 --- a/xmake/modules/lib/detect/has_flags.lua +++ b/xmake/modules/lib/detect/has_flags.lua @@ -90,12 +90,12 @@ end -- @param flags the flags -- @param opt the argument options, .e.g {verbose = false, program = "", toolkind = "[cc|cxx|ld|ar|sh|gc|rc|dc|mm|mxx]"} -- --- @return true or false +-- @return the supported flags or nil -- -- @code --- local ok = has_flags("clang", "-g") --- local ok = has_flags("clang", {"-g", "-O0"}, {program = "xcrun -sdk macosx clang"}) --- local ok = has_flags("clang", "-g", {toolkind = "cxx"}) +-- local flags = has_flags("clang", "-g") +-- local flags = has_flags("clang", {"-g", "-O0"}, {program = "xcrun -sdk macosx clang"}) +-- local flags = has_flags("clang", "-g", {toolkind = "cxx"}) -- @endcode -- function main(name, flags, opt) @@ -104,12 +104,14 @@ function main(name, flags, opt) opt = opt or {} -- has all flags? + local results = nil for _, flag in ipairs(flags) do - if not _has_flag(name, flag, opt) then - return false + if _has_flag(name, flag, opt) then + results = results or {} + table.insert(results, flag) end end - -- ok - return true + -- ok? + return results end |
