From 20b924fdd9c5a03764c180297303a7cefc771940 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 31 Jul 2020 00:24:59 +0800 Subject: improve ldc2 and gdc --- xmake/modules/core/tools/ar.lua | 1 + xmake/modules/core/tools/gdc.lua | 33 +++++++++++++++++++++++++++++---- xmake/modules/core/tools/ldc2.lua | 30 +++++++++++++++++++++--------- xmake/toolchains/dlang/xmake.lua | 2 +- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/xmake/modules/core/tools/ar.lua b/xmake/modules/core/tools/ar.lua index 3e520f887..0a8dec91d 100644 --- a/xmake/modules/core/tools/ar.lua +++ b/xmake/modules/core/tools/ar.lua @@ -25,6 +25,7 @@ import("private.utils.progress") -- init it function init(self) self:set("arflags", "-cr") + self:set("dcarflags", "-cr") -- for dlang/gdc, e.g. x86_64-unknown-linux-gnu-gcc-ar end -- make the strip flag diff --git a/xmake/modules/core/tools/gdc.lua b/xmake/modules/core/tools/gdc.lua index 70da6d389..9f42c69be 100644 --- a/xmake/modules/core/tools/gdc.lua +++ b/xmake/modules/core/tools/gdc.lua @@ -11,15 +11,40 @@ -- 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 +-- @author ruki, BarrOff -- @file gdc.lua -- --- inherit dmd -inherit("dmd") +-- imports +inherit("gcc") + +-- init it +function init(self) + + -- init arflags + self:set("dcarflags", "-cr") + + -- init shflags + self:set("dcshflags", "-shared", "-fPIC") + + -- init dcflags for the kind: shared + self:set("shared.dcflags", "-fPIC") +end +-- make the optimize flag +function nf_optimize(self, level) + local maps = + { + fast = "-O" + , faster = "-O -frelease" + , fastest = "-O -frelease -fbounds-check=off" + , smallest = "-O -frelease -fbounds-check=off" + , aggressive = "-O -frelease -fbounds-check=off" + } + return maps[level] +end diff --git a/xmake/modules/core/tools/ldc2.lua b/xmake/modules/core/tools/ldc2.lua index 9cf9fd48b..d564281c4 100644 --- a/xmake/modules/core/tools/ldc2.lua +++ b/xmake/modules/core/tools/ldc2.lua @@ -11,28 +11,40 @@ -- 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 +-- @author ruki, BarrOff -- @file ldc2.lua -- --- inherit dmd +-- imports inherit("dmd") -- init it function init(self) - - -- init super - _super.init(self) + + -- init arflags + self:set("dcarflags", "-lib") -- init shflags - self:set("dcshflags", "-shared") + self:set("dcshflags", "-shared", "--relocation-model=pic") - -- init cxflags for the kind: shared - self:set("shared.dcflags", "") + -- init dcflags for the kind: shared + self:set("shared.dcflags", "--relocation-model=pic") end +-- make the optimize flag +function nf_optimize(self, level) + local maps = + { + fast = "-O" + , faster = "-O --release" + , fastest = "-O --release --boundscheck=off" + , smallest = "-O --release --boundscheck=off" + , aggressive = "-O --release --boundscheck=off" + } + return maps[level] +end diff --git a/xmake/toolchains/dlang/xmake.lua b/xmake/toolchains/dlang/xmake.lua index e258e9af8..23e8871d0 100644 --- a/xmake/toolchains/dlang/xmake.lua +++ b/xmake/toolchains/dlang/xmake.lua @@ -29,7 +29,7 @@ toolchain("dlang") set_toolset("dc", "$(env DC)", "dmd", "ldc2", "gdc") set_toolset("dcld", "$(env DC)", "dmd", "ldc2", "gdc") set_toolset("dcsh", "$(env DC)", "dmd", "ldc2", "gdc") - set_toolset("dcar", "$(env DC)", "dmd", "ldc2", "gdc") + set_toolset("dcar", "$(env DC)", "dmd", "ldc2", "gcc-ar") -- on load on_load(function (toolchain) -- cgit v1.3.1 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 From 7cc471182dc9e137c2d5ff8d7731157bf227d819 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 31 Jul 2020 00:34:18 +0800 Subject: improve ldc2/gdc toolchain --- xmake/toolchains/dlang/check.lua | 46 ++++++++++++++++++++++++++++++++++++++++ xmake/toolchains/dlang/xmake.lua | 23 ++++++++++++++------ 2 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 xmake/toolchains/dlang/check.lua diff --git a/xmake/toolchains/dlang/check.lua b/xmake/toolchains/dlang/check.lua new file mode 100644 index 000000000..cd40b0d15 --- /dev/null +++ b/xmake/toolchains/dlang/check.lua @@ -0,0 +1,46 @@ +--!A cross-toolchain 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 check.lua +-- + +-- imports +import("core.project.config") +import("detect.sdks.find_cross_toolchain") + +-- check the cross toolchain, for ldc2/gdc toolchain +function main(toolchain) + + -- is cross? + local sdkdir = config.get("sdk") + local bindir = config.get("bin") + local cross = config.get("cross") + if not sdkdir and not bindir and not cross then + return + end + + -- find cross toolchain + local cross_toolchain = find_cross_toolchain(sdkdir, {bindir = bindir, cross = cross}) + if cross_toolchain then + config.set("cross", cross_toolchain.cross, {readonly = true, force = true}) + config.set("bin", cross_toolchain.bindir, {readonly = true, force = true}) + config.set("sdkdir", cross_toolchain.sdkdir, {readonly = true, force = true}) + else + raise("cross toolchain not found!") + end + return cross_toolchain +end diff --git a/xmake/toolchains/dlang/xmake.lua b/xmake/toolchains/dlang/xmake.lua index 23e8871d0..bfe9c20b8 100644 --- a/xmake/toolchains/dlang/xmake.lua +++ b/xmake/toolchains/dlang/xmake.lua @@ -24,15 +24,26 @@ toolchain("dlang") -- set homepage set_homepage("https://dlang.org/") set_description("D Programming Language Compiler") - - -- set toolset - set_toolset("dc", "$(env DC)", "dmd", "ldc2", "gdc") - set_toolset("dcld", "$(env DC)", "dmd", "ldc2", "gdc") - set_toolset("dcsh", "$(env DC)", "dmd", "ldc2", "gdc") - set_toolset("dcar", "$(env DC)", "dmd", "ldc2", "gcc-ar") + + -- check toolchain + on_check("check") -- on load on_load(function (toolchain) + + -- imports + import("core.project.config") + + -- get cross prefix + local cross = config.get("cross") or "" + + -- set toolset + toolchain:set("toolset", "dc", "$(env DC)", "dmd", "ldc2", cross .. "gdc") + toolchain:set("toolset", "dcld", "$(env DC)", "dmd", "ldc2", cross .. "gdc") + toolchain:set("toolset", "dcsh", "$(env DC)", "dmd", "ldc2", cross .. "gdc") + toolchain:set("toolset", "dcar", "$(env DC)", "dmd", "ldc2", cross .. "gcc-ar") + + -- init flags local march = toolchain:is_arch("x86_64", "x64") and "-m64" or "-m32" toolchain:add("dcflags", march) toolchain:add("dcshflags", march) -- cgit v1.3.1 From 2a8c070910d30f70e68434723c0dbe46ae233954 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 31 Jul 2020 00:38:15 +0800 Subject: fix find dmd --- xmake/toolchains/dlang/check.lua | 11 ++++++++--- xmake/toolchains/dlang/xmake.lua | 8 ++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/xmake/toolchains/dlang/check.lua b/xmake/toolchains/dlang/check.lua index cd40b0d15..14baa3282 100644 --- a/xmake/toolchains/dlang/check.lua +++ b/xmake/toolchains/dlang/check.lua @@ -20,17 +20,22 @@ -- imports import("core.project.config") +import("lib.detect.find_tool") import("detect.sdks.find_cross_toolchain") --- check the cross toolchain, for ldc2/gdc toolchain function main(toolchain) - -- is cross? + -- we attempt to find dmd, ldc2 in $PATH + if find_tool("dmd") or find_tool("ldc2") then + return true + end + + -- we need find ldc2 and gdc in the given toolchain sdk directory local sdkdir = config.get("sdk") local bindir = config.get("bin") local cross = config.get("cross") if not sdkdir and not bindir and not cross then - return + return end -- find cross toolchain diff --git a/xmake/toolchains/dlang/xmake.lua b/xmake/toolchains/dlang/xmake.lua index bfe9c20b8..cfb35d9c1 100644 --- a/xmake/toolchains/dlang/xmake.lua +++ b/xmake/toolchains/dlang/xmake.lua @@ -38,10 +38,10 @@ toolchain("dlang") local cross = config.get("cross") or "" -- set toolset - toolchain:set("toolset", "dc", "$(env DC)", "dmd", "ldc2", cross .. "gdc") - toolchain:set("toolset", "dcld", "$(env DC)", "dmd", "ldc2", cross .. "gdc") - toolchain:set("toolset", "dcsh", "$(env DC)", "dmd", "ldc2", cross .. "gdc") - toolchain:set("toolset", "dcar", "$(env DC)", "dmd", "ldc2", cross .. "gcc-ar") + toolchain:add("toolset", "dc", "$(env DC)", "dmd", "ldc2", cross .. "gdc") + toolchain:add("toolset", "dcld", "$(env DC)", "dmd", "ldc2", cross .. "gdc") + toolchain:add("toolset", "dcsh", "$(env DC)", "dmd", "ldc2", cross .. "gdc") + toolchain:add("toolset", "dcar", "$(env DC)", "dmd", "ldc2", cross .. "gcc-ar") -- init flags local march = toolchain:is_arch("x86_64", "x64") and "-m64" or "-m32" -- cgit v1.3.1 From 7374d5a512a57a55c90e031a6b56b71359936d20 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 31 Jul 2020 23:08:07 +0800 Subject: improve dlang arch --- xmake/toolchains/dlang/xmake.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/xmake/toolchains/dlang/xmake.lua b/xmake/toolchains/dlang/xmake.lua index cfb35d9c1..ded15b65c 100644 --- a/xmake/toolchains/dlang/xmake.lua +++ b/xmake/toolchains/dlang/xmake.lua @@ -44,8 +44,13 @@ toolchain("dlang") toolchain:add("toolset", "dcar", "$(env DC)", "dmd", "ldc2", cross .. "gcc-ar") -- init flags - local march = toolchain:is_arch("x86_64", "x64") and "-m64" or "-m32" - toolchain:add("dcflags", march) - toolchain:add("dcshflags", march) - toolchain:add("dcldflags", march) + local march + if toolchain:is_arch("x86_64", "x64") then + march = "-m64" + elseif toolchain:is_arch("i386", "x86") then + march = "-m32" + end + toolchain:add("dcflags", march or "") + toolchain:add("dcshflags", march or "") + toolchain:add("dcldflags", march or "") end) -- cgit v1.3.1 From c05b341f43ab99a6670773d524197411be81b5ea Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 31 Jul 2020 23:54:22 +0800 Subject: improve to check gdc --- xmake/toolchains/dlang/check.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xmake/toolchains/dlang/check.lua b/xmake/toolchains/dlang/check.lua index 14baa3282..88560b7ef 100644 --- a/xmake/toolchains/dlang/check.lua +++ b/xmake/toolchains/dlang/check.lua @@ -25,8 +25,8 @@ import("detect.sdks.find_cross_toolchain") function main(toolchain) - -- we attempt to find dmd, ldc2 in $PATH - if find_tool("dmd") or find_tool("ldc2") then + -- we attempt to find dmd, ldc2, gdc in $PATH + if find_tool("dmd") or find_tool("ldc2") or find_tool("gdc") then return true end -- cgit v1.3.1