diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2018-11-21 21:53:40 -0600 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2018-11-21 21:53:40 -0600 |
| commit | a8bb2dc01f99a2866f586f6e1491a512ce9b3cfa (patch) | |
| tree | 8129d7174ddcf42099a6335e18fda82db2c05e94 | |
| parent | 5b6bfc397e0f855abc76b157e7ce2eb9d1226fd7 (diff) | |
| download | nimterop-a8bb2dc01f99a2866f586f6e1491a512ce9b3cfa.tar.gz nimterop-a8bb2dc01f99a2866f586f6e1491a512ce9b3cfa.zip | |
Fix #7 - separate file searching from include dirs
| -rw-r--r-- | README.md | 10 | ||||
| -rw-r--r-- | nimterop/cimport.nim | 58 | ||||
| -rw-r--r-- | nimterop/globals.nim | 1 | ||||
| -rw-r--r-- | tests/tnimterop.nim | 7 |
4 files changed, 51 insertions, 25 deletions
@@ -57,11 +57,15 @@ Detailed documentation is still forthcoming. `cDebug()` - enable debug messages -`cDefine()` - `#define` an identifer that is forwarded to the compiler using `{.passC: "-DXXX".}` as well as _eventually_ used in processing `#ifdef` statements +`cDefine("XXX")` - `#define` an identifer that is forwarded to the C/C++ compiler using `{.passC: "-DXXX".}` as well as _eventually_ used in processing `#ifdef` statements -`cIncludeDir()` - add an include directory that is forwarded to the compiler using `{.passC: "-IXXX".}` as well as searched for files included using `cImport()` statements and following `cIncludeDir()` statements +`cIncludeDir("XXX")` - add an include directory that is forwarded to the compiler using `{.passC: "-IXXX".}` -`cImport()` - import all supported definitions from specific import header file +`cImport("header.h")` - import all supported definitions from header file + +`cAddSearchDir("XXX")` - add directory XXX to search path in calls to `cSearchPath()` + +`cSearchPath("header.h")` - return a file or directory found in search path configured using `cSearchPath()` - can be used in `cCompile()`, `cIncludeDir()` and `cImport()` calls `gitPull()` - pull a git repository prior to C/C++ interop diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim index 8a1f299..91dd404 100644 --- a/nimterop/cimport.nim +++ b/nimterop/cimport.nim @@ -2,20 +2,31 @@ import macros, os, strformat, strutils import ast, getters, globals, lisp -proc search(path: string): string = - result = joinPath(getProjectPath(), path).replace("\\", "/") +proc findPath(path: string, fail = true): string = + # As is + result = path.replace("\\", "/") if not fileExists(result) and not dirExists(result): - result = path + # Relative to project path + result = joinPath(getProjectPath(), path).replace("\\", "/") if not fileExists(result) and not dirExists(result): - var found = false - for inc in gIncludeDirs: - result = inc & "/" & path - if fileExists(result) or dirExists(result): - found = true - break - if not found: + if fail: echo "File or directory not found: " & path quit(1) + else: + return "" + +proc cSearchPath*(path: string): string = + result = findPath(path, fail = false) + if result.len() == 0: + var found = false + for inc in gSearchDirs: + result = (inc & "/" & path).replace("\\", "/") + if fileExists(result) or dirExists(result): + found = true + break + if not found: + echo "File or directory not found: " & path + quit(1) macro cDebug*(): untyped = gDebug = true @@ -34,13 +45,23 @@ macro cDefine*(name: static[string], val: static[string] = ""): untyped = if gDebug: echo result.repr +macro cAddSearchDir*(dir: static[string]): untyped = + result = newNimNode(nnkStmtList) + + let fullpath = cSearchPath(dir) + if fullpath notin gSearchDirs: + gSearchDirs.add(fullpath) + macro cIncludeDir*(dir: static[string]): untyped = result = newNimNode(nnkStmtList) - let fullpath = search(dir) - gIncludeDirs.add(fullpath) - - let str = "-I\"" & fullpath & "\"" + let + fullpath = findPath(dir) + str = "-I\"" & fullpath & "\"" + + if fullpath notin gIncludeDirs: + gIncludeDirs.add(fullpath) + result.add(quote do: {.passC: `str`.} ) @@ -61,9 +82,8 @@ macro cIncludeC*(): untyped = break if inc: - if gDebug: - echo "Including " & line.strip() - gIncludeDirs.add(line.strip()) + result = quote do: + cIncludeDir(line) macro cCompile*(path: static[string]): untyped = result = newNimNode(nnkStmtList) @@ -94,7 +114,7 @@ macro cCompile*(path: static[string]): untyped = if path.contains("*") or path.contains("?"): dcompile(path) else: - let fpath = search(path) + let fpath = findPath(path) if fileExists(fpath): stmt &= fcompile(fpath) & "\n" elif dirExists(fpath): @@ -114,7 +134,7 @@ macro cImport*(filename: static[string]): untyped = result.add addReorder() let - fullpath = search(filename) + fullpath = findPath(filename) root = parseLisp(fullpath) echo "Importing " & fullpath diff --git a/nimterop/globals.nim b/nimterop/globals.nim index 6ace9f5..e2e155d 100644 --- a/nimterop/globals.nim +++ b/nimterop/globals.nim @@ -25,6 +25,7 @@ var gHeaders* {.compiletime.}: seq[string] gIncludeDirs* {.compiletime.}: seq[string] gProcs* {.compiletime.}: seq[string] + gSearchDirs* {.compiletime.}: seq[string] gTypes* {.compiletime.}: seq[string] gCode* {.compiletime.}: string diff --git a/tests/tnimterop.nim b/tests/tnimterop.nim index 9c5db77..a05cecd 100644 --- a/tests/tnimterop.nim +++ b/tests/tnimterop.nim @@ -2,9 +2,10 @@ import nimterop/cimport cDebug() -cIncludeDir("include") -cCompile("test.c") -cImport("test.h") +cIncludeDir "include" +cAddSearchDir "include" +cCompile cSearchPath("test.c") +cImport cSearchPath "test.h" doAssert TEST_INT == 512 doAssert TEST_FLOAT == 5.12 |
