aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md6
-rw-r--r--nimgen.nimble2
-rw-r--r--src/nimgen/c2nim.nim14
-rw-r--r--src/nimgen/external.nim9
-rw-r--r--src/nimgen/fileops.nim8
-rw-r--r--src/nimgen/gencore.nim23
-rw-r--r--src/nimgen/runcfg.nim53
7 files changed, 55 insertions, 60 deletions
diff --git a/README.md b/README.md
index 5a1945d..c88af16 100644
--- a/README.md
+++ b/README.md
@@ -174,15 +174,17 @@ The following keys apply to library source code (before processing) and generate
```create``` = create a file at exact location with contents specified. File needs to be in the _[n.exclude]_ list in order to be created.
+```pipe``` = execute a command on a file and store the output of the command as the new file contents. E.g. pipe = "cat $file | grep 'static inline'"
+
```search``` = search string providing context for following prepend/append/replace directives
-```pipe``` = execute a command on a file and store the output of the command as the new file contents. Ex: pipe = "cat $file | grep 'static inline'"
+```regex``` = regex search string providing context for the following replace directive. Specify using """ to avoid regex parsing issues
```prepend``` = string value to prepend into file at beginning or before search
```append``` = string value to append into file at the end or after search
-```replace``` = string value to replace search string in file
+```replace``` = string value to replace search string in file. Regex captures can be referred to using $1, $2, etc.
```comment``` = number of lines to comment from search location
diff --git a/nimgen.nimble b/nimgen.nimble
index 3e65545..841cb09 100644
--- a/nimgen.nimble
+++ b/nimgen.nimble
@@ -1,6 +1,6 @@
# Package
-version = "0.3.0"
+version = "0.3.1"
author = "genotrance"
description = "c2nim helper to simplify and automate the wrapping of C libraries"
license = "MIT"
diff --git a/src/nimgen/c2nim.nim b/src/nimgen/c2nim.nim
index 428c767..05954e6 100644
--- a/src/nimgen/c2nim.nim
+++ b/src/nimgen/c2nim.nim
@@ -10,12 +10,7 @@ proc c2nim*(fl, outfile: string, c2nimConfig: c2nimConfigObj) =
if file.len() == 0:
return
- echo " Generating " & outfile
-
- # Remove static inline function bodies
- removeStatic(file)
-
- fixFuncProtos(file)
+ echo "Generating " & outfile
var cfile = file
if c2nimConfig.preprocess:
@@ -100,7 +95,7 @@ proc c2nim*(fl, outfile: string, c2nimConfig: c2nimConfigObj) =
discard
# Nim doesn't like {.cdecl.} for type proc()
- freplace(outfile, re"(?m)(.*? = proc.*?)\{.cdecl.\}", "$#")
+ freplace(outfile, re"(?m)(.*? = proc.*?)\{.cdecl.\}", "$1")
freplace(outfile, " {.cdecl.})", ")")
# Include {.compile.} directives
@@ -121,7 +116,4 @@ proc c2nim*(fl, outfile: string, c2nimConfig: c2nimConfigObj) =
# Add dynamic library
if outlib != "":
- prepend(outfile, outlib)
-
- # Add back static functions for compilation
- reAddStatic(file)
+ prepend(outfile, outlib) \ No newline at end of file
diff --git a/src/nimgen/external.nim b/src/nimgen/external.nim
index 9575c38..571c7e0 100644
--- a/src/nimgen/external.nim
+++ b/src/nimgen/external.nim
@@ -50,7 +50,10 @@ proc gitReset*() =
setCurrentDir(gOutput)
defer: setCurrentDir(gProjectDir)
- discard execProc("git reset --hard HEAD")
+ let cmd = "git reset --hard HEAD"
+ while execCmdEx(cmd)[0].contains("Permission denied"):
+ sleep(1000)
+ echo " Retrying ..."
proc gitCheckout*(file: string) =
echo "Resetting " & file
@@ -59,9 +62,9 @@ proc gitCheckout*(file: string) =
defer: setCurrentDir(gProjectDir)
let cmd = "git checkout $#" % file.replace(gOutput & "/", "")
- if execCmdEx(cmd)[0].contains("Permission denied"):
+ while execCmdEx(cmd)[0].contains("Permission denied"):
sleep(500)
- discard execProc(cmd)
+ echo " Retrying ..."
proc gitRemotePull*(url: string, pull=true) =
if dirExists(gOutput/".git"):
diff --git a/src/nimgen/fileops.nim b/src/nimgen/fileops.nim
index c4e67a0..4e8f858 100644
--- a/src/nimgen/fileops.nim
+++ b/src/nimgen/fileops.nim
@@ -38,13 +38,7 @@ proc freplace*(file: string, pattern: string, repl="") =
proc freplace*(file: string, pattern: Regex, repl="") =
withFile(file):
- var m: RegexMatch
- if content.find(pattern, m):
- if "$#" in repl:
- content = content.replace(pattern,
- proc (m: RegexMatch, s: string): string = repl % s[m.group(0)[0]])
- else:
- content = content.replace(pattern, repl)
+ content = content.replace(pattern, repl)
proc comment*(file: string, pattern: string, numlines: string) =
let
diff --git a/src/nimgen/gencore.nim b/src/nimgen/gencore.nim
index 382fa59..4e79d04 100644
--- a/src/nimgen/gencore.nim
+++ b/src/nimgen/gencore.nim
@@ -4,24 +4,16 @@ import file, globals
proc addEnv*(str: string): string =
var newStr = str
- for pair in envPairs():
- try:
- newStr = newStr % [pair.key, pair.value.string]
- except ValueError:
- # Ignore if there are no values to replace. We
- # want to continue anyway
- discard
- try:
+ if "$output" in newStr or "${output}" in newStr:
newStr = newStr % ["output", gOutput]
- except ValueError:
- # Ignore if there are no values to replace. We
- # want to continue anyway
- discard
- # if there are still format args, print a warning
- if newStr.contains("$") and not newStr.contains("$replace("):
- echo "WARNING: \"", newStr, "\" still contains an uninterpolated value!"
+ for pair in envPairs():
+ if pair.key.len() == 0:
+ continue
+
+ if ("$" & pair.key) in newStr or ("${" & pair.key & "}") in newStr:
+ newStr = newStr % [pair.key, pair.value.string]
return newStr
@@ -87,7 +79,6 @@ proc getDefines*(file: string, inline=false): string =
for incl in incls:
let sincl = search(incl)
if sincl != "":
- echo "Inlining " & sincl
result &= getDefines(sincl)
withFile(file):
for def in content.findAll(re"(?m)^(\s*#\s*define\s+[\w\d_]+\s+[\d\-.xf]+)(?:\r|//|/*).*?$"):
diff --git a/src/nimgen/runcfg.nim b/src/nimgen/runcfg.nim
index 95cb1c4..a0e51c5 100644
--- a/src/nimgen/runcfg.nim
+++ b/src/nimgen/runcfg.nim
@@ -44,6 +44,7 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
var
srch = ""
+ rgx = ""
c2nimConfig = c2nimConfigObj(
flags: "--stdcall", ppflags: "",
@@ -60,7 +61,7 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
writeFileFlush(file, cfg[act])
if file in gExcludes:
gExcludes.delete(gExcludes.find(file))
- sfile = search(file)
+ sfile = file
gDoneRecursive.add(sfile)
elif action in @["prepend", "append", "replace", "comment",
"rename", "compile", "dynlib", "pragma",
@@ -78,6 +79,8 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
elif action == "replace":
if srch != "":
freplace(sfile, cfg[srch], cfg[act])
+ elif rgx != "":
+ freplace(sfile, toPattern(cfg[rgx]), cfg[act])
elif action == "comment":
if srch != "":
comment(sfile, cfg[srch], cfg[act])
@@ -92,8 +95,11 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
elif action == "pipe":
pipe(sfile, cfg[act])
srch = ""
+ rgx = ""
elif action == "search":
srch = act
+ elif action == "regex":
+ rgx = act
if file.splitFile().ext != ".nim":
var
@@ -127,31 +133,38 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
echo "Cannot use recurse and inline simultaneously"
quit(1)
- if not noprocess:
- let outfile = getNimout(sfile)
- c2nim(file, outfile, c2nimConfig)
+ removeStatic(sfile)
+ fixFuncProtos(sfile)
+
+ let outfile = getNimout(sfile)
+ var incout = ""
+ if c2nimConfig.recurse or c2nimConfig.inline:
+ var
+ cfg = newOrderedTable[string, string]()
+ incls = getIncls(sfile)
- if c2nimConfig.recurse:
- var
- cfg = newOrderedTable[string, string]()
- incls = getIncls(sfile)
- incout = ""
+ for name, value in c2nimConfig.fieldPairs:
+ when value is string:
+ cfg[name] = value
+ when value is bool:
+ cfg[name] = $value
- for name, value in c2nimConfig.fieldPairs:
- when value is string:
- cfg[name] = value
- when value is bool:
- cfg[name] = $value
+ for i in c2nimConfig.dynlib:
+ cfg["dynlib." & i] = i
- for i in c2nimConfig.dynlib:
- cfg["dynlib." & i] = i
+ if c2nimConfig.inline:
+ cfg["noprocess"] = "true"
- for inc in incls:
- runFile(inc, cfg)
+ for inc in incls:
+ runFile(inc, cfg)
+ if c2nimConfig.recurse:
incout &= "import $#\n" % inc.search().getNimout()[0 .. ^5]
- if incout.len() != 0:
- prepend(outfile, incout)
+ if not noprocess:
+ c2nim(file, outfile, c2nimConfig)
+
+ if c2nimConfig.recurse and incout.len() != 0:
+ prepend(outfile, incout)
if reset:
gitCheckout(sfile)