diff options
| author | ruki <[email protected]> | 2021-10-21 22:42:59 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-10-21 22:42:59 +0800 |
| commit | c03c36da556816137e18916dcca4517a8a10c6b7 (patch) | |
| tree | 77c297320f32c80c0194009982c7a796c2b78e93 | |
| parent | 7251eee6ab7ec54f5a052860ac28f46a02371eb9 (diff) | |
add nim lang
| -rwxr-xr-x | tests/projects/nim/console/src/main | bin | 0 -> 92184 bytes | |||
| -rw-r--r-- | tests/projects/nim/console/src/main.nim | 1 | ||||
| -rw-r--r-- | tests/projects/nim/console/xmake.lua | 5 | ||||
| -rw-r--r-- | xmake/core/tool/toolchain.lua | 4 | ||||
| -rw-r--r-- | xmake/languages/nim/load.lua | 56 | ||||
| -rw-r--r-- | xmake/languages/nim/xmake.lua | 78 | ||||
| -rw-r--r-- | xmake/modules/core/tools/nim.lua | 89 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/nim/has_flags.lua | 98 | ||||
| -rw-r--r-- | xmake/platforms/linux/xmake.lua | 2 | ||||
| -rw-r--r-- | xmake/platforms/macosx/xmake.lua | 2 | ||||
| -rw-r--r-- | xmake/platforms/windows/xmake.lua | 2 | ||||
| -rw-r--r-- | xmake/rules/nim/xmake.lua | 37 | ||||
| -rw-r--r-- | xmake/rules/rust/build/target.lua | 7 | ||||
| -rw-r--r-- | xmake/toolchains/nim/xmake.lua | 38 |
14 files changed, 413 insertions, 6 deletions
diff --git a/tests/projects/nim/console/src/main b/tests/projects/nim/console/src/main Binary files differnew file mode 100755 index 000000000..84f0819ac --- /dev/null +++ b/tests/projects/nim/console/src/main diff --git a/tests/projects/nim/console/src/main.nim b/tests/projects/nim/console/src/main.nim new file mode 100644 index 000000000..17179284f --- /dev/null +++ b/tests/projects/nim/console/src/main.nim @@ -0,0 +1 @@ +echo "hello xmake!" diff --git a/tests/projects/nim/console/xmake.lua b/tests/projects/nim/console/xmake.lua new file mode 100644 index 000000000..35bcd3792 --- /dev/null +++ b/tests/projects/nim/console/xmake.lua @@ -0,0 +1,5 @@ +add_rules("mode.debug", "mode.release") + +target("test") + set_kind("binary") + add_files("src/*.nim") diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index 868d05746..59e171d58 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -369,6 +369,10 @@ function _instance:_description(toolkind) cu = "the cuda compiler", culd = "the cuda linker", cuccbin = "the cuda host c++ compiler", + nc = "the nim compiler", + ncld = "the nim linker", + ncsh = "the nim shared library linker", + ncar = "the nim static library archiver" } self._DESCRIPTIONS = descriptions end diff --git a/xmake/languages/nim/load.lua b/xmake/languages/nim/load.lua new file mode 100644 index 000000000..1e96d7c39 --- /dev/null +++ b/xmake/languages/nim/load.lua @@ -0,0 +1,56 @@ +--!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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file load.lua +-- + +function _get_apis() + local apis = {} + apis.values = { + -- target.add_xxx + "target.add_ncflags" + , "target.add_ldflags" + , "target.add_arflags" + , "target.add_shflags" + , "target.add_rpathdirs" -- @note do not translate path, it's usually an absolute path or contains $ORIGIN/@loader_path + -- option.add_xxx + , "option.add_ncflags" + , "option.add_ldflags" + , "option.add_arflags" + , "option.add_shflags" + , "option.add_rpathdirs" + -- toolchain.add_xxx + , "toolchain.add_ncflags" + , "toolchain.add_ldflags" + , "toolchain.add_arflags" + , "toolchain.add_shflags" + , "toolchain.add_rpathdirs" + } + apis.paths = { + -- target.add_xxx + "target.add_linkdirs" + -- option.add_xxx + , "option.add_linkdirs" + } + return apis +end + +function main() + return {apis = _get_apis()} +end + + diff --git a/xmake/languages/nim/xmake.lua b/xmake/languages/nim/xmake.lua new file mode 100644 index 000000000..a7ffab4f9 --- /dev/null +++ b/xmake/languages/nim/xmake.lua @@ -0,0 +1,78 @@ +--!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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +language("nim") + add_rules("nim") + set_sourcekinds {nc = ".nim"} + set_sourceflags {nc = "ncflags"} + set_targetkinds {binary = "ncld", static = "ncar", shared = "ncsh"} + set_targetflags {binary = "ldflags", static = "arflags", shared = "shflags"} + set_langkinds {nim = "nc"} + set_mixingkinds("nc") + + on_load("load") + + set_nameflags { + object = { + "target.symbols" + , "target.warnings" + , "target.defines" + , "target.undefines" + , "target.optimize:check" + , "target.vectorexts:check" + } + , binary = { + "config.linkdirs" + , "target.linkdirs" + , "target.rpathdirs" + , "target.strip" + , "target.symbols" + , "toolchain.linkdirs" + , "toolchain.rpathdirs" + } + , shared = { + "config.linkdirs" + , "target.linkdirs" + , "target.strip" + , "target.symbols" + , "toolchain.linkdirs" + } + , static = { + "target.strip" + , "target.symbols" + } + } + + set_menu { + config = + { + {category = "Cross Complation Configuration/Compiler Configuration" } + , {nil, "nc", "kv", nil, "The Nim Compiler" } + + , {category = "Cross Complation Configuration/Linker Configuration" } + , {nil, "ncld", "kv", nil, "The Nim Linker" } + , {nil, "ncar", "kv", nil, "The Nim Static Library Archiver" } + , {nil, "ncsh", "kv", nil, "The Nim Shared Library Linker" } + + , {category = "Cross Complation Configuration/Builtin Flags Configuration" } + , {nil, "linkdirs", "kv", nil, "The Link Search Directories" } + } + } + diff --git a/xmake/modules/core/tools/nim.lua b/xmake/modules/core/tools/nim.lua new file mode 100644 index 000000000..4cee95c95 --- /dev/null +++ b/xmake/modules/core/tools/nim.lua @@ -0,0 +1,89 @@ +--!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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file nim.lua +-- + +-- imports +import("core.base.option") +import("core.project.config") +import("core.project.project") + +-- init it +function init(self) + + -- init arflags + self:set("ncarflags", "--app:staticlib") + + -- init shflags + self:set("ncshflags", "--app:lib") +end + +-- make the define flag +function nf_define(self, macro) + return "--define:" .. macro +end + +-- make the undefine flag +function nf_undefine(self, macro) + return "--undef:" .. macro +end + +-- make the optimize flag +function nf_optimize(self, level) + local maps = + { + none = "--opt:none" + , fast = "--opt:speed" + , faster = "--opt:speed" + , fastest = "--opt:speed" + , smallest = "--opt:size" + , aggressive = "--opt:speed" + } + return maps[level] +end + +-- make the symbol flag +function nf_symbol(self, level) + local maps = + { + debug = "--stackTrace:on" + } + return maps[level] +end + +-- make the link flag +function nf_link(self, lib) + return "--passL:" .. lib +end + +-- make the linkdir flag +function nf_linkdir(self, dir) + return {"-L" .. dir} +end + +-- make the build arguments list +function buildargv(self, sourcefiles, targetkind, targetfile, flags) + return self:program(), table.join(flags, "c", "-o:" .. targetfile, sourcefiles) +end + +-- build the target file +function build(self, sourcefiles, targetkind, targetfile, flags) + os.mkdir(path.directory(targetfile)) + os.runv(buildargv(self, sourcefiles, targetkind, targetfile, flags)) +end + diff --git a/xmake/modules/detect/tools/nim/has_flags.lua b/xmake/modules/detect/tools/nim/has_flags.lua new file mode 100644 index 000000000..2fe415241 --- /dev/null +++ b/xmake/modules/detect/tools/nim/has_flags.lua @@ -0,0 +1,98 @@ +--!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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file has_flags.lua +-- + +-- imports +import("core.cache.detectcache") + +-- try running +function _try_running(...) + + local argv = {...} + local errors = nil + return try { function () os.runv(table.unpack(argv)); 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) + + -- only one flag? + if #flags > 1 then + return + end + + -- make cache key + local key = "detect.tools.nim.has_flags" + + -- make allflags key + local flagskey = opt.program .. "_" .. (opt.programver or "") + + -- get all allflags from argument list + local allflags = detectcache:get2(key, flagskey) + if not allflags then + + -- get argument list + allflags = {} + local arglist = os.iorunv(opt.program, {"--help"}) + if arglist then + for arg in arglist:gmatch("%s+(%-[%-%a%d]+)%s+") do + allflags[arg] = true + end + end + + -- save cache + detectcache:set2(key, flagskey, allflags) + detectcache:save() + end + return allflags[flags[1]] +end + +-- try running to check flags +function _check_try_running(flags, opt) + + -- make an stub source file + local sourcefile = path.join(os.tmpdir(), "detect", "nim_has_flags.nim") + if not os.isfile(sourcefile) then + io.writefile(sourcefile, "echo \"hello\"") + end + + -- check it + local binaryfile = os.tmpfile() + local ok, errors = _try_running(opt.program, table.join("c", flags, "-o:" .. binaryfile, sourcefile)) + os.tryrm(binaryfile) + return ok, errors +end + +-- has_flags(flags)? +-- +-- @param opt the argument options, e.g. {toolname = "", program = "", programver = "", toolkind = "[cc|cxx|ld|ar|sh|gc|rc|dc|mm|mxx]"} +-- +-- @return true or false +-- +function main(flags, opt) + + -- attempt to check it from the argument list + if _check_from_arglist(flags, opt) then + return true + end + + -- try running to check it + return _check_try_running(flags, opt) +end + diff --git a/xmake/platforms/linux/xmake.lua b/xmake/platforms/linux/xmake.lua index f855903f3..ab7c71f75 100644 --- a/xmake/platforms/linux/xmake.lua +++ b/xmake/platforms/linux/xmake.lua @@ -40,7 +40,7 @@ platform("linux") set_installdir("/usr/local") -- set toolchains - set_toolchains("envs", "cross", "gcc", "clang", "yasm", "nasm", "fasm", "cuda", "dlang", "go", "rust", "gfortran", "zig", "fpc") + set_toolchains("envs", "cross", "gcc", "clang", "yasm", "nasm", "fasm", "cuda", "dlang", "go", "rust", "gfortran", "zig", "fpc", "nim") -- set menu set_menu { diff --git a/xmake/platforms/macosx/xmake.lua b/xmake/platforms/macosx/xmake.lua index 792a5cad3..8571a13c4 100644 --- a/xmake/platforms/macosx/xmake.lua +++ b/xmake/platforms/macosx/xmake.lua @@ -40,7 +40,7 @@ platform("macosx") set_installdir("/usr/local") -- set toolchains - set_toolchains("envs", "xcode", "clang", "gcc", "yasm", "nasm", "cuda", "dlang", "rust", "go", "gfortran", "zig", "fpc") + set_toolchains("envs", "xcode", "clang", "gcc", "yasm", "nasm", "cuda", "dlang", "rust", "go", "gfortran", "zig", "fpc", "nim") -- set menu set_menu { diff --git a/xmake/platforms/windows/xmake.lua b/xmake/platforms/windows/xmake.lua index caf707d50..f7687f44b 100644 --- a/xmake/platforms/windows/xmake.lua +++ b/xmake/platforms/windows/xmake.lua @@ -38,7 +38,7 @@ platform("windows") set_formats("symbol", "$(name).pdb") -- set toolchains - set_toolchains("msvc", "clang", "yasm", "nasm", "cuda", "dlang", "rust", "go", "gfortran", "zig", "fpc") + set_toolchains("msvc", "clang", "yasm", "nasm", "cuda", "dlang", "rust", "go", "gfortran", "zig", "fpc", "nim") -- set menu set_menu { diff --git a/xmake/rules/nim/xmake.lua b/xmake/rules/nim/xmake.lua new file mode 100644 index 000000000..7d65c9c12 --- /dev/null +++ b/xmake/rules/nim/xmake.lua @@ -0,0 +1,37 @@ +--!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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- define rule: nim.build +rule("nim.build") + set_sourcekinds("nc") + on_load(function (target) + local cachedir = path.join(target:autogendir(), "nimcache") + target:add("ncflags", "--nimcache:" .. cachedir, {force = true}) + end) + on_build("build.target") + +-- define rule: nim +rule("nim") + + -- add build rules + add_deps("nim.build") + + -- inherit links and linkdirs of all dependent targets by default + add_deps("utils.inherit.links") diff --git a/xmake/rules/rust/build/target.lua b/xmake/rules/rust/build/target.lua index a1f34beff..5f0167693 100644 --- a/xmake/rules/rust/build/target.lua +++ b/xmake/rules/rust/build/target.lua @@ -76,10 +76,11 @@ end function main(target, opt) -- @note only support one source kind! - for _, sourcebatch in pairs(target:sourcebatches()) do - if sourcebatch.sourcekind == "rc" then + local sourcebatches = target:sourcebatches() + if sourcebatches then + local sourcebatch = sourcebatches["rust.build"] + if sourcebatch then build_sourcefiles(target, sourcebatch, opt) - break end end end diff --git a/xmake/toolchains/nim/xmake.lua b/xmake/toolchains/nim/xmake.lua new file mode 100644 index 000000000..660f63f56 --- /dev/null +++ b/xmake/toolchains/nim/xmake.lua @@ -0,0 +1,38 @@ +--!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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- define toolchain +toolchain("nim") + + -- set homepage + set_homepage("https://nim-lang.org/") + set_description("Nim Programming Language Compiler") + + -- set toolset + set_toolset("nc", "$(env NC)", "nim") + set_toolset("ncld", "$(env NC)", "nim") + set_toolset("ncsh", "$(env NC)", "nim") + set_toolset("ncar", "$(env NC)", "nim") + + -- on load + on_load(function (toolchain) + toolchain:set("ncshflags", "") + toolchain:set("ncldflags", "") + end) |
