diff options
| author | ruki <[email protected]> | 2018-03-02 00:45:23 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-03-01 22:32:20 +0800 |
| commit | d96c1bb9d0bc3c6c7f44fe7fffda6c020a31019b (patch) | |
| tree | f9024b65923743539c90a41c864f29ecbcfa118f | |
| parent | d9ab5105223c61f1ffdf31db06726d4fd45f8390 (diff) | |
add cuda language
| -rw-r--r-- | xmake/languages/c++/xmake.lua | 10 | ||||
| -rw-r--r-- | xmake/languages/cuda/api.lua | 60 | ||||
| -rw-r--r-- | xmake/languages/cuda/check_main.lua | 45 | ||||
| -rw-r--r-- | xmake/languages/cuda/load.lua | 38 | ||||
| -rw-r--r-- | xmake/languages/cuda/xmake.lua | 119 | ||||
| -rw-r--r-- | xmake/modules/core/tools/nvcc.lua | 93 | ||||
| -rw-r--r-- | xmake/platforms/macosx/check.lua | 6 | ||||
| -rw-r--r-- | xmake/platforms/macosx/load.lua | 13 |
8 files changed, 360 insertions, 24 deletions
diff --git a/xmake/languages/c++/xmake.lua b/xmake/languages/c++/xmake.lua index 87a6256bd..34ed07fc8 100644 --- a/xmake/languages/c++/xmake.lua +++ b/xmake/languages/c++/xmake.lua @@ -26,10 +26,10 @@ language("c++") -- set source file kinds - set_sourcekinds {cc = ".c", cxx = {".cc", ".cpp", ".cxx"}, cu = ".cu"} + set_sourcekinds {cc = ".c", cxx = {".cc", ".cpp", ".cxx"}} -- set source file flags - set_sourceflags {cc = {"cflags", "cxflags"}, cxx = {"cxxflags", "cxflags"}, cu = {"cuflags"}} + set_sourceflags {cc = {"cflags", "cxflags"}, cxx = {"cxxflags", "cxflags"}} -- set target kinds set_targetkinds {binary = "ld", static = "ar", shared = "sh"} @@ -38,10 +38,10 @@ language("c++") set_targetflags {binary = "ldflags", static = "arflags", shared = "shflags"} -- set language kinds - set_langkinds {c = "cc", cxx = "cxx", cu = "cu"} + set_langkinds {c = "cc", cxx = "cxx"} -- set mixing kinds - set_mixingkinds("cc", "cxx", "cu", "as", "mrc") + set_mixingkinds("cc", "cxx", "as", "mrc") -- on load on_load("load") @@ -150,7 +150,6 @@ language("c++") {category = "Cross Complation Configuration/Compiler Configuration" } , {nil, "cc", "kv", nil, "The C Compiler" } , {nil, "cxx", "kv", nil, "The C++ Compiler" } - , {nil, "cu", "kv", nil, "The Cuda Compiler" } , {category = "Cross Complation Configuration/Linker Configuration" } , {nil, "ld", "kv", nil, "The Linker" } @@ -161,7 +160,6 @@ language("c++") , {nil, "cflags", "kv", nil, "The C Compiler Flags" } , {nil, "cxflags", "kv", nil, "The C/C++ compiler Flags" } , {nil, "cxxflags", "kv", nil, "The C++ Compiler Flags" } - , {nil, "cuflags", "kv", nil, "The Cuda Compiler Flags" } , {category = "Cross Complation Configuration/Linker Flags Configuration" } , {nil, "ldflags", "kv", nil, "The Binary Linker Flags" } diff --git a/xmake/languages/cuda/api.lua b/xmake/languages/cuda/api.lua new file mode 100644 index 000000000..45cd432c3 --- /dev/null +++ b/xmake/languages/cuda/api.lua @@ -0,0 +1,60 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2018, TBOOX Open Source Group. +-- +-- @author ruki +-- @file api.lua +-- + +-- get apis +function apis() + + -- init apis + _g.values = + { + -- target.add_xxx + "target.add_links" + , "target.add_cuflags" + , "target.add_ldflags" + , "target.add_arflags" + , "target.add_shflags" + -- option.add_xxx + , "option.add_links" + , "option.add_cuflags" + , "option.add_ldflags" + , "option.add_arflags" + , "option.add_shflags" + } + _g.pathes = + { + -- target.add_xxx + "target.add_linkdirs" + , "target.add_rpathdirs" + , "target.add_includedirs" + -- option.add_xxx + , "option.add_linkdirs" + , "option.add_rpathdirs" + , "option.add_includedirs" + } + + -- ok + return _g +end + + diff --git a/xmake/languages/cuda/check_main.lua b/xmake/languages/cuda/check_main.lua new file mode 100644 index 000000000..8384d5cd9 --- /dev/null +++ b/xmake/languages/cuda/check_main.lua @@ -0,0 +1,45 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2018, TBOOX Open Source Group. +-- +-- @author ruki +-- @file check_main.lua +-- + + +-- check it +function main(sourcefile) + + -- load source code + local sourcecode = io.readfile(sourcefile) + + -- remove comment first + sourcecode = sourcecode:gsub("/%*.-%*/", "") + sourcecode = sourcecode:gsub("//.-\n", "\n") + + -- find int main(int argc, char** argv) {} + if sourcecode:find("%s+main%s*%(.-%)") then + return true + end + + -- no main function + return false +end + + diff --git a/xmake/languages/cuda/load.lua b/xmake/languages/cuda/load.lua new file mode 100644 index 000000000..d0123cd08 --- /dev/null +++ b/xmake/languages/cuda/load.lua @@ -0,0 +1,38 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2018, TBOOX Open Source Group. +-- +-- @author ruki +-- @file load.lua +-- + +-- imports +import("api") + +-- load it +function main() + + -- init apis + _g.apis = api.apis() + + -- ok + return _g +end + + diff --git a/xmake/languages/cuda/xmake.lua b/xmake/languages/cuda/xmake.lua new file mode 100644 index 000000000..2cf982ba2 --- /dev/null +++ b/xmake/languages/cuda/xmake.lua @@ -0,0 +1,119 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2018, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- define language +language("cuda") + + -- set source file kinds + set_sourcekinds {cu = ".cu"} + + -- set source file flags + set_sourceflags {cu = "cuflags"} + + -- set target kinds + set_targetkinds {binary = "cu-ld", static = "cu-ar", shared = "cu-sh"} + + -- set target flags + set_targetflags {binary = "ldflags", static = "arflags", shared = "shflags"} + + -- set language kinds + set_langkinds {cu = "cu"} + + -- set mixing kinds + set_mixingkinds("cu", "cc", "cxx", "as") + + -- on load + on_load("load") + + -- on check_main + on_check_main("check_main") + + -- set name flags + set_nameflags + { + object = + { + "config.includedirs" + , "target.symbols" + , "target.warnings" + , "target.optimize:check" + , "target.vectorexts:check" + , "target.includedirs" + , "platform.includedirs" + } + , binary = + { + "config.linkdirs" + , "target.linkdirs" + , "target.rpathdirs" + , "target.strip" + , "target.symbols" + , "option.linkdirs" + , "option.rpathdirs" + , "platform.linkdirs" + , "platform.rpathdirs" + , "config.links" + , "target.links" + , "option.links" + , "platform.links" + } + , shared = + { + "config.linkdirs" + , "target.linkdirs" + , "target.strip" + , "target.symbols" + , "option.linkdirs" + , "platform.linkdirs" + , "config.links" + , "target.links" + , "option.links" + , "platform.links" + } + , static = + { + "target.strip" + , "target.symbols" + } + } + + -- set menu + set_menu { + config = + { + {category = "Cross Complation Configuration/Compiler Configuration" } + , {nil, "cu", "kv", nil, "The Cuda Compiler" } + + , {category = "Cross Complation Configuration/Linker Configuration" } + , {nil, "cu-ld", "kv", nil, "The Cuda Linker" } + , {nil, "cu-ar", "kv", nil, "The Cuda Static Library Archiver" } + , {nil, "cu-sh", "kv", nil, "The Cuda Shared Library Linker" } + + , {category = "Cross Complation Configuration/Builtin Flags Configuration" } + , {nil, "links", "kv", nil, "The Link Libraries" } + , {nil, "linkdirs", "kv", nil, "The Link Search Directories" } + , {nil, "includedirs","kv", nil, "The Include Search Directories" } + } + } + diff --git a/xmake/modules/core/tools/nvcc.lua b/xmake/modules/core/tools/nvcc.lua index fb3c87904..551b25f8c 100644 --- a/xmake/modules/core/tools/nvcc.lua +++ b/xmake/modules/core/tools/nvcc.lua @@ -37,23 +37,6 @@ function init(self) _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"} @@ -65,6 +48,10 @@ function init(self) ["-W1"] = "-Wall" , ["-W2"] = "-Wall" , ["-W3"] = "-Wall" + + -- strip + , ["-s"] = "-s" + , ["-S"] = "-S" } -- init buildmodes @@ -79,6 +66,27 @@ function get(self, name) return _g[name] end +-- make the strip flag +function nf_strip(self, level) + + -- the maps + local maps = + { + debug = "-S" + , all = "-s" + } + + -- for macho target + local plat = config.plat() + if plat == "macosx" or plat == "iphoneos" then + maps.all = "-Wl,-x" + maps.debug = "-Wl,-S" + end + + -- make it + return maps[level] +end + -- make the symbol flag function nf_symbol(self, level) @@ -143,6 +151,28 @@ function nf_includedir(self, dir) return "-I" .. os.args(dir) end +-- make the link flag +function nf_link(self, lib) + return "-l" .. lib +end + +-- make the linkdir flag +function nf_linkdir(self, dir) + return "-L" .. os.args(dir) +end + +-- make the rpathdir flag +function nf_rpathdir(self, dir) + if self:has_flags("-Wl,-rpath=" .. dir) then + return "-Wl,-rpath=" .. os.args(dir:gsub("@[%w_]+", function (name) + local maps = {["@loader_path"] = "$ORIGIN", ["@executable_path"] = "$ORIGIN"} + return maps[name] + end)) + elseif self:has_flags("-Xlinker -rpath -Xlinker " .. dir) then + return "-Xlinker -rpath -Xlinker " .. os.args(dir:gsub("%$ORIGIN", "@loader_path")) + end +end + -- make the c precompiled header flag function nf_pcheader(self, pcheaderfile, target) return "-include " .. os.args(pcheaderfile) @@ -153,6 +183,35 @@ function nf_pcxxheader(self, pcheaderfile, target) return "-include " .. os.args(pcheaderfile) end +-- make the link arguments list +function linkargv(self, objectfiles, targetkind, targetfile, flags) + + -- add rpath for dylib (macho), .e.g -install_name @rpath/file.dylib + local flags_extra = {} + if targetkind == "shared" and targetfile:endswith(".dylib") then + table.insert(flags_extra, "-install_name") + table.insert(flags_extra, "@rpath/" .. path.filename(targetfile)) + end + + -- add `-Wl,--out-implib,outputdir/libxxx.a` for xxx.dll on mingw/gcc + if targetkind == "shared" and config.plat() == "mingw" then + table.insert(flags_extra, "-Wl,--out-implib," .. os.args(path.join(path.directory(targetfile), path.basename(targetfile) .. ".a"))) + end + + -- make link args + return self:program(), table.join("-o", targetfile, objectfiles, flags, flags_extra) +end + +-- link the target file +function link(self, objectfiles, targetkind, targetfile, flags) + + -- ensure the target directory + os.mkdir(path.directory(targetfile)) + + -- link it + os.runv(linkargv(self, objectfiles, targetkind, targetfile, flags)) +end + -- make the complie arguments list function _compargv1(self, sourcefile, objectfile, flags) diff --git a/xmake/platforms/macosx/check.lua b/xmake/platforms/macosx/check.lua index 6f96a32d8..5fd68f314 100644 --- a/xmake/platforms/macosx/check.lua +++ b/xmake/platforms/macosx/check.lua @@ -46,7 +46,6 @@ function _toolchains(config) checker.toolchain_insert(toolchains, "cc", "xcrun -sdk macosx ", "clang", "the c compiler") checker.toolchain_insert(toolchains, "cxx", "xcrun -sdk macosx ", "clang", "the c++ compiler") checker.toolchain_insert(toolchains, "cxx", "xcrun -sdk macosx ", "clang++", "the c++ compiler") - checker.toolchain_insert(toolchains, "cu", "", "nvcc", "the cuda compiler") checker.toolchain_insert(toolchains, "ld", "xcrun -sdk macosx ", "clang++", "the linker") checker.toolchain_insert(toolchains, "ld", "xcrun -sdk macosx ", "clang", "the linker") checker.toolchain_insert(toolchains, "ar", "xcrun -sdk macosx ", "ar", "the static library archiver") @@ -112,6 +111,11 @@ function _toolchains(config) checker.toolchain_insert(toolchains, "rc-sh", "", "rustc", "the rust shared library linker") checker.toolchain_insert(toolchains, "rc-ld", "", "rustc", "the rust linker") + -- insert cuda tools to toolchains + checker.toolchain_insert(toolchains, "cu", "", "nvcc", "the cuda compiler") + checker.toolchain_insert(toolchains, "cu-sh", "", "nvcc", "the cuda shared library linker") + checker.toolchain_insert(toolchains, "cu-ld", "", "nvcc", "the cuda linker") + -- save toolchains _g.TOOLCHAINS = toolchains diff --git a/xmake/platforms/macosx/load.lua b/xmake/platforms/macosx/load.lua index 94236a621..2984793a0 100644 --- a/xmake/platforms/macosx/load.lua +++ b/xmake/platforms/macosx/load.lua @@ -91,6 +91,19 @@ function main() _g["rc-shflags"] = {} _g["rc-ldflags"] = {} + -- init flags for cuda + local cu_archs = { i386 = "-m32 -Xcompiler -arch -Xcompiler i386", x86_64 = "-m64 -Xcompiler -arch -Xcompiler x86_64" } + _g.cuflags = {cu_archs[arch] or ""} + _g["cu-shflags"] = {"-ccbin", "clang++", cu_archs[arch] or ""} + _g["cu-ldflags"] = {"-ccbin", "clang++", cu_archs[arch] or ""} + local cuda_dir = config.get("cuda_dir") + if cuda_dir then + table.insert(_g.cuflags, "-I" .. path.join(cuda_dir, "include")) + table.insert(_g["cu-ldflags"], "-L" .. path.join(cuda_dir, "lib")) + table.insert(_g["cu-shflags"], "-L" .. path.join(cuda_dir, "lib")) + table.insert(_g["cu-ldflags"], "-Xlinker -rpath -Xlinker " .. path.join(cuda_dir, "lib")) + end + -- ok return _g end |
