From 60ddb4c5a6725d11a7821d691dfd53c23df282bc Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 9 Jun 2019 23:15:08 +0800 Subject: improve to the device link for cuda --- xmake/platforms/linux/load.lua | 5 ----- 1 file changed, 5 deletions(-) (limited to 'xmake/platforms/linux/load.lua') diff --git a/xmake/platforms/linux/load.lua b/xmake/platforms/linux/load.lua index b9ff210b1..4b49bec9a 100644 --- a/xmake/platforms/linux/load.lua +++ b/xmake/platforms/linux/load.lua @@ -81,14 +81,9 @@ function main(platform) -- init flags for cuda local cu_archs = { i386 = "-m32 -Xcompiler -m32", x86_64 = "-m64 -Xcompiler -m64" } platform:add("cuflags", cu_archs[arch] or "") - platform:add("cu-shflags", cu_archs[arch] or "") - platform:add("cu-ldflags", cu_archs[arch] or "") local cuda_dir = config.get("cuda") if cuda_dir then platform:add("cuflags", "-I" .. os.args(path.join(cuda_dir, "include"))) - platform:add("cu-ldflags", "-L" .. os.args(path.join(cuda_dir, "lib"))) - platform:add("cu-shflags", "-L" .. os.args(path.join(cuda_dir, "lib"))) - platform:add("cu-ldflags", "-Xlinker -rpath=" .. os.args(path.join(cuda_dir, "lib"))) end local cu_cxx = config.get("cu-cxx") if cu_cxx then -- cgit v1.3.1 From eefa90347085c76301cb84920ed18321362bede4 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 11 Jun 2019 00:20:40 +0800 Subject: fix some device link issues --- xmake/core/tool/builder.lua | 2 +- xmake/languages/cuda/xmake.lua | 14 ++++++++++++-- xmake/platforms/linux/load.lua | 3 ++- xmake/platforms/macosx/load.lua | 5 +++-- xmake/platforms/windows/load.lua | 1 + xmake/rules/cuda/device_link/xmake.lua | 20 ++++++++++++++++---- xmake/rules/cuda/gencodes/xmake.lua | 2 +- 7 files changed, 36 insertions(+), 11 deletions(-) (limited to 'xmake/platforms/linux/load.lua') diff --git a/xmake/core/tool/builder.lua b/xmake/core/tool/builder.lua index 5a7dcf517..f06f007e7 100644 --- a/xmake/core/tool/builder.lua +++ b/xmake/core/tool/builder.lua @@ -361,7 +361,7 @@ function builder:_add_flags_from_argument(flags, target, args) -- add flags (named) from the language if target then local key = target:type() - self:_add_flags_from_language(flags, target, {[key] = function (name) print(name, args[name]); return args[name] end}) + self:_add_flags_from_language(flags, target, {[key] = function (name) return args[name] end}) else self:_add_flags_from_language(flags, nil, {target = function (name) return args[name] end}) end diff --git a/xmake/languages/cuda/xmake.lua b/xmake/languages/cuda/xmake.lua index 1d1f5b9d5..1949708e6 100644 --- a/xmake/languages/cuda/xmake.lua +++ b/xmake/languages/cuda/xmake.lua @@ -118,8 +118,18 @@ language("cuda") } , gpucode = { - "target.culdflags" - , "option.culdflags" + "config.linkdirs" + , "target.linkdirs" + , "option.linkdirs" + , "platform.linkdirs" + , "config.links" + , "target.links" + , "option.links" + , "platform.links" + , "config.syslinks" + , "target.syslinks" + , "option.syslinks" + , "platform.syslinks" } } diff --git a/xmake/platforms/linux/load.lua b/xmake/platforms/linux/load.lua index 4b49bec9a..184c2fa64 100644 --- a/xmake/platforms/linux/load.lua +++ b/xmake/platforms/linux/load.lua @@ -79,8 +79,9 @@ function main(platform) platform:set("rc-ldflags", "") -- init flags for cuda - local cu_archs = { i386 = "-m32 -Xcompiler -m32", x86_64 = "-m64 -Xcompiler -m64" } + local cu_archs = { i386 = "-m32", x86_64 = "-m64" } platform:add("cuflags", cu_archs[arch] or "") + platform:add("cu-ldflags", cu_archs[arch] or "") local cuda_dir = config.get("cuda") if cuda_dir then platform:add("cuflags", "-I" .. os.args(path.join(cuda_dir, "include"))) diff --git a/xmake/platforms/macosx/load.lua b/xmake/platforms/macosx/load.lua index 79c179deb..142d7997f 100644 --- a/xmake/platforms/macosx/load.lua +++ b/xmake/platforms/macosx/load.lua @@ -90,8 +90,9 @@ function main(platform) platform:set("rc-ldflags", "") -- init flags for cuda - local cuflags_arch = { i386 = "-m32 -Xcompiler -arch -Xcompiler i386", x86_64 = "-m64 -Xcompiler -arch -Xcompiler x86_64" } - platform:add("cuflags", cuflags_arch[arch] or "") + local cu_archs = { i386 = "-m32", x86_64 = "-m64" } + platform:add("cuflags", cu_archs[arch] or "") + platform:add("cu-ldflags", cu_archs[arch] or "") local cuda_dir = config.get("cuda") if cuda_dir then platform:add("cuflags", "-I" .. os.args(path.join(cuda_dir, "include"))) diff --git a/xmake/platforms/windows/load.lua b/xmake/platforms/windows/load.lua index 12a6a3cfe..444b3feb6 100644 --- a/xmake/platforms/windows/load.lua +++ b/xmake/platforms/windows/load.lua @@ -39,6 +39,7 @@ function main(platform) -- init flags for cuda local cu_archs = { x86 = "-m32", x64 = "-m64" } platform:add("cuflags", cu_archs[arch] or "") + platform:add("cu-ldflags", cu_archs[arch] or "") local cuda_dir = config.get("cuda") if cuda_dir then platform:add("cuflags", "-I" .. os.args(path.join(cuda_dir, "include"))) diff --git a/xmake/rules/cuda/device_link/xmake.lua b/xmake/rules/cuda/device_link/xmake.lua index 6c1d96983..dae37f32a 100644 --- a/xmake/rules/cuda/device_link/xmake.lua +++ b/xmake/rules/cuda/device_link/xmake.lua @@ -28,9 +28,18 @@ rule("cuda.device_link") local cuda_dir = assert(get_config("cuda"), "Cuda SDK directory not found!") -- add links - target:add("links", "cudart") - target:add("linkdirs", path.join(cuda_dir, "lib")) - target:add("rpathdirs", path.join(cuda_dir, "lib")) + target:add("links", "cudadevrt", "cudart_static") + if is_plat("linux") then + target:add("links", "rt", "pthread", "dl") + end + if is_plat("windows") then + local subdir = is_arch("x64") and "x64" or "Win32" + target:add("linkdirs", path.join(cuda_dir, "lib", subdir)) + target:add("rpathdirs", path.join(cuda_dir, "lib", subdir)) + else + target:add("linkdirs", path.join(cuda_dir, "lib")) + target:add("rpathdirs", path.join(cuda_dir, "lib")) + end end) -- @see https://devblogs.nvidia.com/separate-compilation-linking-cuda-device-code/ @@ -46,8 +55,11 @@ rule("cuda.device_link") -- load linker instance local linkinst = linker.load("gpucode", "cu", {target = target}) + -- init culdflags + local culdflags = {"-dlink"} + -- get link flags - local linkflags = linkinst:linkflags({target = target, configs = {force = {culdflags = "-dlink"}}}) + local linkflags = linkinst:linkflags({target = target, configs = {force = {culdflags = culdflags}}}) -- get target file local targetfile = target:objectfile(path.join(".cuda", "devlink", target:basename() .. "_gpucode.cu")) diff --git a/xmake/rules/cuda/gencodes/xmake.lua b/xmake/rules/cuda/gencodes/xmake.lua index 1cd92fa5a..9fcdbcd3a 100644 --- a/xmake/rules/cuda/gencodes/xmake.lua +++ b/xmake/rules/cuda/gencodes/xmake.lua @@ -119,7 +119,7 @@ rule("cuda.gencodes") local cugencodes = table.wrap(target:get("cugencodes")) for _, opt in ipairs(target:orderopts()) do - table.join2(gencodes, opt:get("cugencodes")) + table.join2(cugencodes, opt:get("cugencodes")) end for _, v in ipairs(cugencodes) do local flag = nf_cugencode(v) -- cgit v1.3.1 From f63112325a658b582b87428a1f78c7e89f3b074e Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 11 Jun 2019 00:29:12 +0800 Subject: rename --cu-cxx to --cu-ccbin --- xmake/languages/cuda/xmake.lua | 2 +- xmake/platforms/linux/config.lua | 10 +++++----- xmake/platforms/linux/load.lua | 7 ++++--- xmake/platforms/macosx/config.lua | 10 +++++----- xmake/platforms/macosx/load.lua | 7 ++++--- 5 files changed, 19 insertions(+), 17 deletions(-) (limited to 'xmake/platforms/linux/load.lua') diff --git a/xmake/languages/cuda/xmake.lua b/xmake/languages/cuda/xmake.lua index 1949708e6..75b4bf5b8 100644 --- a/xmake/languages/cuda/xmake.lua +++ b/xmake/languages/cuda/xmake.lua @@ -139,7 +139,7 @@ language("cuda") { {category = "Cross Complation Configuration/Compiler Configuration" } , {nil, "cu", "kv", nil, "The Cuda Compiler" } - , {nil, "cu-cxx", "kv", nil, "The Cuda Host C++ Compiler" } + , {nil, "cu-ccbin", "kv", nil, "The Cuda Host C++ Compiler" } , {category = "Cross Complation Configuration/Linker Configuration" } , {nil, "cu-ld", "kv", nil, "The Cuda Linker" } diff --git a/xmake/platforms/linux/config.lua b/xmake/platforms/linux/config.lua index 60cc98676..4cbed376d 100644 --- a/xmake/platforms/linux/config.lua +++ b/xmake/platforms/linux/config.lua @@ -78,13 +78,13 @@ function _toolchains() local cu = toolchain("the cuda compiler") local cu_ld = toolchain("the cuda linker") local cu_sh = toolchain("the cuda shared library linker") - local cu_cxx = toolchain("the cuda host c++ compiler") + local cu_ccbin = toolchain("the cuda host c++ compiler") local toolchains = {cc = cc, cxx = cxx, as = as, ld = ld, sh = sh, ar = ar, ex = ex, mm = mm, mxx = mxx, gc = gc, ["gc-ld"] = gc_ld, ["gc-ar"] = gc_ar, dc = dc, ["dc-ld"] = dc_ld, ["dc-sh"] = dc_sh, ["dc-ar"] = dc_ar, rc = rc, ["rc-ld"] = rc_ld, ["rc-sh"] = rc_sh, ["rc-ar"] = rc_ar, - cu = cu, ["cu-ld"] = cu_ld, ["cu-sh"] = cu_sh, ["cu-cxx"] = cu_cxx} + cu = cu, ["cu-ld"] = cu_ld, ["cu-sh"] = cu_sh, ["cu-ccbin"] = cu_ccbin} -- init the c compiler cc:add("$(env CC)", {name = "gcc", cross = cross}, {name = "clang", cross = cross}) @@ -151,7 +151,7 @@ function _toolchains() cu_ld:add("nvcc") cu_sh:add("nvcc") if not cross or cross == "" then - cu_cxx:add("$(env CXX)", "$(env CC)", "gcc", "clang", "g++", "clang++") + cu_ccbin:add("$(env CXX)", "$(env CC)", "gcc", "clang", "g++", "clang++") end return toolchains @@ -174,10 +174,10 @@ function main(platform, name) -- check cuda check_cuda(config) - -- check cu-cxx after checking arch + -- check cu-ccbin after checking arch if config.get("cuda") then local toolchains = singleton.get("linux.toolchains", _toolchains) - check_toolchain(config, "cu-cxx", toolchains["cu-cxx"]) + check_toolchain(config, "cu-ccbin", toolchains["cu-ccbin"]) end end end diff --git a/xmake/platforms/linux/load.lua b/xmake/platforms/linux/load.lua index 184c2fa64..65761d0ac 100644 --- a/xmake/platforms/linux/load.lua +++ b/xmake/platforms/linux/load.lua @@ -86,9 +86,10 @@ function main(platform) if cuda_dir then platform:add("cuflags", "-I" .. os.args(path.join(cuda_dir, "include"))) end - local cu_cxx = config.get("cu-cxx") - if cu_cxx then - platform:add("cuflags", "-ccbin", os.args(cu_cxx)) + local cu_ccbin = config.get("cu-ccbin") + if cu_ccbin then + platform:add("cuflags", "-ccbin", os.args(cu_ccbin)) + platform:add("cu-ldflags", "-ccbin", os.args(cu_ccbin)) end end diff --git a/xmake/platforms/macosx/config.lua b/xmake/platforms/macosx/config.lua index 59ad2d7bb..d2eaa807d 100644 --- a/xmake/platforms/macosx/config.lua +++ b/xmake/platforms/macosx/config.lua @@ -60,13 +60,13 @@ function _toolchains() local cu = toolchain("the cuda compiler") local cu_ld = toolchain("the cuda linker") local cu_sh = toolchain("the cuda shared library linker") - local cu_cxx = toolchain("the cuda host c++ compiler") + local cu_ccbin = toolchain("the cuda host c++ compiler") local toolchains = {cc = cc, cxx = cxx, as = as, ld = ld, sh = sh, ar = ar, ex = ex, mm = mm, mxx = mxx, sc = sc, ["sc-ld"] = sc_ld, ["sc-sh"] = sc_sh, gc = gc, ["gc-ld"] = gc_ld, ["gc-ar"] = gc_ar, dc = dc, ["dc-ld"] = dc_ld, ["dc-sh"] = dc_sh, ["dc-ar"] = dc_ar, rc = rc, ["rc-ld"] = rc_ld, ["rc-sh"] = rc_sh, ["rc-ar"] = rc_ar, - cu = cu, ["cu-ld"] = cu_ld, ["cu-sh"] = cu_sh, ["cu-cxx"] = cu_cxx} + cu = cu, ["cu-ld"] = cu_ld, ["cu-sh"] = cu_sh, ["cu-ccbin"] = cu_ccbin} -- init the c compiler cc:add("$(env CC)", {name = "clang", cross = cross}, "clang", "gcc") @@ -133,7 +133,7 @@ function _toolchains() cu:add("nvcc") cu_ld:add("nvcc") cu_sh:add("nvcc") - cu_cxx:add("$(env CXX)", "$(env CC)", "clang", "gcc") + cu_ccbin:add("$(env CXX)", "$(env CC)", "clang", "gcc") return toolchains end @@ -158,10 +158,10 @@ function main(platform, name) -- check cuda check_cuda(config) - -- check cu-cxx after checking arch + -- check cu-ccbin after checking arch if config.get("cuda") then local toolchains = singleton.get("macosx.toolchains." .. (config.get("arch") or os.arch()), _toolchains) - check_toolchain(config, "cu-cxx", toolchains["cu-cxx"]) + check_toolchain(config, "cu-ccbin", toolchains["cu-ccbin"]) end end end diff --git a/xmake/platforms/macosx/load.lua b/xmake/platforms/macosx/load.lua index 142d7997f..19c2182b9 100644 --- a/xmake/platforms/macosx/load.lua +++ b/xmake/platforms/macosx/load.lua @@ -97,9 +97,10 @@ function main(platform) if cuda_dir then platform:add("cuflags", "-I" .. os.args(path.join(cuda_dir, "include"))) end - local cu_cxx = config.get("cu-cxx") - if cu_cxx then - platform:add("cuflags", "-ccbin", os.args(cu_cxx)) + local cu_ccbin = config.get("cu-ccbin") + if cu_ccbin then + platform:add("cuflags", "-ccbin", os.args(cu_ccbin)) + platform:add("cu-ldflags", "-ccbin", os.args(cu_ccbin)) end end -- cgit v1.3.1 From 21fbe003a96514fe92106a6b8fb88fab7ed02fce Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 11 Jun 2019 00:31:15 +0800 Subject: move platform cuda setting to env rule --- xmake/platforms/linux/load.lua | 14 ---------- xmake/platforms/macosx/load.lua | 14 ---------- xmake/platforms/windows/load.lua | 9 ------- xmake/rules/cuda/device_link/xmake.lua | 8 +++--- xmake/rules/cuda/env/xmake.lua | 49 ++++++++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 41 deletions(-) create mode 100644 xmake/rules/cuda/env/xmake.lua (limited to 'xmake/platforms/linux/load.lua') diff --git a/xmake/platforms/linux/load.lua b/xmake/platforms/linux/load.lua index 65761d0ac..dcdacd8c1 100644 --- a/xmake/platforms/linux/load.lua +++ b/xmake/platforms/linux/load.lua @@ -77,19 +77,5 @@ function main(platform) -- init flags for rust platform:set("rc-shflags", "") platform:set("rc-ldflags", "") - - -- init flags for cuda - local cu_archs = { i386 = "-m32", x86_64 = "-m64" } - platform:add("cuflags", cu_archs[arch] or "") - platform:add("cu-ldflags", cu_archs[arch] or "") - local cuda_dir = config.get("cuda") - if cuda_dir then - platform:add("cuflags", "-I" .. os.args(path.join(cuda_dir, "include"))) - end - local cu_ccbin = config.get("cu-ccbin") - if cu_ccbin then - platform:add("cuflags", "-ccbin", os.args(cu_ccbin)) - platform:add("cu-ldflags", "-ccbin", os.args(cu_ccbin)) - end end diff --git a/xmake/platforms/macosx/load.lua b/xmake/platforms/macosx/load.lua index 19c2182b9..e53dc1b8c 100644 --- a/xmake/platforms/macosx/load.lua +++ b/xmake/platforms/macosx/load.lua @@ -88,19 +88,5 @@ function main(platform) -- init flags for rust platform:set("rc-shflags", "") platform:set("rc-ldflags", "") - - -- init flags for cuda - local cu_archs = { i386 = "-m32", x86_64 = "-m64" } - platform:add("cuflags", cu_archs[arch] or "") - platform:add("cu-ldflags", cu_archs[arch] or "") - local cuda_dir = config.get("cuda") - if cuda_dir then - platform:add("cuflags", "-I" .. os.args(path.join(cuda_dir, "include"))) - end - local cu_ccbin = config.get("cu-ccbin") - if cu_ccbin then - platform:add("cuflags", "-ccbin", os.args(cu_ccbin)) - platform:add("cu-ldflags", "-ccbin", os.args(cu_ccbin)) - end end diff --git a/xmake/platforms/windows/load.lua b/xmake/platforms/windows/load.lua index 444b3feb6..f7789ddb9 100644 --- a/xmake/platforms/windows/load.lua +++ b/xmake/platforms/windows/load.lua @@ -35,13 +35,4 @@ function main(platform) platform:add("dcflags", dc_archs[arch]) platform:add("dc-shflags", dc_archs[arch]) platform:add("dc-ldflags", dc_archs[arch]) - - -- init flags for cuda - local cu_archs = { x86 = "-m32", x64 = "-m64" } - platform:add("cuflags", cu_archs[arch] or "") - platform:add("cu-ldflags", cu_archs[arch] or "") - local cuda_dir = config.get("cuda") - if cuda_dir then - platform:add("cuflags", "-I" .. os.args(path.join(cuda_dir, "include"))) - end end diff --git a/xmake/rules/cuda/device_link/xmake.lua b/xmake/rules/cuda/device_link/xmake.lua index 8489d8c83..90481c34b 100644 --- a/xmake/rules/cuda/device_link/xmake.lua +++ b/xmake/rules/cuda/device_link/xmake.lua @@ -21,14 +21,14 @@ -- define rule: device-link rule("cuda.device_link") + -- add rule: cuda environment + add_deps("cuda.env") + -- after load after_load(function (target) - -- imports - import("detect.sdks.find_cuda") - -- get cuda - local cuda = assert(find_cuda(nil, {verbose = true}), "Cuda SDK directory not found!") + local cuda = assert(target:data("cuda")) -- add links target:add("links", "cudadevrt", "cudart_static") diff --git a/xmake/rules/cuda/env/xmake.lua b/xmake/rules/cuda/env/xmake.lua new file mode 100644 index 000000000..a334bb200 --- /dev/null +++ b/xmake/rules/cuda/env/xmake.lua @@ -0,0 +1,49 @@ +--!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 - 2019, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- define rule: environment +rule("cuda.env") + after_load(function (target) + + -- get cuda sdk + import("detect.sdks.find_cuda") + local cuda = assert(find_cuda(nil, {verbose = true}), "Cuda SDK not found!") + target:data_set("cuda", cuda) + + -- add arch + if is_arch("x86_64", "x64") then + target:add("cuflags", "-m64", {force = true}) + target:add("culdflags", "-m64", {force = true}) + else + target:add("cuflags", "-m32", {force = true}) + target:add("culdflags", "-m32", {force = true}) + end + + -- add -ccbin + local cu_ccbin = get_config("cu-ccbin") + if cu_ccbin then + target:add("cuflags", "-ccbin", os.args(cu_ccbin), {force = true}) + target:add("culdflags", "-ccbin", os.args(cu_ccbin), {force = true}) + end + + -- add includedirs + target:add("includedirs", cuda.includedirs) + end) + -- cgit v1.3.1