summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-07-31 17:45:29 +0800
committerGitHub <[email protected]>2020-07-31 17:45:29 +0800
commit02ffe167ede48cc1c7cfc012c6f1b6b8f12a2f07 (patch)
tree2e658c8b128d6bbd45a81bc41049002b996d7eee
parentdeeef0bf58b2cc3a4389c782d7705ddf7c932f56 (diff)
parentc05b341f43ab99a6670773d524197411be81b5ea (diff)
Merge pull request #910 from xmake-io/dlang
Improve ldc2 and gdc support for dlang
-rw-r--r--xmake/modules/core/tools/ar.lua1
-rw-r--r--xmake/modules/core/tools/gdc.lua33
-rw-r--r--xmake/modules/core/tools/ldc2.lua30
-rw-r--r--xmake/modules/detect/tools/gcc/has_flags.lua2
-rw-r--r--xmake/modules/detect/tools/gdc/has_flags.lua102
-rw-r--r--xmake/modules/detect/tools/ldc2/has_flags.lua (renamed from xmake/modules/detect/tools/ldc/has_flags.lua)0
-rw-r--r--xmake/toolchains/dlang/check.lua51
-rw-r--r--xmake/toolchains/dlang/xmake.lua36
8 files changed, 230 insertions, 25 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/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
diff --git a/xmake/toolchains/dlang/check.lua b/xmake/toolchains/dlang/check.lua
new file mode 100644
index 000000000..88560b7ef
--- /dev/null
+++ b/xmake/toolchains/dlang/check.lua
@@ -0,0 +1,51 @@
+--!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("lib.detect.find_tool")
+import("detect.sdks.find_cross_toolchain")
+
+function main(toolchain)
+
+ -- 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
+
+ -- 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
+ 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 e258e9af8..ded15b65c 100644
--- a/xmake/toolchains/dlang/xmake.lua
+++ b/xmake/toolchains/dlang/xmake.lua
@@ -24,17 +24,33 @@ 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", "gdc")
+
+ -- check toolchain
+ on_check("check")
-- on load
on_load(function (toolchain)
- local march = toolchain:is_arch("x86_64", "x64") and "-m64" or "-m32"
- toolchain:add("dcflags", march)
- toolchain:add("dcshflags", march)
- toolchain:add("dcldflags", march)
+
+ -- imports
+ import("core.project.config")
+
+ -- get cross prefix
+ local cross = config.get("cross") or ""
+
+ -- set toolset
+ 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
+ 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)