diff options
| author | ruki <[email protected]> | 2017-06-25 22:34:02 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-06-25 22:34:02 +0800 |
| commit | f57db3954d227c29d15b68316e9b18838a55c3d4 (patch) | |
| tree | 6dd9e2f36fe0b76c3f4ae3506f28efab6efde87a /xmake/modules | |
| parent | 918093af27a737b7dee986f5b70ca4d43cb1fb2f (diff) | |
rename has_snippets to has_cxsnippets
Diffstat (limited to 'xmake/modules')
| -rw-r--r-- | xmake/modules/lib/detect/has_ctypes.lua | 73 | ||||
| -rw-r--r-- | xmake/modules/lib/detect/has_cxsnippets.lua | 221 | ||||
| -rw-r--r-- | xmake/modules/lib/detect/has_snippets.lua | 112 |
3 files changed, 221 insertions, 185 deletions
diff --git a/xmake/modules/lib/detect/has_ctypes.lua b/xmake/modules/lib/detect/has_ctypes.lua deleted file mode 100644 index dad3c77c4..000000000 --- a/xmake/modules/lib/detect/has_ctypes.lua +++ /dev/null @@ -1,73 +0,0 @@ ---!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_ctypes.lua --- - --- imports -import("lib.detect.has_snippets") - --- has this ctype? -function _has_ctype(ctype, opt) - - -- init name and type - opt.name = ctype - opt.kind = "c type" - opt.sourcekind = "cc" - opt.extension = ".c" - - -- make snippet - local snippet = "" - for _, include in ipairs(opt.includes) do - snippet = format("%s\n#include <%s>", snippet, include) - end - snippet = format("%s\n\ntypedef %s __type_xxx;", snippet, ctype) - - -- ok? - return has_snippets(snippet, opt) -end - --- has the given ctypes? --- --- @param ctypes the ctypes --- @param opt the argument options, .e.g {verbose = false, target = [target|option], includes = {"stdio.h", "stdlib.h"}} --- --- @return true or false --- --- @code --- local ok = has_ctypes("wchar_t") --- @endcode --- -function main(ctypes, opt) - - -- init options - opt = opt or {} - - -- has all ctypes? - for _, ctype in ipairs(ctypes) do - if not _has_ctype(ctype, opt) then - return false - end - end - - -- ok - return true -end diff --git a/xmake/modules/lib/detect/has_cxsnippets.lua b/xmake/modules/lib/detect/has_cxsnippets.lua new file mode 100644 index 000000000..44011b842 --- /dev/null +++ b/xmake/modules/lib/detect/has_cxsnippets.lua @@ -0,0 +1,221 @@ +--!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_csnippets.lua +-- + +-- imports +import("core.base.option") +import("core.tool.linker") +import("core.tool.compiler") +import("core.language.language") + +-- get function name +-- +-- sigsetjmp +-- sigsetjmp((void*)0, 0) +-- sigsetjmp{sigsetjmp((void*)0, 0);} +-- sigsetjmp{int a = 0; sigsetjmp((void*)a, a);} +-- +function _funcname(funcinfo) + + -- parse function name + local name = string.match(funcinfo, "(.+){.+}") + if name == nil then + local pos = funcinfo:find("%(") + if pos then + name = funcinfo:sub(1, pos - 1) + else + name = funcinfo + end + end + + -- ok + return name:trim() +end + +-- get function code +-- +-- sigsetjmp +-- sigsetjmp((void*)0, 0) +-- sigsetjmp{sigsetjmp((void*)0, 0);} +-- sigsetjmp{int a = 0; sigsetjmp((void*)a, a);} +-- +function _funccode(funcinfo) + + -- parse function code + local code = string.match(funcinfo, ".+{(.+)}") + if code == nil then + local pos = funcinfo:find("%(") + if pos then + code = funcinfo + else + code = string.format("volatile void* p%s = (void*)&%s;", funcinfo, funcinfo) + end + end + + -- ok + return code +end + +-- make source code +function _sourcecode(snippets, opt) + + -- add includes + local sourcecode = "" + for _, include in ipairs(opt.includes) do + sourcecode = format("%s\n#include <%s>", sourcecode, include) + end + sourcecode = sourcecode .. "\n" + + -- add types + for _, typename in ipairs(opt.types) do + sourcecode = format("%s\ntypedef %s __type_%s;", sourcecode, typename, typename:gsub("[^%a]", "_")) + end + sourcecode = sourcecode .. "\n" + + -- add snippets + for _, snippet in ipairs(snippets) do + sourcecode = sourcecode .. "\n" .. snippet + end + sourcecode = sourcecode .. "\n" + + -- enter main function + sourcecode = sourcecode .. "int main(int argc, char** argv)\n{\n" + + -- add functions + for _, funcinfo in ipairs(opt.functions) do + sourcecode = format("%s\n %s;", _funccode(funcinfo)) + end + + -- leave main function + sourcecode = sourcecode .. "\n return 0;\n}\n" + + -- done + return sourcecode +end + +-- has the given c snippets? +-- +-- @param snippets the snippets +-- @param opt the argument options +-- .e.g +-- { name = "", verbose = false, target = [target|option], sourcekind = "[cc|cxx]" +-- , types = {"wchar_t", "char*"}, includes = "stdio.h", functions = {"sigsetjmp", "sigsetjmp((void*)0, 0)"} +-- , links = {"pthread", "z"}} +-- +-- functions: +-- sigsetjmp +-- sigsetjmp((void*)0, 0) +-- sigsetjmp{sigsetjmp((void*)0, 0);} +-- sigsetjmp{int a = 0; sigsetjmp((void*)a, a);} +-- +-- @return true or false +-- +-- @code +-- local ok = has_csnippets("void test() {}") +-- local ok = has_csnippets({"void test(){}", "#define TEST 1"}, {types = "wchar_t", includes = "stdio.h"}) +-- @endcode +-- +function main(csnippets, opt) + + -- init options + opt = opt or {} + + -- init cache and key + local key = opt.name + local results = _g._RESULTS or {} + + -- get result from the cache first + if key ~= nil then + local ok = results[key] + if ok ~= nil then + return ok + end + end + + -- make source code + local sourcecode = _sourcecode(snippets, opt) + + -- get c/c++ extension + local extension = ".c" + if 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, sourcecode) + + -- attempt to compile it + local ok = try + { + function () + compiler.compile(sourcefile, objectfile, nil, opt.target) +-- linker.link(objectfile, os.nuldev(), opt.target) + return true + end, + catch + { + function (errors) + if option.get("verbose") then + cprint("${red}%s", errors) + end + end + } + } + + -- remove some files + os.tryrm(sourcefile) + os.tryrm(objectfile) + + -- trace + if opt.verbose or option.get("verbose") then + if opt.name then + cprint("checking for the %s ... %s", opt.name, ifelse(ok, "${green}ok", "${red}no")) + else + local kind = ifelse(sourcekind == "cc", "c", "c++") + for _, include in ipairs(opt.includes) do + cprint("checking for the %s include %s ... %s", kind, include, ifelse(ok, "${green}ok", "${red}no")) + end + for _, typename in ipairs(opt.types) do + cprint("checking for the %s type %s ... %s", kind, typename, ifelse(ok, "${green}ok", "${red}no")) + end + for _, funcinfo in ipairs(opt.functions) do + cprint("checking for the %s function %s ... %s", kind, _funcname(funcinfo), ifelse(ok, "${green}ok", "${red}no")) + end + for _, snippet in ipairs(opt.snippets) do + cprint("checking for the %s snippet %s ... %s", kind, snippet:sub(1, 16), ifelse(ok, "${green}ok", "${red}no")) + end + end + end + + -- save result to cache + if key ~= nil then + results[key] = ifelse(ok, ok, false) + _g._RESULTS = results + end + + -- ok? + return ok +end + diff --git a/xmake/modules/lib/detect/has_snippets.lua b/xmake/modules/lib/detect/has_snippets.lua deleted file mode 100644 index 86036c7e7..000000000 --- a/xmake/modules/lib/detect/has_snippets.lua +++ /dev/null @@ -1,112 +0,0 @@ ---!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.tool.compiler") -import("core.language.language") - --- has this snippet? -function _has_snippet(snippet, opt) - - -- 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 - - -- init cache and key - local key = opt.name - local results = _g._RESULTS or {} - - -- get result from the cache first - if key ~= nil then - key = key .. sourcekind - local result = results[key] - if result ~= nil then - return result - end - 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 %s %s ... %s", opt.kind or "snippet", 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? --- --- @param snippets the snippets --- @param opt the argument options, .e.g {kind = "snippet", 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 |
