From d00aeeafdd54134ff294dc4459a6b8cbaa560f8d Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Tue, 27 Nov 2018 18:06:17 -0500 Subject: add syntax cIncludeDir "$projpath/include"; do not use recursive search paths; add test tests/tnimterop_cpp.nim --- nimterop.nimble | 3 ++- nimterop/cimport.nim | 30 ++++++++++++++++++------------ tests/include/test2.cpp | 8 ++++++++ tests/include/test2.hpp | 20 ++++++++++++++++++++ tests/tnimterop.nim | 46 ---------------------------------------------- tests/tnimterop_c.nim | 46 ++++++++++++++++++++++++++++++++++++++++++++++ tests/tnimterop_cpp.nim | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 143 insertions(+), 59 deletions(-) create mode 100644 tests/include/test2.cpp create mode 100644 tests/include/test2.hpp delete mode 100644 tests/tnimterop.nim create mode 100644 tests/tnimterop_c.nim create mode 100644 tests/tnimterop_cpp.nim diff --git a/nimterop.nimble b/nimterop.nimble index 553b308..52241a3 100644 --- a/nimterop.nimble +++ b/nimterop.nimble @@ -17,7 +17,8 @@ proc execCmd(cmd:string)= exec cmd task test, "Test": - execCmd "nim c -r tests/tnimterop" + execCmd "nim c -r tests/tnimterop_c.nim" + execCmd "nim c -r tests/tnimterop_cpp.nim" task installWithDeps, "install dependencies": for a in ["http://github.com/genotrance/nimtreesitter?subdir=treesitter", diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim index 9c63665..0317787 100644 --- a/nimterop/cimport.nim +++ b/nimterop/cimport.nim @@ -2,20 +2,29 @@ import macros, os, strformat, strutils import ast, getters, globals, lisp +proc interpPath(dir: string): string= + # TODO: more robust: needs a DirSep after "$projpath" + result = dir.replace("$projpath", getProjectPath()) + +proc joinPathIfRel(path1: string, path2: string): string = + if path2.isAbsolute: + result = path2 + else: + result = joinPath(path1, path2) + proc findPath(path: string, fail = true): string = # As is result = path.replace("\\", "/") if not fileExists(result) and not dirExists(result): # Relative to project path - result = joinPath(getProjectPath(), path).replace("\\", "/") + result = joinPathIfRel(getProjectPath(), path).replace("\\", "/") if not fileExists(result) and not dirExists(result): if fail: - echo "File or directory not found: " & path - quit(1) + doAssert false, "File or directory not found: " & path else: return "" -proc cSearchPath*(path: string): string = +proc cSearchPath*(path: string): string {.compileTime.}= result = findPath(path, fail = false) if result.len == 0: var found = false @@ -24,9 +33,7 @@ proc cSearchPath*(path: string): string = if fileExists(result) or dirExists(result): found = true break - if not found: - echo "File or directory not found: " & path - quit(1) + doAssert found, "File or directory not found: " & path & " gSearchDirs: " & $gSearchDirs macro cDebug*(): untyped = gDebug = true @@ -50,13 +57,12 @@ macro cDefine*(name: static string, val: static string = ""): untyped = echo result.repr macro cAddSearchDir*(dir: static string): untyped = - result = newNimNode(nnkStmtList) - - let fullpath = cSearchPath(dir) - if fullpath notin gSearchDirs: - gSearchDirs.add(fullpath) + var dir = interpPath(dir) + if dir notin gSearchDirs: + gSearchDirs.add(dir) macro cIncludeDir*(dir: static string): untyped = + var dir = interpPath(dir) result = newNimNode(nnkStmtList) let diff --git a/tests/include/test2.cpp b/tests/include/test2.cpp new file mode 100644 index 0000000..4a40e90 --- /dev/null +++ b/tests/include/test2.cpp @@ -0,0 +1,8 @@ +#include "test2.hpp" + +// BUG: should complain +// # include test2.h + +int test_call_int() { + return 5; +} diff --git a/tests/include/test2.hpp b/tests/include/test2.hpp new file mode 100644 index 0000000..1ba9a8d --- /dev/null +++ b/tests/include/test2.hpp @@ -0,0 +1,20 @@ +#define TEST_INT 512 + +int test_call_int(); + +struct Foo{ + int bar; +}; + +class Foo1{ + int bar1; +}; + +template +struct Foo2{ + int bar2; +}; + +typedef Foo2 Foo2_int; + + diff --git a/tests/tnimterop.nim b/tests/tnimterop.nim deleted file mode 100644 index dd822cb..0000000 --- a/tests/tnimterop.nim +++ /dev/null @@ -1,46 +0,0 @@ -import nimterop/cimport - -cDebug() - -cDefine("FORCE") -cIncludeDir "include" -cAddSearchDir "include" -cCompile cSearchPath("test.c") -cImport cSearchPath "test.h" - -doAssert TEST_INT == 512 -doAssert TEST_FLOAT == 5.12 -doAssert TEST_HEX == 0x512 - -var - pt: PRIMTYPE - ct: CUSTTYPE - - s: STRUCT1 - s2: STRUCT2 - s3: STRUCT3 - s4: STRUCT4 - - e: ENUM - e2: ENUM2 = enum5 - - vptr: VOIDPTR - iptr: INTPTR - -pt = 3 -ct = 4 - -s.field1 = 5 -s2.field1 = 6 -s3.field1 = 7 - -e = enum1 -e2 = enum4 - -doAssert test_call_int() == 5 -doAssert test_call_int_param(5).field1 == 5 -doAssert test_call_int_param2(5, s2).field1 == 11 -doAssert test_call_int_param3(5, s).field1 == 10 -doAssert test_call_int_param4(e) == e2 - -cAddStdDir() diff --git a/tests/tnimterop_c.nim b/tests/tnimterop_c.nim new file mode 100644 index 0000000..1f2aaab --- /dev/null +++ b/tests/tnimterop_c.nim @@ -0,0 +1,46 @@ +import nimterop/cimport + +cDebug() + +cDefine("FORCE") +cIncludeDir "$projpath/include" +cAddSearchDir "$projpath/include" +cCompile cSearchPath("test.c") +cImport cSearchPath "test.h" + +doAssert TEST_INT == 512 +doAssert TEST_FLOAT == 5.12 +doAssert TEST_HEX == 0x512 + +var + pt: PRIMTYPE + ct: CUSTTYPE + + s: STRUCT1 + s2: STRUCT2 + s3: STRUCT3 + s4: STRUCT4 + + e: ENUM + e2: ENUM2 = enum5 + + vptr: VOIDPTR + iptr: INTPTR + +pt = 3 +ct = 4 + +s.field1 = 5 +s2.field1 = 6 +s3.field1 = 7 + +e = enum1 +e2 = enum4 + +doAssert test_call_int() == 5 +doAssert test_call_int_param(5).field1 == 5 +doAssert test_call_int_param2(5, s2).field1 == 11 +doAssert test_call_int_param3(5, s).field1 == 10 +doAssert test_call_int_param4(e) == e2 + +cAddStdDir() diff --git a/tests/tnimterop_cpp.nim b/tests/tnimterop_cpp.nim new file mode 100644 index 0000000..b3d62f5 --- /dev/null +++ b/tests/tnimterop_cpp.nim @@ -0,0 +1,49 @@ +import nimterop/cimport +import unittest + +cDebug() + +cIncludeDir "$projpath/include" +cAddSearchDir "$projpath/include" +cCompile cSearchPath "test2.cpp" +# TODO: allow this to have correct language: cImport("test2.h") +cImport cSearchPath "test2.hpp" + +check TEST_INT == 512 +check test_call_int() == 5 + +var foo: Foo +check foo.bar == 2 + +# var foo2: Foo2[int] +# var foo2: Foo2Int + +when false: + doAssert TEST_FLOAT == 5.12 + doAssert TEST_HEX == 0x512 + + var + pt: PRIMTYPE + ct: CUSTTYPE + + s: STRUCT1 + s2: STRUCT2 + s3: STRUCT3 + + e: ENUM + e2: ENUM2 = enum5 + + pt = 3 + ct = 4 + + s.field1 = 5 + s2.field1 = 6 + s3.field1 = 7 + + e = enum1 + e2 = enum4 + + doAssert test_call_int_param(5).field1 == 5 + doAssert test_call_int_param2(5, s2).field1 == 11 + doAssert test_call_int_param3(5, s).field1 == 10 + doAssert test_call_int_param4(e) == e2 -- cgit v1.2.3 From 6a4158dab4ee36214678a92a1e5605a00402f01c Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Tue, 27 Nov 2018 18:25:34 -0500 Subject: fixup --- nimterop.nimble | 2 +- tests/include/test.h | 2 +- tests/include/test2.cpp | 3 --- tests/include/test2.hpp | 4 ++++ tests/tnimterop_cpp.nim | 38 +++----------------------------------- 5 files changed, 9 insertions(+), 40 deletions(-) diff --git a/nimterop.nimble b/nimterop.nimble index 52241a3..aef56fc 100644 --- a/nimterop.nimble +++ b/nimterop.nimble @@ -18,7 +18,7 @@ proc execCmd(cmd:string)= task test, "Test": execCmd "nim c -r tests/tnimterop_c.nim" - execCmd "nim c -r tests/tnimterop_cpp.nim" + execCmd "nim cpp -r tests/tnimterop_cpp.nim" task installWithDeps, "install dependencies": for a in ["http://github.com/genotrance/nimtreesitter?subdir=treesitter", diff --git a/tests/include/test.h b/tests/include/test.h index 7de6eed..6138ca3 100644 --- a/tests/include/test.h +++ b/tests/include/test.h @@ -40,4 +40,4 @@ int test_call_int(); struct STRUCT1 _test_call_int_param_(int param1); STRUCT2 test_call_int_param2(int param1, STRUCT2 param2); STRUCT2 test_call_int_param3(int param1, struct STRUCT1 param2); -ENUM2 test_call_int_param4(enum ENUM param1); \ No newline at end of file +ENUM2 test_call_int_param4(enum ENUM param1); diff --git a/tests/include/test2.cpp b/tests/include/test2.cpp index 4a40e90..7039cde 100644 --- a/tests/include/test2.cpp +++ b/tests/include/test2.cpp @@ -1,8 +1,5 @@ #include "test2.hpp" -// BUG: should complain -// # include test2.h - int test_call_int() { return 5; } diff --git a/tests/include/test2.hpp b/tests/include/test2.hpp index 1ba9a8d..0fd90ee 100644 --- a/tests/include/test2.hpp +++ b/tests/include/test2.hpp @@ -1,4 +1,8 @@ +#include + #define TEST_INT 512 +#define TEST_FLOAT 5.12 +#define TEST_HEX 0x512 int test_call_int(); diff --git a/tests/tnimterop_cpp.nim b/tests/tnimterop_cpp.nim index b3d62f5..bed052c 100644 --- a/tests/tnimterop_cpp.nim +++ b/tests/tnimterop_cpp.nim @@ -6,44 +6,12 @@ cDebug() cIncludeDir "$projpath/include" cAddSearchDir "$projpath/include" cCompile cSearchPath "test2.cpp" -# TODO: allow this to have correct language: cImport("test2.h") cImport cSearchPath "test2.hpp" check TEST_INT == 512 +check TEST_FLOAT == 5.12 +check TEST_HEX == 0x512 check test_call_int() == 5 var foo: Foo -check foo.bar == 2 - -# var foo2: Foo2[int] -# var foo2: Foo2Int - -when false: - doAssert TEST_FLOAT == 5.12 - doAssert TEST_HEX == 0x512 - - var - pt: PRIMTYPE - ct: CUSTTYPE - - s: STRUCT1 - s2: STRUCT2 - s3: STRUCT3 - - e: ENUM - e2: ENUM2 = enum5 - - pt = 3 - ct = 4 - - s.field1 = 5 - s2.field1 = 6 - s3.field1 = 7 - - e = enum1 - e2 = enum4 - - doAssert test_call_int_param(5).field1 == 5 - doAssert test_call_int_param2(5, s2).field1 == 11 - doAssert test_call_int_param3(5, s).field1 == 10 - doAssert test_call_int_param4(e) == e2 +check foo.bar == 0 -- cgit v1.2.3 From c1ea14c0d3c80e401aabeb2d99431d41c4347fe1 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Tue, 27 Nov 2018 18:27:59 -0500 Subject: doAssert => check --- tests/tnimterop_c.nim | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/tnimterop_c.nim b/tests/tnimterop_c.nim index 1f2aaab..eff7039 100644 --- a/tests/tnimterop_c.nim +++ b/tests/tnimterop_c.nim @@ -1,3 +1,4 @@ +import std/unittest import nimterop/cimport cDebug() @@ -8,9 +9,9 @@ cAddSearchDir "$projpath/include" cCompile cSearchPath("test.c") cImport cSearchPath "test.h" -doAssert TEST_INT == 512 -doAssert TEST_FLOAT == 5.12 -doAssert TEST_HEX == 0x512 +check TEST_INT == 512 +check TEST_FLOAT == 5.12 +check TEST_HEX == 0x512 var pt: PRIMTYPE @@ -37,10 +38,10 @@ s3.field1 = 7 e = enum1 e2 = enum4 -doAssert test_call_int() == 5 -doAssert test_call_int_param(5).field1 == 5 -doAssert test_call_int_param2(5, s2).field1 == 11 -doAssert test_call_int_param3(5, s).field1 == 10 -doAssert test_call_int_param4(e) == e2 +check test_call_int() == 5 +check test_call_int_param(5).field1 == 5 +check test_call_int_param2(5, s2).field1 == 11 +check test_call_int_param3(5, s).field1 == 10 +check test_call_int_param4(e) == e2 cAddStdDir() -- cgit v1.2.3