diff options
| author | ruki <[email protected]> | 2018-02-28 22:48:23 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-02-28 10:50:19 +0800 |
| commit | d9ab5105223c61f1ffdf31db06726d4fd45f8390 (patch) | |
| tree | 029d3d1259bacee66b7fde9febbb4f24bb07e16e | |
| parent | e69e78bddc912b935990382671a3317bd9830073 (diff) | |
finish link cuda object on macosx
| -rw-r--r-- | xmake/modules/core/tools/nvcc.lua | 234 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_nvcc.lua | 15 | ||||
| -rw-r--r-- | xmake/platforms/checker.lua | 21 | ||||
| -rw-r--r-- | xmake/platforms/macosx/check.lua | 2 |
4 files changed, 266 insertions, 6 deletions
diff --git a/xmake/modules/core/tools/nvcc.lua b/xmake/modules/core/tools/nvcc.lua index f1a76b60d..fb3c87904 100644 --- a/xmake/modules/core/tools/nvcc.lua +++ b/xmake/modules/core/tools/nvcc.lua @@ -22,12 +22,236 @@ -- @file nvcc.lua -- --- inherit gcc -inherit("gcc") +-- imports +import("core.base.option") +import("core.project.config") +import("core.project.project") +import("core.language.language") +import("detect.tools.find_ccache") -- init it function init(self) - - -- init super - _super.init(self) + + -- init flags + _g.cuflags = {} + _g.ldflags = {} + _g.shflags = { "-shared", "-fPIC" } + + -- add link and include directories + local cuda_dir = config.get("cuda_dir") + if cuda_dir then + table.insert(_g.cuflags, "-I" .. path.join(cuda_dir, "include")) + table.insert(_g.ldflags, "-L" .. path.join(cuda_dir, "lib")) + table.insert(_g.ldflags, "-lcudart") + table.insert(_g.ldflags, "-lcublas") + table.insert(_g.ldflags, "-lcurand") + table.insert(_g.shflags, "-L" .. path.join(cuda_dir, "lib")) + table.insert(_g.shflags, "-lcudart") + table.insert(_g.shflags, "-lcublas") + table.insert(_g.shflags, "-lcurand") + end + + -- TODO +-- _g.rpathdirs = {path.join(cuda_dir, "lib")} + + -- init cuflags for the kind: shared + _g.shared = {} + _g.shared.cuflags = {"-fPIC"} + + -- init flags map + _g.mapflags = + { + -- warnings + ["-W1"] = "-Wall" + , ["-W2"] = "-Wall" + , ["-W3"] = "-Wall" + } + + -- init buildmodes + _g.buildmodes = + { + ["object:sources"] = false + } +end + +-- get the property +function get(self, name) + return _g[name] +end + +-- make the symbol flag +function nf_symbol(self, level) + + -- the maps + local maps = + { + debug = "-g" + , hidden = "-fvisibility=hidden" + } + + -- make it + return maps[level] +end + +-- make the warning flag +function nf_warning(self, level) + + -- the maps + local maps = + { + none = "-w" + , less = "-W1" + , more = "-W3" + , all = "-Wall" + , error = "-Werror" + } + + -- make it + return maps[level] +end + +-- make the optimize flag +function nf_optimize(self, level) + + -- the maps + local maps = + { + none = "-O0" + , fast = "-O1" + , faster = "-O2" + , fastest = "-O3" + , smallest = "-Os" + , aggressive = "-Ofast" + } + + -- make it + return maps[level] +end + +-- make the define flag +function nf_define(self, macro) + return "-D" .. macro +end + +-- make the undefine flag +function nf_undefine(self, macro) + return "-U" .. macro +end + +-- make the includedir flag +function nf_includedir(self, dir) + return "-I" .. os.args(dir) +end + +-- make the c precompiled header flag +function nf_pcheader(self, pcheaderfile, target) + return "-include " .. os.args(pcheaderfile) +end + +-- make the c++ precompiled header flag +function nf_pcxxheader(self, pcheaderfile, target) + return "-include " .. os.args(pcheaderfile) +end + +-- make the complie arguments list +function _compargv1(self, sourcefile, objectfile, flags) + + -- get ccache + local ccache = nil + if config.get("ccache") then + ccache = find_ccache() + end + + -- make argv + local argv = table.join("-c", flags, "-o", objectfile, sourcefile) + + -- uses cache? + local program = self:program() + if ccache then + + -- parse the filename and arguments, .e.g "xcrun -sdk macosx clang" + if not os.isexec(program) then + argv = table.join(program:split("%s"), argv) + else + table.insert(argv, 1, program) + end + return ccache, argv + end + + -- no cache + return program, argv end + +-- complie the source file +function _compile1(self, sourcefile, objectfile, depinfo, flags) + + -- ensure the object directory + os.mkdir(path.directory(objectfile)) + + -- compile it + try + { + function () + local outdata, errdata = os.iorunv(_compargv1(self, sourcefile, objectfile, flags)) + return (outdata or "") .. (errdata or "") + end, + catch + { + function (errors) + + -- try removing the old object file for forcing to rebuild this source file + os.tryrm(objectfile) + + -- find the start line of error + local lines = errors:split("\n") + local start = 0 + for index, line in ipairs(lines) do + if line:find("error:", 1, true) or line:find("错误:", 1, true) then + start = index + break + end + end + + -- get 16 lines of errors + if start > 0 or not option.get("verbose") then + if start == 0 then start = 1 end + errors = table.concat(table.slice(lines, start, start + ifelse(#lines - start > 16, 16, #lines - start)), "\n") + end + + -- raise compiling errors + raise(errors) + end + }, + finally + { + function (ok, warnings) + + -- print some warnings + if warnings and #warnings > 0 and (option.get("verbose") or option.get("warning")) then + cprint("${yellow}%s", table.concat(table.slice(warnings:split('\n'), 1, 8), '\n')) + end + end + } + } +end + +-- make the complie arguments list +function compargv(self, sourcefiles, objectfile, flags) + + -- only support single source file now + assert(type(sourcefiles) ~= "table", "'object:sources' not support!") + + -- for only single source file + return _compargv1(self, sourcefiles, objectfile, flags) +end + +-- complie the source file +function compile(self, sourcefiles, objectfile, depinfo, flags) + + -- only support single source file now + assert(type(sourcefiles) ~= "table", "'object:sources' not support!") + + -- for only single source file + _compile1(self, sourcefiles, objectfile, depinfo, flags) +end + diff --git a/xmake/modules/detect/tools/find_nvcc.lua b/xmake/modules/detect/tools/find_nvcc.lua index 92b37f6e8..7892726a1 100644 --- a/xmake/modules/detect/tools/find_nvcc.lua +++ b/xmake/modules/detect/tools/find_nvcc.lua @@ -23,8 +23,10 @@ -- -- imports +import("core.project.config") import("lib.detect.find_program") import("lib.detect.find_programver") +import("detect.sdks.find_cuda_toolchains") -- find nvcc -- @@ -43,10 +45,21 @@ function main(opt) -- init options opt = opt or {} - + -- find program local program = find_program(opt.program or "nvcc", opt) + -- not found? attempt to find program from cuda toolchains + if not program then + local cudadir = config.get("cuda_dir") + if cudadir then + local toolchains = find_cuda_toolchains(cudadir) + if toolchains then + program = find_program(path.join(toolchains.bindir, "nvcc"), opt) + end + end + end + -- find program version local version = nil if program and opt and opt.version then diff --git a/xmake/platforms/checker.lua b/xmake/platforms/checker.lua index e23228943..517c8baff 100644 --- a/xmake/platforms/checker.lua +++ b/xmake/platforms/checker.lua @@ -26,6 +26,7 @@ import("core.base.option") import("detect.sdks.find_xcode_dir") import("detect.sdks.find_xcode_sdkvers") +import("detect.sdks.find_cuda_toolchains") import("lib.detect.find_tool") -- find the given tool @@ -179,6 +180,26 @@ function check_xcode_sdkver(config, optional) end end +-- check the cuda sdk toolchains +function check_cuda_toolchains(config) + + -- get the cuda directory + local cuda_dir = config.get("cuda_dir") + if not cuda_dir then + + -- check ok? update it + local toolchains = find_cuda_toolchains() + if toolchains then + + -- save it + config.set("cuda_dir", toolchains.cudadir) + + -- trace + cprint("checking for the Cuda SDK directory ... ${green}%s", toolchains.cudadir) + end + end +end + -- insert toolchain function toolchain_insert(toolchains, toolkind, cross, name, description, check) diff --git a/xmake/platforms/macosx/check.lua b/xmake/platforms/macosx/check.lua index 485284703..6f96a32d8 100644 --- a/xmake/platforms/macosx/check.lua +++ b/xmake/platforms/macosx/check.lua @@ -133,12 +133,14 @@ function main(kind, toolkind) checker.check_arch , { checker.check_xcode_dir, true } , { checker.check_xcode_sdkver, true } + , checker.check_cuda_toolchains } -- init the check list of global _g.global = { checker.check_xcode_dir + , checker.check_cuda_toolchains } -- check it |
