summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-06-09 23:15:08 +0800
committerruki <[email protected]>2019-06-09 23:15:08 +0800
commit60ddb4c5a6725d11a7821d691dfd53c23df282bc (patch)
tree2cbce0fbb1afc08141761e18699fc23407b3f519
parent7a8d81f043060ace914fc13f3ca914419605ab27 (diff)
improve to the device link for cuda
-rw-r--r--xmake/core/tool/builder.lua2
-rw-r--r--xmake/core/tool/linker.lua8
-rw-r--r--xmake/includes/add_cugencodes.lua44
-rw-r--r--xmake/languages/cuda/api.lua4
-rw-r--r--xmake/languages/cuda/xmake.lua9
-rw-r--r--xmake/modules/core/tools/nvcc.lua3
-rw-r--r--xmake/platforms/linux/load.lua5
-rw-r--r--xmake/platforms/macosx/load.lua6
-rw-r--r--xmake/platforms/windows/load.lua4
-rw-r--r--xmake/rules/cuda/device_link/xmake.lua29
-rw-r--r--xmake/rules/cuda/gencodes/xmake.lua8
11 files changed, 39 insertions, 83 deletions
diff --git a/xmake/core/tool/builder.lua b/xmake/core/tool/builder.lua
index f06f007e7..5a7dcf517 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) return args[name] end})
+ self:_add_flags_from_language(flags, target, {[key] = function (name) print(name, args[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/core/tool/linker.lua b/xmake/core/tool/linker.lua
index 78d6a85f1..a82b12918 100644
--- a/xmake/core/tool/linker.lua
+++ b/xmake/core/tool/linker.lua
@@ -132,7 +132,7 @@ function linker.load(targetkind, sourcekinds, target)
local linkerinfo = linkerinfo_or_errors
-- init cache key
- local cachekey = linkerinfo.linkerkind .. (linkerinfo.program or "") .. (config.get("arch") or os.arch())
+ local cachekey = targetkind .. "_" .. linkerinfo.linkerkind .. (linkerinfo.program or "") .. (config.get("arch") or os.arch())
-- get it directly from cache dirst
builder._INSTANCES = builder._INSTANCES or {}
@@ -242,12 +242,6 @@ function linker:linkflags(opt)
-- add flags from the platform
self:_add_flags_from_platform(flags, targetkind)
- --[[
- -- add flags from the compiler
- if target then
- self:_add_flags_from_compiler(flags, target, targetkind)
- end]]
-
-- add flags from the linker
self:_add_flags_from_linker(flags)
diff --git a/xmake/includes/add_cugencodes.lua b/xmake/includes/add_cugencodes.lua
deleted file mode 100644
index 71e5f8b4e..000000000
--- a/xmake/includes/add_cugencodes.lua
+++ /dev/null
@@ -1,44 +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 - 2019, TBOOX Open Source Group.
---
--- @author OpportunityLiu
--- @file add_cugencodes.lua
---
-
--- add cuda `-gencode` flags to target
---
--- the gpu arch format syntax
--- - compute_xx --> `-gencode arch=compute_xx,code=compute_xx`
--- - sm_xx --> `-gencode arch=compute_xx,code=sm_xx`
--- - sm_xx,sm_yy --> `-gencode arch=compute_xx,code=[sm_xx,sm_yy]`
--- - compute_xx,sm_yy --> `-gencode arch=compute_xx,code=sm_yy`
--- - compute_xx,sm_yy,sm_zz --> `-gencode arch=compute_xx,code=[sm_yy,sm_zz]`
--- - native --> match the fastest cuda device on current host,
--- eg. for a Tesla P100, `-gencode arch=compute_60,code=sm_60` will be added,
--- if no available device is found, no `-gencode` flags will be added
--- @seealso xmake/modules/lib/detect/find_cudadevices
---
--- e.g.
--- includes("add_cugencodes.lua")
--- target("test")
--- add_rules("cuda.console")
--- add_files("src/*.cu")
--- add_cugencodes("native", "compute_50,sm_50", "compute_70")
---
-function add_cugencodes(...)
- add_values("cuda.gencode", ...)
-end
-
diff --git a/xmake/languages/cuda/api.lua b/xmake/languages/cuda/api.lua
index 589eb5579..f825c9b72 100644
--- a/xmake/languages/cuda/api.lua
+++ b/xmake/languages/cuda/api.lua
@@ -27,7 +27,9 @@ function apis()
-- target.add_xxx
"target.add_links"
, "target.add_syslinks"
+ , "target.add_cugencodes"
, "target.add_cuflags"
+ , "target.add_culdflags"
, "target.add_ldflags"
, "target.add_arflags"
, "target.add_shflags"
@@ -37,7 +39,9 @@ function apis()
-- option.add_xxx
, "option.add_links"
, "option.add_syslinks"
+ , "option.add_cugencodes"
, "option.add_cuflags"
+ , "option.add_culdflags"
, "option.add_ldflags"
, "option.add_arflags"
, "option.add_shflags"
diff --git a/xmake/languages/cuda/xmake.lua b/xmake/languages/cuda/xmake.lua
index 46ba8f902..e73823818 100644
--- a/xmake/languages/cuda/xmake.lua
+++ b/xmake/languages/cuda/xmake.lua
@@ -28,10 +28,10 @@ language("cuda")
set_sourceflags {cu = "cuflags"}
-- set target kinds
- set_targetkinds {binary = "cu-ld", static = "cu-ar", shared = "cu-sh"}
+ set_targetkinds {gpucode = "cu-ld", binary = "ld", static = "ar", shared = "sh"}
-- set target flags
- set_targetflags {binary = "ldflags", static = "arflags", shared = "shflags"}
+ set_targetflags {gpucode = "culdflags", binary = "ldflags", static = "arflags", shared = "shflags"}
-- set language kinds
set_langkinds {cu = "cu"}
@@ -113,6 +113,11 @@ language("cuda")
"target.strip"
, "target.symbols"
}
+ , gpucode =
+ {
+ "target.culdflags"
+ , "option.culdflags"
+ }
}
-- set menu
diff --git a/xmake/modules/core/tools/nvcc.lua b/xmake/modules/core/tools/nvcc.lua
index 568c6aef9..592634ac8 100644
--- a/xmake/modules/core/tools/nvcc.lua
+++ b/xmake/modules/core/tools/nvcc.lua
@@ -29,9 +29,6 @@ import("private.tools.nvcc.parse_deps")
-- init it
function init(self)
- -- init shflags
- self:set("cu-shflags", "-shared")
-
-- init flags
if not is_plat("windows") then
self:set("shared.cuflags", "-Xcompiler -fPIC")
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
diff --git a/xmake/platforms/macosx/load.lua b/xmake/platforms/macosx/load.lua
index f47e7a31d..79c179deb 100644
--- a/xmake/platforms/macosx/load.lua
+++ b/xmake/platforms/macosx/load.lua
@@ -91,16 +91,10 @@ function main(platform)
-- init flags for cuda
local cuflags_arch = { i386 = "-m32 -Xcompiler -arch -Xcompiler i386", x86_64 = "-m64 -Xcompiler -arch -Xcompiler x86_64" }
- local ldflags_arch = { i386 = "-m32 -Xlinker -arch -Xlinker i386", x86_64 = "-m64 -Xlinker -arch -Xlinker x86_64" }
platform:add("cuflags", cuflags_arch[arch] or "")
- platform:add("cu-shflags", ldflags_arch[arch] or "")
- platform:add("cu-ldflags", ldflags_arch[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 -Xlinker " .. os.args(path.join(cuda_dir, "lib")))
end
local cu_cxx = config.get("cu-cxx")
if cu_cxx then
diff --git a/xmake/platforms/windows/load.lua b/xmake/platforms/windows/load.lua
index 2de1a138e..12a6a3cfe 100644
--- a/xmake/platforms/windows/load.lua
+++ b/xmake/platforms/windows/load.lua
@@ -39,12 +39,8 @@ function main(platform)
-- init flags for cuda
local cu_archs = { x86 = "-m32", x64 = "-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")))
end
end
diff --git a/xmake/rules/cuda/device_link/xmake.lua b/xmake/rules/cuda/device_link/xmake.lua
index 4cd63646f..43905f044 100644
--- a/xmake/rules/cuda/device_link/xmake.lua
+++ b/xmake/rules/cuda/device_link/xmake.lua
@@ -21,6 +21,18 @@
-- define rule: device-link
rule("cuda.device_link")
+ -- after load
+ after_load(function (target)
+
+ -- get cuda directory
+ 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"))
+ end)
+
-- before link
before_link(function (target, opt)
@@ -29,13 +41,13 @@ rule("cuda.device_link")
import("core.theme.theme")
import("core.project.config")
import("core.project.depend")
- import("core.platform.platform")
+ import("core.tool.linker")
- -- get nvcc
- local nvcc = assert(platform.tool("cu-ld"), "nvcc not found!")
+ -- load linker instance
+ local linkinst = linker.load("gpucode", "cu", {target = target})
-- get link flags
- local linkflags = {"-dlink"}
+ local linkflags = linkinst:linkflags({target = target, configs = {force = {culdflags = "-dlink"}}})
-- get target file
local targetfile = target:objectfile(path.join(".cuda", "devlink", target:basename() .. "_gpucode.cu"))
@@ -76,17 +88,16 @@ rule("cuda.device_link")
cprint("${color.build.target}devlinking.$(mode) %s", path.filename(targetfile))
end
- -- ensure the target directory
- local targetdir = path.directory(targetfile)
- if not os.isdir(targetdir) then
- os.mkdir(targetdir)
+ -- trace verbose info
+ if verbose then
+ print(linkinst:linkcmd(objectfiles, targetfile, {linkflags = linkflags}))
end
-- flush io buffer to update progress info
io.flush()
-- link it
- os.vrunv(nvcc, table.join(linkflags, objectfiles, "-o", targetfile))
+ assert(linkinst:link(objectfiles, targetfile, {linkflags = linkflags}))
-- update files and values to the dependent file
dependinfo.files = depfiles
diff --git a/xmake/rules/cuda/gencodes/xmake.lua b/xmake/rules/cuda/gencodes/xmake.lua
index d7dedb197..1cd92fa5a 100644
--- a/xmake/rules/cuda/gencodes/xmake.lua
+++ b/xmake/rules/cuda/gencodes/xmake.lua
@@ -117,11 +117,15 @@ rule("cuda.gencodes")
end
end
- for _, v in ipairs(target:values("cuda.gencode")) do
+ local cugencodes = table.wrap(target:get("cugencodes"))
+ for _, opt in ipairs(target:orderopts()) do
+ table.join2(gencodes, opt:get("cugencodes"))
+ end
+ for _, v in ipairs(cugencodes) do
local flag = nf_cugencode(v)
if flag then
target:add('cuflags', flag)
- target:add('ldflags', flag)
+ target:add('culdflags', flag)
end
end
end)