Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Nim Language #1759

Merged
merged 7 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [#1747](https://github.com/xmake-io/xmake/issues/1747): Add `set_kind("headeronly")` for target to install files for headeronly library
* [#1019](https://github.com/xmake-io/xmake/issues/1019): Support Unity build
* [#1438](https://github.com/xmake-io/xmake/issues/1438): Support code amalgamation, `xmake l cli.amalgamate`
* [#1765](https://github.com/xmake-io/xmake/issues/1756): Support nim language

### Changes

Expand Down Expand Up @@ -1114,6 +1115,7 @@
* [#1747](https://github.com/xmake-io/xmake/issues/1747): 添加 `set_kind("headeronly")` 更好的处理 headeronly 库的安装
* [#1019](https://github.com/xmake-io/xmake/issues/1019): 支持 Unity build
* [#1438](https://github.com/xmake-io/xmake/issues/1438): 增加 `xmake l cli.amalgamate` 命令支持代码合并
* [#1765](https://github.com/xmake-io/xmake/issues/1756): 支持 nim 语言

### 改进

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ ifort Intel Fortran Compiler
muslcc The musl-based cross-compilation toolchain
fpc Free Pascal Programming Language Compiler
wasi WASI-enabled WebAssembly C/C++ toolchain
nim Nim Programming Language Compiler
```

## Supported Languages
Expand All @@ -242,6 +243,7 @@ wasi WASI-enabled WebAssembly C/C++ toolchain
* Zig
* Vala
* Pascal
* Nim

## Supported Features

Expand Down
2 changes: 2 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ ifort Intel Fortran Compiler
muslcc The musl-based cross-compilation toolchain
fpc Free Pascal Programming Language Compiler
wasi WASI-enabled WebAssembly C/C++ toolchain
nim Nim Programming Language Compiler
```

## 支持语言
Expand All @@ -249,6 +250,7 @@ wasi WASI-enabled WebAssembly C/C++ toolchain
* Zig
* Vala
* Pascal
* Nim

## 支持特性

Expand Down
Binary file added tests/projects/nim/console/src/main
Binary file not shown.
1 change: 1 addition & 0 deletions tests/projects/nim/console/src/main.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "hello xmake!"
5 changes: 5 additions & 0 deletions tests/projects/nim/console/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_rules("mode.debug", "mode.release")

target("test")
set_kind("binary")
add_files("src/main.nim")
3 changes: 3 additions & 0 deletions tests/projects/nim/console_with_c/src/foo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int foo(int n) {
return n;
}
3 changes: 3 additions & 0 deletions tests/projects/nim/console_with_c/src/main.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
proc foo(n: int): int {.cdecl, importc}

echo foo(2)
10 changes: 10 additions & 0 deletions tests/projects/nim/console_with_c/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_rules("mode.debug", "mode.release")

target("foo")
set_kind("static")
add_files("src/*.c")

target("test")
set_kind("binary")
add_deps("foo")
add_files("src/main.nim")
3 changes: 3 additions & 0 deletions tests/projects/nim/console_with_packages/src/main.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
proc zlibVersion(): cstring {.cdecl, importc}

echo zlibVersion()
8 changes: 8 additions & 0 deletions tests/projects/nim/console_with_packages/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_rules("mode.debug", "mode.release")

add_requires("zlib")

target("test")
set_kind("binary")
add_files("src/main.nim")
add_packages("zlib")
5 changes: 5 additions & 0 deletions tests/projects/nim/shared_library/src/foo.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
proc foo(n: int): int {.cdecl, exportc, dynlib.} =
if n < 2:
result = n
else:
result = foo(n - 1) + (n - 2).foo
4 changes: 4 additions & 0 deletions tests/projects/nim/shared_library/src/main.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
proc foo(n: int): int {.cdecl, importc}

echo foo(2)

12 changes: 12 additions & 0 deletions tests/projects/nim/shared_library/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_rules("mode.debug", "mode.release")

target("foo")
set_kind("shared")
add_files("src/foo.nim")

target("test")
set_kind("binary")
add_deps("foo")
add_files("src/main.nim")


5 changes: 5 additions & 0 deletions tests/projects/nim/static_library/src/bar.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
proc bar(n: int): int {.cdecl, exportc} =
if n < 2:
result = n
else:
result = bar(n - 1) + (n - 2).bar
8 changes: 8 additions & 0 deletions tests/projects/nim/static_library/src/foo.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import bar

proc foo(n: int): int {.cdecl, exportc} =
if n < 2:
result = n
else:
result = foo(n - 1) + (n - 2).foo

5 changes: 5 additions & 0 deletions tests/projects/nim/static_library/src/main.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
proc foo(n: int): int {.cdecl, importc}
proc bar(n: int): int {.cdecl, importc}

echo foo(2)
echo bar(2)
12 changes: 12 additions & 0 deletions tests/projects/nim/static_library/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_rules("mode.debug", "mode.release")

target("foo")
set_kind("static")
add_files("src/foo.nim")

target("test")
set_kind("binary")
add_deps("foo")
add_files("src/main.nim")


4 changes: 4 additions & 0 deletions xmake/core/tool/toolchain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions xmake/languages/nim/load.lua
Original file line number Diff line number Diff line change
@@ -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


87 changes: 87 additions & 0 deletions xmake/languages/nim/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
--!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 = {
"config.includedirs"
, "target.symbols"
, "target.warnings"
, "target.defines"
, "target.undefines"
, "target.optimize:check"
, "target.vectorexts:check"
, "target.includedirs"
, "toolchain.includedirs"
}
, binary = {
"config.linkdirs"
, "target.linkdirs"
, "target.rpathdirs"
, "target.strip"
, "target.symbols"
, "toolchain.linkdirs"
, "toolchain.rpathdirs"
, "config.links"
, "target.links"
, "toolchain.links"
}
, shared = {
"config.linkdirs"
, "target.linkdirs"
, "target.strip"
, "target.symbols"
, "toolchain.linkdirs"
, "config.links"
, "target.links"
, "toolchain.links"
}
, 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" }
}
}

Loading