Style fixes

This commit is contained in:
Ganesh Viswanathan 2018-06-11 01:47:08 -05:00
commit 7fbc8fbcde
3 changed files with 561 additions and 546 deletions

View file

@ -48,6 +48,8 @@ To see examples of nimgen in action check out the following wrappers:-
* download header file * download header file
* [nimkerberos](https://github.com/genotrance/nimkerberos) - WinKerberos wrapper: [docs](http://nimgen.genotrance.com/nimkerberos) * [nimkerberos](https://github.com/genotrance/nimkerberos) - WinKerberos wrapper: [docs](http://nimgen.genotrance.com/nimkerberos)
* git sparse checkout * git sparse checkout
* [nimpcre](https://github.com/genotrance/nimpcre) - PCRE wrapper: [docs](http://nimgen.genotrance.com/nimpcre)
* git checkout
* [nimrax](https://github.com/genotrance/nimrax) - Radix tree wrapper: [docs](http://nimgen.genotrance.com/nimrax) * [nimrax](https://github.com/genotrance/nimrax) - Radix tree wrapper: [docs](http://nimgen.genotrance.com/nimrax)
* git checkout * git checkout
* [nimssl](https://github.com/genotrance/nimssl) - OpenSSL wrapper: [docs](http://nimgen.genotrance.com/nimssl) * [nimssl](https://github.com/genotrance/nimssl) - OpenSSL wrapper: [docs](http://nimgen.genotrance.com/nimssl)

View file

@ -1,26 +1,17 @@
import nre import nre, os, ospaths, osproc, parsecfg, pegs, ropes, sequtils, streams, strutils, tables
import os
import ospaths
import osproc
import parsecfg
import pegs
import ropes
import sequtils
import streams
import strutils
import tables
var DONE: seq[string] = @[] var
var DONE_INLINE: seq[string] = @[] DONE: seq[string] = @[]
DONE_INLINE: seq[string] = @[]
var CONFIG: Config CONFIG: Config
var FILTER = "" FILTER = ""
var QUOTES = true QUOTES = true
var OUTPUT = "" OUTPUT = ""
var INCLUDES: seq[string] = @[] INCLUDES: seq[string] = @[]
var EXCLUDES: seq[string] = @[] EXCLUDES: seq[string] = @[]
var RENAMES = initTable[string, string]() RENAMES = initTable[string, string]()
var WILDCARDS = newConfig() WILDCARDS = newConfig()
const DOC = """ const DOC = """
Nimgen is a helper for c2nim to simpilfy and automate the wrapping of C libraries Nimgen is a helper for c2nim to simpilfy and automate the wrapping of C libraries
@ -36,11 +27,13 @@ Options:
# Helpers # Helpers
proc execProc(cmd: string): string = proc execProc(cmd: string): string =
var p = startProcess(cmd, options = {poStdErrToStdOut, poUsePath, poEvalCommand})
result = "" result = ""
var outp = outputStream(p) var
var line = newStringOfCap(120).TaintedString p = startProcess(cmd, options = {poStdErrToStdOut, poUsePath, poEvalCommand})
outp = outputStream(p)
line = newStringOfCap(120).TaintedString
while true: while true:
if outp.readLine(line): if outp.readLine(line):
result.add(line) result.add(line)
@ -66,8 +59,9 @@ proc extractZip(zipfile: string) =
discard execProc(cmd % zipfile) discard execProc(cmd % zipfile)
proc downloadUrl(url: string) = proc downloadUrl(url: string) =
let file = url.extractFilename() let
let ext = file.splitFile().ext.toLowerAscii() file = url.extractFilename()
ext = file.splitFile().ext.toLowerAscii()
var cmd = "curl $# -o $#" var cmd = "curl $# -o $#"
if defined(Windows): if defined(Windows):
@ -239,11 +233,15 @@ proc freplace(file: string, pattern: Regex, repl="") =
content = content.replace(pattern, repl) content = content.replace(pattern, repl)
proc comment(file: string, pattern: string, numlines: string) = proc comment(file: string, pattern: string, numlines: string) =
let ext = file.splitFile().ext.toLowerAscii() let
let cmtchar = if ext == ".nim": "#" else: "//" ext = file.splitFile().ext.toLowerAscii()
cmtchar = if ext == ".nim": "#" else: "//"
withFile(file): withFile(file):
var idx = content.find(pattern) var
var num = 0 idx = content.find(pattern)
num = 0
try: try:
num = numlines.parseInt() num = numlines.parseInt()
except ValueError: except ValueError:
@ -351,10 +349,10 @@ proc getDefines(file: string, inline=false): string =
result &= def.captures[0] & "\n" result &= def.captures[0] & "\n"
proc runPreprocess(file, ppflags, flags: string, inline: bool): string = proc runPreprocess(file, ppflags, flags: string, inline: bool): string =
var pproc = "gcc" var
if flags.contains("cpp"): pproc = if flags.contains("cpp"): "g++" else: "gcc"
pproc = "g++" cmd = "$# -E $# $#" % [pproc, ppflags, file]
var cmd = "$# -E $# $#" % [pproc, ppflags, file]
for inc in INCLUDES: for inc in INCLUDES:
cmd &= " -I " & inc cmd &= " -I " & inc
@ -362,9 +360,11 @@ proc runPreprocess(file, ppflags, flags: string, inline: bool): string =
var data = execProc(cmd) var data = execProc(cmd)
# Include content only from file # Include content only from file
var rdata: Rope var
var start = false rdata: Rope
var sfile = file.replace("\\", "/") start = false
sfile = file.replace("\\", "/")
if inline: if inline:
sfile = sfile.parentDir() sfile = sfile.parentDir()
for line in data.splitLines(): for line in data.splitLines():
@ -389,10 +389,11 @@ proc runPreprocess(file, ppflags, flags: string, inline: bool): string =
return $rdata return $rdata
proc runCtags(file: string): string = proc runCtags(file: string): string =
var cmd = "ctags -o - --fields=+S+K --c-kinds=p --file-scope=no " & file var
var fps = execProc(cmd) cmd = "ctags -o - --fields=+S+K --c-kinds=p --file-scope=no " & file
fps = execProc(cmd)
fdata = ""
var fdata = ""
for line in fps.splitLines(): for line in fps.splitLines():
var spl = line.split(re"\t") var spl = line.split(re"\t")
if spl.len() > 4: if spl.len() > 4:
@ -449,10 +450,11 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, inline, preprocess, cta
if defines and (preprocess or ctags): if defines and (preprocess or ctags):
prepend(cfile, getDefines(file, inline)) prepend(cfile, getDefines(file, inline))
var extflags = "" var
var passC = "" extflags = ""
var outlib = "" passC = ""
var outpragma = "" outlib = ""
outpragma = ""
passC = "import strutils\n" passC = "import strutils\n"
for inc in INCLUDES: for inc in INCLUDES:
@ -463,13 +465,17 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, inline, preprocess, cta
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
let lin = "when defined(Linux):\n" win = "when defined(Windows):\n"
let osx = "when defined(MacOSX):\n" lin = "when defined(Linux):\n"
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] let
let ext = dl.splitFile().ext lib = " const dynlib$# = \"$#\"\n" % [fname, dl]
ext = dl.splitFile().ext
if ext == ".dll": if ext == ".dll":
winlib &= lib winlib &= lib
elif ext == ".so": elif ext == ".so":
@ -534,8 +540,9 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, inline, preprocess, cta
# Processor # Processor
proc runFile(file: string, cfgin: OrderedTableRef) = proc runFile(file: string, cfgin: OrderedTableRef) =
var cfg = cfgin var
var sfile = search(file) cfg = cfgin
sfile = search(file)
for pattern in WILDCARDS.keys(): for pattern in WILDCARDS.keys():
let pat = pattern.replace(".", "\\.").replace("*", ".*").replace("?", ".?") let pat = pattern.replace(".", "\\.").replace("*", ".*").replace("?", ".?")
@ -544,10 +551,12 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
for key in WILDCARDS[pattern].keys(): for key in WILDCARDS[pattern].keys():
cfg[key & "." & pattern] = WILDCARDS[pattern][key] cfg[key & "." & pattern] = WILDCARDS[pattern][key]
var srch = "" var
var compile: seq[string] = @[] srch = ""
var dynlib: seq[string] = @[] compile: seq[string] = @[]
var pragma: seq[string] = @[] dynlib: seq[string] = @[]
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:
@ -584,14 +593,15 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
srch = act srch = act
if file.splitFile().ext != ".nim": if file.splitFile().ext != ".nim":
var recurse = false var
var inline = false recurse = false
var preprocess = false inline = false
var ctags = false preprocess = false
var defines = false ctags = false
var noprocess = false defines = false
var flags = "--stdcall" noprocess = false
var ppflags = "" flags = "--stdcall"
ppflags = ""
for act in cfg.keys(): for act in cfg.keys():
if cfg[act] == "true": if cfg[act] == "true":

View file

@ -2,8 +2,11 @@ import distros
import ospaths import ospaths
import strutils import strutils
var full = false var
var comps = @["libsvm", "nim7z", "nimarchive", "nimbass", "nimbigwig", "nimfuzz", "nimrax", "nimssl", "nimssh2"] full = false
comps = @["libsvm", "nim7z", "nimarchive", "nimbass", "nimbigwig", "nimfuzz",
"nimpcre", "nimrax", "nimssl", "nimssh2"]
if detectOs(Windows): if detectOs(Windows):
comps.add("nimkerberos") comps.add("nimkerberos")