- Recurse only quoted in #include

- Unique header/dynlib naming
This commit is contained in:
Ganesh Viswanathan 2017-11-14 22:34:12 -06:00
commit e5eb693d2e
2 changed files with 19 additions and 10 deletions

View file

@ -40,6 +40,8 @@ _[n.global]_
output = name of the Nimble project once installed, also location to place generated .nim files output = name of the Nimble project once installed, also location to place generated .nim files
quotes = pick up any headers included using "" (and not <> which is typically used for standard headers) [default: true]
filter = string to identify and recurse into library .h files in #include statements and exclude standard headers filter = string to identify and recurse into library .h files in #include statements and exclude standard headers
_[n.include]_ _[n.include]_

View file

@ -13,6 +13,7 @@ var DONE: seq[string] = @[]
var CONFIG: Config var CONFIG: Config
var FILTER = "" var FILTER = ""
var QUOTES = true
var OUTPUT = "" var OUTPUT = ""
var INCLUDES: seq[string] = @[] var INCLUDES: seq[string] = @[]
var EXCLUDES: seq[string] = @[] var EXCLUDES: seq[string] = @[]
@ -165,9 +166,9 @@ proc getincls(file: string): seq[string] =
result = @[] result = @[]
withFile(file): withFile(file):
for f in content.findIter(re"(?m)^\s*#\s*include\s+(.*?)$"): for f in content.findIter(re"(?m)^\s*#\s*include\s+(.*?)$"):
var inc = f.captures[0].replace(re"""[<>"]""", "").strip() var inc = f.captures[0].strip()
if FILTER in inc and (not exclude(inc)): if ((QUOTES and inc.contains("\"")) or (FILTER != "" and FILTER in inc)) and (not exclude(inc)):
result.add(inc) result.add(inc.replace(re"""[<>"]""", "").strip())
result = result.deduplicate() result = result.deduplicate()
@ -266,18 +267,20 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, preproc, ctag, define:
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"
let fname = file.splitFile().name
if dynlib.len() != 0: if dynlib.len() != 0:
let win = "when defined(Windows):\n" let win = "when defined(Windows):\n"
let lin = "when defined(Linux):\n" let lin = "when defined(Linux):\n"
let osx = "when defined(MacOSX):\n" let osx = "when defined(MacOSX):\n"
var winlib, linlib, osxlib: string = "" var winlib, linlib, osxlib: string = ""
for dl in dynlib: for dl in dynlib:
let lib = " const dynlib$# = \"$#\"\n" % [fname, dl]
if dl.splitFile().ext == ".dll": if dl.splitFile().ext == ".dll":
winlib &= " const dynlib$# = \"$#\"\n" % [OUTPUT, dl] winlib &= lib
if dl.splitFile().ext == ".so": if dl.splitFile().ext == ".so":
linlib &= " const dynlib$# = \"$#\"\n" % [OUTPUT, dl] linlib &= lib
if dl.splitFile().ext == ".dylib": if dl.splitFile().ext == ".dylib":
osxlib &= " const dynlib$# = \"$#\"\n" % [OUTPUT, dl] osxlib &= lib
if winlib != "": if winlib != "":
outlib &= win & winlib & "\n" outlib &= win & winlib & "\n"
@ -287,10 +290,10 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, preproc, ctag, define:
outlib &= osx & osxlib & "\n" outlib &= osx & osxlib & "\n"
if outlib != "": if outlib != "":
extflags &= " --dynlib:dynlib$#" % OUTPUT extflags &= " --dynlib:dynlib$#" % fname
else: else:
passC &= "const header$# = \"$#\"\n" % [OUTPUT, fl] passC &= "const header$# = \"$#\"\n" % [fname, fl]
extflags = "--header:header$#" % OUTPUT extflags = "--header:header$#" % fname
# Run c2nim on generated file # Run c2nim on generated file
var cmd = "c2nim $# $# --out:$# $#" % [flags, extflags, outfile, cfile] var cmd = "c2nim $# $# --out:$# $#" % [flags, extflags, outfile, cfile]
@ -342,6 +345,9 @@ proc runcfg(cfg: string) =
OUTPUT = CONFIG["n.global"]["output"] OUTPUT = CONFIG["n.global"]["output"]
if CONFIG["n.global"].hasKey("filter"): if CONFIG["n.global"].hasKey("filter"):
FILTER = CONFIG["n.global"]["filter"] FILTER = CONFIG["n.global"]["filter"]
if CONFIG["n.global"].hasKey("quotes"):
if CONFIG["n.global"]["quotes"] == "false":
QUOTES = false
if CONFIG.hasKey("n.include"): if CONFIG.hasKey("n.include"):
for inc in CONFIG["n.include"].keys(): for inc in CONFIG["n.include"].keys():
@ -364,6 +370,7 @@ proc runcfg(cfg: string) =
for act in CONFIG[file].keys(): for act in CONFIG[file].keys():
action = act.replace(re"\..*", "") action = act.replace(re"\..*", "")
if action == "create": if action == "create":
createDir(file.splitPath().head)
writeFile(file, CONFIG[file][act]) writeFile(file, CONFIG[file][act])
elif action in @["prepend", "append", "replace", "compile", "dynlib"] and sfile != "": elif action in @["prepend", "append", "replace", "compile", "dynlib"] and sfile != "":
if action == "prepend": if action == "prepend":