Skip to content

Commit

Permalink
Make Nim/SV interface work even when Nim is compiled to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Apr 26, 2019
1 parent 4d17613 commit 5344cac
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 7 additions & 1 deletion hello_from_nim/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Time-stamp: <2019-01-22 13:40:01 kmodi>
# Time-stamp: <2019-04-26 17:48:17 kmodi>

.DEFAULT_GOAL := default

GIT_ROOT = $(shell git rev-parse --show-toplevel)
include $(GIT_ROOT)/makefile

# Compiles to C
default: libdpi nc

# Compiles to C++
cpp:
$(MAKE) libdpi NIM_COMPILES_TO=cpp
$(MAKE) nc
6 changes: 6 additions & 0 deletions hello_from_nim/libdpi.nim
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
when defined(cpp):
{.push codegenDecl: """extern "C" $1 $2 $3""".}

proc hello() {.exportc.} =
echo "Hello from Nim!"

when defined(cpp):
{.pop.}
6 changes: 4 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Time-stamp: <2019-04-26 15:05:09 kmodi>
# Time-stamp: <2019-04-26 17:46:54 kmodi>
# Author : Kaushal Modi

FILES = tb.sv
Expand All @@ -23,6 +23,8 @@ else
GCC_ARCH_FLAG := -m32
endif

# Possible values of NIM_COMPILES_TO: c, cpp
NIM_COMPILES_TO ?= c
NIM_GC ?=
NIM_DEFINES =

Expand All @@ -39,7 +41,7 @@ clean:
# https://irclogs.nim-lang.org/21-01-2019.html#17:16:39
libdpi:
@find . \( -name libdpi.o -o -name libdpi.so \) -delete
nim c --out:libdpi.so --app:lib --nimcache:./.nimcache $(NIM_ARCH_FLAGS) $(NIM_GC) --hint[Processing]:off $(NIM_DEFINES) libdpi.nim
nim $(NIM_COMPILES_TO) --out:libdpi.so --app:lib --nimcache:./.nimcache $(NIM_ARCH_FLAGS) $(NIM_GC) --hint[Processing]:off $(NIM_DEFINES) libdpi.nim

nc:
xrun -sv $(NC_ARCH_FLAGS) \
Expand Down

0 comments on commit 5344cac

Please sign in to comment.