Implement relative import support in header files.
You must currently replace relative imports using nimgen's commands, but relative files don't crash nimgen anymore.
This commit is contained in:
parent
77e28545ef
commit
acb07ba3e1
1 changed files with 18 additions and 3 deletions
21
nimgen.nim
21
nimgen.nim
|
|
@ -156,6 +156,9 @@ proc getKey(ukey: string): tuple[key: string, val: bool] =
|
|||
# File loction
|
||||
|
||||
proc getNimout(file: string, rename=true): string =
|
||||
if file == "":
|
||||
return ""
|
||||
|
||||
result = file.splitFile().name.replace(re"[\-\.]", "_") & ".nim"
|
||||
if gOutput != "":
|
||||
result = gOutput/result
|
||||
|
|
@ -193,6 +196,8 @@ proc search(file: string): string =
|
|||
echo "File doesn't exist: " & file
|
||||
quit(1)
|
||||
|
||||
# Only keep relative directory
|
||||
result = result.replace(gProjectDir & $DirSep, "")
|
||||
return result.replace(re"[\\/]", $DirSep)
|
||||
|
||||
# ###
|
||||
|
|
@ -342,12 +347,20 @@ proc getIncls(file: string, inline=false): seq[string] =
|
|||
if inline and file in gDoneInline:
|
||||
return
|
||||
|
||||
var curPath = splitFile(expandFileName(file))[0]
|
||||
withFile(file):
|
||||
for f in content.findIter(re"(?m)^\s*#\s*include\s+(.*?)$"):
|
||||
var inc = f.captures[0].strip()
|
||||
if ((gQuotes and inc.contains("\"")) or (gFilter != "" and gFilter in inc)) and (not exclude(inc)):
|
||||
result.add(
|
||||
inc.replace(re"""[<>"]""", "").replace(re"\/[\*\/].*$", "").strip())
|
||||
var addInc = inc.replace(re"""[<>"]""", "").replace(re"\/[\*\/].*$", "").strip()
|
||||
try:
|
||||
# Try searching for a local library
|
||||
let finc = expandFileName(curPath & "/" & addInc)
|
||||
let fname = extractFileName(finc)
|
||||
result.add(fname.search())
|
||||
except OSError:
|
||||
# If it's a system library
|
||||
result.add(addInc)
|
||||
|
||||
result = result.deduplicate()
|
||||
|
||||
|
|
@ -469,7 +482,9 @@ proc c2nim(fl, outfile: string, c2nimConfig: c2nimConfigObj) =
|
|||
|
||||
for inc in incls:
|
||||
runFile(inc, cfg)
|
||||
incout &= "import $#\n" % inc.search().getNimout()[0 .. ^5]
|
||||
let nimout = inc.search().getNimout()
|
||||
if nimout.len() > 0:
|
||||
incout &= "import $#\n" % nimout[0 .. ^5]
|
||||
|
||||
var cfile = file
|
||||
if c2nimConfig.preprocess:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue