Add preliminary support for removing static funcs
This commit is contained in:
parent
84894bfa0c
commit
b32d39ad08
1 changed files with 70 additions and 2 deletions
72
nimgen.nim
72
nimgen.nim
|
|
@ -25,7 +25,7 @@ var
|
||||||
type
|
type
|
||||||
c2nimConfigObj = object
|
c2nimConfigObj = object
|
||||||
flags, ppflags: string
|
flags, ppflags: string
|
||||||
recurse, inline, preprocess, ctags, defines: bool
|
recurse, inline, preprocess, ctags, defines, remove_static: bool
|
||||||
dynlib, compile, pragma: seq[string]
|
dynlib, compile, pragma: seq[string]
|
||||||
|
|
||||||
const DOC = """
|
const DOC = """
|
||||||
|
|
@ -92,7 +92,9 @@ proc execProc(cmd: string): string =
|
||||||
proc extractZip(zipfile: string) =
|
proc extractZip(zipfile: string) =
|
||||||
var cmd = "unzip -o $#"
|
var cmd = "unzip -o $#"
|
||||||
if defined(Windows):
|
if defined(Windows):
|
||||||
cmd = "powershell -nologo -noprofile -command \"& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('$#', '.'); }\""
|
cmd = "powershell -nologo -noprofile -command \"& { Add-Type -A " &
|
||||||
|
"'System.IO.Compression.FileSystem'; " &
|
||||||
|
"[IO.Compression.ZipFile]::ExtractToDirectory('$#', '.'); }\""
|
||||||
|
|
||||||
setCurrentDir(gOutput)
|
setCurrentDir(gOutput)
|
||||||
defer: setCurrentDir(gProjectDir)
|
defer: setCurrentDir(gProjectDir)
|
||||||
|
|
@ -322,6 +324,67 @@ proc comment(file: string, pattern: string, numlines: string) =
|
||||||
idx += 1
|
idx += 1
|
||||||
break
|
break
|
||||||
|
|
||||||
|
proc removeStatic(filename: string) =
|
||||||
|
|
||||||
|
if not fileExists(filename):
|
||||||
|
echo "Missing file: " & filename
|
||||||
|
return
|
||||||
|
|
||||||
|
# This function should ideally be a regex. However, I could not
|
||||||
|
# get it to work as intended with the current re implementation
|
||||||
|
# in Nim.
|
||||||
|
#
|
||||||
|
# withFile(filename):
|
||||||
|
# content = content.replacef(
|
||||||
|
# re"(static inline.*?\))([ \r\n]*?\{([ \r\n]*?.*?)*[\n\r]\})", "$1;")
|
||||||
|
# )
|
||||||
|
#
|
||||||
|
# This currently won't even run, but if the replacef function is modified
|
||||||
|
# to not have nil checks, it will run on 1/3 of the input file. Maybe there's
|
||||||
|
# a buffer length issue.
|
||||||
|
|
||||||
|
var
|
||||||
|
file = open(filename)
|
||||||
|
stack: seq[string] = @[]
|
||||||
|
foundBrace = false
|
||||||
|
foundStatic = false
|
||||||
|
writeOutput = true
|
||||||
|
output = newStringofCap(getFileSize(filename))
|
||||||
|
|
||||||
|
for line in file.lines():
|
||||||
|
var modLine = line
|
||||||
|
|
||||||
|
if not foundStatic:
|
||||||
|
writeOutput = true
|
||||||
|
if line.startswith("static inline"):
|
||||||
|
foundStatic = true
|
||||||
|
let index = modLine.find("{")
|
||||||
|
if index != -1:
|
||||||
|
foundBrace = true
|
||||||
|
modLine.setLen(index)
|
||||||
|
elif not foundBrace:
|
||||||
|
writeOutput = true
|
||||||
|
if modLine.strip().startswith("{"):
|
||||||
|
foundBrace = true
|
||||||
|
writeOutput = false
|
||||||
|
else:
|
||||||
|
if modLine.startswith("}"):
|
||||||
|
foundBrace = false
|
||||||
|
foundStatic = false
|
||||||
|
output[^1] = ';'
|
||||||
|
output &= "\n"
|
||||||
|
|
||||||
|
if writeOutput:
|
||||||
|
output &= modLine
|
||||||
|
output &= "\n"
|
||||||
|
writeOutput = false
|
||||||
|
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
var f = open(filename, fmWrite)
|
||||||
|
write(f, output)
|
||||||
|
f.close()
|
||||||
|
|
||||||
proc rename(file: string, renfile: string) =
|
proc rename(file: string, renfile: string) =
|
||||||
if file.splitFile().ext == ".nim":
|
if file.splitFile().ext == ".nim":
|
||||||
return
|
return
|
||||||
|
|
@ -519,6 +582,9 @@ proc c2nim(fl, outfile: string, c2nimConfig: c2nimConfigObj) =
|
||||||
if c2nimConfig.defines and (c2nimConfig.preprocess or c2nimConfig.ctags):
|
if c2nimConfig.defines and (c2nimConfig.preprocess or c2nimConfig.ctags):
|
||||||
prepend(cfile, getDefines(file, c2nimConfig.inline))
|
prepend(cfile, getDefines(file, c2nimConfig.inline))
|
||||||
|
|
||||||
|
if c2nimConfig.remove_static:
|
||||||
|
removeStatic(cfile)
|
||||||
|
|
||||||
var
|
var
|
||||||
extflags = ""
|
extflags = ""
|
||||||
passC = ""
|
passC = ""
|
||||||
|
|
@ -697,6 +763,8 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
|
||||||
c2nimConfig.ctags = true
|
c2nimConfig.ctags = true
|
||||||
elif action == "defines":
|
elif action == "defines":
|
||||||
c2nimConfig.defines = true
|
c2nimConfig.defines = true
|
||||||
|
elif act == "remove_static":
|
||||||
|
c2nimConfig.remove_static = true
|
||||||
elif action == "noprocess":
|
elif action == "noprocess":
|
||||||
noprocess = true
|
noprocess = true
|
||||||
elif action == "flags":
|
elif action == "flags":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue