diff options
| author | ruki <[email protected]> | 2020-07-31 00:27:39 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-07-31 00:27:39 +0800 |
| commit | ec97f73a2a0f954ab15647036360f307f4e7f3e4 (patch) | |
| tree | a8b969176f8189638b03e6b7761697672e5e7862 | |
| parent | 20b924fdd9c5a03764c180297303a7cefc771940 (diff) | |
improve ldc2 and gdc
| -rw-r--r-- | xmake/modules/detect/tools/gcc/has_flags.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/gdc/has_flags.lua | 102 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/ldc2/has_flags.lua (renamed from xmake/modules/detect/tools/ldc/has_flags.lua) | 0 |
3 files changed, 102 insertions, 2 deletions
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,<arg>" or "-Xlinker <arg>"? + 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/ldc2/has_flags.lua index bc4987385..bc4987385 100644 --- a/xmake/modules/detect/tools/ldc/has_flags.lua +++ b/xmake/modules/detect/tools/ldc2/has_flags.lua |
