diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2020-04-12 12:56:50 -0500 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2020-04-12 12:56:50 -0500 |
| commit | 0afb634b59d55ff369bd25d85e13d156c6836db0 (patch) | |
| tree | 69b06f5388a0b4769cee96534f81fd5e0b7081b7 | |
| parent | 9cd39600d43c90045b0da6873900881449e6d609 (diff) | |
| download | nimterop-0afb634b59d55ff369bd25d85e13d156c6836db0.tar.gz nimterop-0afb634b59d55ff369bd25d85e13d156c6836db0.zip | |
Fix #181 - cPluginPath()
| -rw-r--r-- | nimterop/cimport.nim | 18 | ||||
| -rw-r--r-- | tests/tnimterop_c.nim | 9 | ||||
| -rw-r--r-- | tests/tnimterop_c_plugin.nim | 7 |
3 files changed, 24 insertions, 10 deletions
diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim index c4ebc3f..69aaf90 100644 --- a/nimterop/cimport.nim +++ b/nimterop/cimport.nim @@ -273,7 +273,7 @@ proc cPluginHelper(body: string) = if gStateCT.pluginSource.nBl or gStateCT.overrides.nBl: let - data = "import macros, nimterop/plugin\n\n" & body & gStateCT.overrides + data = "import macros, nimterop/plugin\n\n" & body & "\n\n" & gStateCT.overrides hash = data.hash().abs() path = getProjectCacheDir("cPlugins", forceClean = false) / "nimterop_" & $hash & ".nim" @@ -317,7 +317,7 @@ macro cPlugin*(body): untyped = ## - `nskEnumField` for enum (field) names, though they are in the global namespace as `nskConst` ## - `nskProc` - for proc names ## - ## `nimterop/plugins` is implicitly imported to provide access to standard + ## `macros` and `nimterop/plugins` are implicitly imported to provide access to standard ## plugin facilities. ## ## `cPlugin() <cimport.html#cPlugin.m>`_ only affects calls to @@ -342,6 +342,20 @@ macro cPlugin*(body): untyped = cPluginHelper(body.repr) +macro cPluginPath*(path: static[string]): untyped = + ## Rather than embedding the `cPlugin()` code within the wrapper, it might be + ## preferable to have it stored in a separate source file. This allows for reuse + ## across multiple wrappers when applicable. + ## + ## The `cPluginPath()` macro enables this functionality - provide a path to the + ## plugin file and it will be consumed in the same way as `cPlugin()`. + ## + ## `path` is relative to the current dir and not necessarily relative to the + ## location of the wrapper file. Use `currentSourcePath.parentDir()` to specify + ## path relative to the wrapper file. + doAssert fileExists(path), "Plugin file not found: " & path + cPluginHelper(readFile(path)) + proc cSearchPath*(path: string): string {.compileTime.}= ## Get full path to file or directory `path` in search path configured ## using `cAddSearchDir() <cimport.html#cAddSearchDir%2Cstring>`_ and diff --git a/tests/tnimterop_c.nim b/tests/tnimterop_c.nim index e78e7f2..0360225 100644 --- a/tests/tnimterop_c.nim +++ b/tests/tnimterop_c.nim @@ -11,14 +11,7 @@ cDefine("FORCE") cIncludeDir testsIncludeDir() cCompile cSearchPath("test.c") -cPlugin: - import strutils - - proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} = - if sym.name == "_Kernel": - sym.name = "uKernel" - else: - sym.name = sym.name.strip(chars={'_'}) +cPluginPath("tests/tnimterop_c_plugin.nim") cOverride: type diff --git a/tests/tnimterop_c_plugin.nim b/tests/tnimterop_c_plugin.nim new file mode 100644 index 0000000..68bb4d6 --- /dev/null +++ b/tests/tnimterop_c_plugin.nim @@ -0,0 +1,7 @@ +import strutils + +proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} = + if sym.name == "_Kernel": + sym.name = "uKernel" + else: + sym.name = sym.name.strip(chars={'_'})
\ No newline at end of file |
