Skip to content

Commit

Permalink
Merge pull request #14 from timotheecour/pr_tnimterop_cpp
Browse files Browse the repository at this point in the history
misc changes
  • Loading branch information
genotrance committed Nov 27, 2018
2 parents fb053dd + c1ea14c commit 7aed05b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 24 deletions.
3 changes: 2 additions & 1 deletion nimterop.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -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 cpp -r tests/tnimterop_cpp.nim"

task installWithDeps, "install dependencies":
for a in ["http://github.com/genotrance/nimtreesitter?subdir=treesitter",
Expand Down
30 changes: 18 additions & 12 deletions nimterop/cimport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/include/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
ENUM2 test_call_int_param4(enum ENUM param1);
5 changes: 5 additions & 0 deletions tests/include/test2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "test2.hpp"

int test_call_int() {
return 5;
}
24 changes: 24 additions & 0 deletions tests/include/test2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdint.h>

#define TEST_INT 512
#define TEST_FLOAT 5.12
#define TEST_HEX 0x512

int test_call_int();

struct Foo{
int bar;
};

class Foo1{
int bar1;
};

template<typename T>
struct Foo2{
int bar2;
};

typedef Foo2<int> Foo2_int;


21 changes: 11 additions & 10 deletions tests/tnimterop.nim → tests/tnimterop_c.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import std/unittest
import nimterop/cimport

cDebug()

cDefine("FORCE")
cIncludeDir "include"
cAddSearchDir "include"
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
check TEST_INT == 512
check TEST_FLOAT == 5.12
check TEST_HEX == 0x512

var
pt: PRIMTYPE
Expand All @@ -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()
17 changes: 17 additions & 0 deletions tests/tnimterop_cpp.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import nimterop/cimport
import unittest

cDebug()

cIncludeDir "$projpath/include"
cAddSearchDir "$projpath/include"
cCompile cSearchPath "test2.cpp"
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 == 0

0 comments on commit 7aed05b

Please sign in to comment.