From ec97f73a2a0f954ab15647036360f307f4e7f3e4 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 31 Jul 2020 00:27:39 +0800 Subject: improve ldc2 and gdc --- xmake/modules/detect/tools/gcc/has_flags.lua | 2 +- xmake/modules/detect/tools/gdc/has_flags.lua | 102 +++++++++++++++++++++++++- xmake/modules/detect/tools/ldc/has_flags.lua | 23 ------ xmake/modules/detect/tools/ldc2/has_flags.lua | 23 ++++++ 4 files changed, 125 insertions(+), 25 deletions(-) delete mode 100644 xmake/modules/detect/tools/ldc/has_flags.lua create mode 100644 xmake/modules/detect/tools/ldc2/has_flags.lua diff --git a/xmake/modules/detect/tools/gcc/has_flags.lua b/xmake/modules/detect/tools/gcc/has_flags.lua index 04956998b..43d4ea53e 100644 --- a/xmake/modules/detect/tools/gcc/has_flags.lua +++ b/xmake/modules/detect/tools/gcc/has_flags.lua @@ -33,7 +33,7 @@ function _islinker(flags, opt) -- the tool kind is ld or sh? local toolkind = opt.toolkind or "" - return toolkind == "ld" or toolkind == "sh" or toolkind:endswith("-ld") or toolkind:endswith("-sh") + return toolkind == "ld" or toolkind == "sh" or toolkind:endswith("ld") or toolkind:endswith("sh") end -- try running diff --git a/xmake/modules/detect/tools/gdc/has_flags.lua b/xmake/modules/detect/tools/gdc/has_flags.lua index bc4987385..b16c07bc3 100644 --- a/xmake/modules/detect/tools/gdc/has_flags.lua +++ b/xmake/modules/detect/tools/gdc/has_flags.lua @@ -19,5 +19,105 @@ -- -- imports -inherit("detect.tools.dmd.has_flags") +import("lib.detect.cache") +import("core.language.language") + +-- is linker? +function _islinker(flags, opt) + + -- the flags is "-Wl," or "-Xlinker "? + local flags_str = table.concat(flags, " ") + if flags_str:startswith("-Wl,") or flags_str:startswith("-Xlinker ") then + return true + end + + -- the tool kind is ld or sh? + local toolkind = opt.toolkind or "" + return toolkind == "dcld" or toolkind == "dcsh" +end + +-- try running +function _try_running(program, argv, opt) + local errors = nil + return try { function () os.runv(program, argv, opt); return true end, catch { function (errs) errors = (errs or ""):trim() end }}, errors +end + +-- attempt to check it from the argument list +function _check_from_arglist(flags, opt, islinker) + + -- only for compiler + if islinker or #flags > 1 then + return + end + + -- make cache key + local key = "detect.tools.gdc.has_flags" + + -- make flags key + local flagskey = opt.program .. "_" .. (opt.programver or "") + + -- load cache + local cacheinfo = cache.load(key) + + -- get all flags from argument list + local allflags = cacheinfo[flagskey] + if not allflags then + + -- get argument list + allflags = {} + local arglist = os.iorunv(opt.program, {"--help"}, {envs = opt.envs}) + if arglist then + for arg in arglist:gmatch("%s+(%-[%-%a%d]+)%s+") do + allflags[arg] = true + end + end + + -- save cache + cacheinfo[flagskey] = allflags + cache.save(key, cacheinfo) + end + + -- ok? + return allflags[flags[1]] +end + +-- try running to check flags +function _check_try_running(flags, opt, islinker) + + -- make an stub source file + local sourcefile = path.join(os.tmpdir(), "detect", "gdc_has_flags.d") + if not os.isfile(sourcefile) then + io.writefile(sourcefile, "void main()\n{}") + end + + -- check flags for linker + if islinker then + return _try_running(opt.program, table.join(flags, "-o", os.tmpfile(), sourcefile), opt) + end + + -- check flags for compiler + -- @note we cannot use os.nuldev() as the output file, maybe run failed for some flags, e.g. --coverage + return _try_running(opt.program, table.join(flags, "-S", "-o", os.tmpfile(), sourcefile), opt) +end + +-- has_flags(flags)? +-- +-- @param opt the argument options, e.g. {toolname = "", program = "", programver = "", toolkind = "[cc|cxx|ld|ar|sh|gc|mm|mxx]"} +-- +-- @return true or false +-- +function main(flags, opt) + + -- is linker? + local islinker = _islinker(flags, opt) + + -- attempt to check it from the argument list + if _check_from_arglist(flags, opt, islinker) then + return true + end + + -- try running to check it + return _check_try_running(flags, opt, islinker) +end + diff --git a/xmake/modules/detect/tools/ldc/has_flags.lua b/xmake/modules/detect/tools/ldc/has_flags.lua deleted file mode 100644 index bc4987385..000000000 --- a/xmake/modules/detect/tools/ldc/has_flags.lua +++ /dev/null @@ -1,23 +0,0 @@ ---!A cross-platform build utility based on Lua --- --- Licensed 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-2020, TBOOX Open Source Group. --- --- @author ruki --- @file has_flags.lua --- - --- imports -inherit("detect.tools.dmd.has_flags") - diff --git a/xmake/modules/detect/tools/ldc2/has_flags.lua b/xmake/modules/detect/tools/ldc2/has_flags.lua new file mode 100644 index 000000000..bc4987385 --- /dev/null +++ b/xmake/modules/detect/tools/ldc2/has_flags.lua @@ -0,0 +1,23 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed 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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file has_flags.lua +-- + +-- imports +inherit("detect.tools.dmd.has_flags") + -- cgit v1.3.1