diff options
| author | ruki <[email protected]> | 2017-06-23 21:09:13 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-06-23 21:09:13 +0800 |
| commit | b359250e9cdec42b2503ee3f148c52d0e35a32b4 (patch) | |
| tree | a13f589d87a7746ac95d9de802478540b6c3db38 | |
| parent | 0e728f8f990f61d2f31f53f346a64b9b2edd70e1 (diff) | |
add has_snippets
| -rw-r--r-- | xmake/modules/lib/detect/has_flags.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/lib/detect/has_snippets.lua | 111 |
2 files changed, 113 insertions, 2 deletions
diff --git a/xmake/modules/lib/detect/has_flags.lua b/xmake/modules/lib/detect/has_flags.lua index 7da2d98d8..f159e3103 100644 --- a/xmake/modules/lib/detect/has_flags.lua +++ b/xmake/modules/lib/detect/has_flags.lua @@ -62,7 +62,7 @@ function _has_flag(name, flag, opt) end -- trace - if option.get("verbose") then + if option.get("verbose") or opt.verbose then cprint("checking for the flags %s ... %s", flag, ifelse(result, "${green}ok", "${red}no")) end @@ -78,7 +78,7 @@ end -- -- @param name the tool name -- @param flags the flags --- @param opt the argument options, .e.g {program = "", toolkind = "[cc|cxx|ld|ar|sh|gc|rc|dc|mm|mxx]"} +-- @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 -- diff --git a/xmake/modules/lib/detect/has_snippets.lua b/xmake/modules/lib/detect/has_snippets.lua new file mode 100644 index 000000000..6ae58489e --- /dev/null +++ b/xmake/modules/lib/detect/has_snippets.lua @@ -0,0 +1,111 @@ +--!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_snippets.lua +-- + +-- imports +import("core.base.option") +import("core.language.language") +import("core.tool.compiler") + +-- has this snippet? +function _has_snippet(snippet, opt) + + -- init cache and key + local key = opt.name + local results = _g._RESULTS or {} + + -- get result from the cache first + if key ~= nil then + local result = results[key] + if result ~= nil then + return result + end + end + + -- get the source kind + local sourcekind = opt.sourcekind + if not sourcekind and opt.extension then + sourcekind = language.extensions()[opt.extension] or "cc" + end + + -- get the extension + local extension = opt.extension + if not extension and opt.sourcekind then + extension = table.wrap(language.sourcekinds()[opt.sourcekind])[1] or ".c" + end + + -- make the source file + local sourcefile = os.tmpfile() .. extension + local objectfile = os.tmpfile() .. ".o" + io.writefile(sourcefile, snippet) + + -- attempt to compile it + local result = try { function () compiler.compile(sourcefile, objectfile, nil, opt.target, sourcekind); return true end } + + -- remove some files + os.tryrm(sourcefile) + os.tryrm(objectfile) + + -- trace + if opt.name and (option.get("verbose") or opt.verbose) then + cprint("checking for the snippets %s ... %s", opt.name, ifelse(result, "${green}ok", "${red}no")) + end + + -- save result to cache + if key ~= nil then + results[key] = ifelse(result, result, false) + _g._RESULTS = results + end + + -- ok? + return result +end + +-- has the given snippets for the current tool? +-- +-- @param snippets the snippets +-- @param opt the argument options, .e.g {name = "", verbose = false, target = [target|option], sourcekind = "[cc|cxx|mm|mxx|sc|dc|gc|rc]", extension = "[.c|.cpp|.m|.mm|.swift|.d|.go|.rs]"} +-- +-- @return true or false +-- +-- @code +-- local ok = has_snippets("void test() {}") -- default: .c +-- local ok = has_snippets("void test() {}", {extension = ".cpp"}) +-- local ok = has_snippets({"typedef wchar wchar_t;", "void test(){}"}, {sourcekind = "cxx"}) +-- @endcode +-- +function main(snippets, opt) + + -- init options + opt = opt or {} + + -- has all snippets? + for _, snippet in ipairs(snippets) do + if not _has_snippet(snippet, opt) then + return false + end + end + + -- ok + return true +end |
