Support for pragma definitions

This commit is contained in:
Ganesh Viswanathan 2018-01-05 19:16:49 -06:00
commit 53f0626bed
3 changed files with 22 additions and 6 deletions

View file

@ -6,7 +6,7 @@ __Installation__
Nimgen can be installed via [Nimble](https://github.com/nim-lang/nimble): Nimgen can be installed via [Nimble](https://github.com/nim-lang/nimble):
```> nimble install https://github.com/genotrance/nimgen``` ```> nimble install nimgen```
This will download, build and install nimgen in the standard Nimble package location, typically ~/.nimble. Once installed, it can be run just like c2nim. This will download, build and install nimgen in the standard Nimble package location, typically ~/.nimble. Once installed, it can be run just like c2nim.
@ -40,6 +40,9 @@ To see examples of nimgen in action check out the following wrappers:-
* [nimssl](https://github.com/genotrance/nimssl) - OpenSSL wrapper * [nimssl](https://github.com/genotrance/nimssl) - OpenSSL wrapper
* git sparse checkout * git sparse checkout
* Compile C code into binary * Compile C code into binary
* [nimssh2](https://github.com/genotrance/nimssh2) - libssh2 wrapper
* git sparse checkout
* Compile libssh2 in as static binary
Nimgen only supports the ```gcc``` preprocessor at this time. Support for detecting and using other preprocessors is TBD. Nimgen only supports the ```gcc``` preprocessor at this time. Support for detecting and using other preprocessors is TBD.
@ -87,7 +90,7 @@ File wildcards such as *.nim, ssl*.h, etc. can be used to perform tasks across a
_[sourcefile]_ _[sourcefile]_
The following keys apply to library source code and help with generating the .nim files. -win, -lin and -osx can be used for OS specific tasks. E.g. dynlib-win The following keys apply to library source code and help with generating the .nim files. -win, -lin and -osx can be used for OS specific tasks. E.g. dynlib-win, pragma-win
```recurse``` = find #include files and process them [default: false] ```recurse``` = find #include files and process them [default: false]
@ -107,6 +110,8 @@ Multiple entries for the all following keys are possible by appending any .strin
```compile``` = file or dir of files of source code to {.compile.} into generated .nim ```compile``` = file or dir of files of source code to {.compile.} into generated .nim
```pragma``` = pragmas to define in generated .nim file. E.g. pragma = "passL: \"-lssl\"" => {.passL: "-lssl".}
```dynlib``` = dynamic library to load at runtime for generated .nim procs ```dynlib``` = dynamic library to load at runtime for generated .nim procs
The following keys apply to library source code (before processing) and generated .nim files (after processing) and allow manipulating the files as required to enable successful wrapping. They are not propagated to #include files when ```recurse = true```. The following keys apply to library source code (before processing) and generated .nim files (after processing) and allow manipulating the files as required to enable successful wrapping. They are not propagated to #include files when ```recurse = true```.

View file

@ -326,7 +326,7 @@ proc runCtags(file: string): string =
proc runFile(file: string, cfgin: OrderedTableRef) proc runFile(file: string, cfgin: OrderedTableRef)
proc c2nim(fl, outfile, flags, ppflags: string, recurse, preprocess, ctags, defines: bool, dynlib, compile: seq[string] = @[]) = proc c2nim(fl, outfile, flags, ppflags: string, recurse, preprocess, ctags, defines: bool, dynlib, compile, pragma: seq[string] = @[]) =
var file = search(fl) var file = search(fl)
if file == "": if file == "":
return return
@ -369,11 +369,15 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, preprocess, ctags, defi
var extflags = "" var extflags = ""
var passC = "" var passC = ""
var outlib = "" var outlib = ""
var outpragma = ""
passC = "import strutils\n" passC = "import strutils\n"
for inc in INCLUDES: for inc in INCLUDES:
passC &= ("""{.passC: "-I\"" & gorge("nimble path $#").strip() & "/$#\"".}""" % [OUTPUT, inc]) & "\n" passC &= ("""{.passC: "-I\"" & gorge("nimble path $#").strip() & "/$#\"".}""" % [OUTPUT, inc]) & "\n"
for prag in pragma:
outpragma &= "{." & prag & ".}\n"
let fname = file.splitFile().name.replace(re"[\.\-]", "_") let fname = file.splitFile().name.replace(re"[\.\-]", "_")
if dynlib.len() != 0: if dynlib.len() != 0:
let win = "when defined(Windows):\n" let win = "when defined(Windows):\n"
@ -430,6 +434,10 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, preprocess, ctags, defi
else: else:
prepend(outfile, compile(dir=cpl)) prepend(outfile, compile(dir=cpl))
# Add any pragmas
if outpragma != "":
prepend(outfile, outpragma)
# Add header file and include paths # Add header file and include paths
if passC != "": if passC != "":
prepend(outfile, passC) prepend(outfile, passC)
@ -455,13 +463,14 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
var srch = "" var srch = ""
var compile: seq[string] = @[] var compile: seq[string] = @[]
var dynlib: seq[string] = @[] var dynlib: seq[string] = @[]
var pragma: seq[string] = @[]
for act in cfg.keys(): for act in cfg.keys():
let (action, val) = getKey(act) let (action, val) = getKey(act)
if val == true: if val == true:
if action == "create": if action == "create":
createDir(file.splitPath().head) createDir(file.splitPath().head)
writeFile(file, cfg[act]) writeFile(file, cfg[act])
elif action in @["prepend", "append", "replace", "compile", "dynlib"] and sfile != "": elif action in @["prepend", "append", "replace", "compile", "dynlib", "pragma"] and sfile != "":
if action == "prepend": if action == "prepend":
if srch != "": if srch != "":
prepend(sfile, cfg[act], cfg[srch]) prepend(sfile, cfg[act], cfg[srch])
@ -479,6 +488,8 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
compile.add(cfg[act]) compile.add(cfg[act])
elif action == "dynlib": elif action == "dynlib":
dynlib.add(cfg[act]) dynlib.add(cfg[act])
elif action == "pragma":
pragma.add(cfg[act])
srch = "" srch = ""
elif action == "search": elif action == "search":
srch = act srch = act
@ -510,7 +521,7 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
ppflags = cfg[act] ppflags = cfg[act]
if not noprocess: if not noprocess:
c2nim(file, getNimout(file), flags, ppflags, recurse, preprocess, ctags, defines, dynlib, compile) c2nim(file, getNimout(file), flags, ppflags, recurse, preprocess, ctags, defines, dynlib, compile, pragma)
proc runCfg(cfg: string) = proc runCfg(cfg: string) =
if not fileExists(cfg): if not fileExists(cfg):

View file

@ -1,7 +1,7 @@
import ospaths import ospaths
import strutils import strutils
for comp in @["nimbass", "nimfuzz", "nimssl"]: for comp in @["nimbass", "nimfuzz", "nimssl", "nimssh2"]:
withDir(".."/comp): withDir(".."/comp):
exec "nimble install -y" exec "nimble install -y"
exec "nimble test" exec "nimble test"