findFile speed improvement

This commit is contained in:
Ganesh Viswanathan 2019-10-05 00:42:39 -05:00
commit e78fa71ed1
3 changed files with 52 additions and 29 deletions

View file

@ -1,4 +1,4 @@
import macros, osproc, regex, strformat, strutils, tables import macros, osproc, strformat, strutils, tables
import os except findExe, sleep import os except findExe, sleep
@ -253,31 +253,50 @@ proc gitPull*(url: string, outdir = "", plist = "", checkout = "") =
echo "# Pulling repository" echo "# Pulling repository"
discard execAction(&"cd {outdirQ} && git pull --depth=1 origin master") discard execAction(&"cd {outdirQ} && git pull --depth=1 origin master")
proc findFile*(file: string|Regex, dir: string, recurse = true, first = false): string = proc findFile*(file: string, dir: string, recurse = true, first = false, regex = false): string =
## Find the file in the specified directory ## Find the file in the specified directory
## ##
## `file` can be a string or a regex object ## `file` is a regular expression if `regex` is true
## ##
## Turn off recursive search with `recurse` and stop on first match with ## Turn off recursive search with `recurse` and stop on first match with
## `first`. Without it, the shortest match is returned. ## `first`. Without it, the shortest match is returned.
when file is Regex: var
var cmd =
rm: RegexMatch when defined(windows):
"nimgrep --filenames --oneline --nocolor $1 $2 $3"
else:
"find $3 $1 -regextype egrep -regex $2"
recursive = ""
if recurse:
when defined(windows):
recursive = "--recursive"
else: else:
when not defined(windows):
recursive = "-maxdepth 1"
if not regex:
let let
dir = dir / file.parentDir() dir = dir / file.parentDir()
file = file.extractFilename file = file.extractFilename
for f in walkDirRec(dir, yieldFilter = {pcFile, pcLinkToFile}, cmd = cmd % [recursive, (".*[\\/]" & file & "$").quoteShell, dir.sanitizePath]
followFilter = if recurse: {pcDir} else: {}):
let let
fn = f.extractFilename() (files, ret) = gorgeEx(cmd)
when file is string: if ret == 0:
if (result.len == 0 or result.len > f.len) and fn == file: for line in files.splitLines():
result = f let f =
if first: break when defined(windows):
else: if ":" in line:
if (result.len == 0 or result.len > f.len) and fn.match(file, rm): line.split(":", maxsplit = 1)[0]
else:
""
else:
line
if (f.len != 0 and result.len == 0 or result.len > f.len):
result = f result = f
if first: break if first: break
@ -442,7 +461,7 @@ proc cmake*(path, check, flags: string) =
doAssert (path / check).fileExists(), "# cmake failed" doAssert (path / check).fileExists(), "# cmake failed"
proc make*(path, check: string|Regex, flags = "") = proc make*(path, check: string, flags = "", regex = false) =
## Run the `make` command to build all binaries in the specified path ## Run the `make` command to build all binaries in the specified path
## ##
## `check` is a file that will be generated by the `make` command. ## `check` is a file that will be generated by the `make` command.
@ -451,9 +470,11 @@ proc make*(path, check: string|Regex, flags = "") =
## ##
## `flags` are any flags that should be passed to the `make` command. ## `flags` are any flags that should be passed to the `make` command.
## ##
## `regex` can be set to true if `check` is a regular expression.
##
## If `make.exe` is missing and `mingw32-make.exe` is available, it will ## If `make.exe` is missing and `mingw32-make.exe` is available, it will
## be copied over to make.exe in the same location. ## be copied over to make.exe in the same location.
if findFile(check, path).len != 0: if findFile(check, path, regex = regex).len != 0:
return return
echo "# Running make " & flags echo "# Running make " & flags
@ -474,7 +495,7 @@ proc make*(path, check: string|Regex, flags = "") =
echo execAction(cmd) echo execAction(cmd)
doAssert findFile(check, path).len != 0, "# make failed" doAssert findFile(check, path, regex = regex).len != 0, "# make failed"
proc getGccPaths*(mode = "c"): seq[string] = proc getGccPaths*(mode = "c"): seq[string] =
var var
@ -532,7 +553,7 @@ proc getStdPath(header: string): string =
proc getStdLibPath(lname: string): string = proc getStdLibPath(lname: string): string =
for lib in getGccLibPaths(): for lib in getGccLibPaths():
result = findFile(re(lname), lib, recurse = false, first = true) result = findFile(lname, lib, recurse = false, first = true, regex = true)
if result.len != 0: if result.len != 0:
break break
@ -596,7 +617,7 @@ proc buildLibrary(lname, outdir, conFlags, cmakeFlags, makeFlags: string): strin
conDepStr = "" conDepStr = ""
cmakeDeps = false cmakeDeps = false
cmakeDepStr = "" cmakeDepStr = ""
lpath = findFile(re(lname), outdir) lpath = findFile(lname, outdir, regex = true)
makeFlagsProc = &"-j {getNumProcs()} {makeFlags}" makeFlagsProc = &"-j {getNumProcs()} {makeFlags}"
made = false made = false
makePath = outdir makePath = outdir
@ -643,7 +664,7 @@ proc buildLibrary(lname, outdir, conFlags, cmakeFlags, makeFlags: string): strin
conDepStr &= "bash executable missing" conDepStr &= "bash executable missing"
if fileExists(makePath / "Makefile"): if fileExists(makePath / "Makefile"):
make(makePath, re(lname), makeFlagsProc) make(makePath, lname, makeFlagsProc, regex = true)
made = true made = true
var var
@ -656,7 +677,7 @@ proc buildLibrary(lname, outdir, conFlags, cmakeFlags, makeFlags: string): strin
error = "No build files found in " & outdir error = "No build files found in " & outdir
doAssert cmakeDeps or conDeps or made, &"\n# Build configuration failed - {error}\n" doAssert cmakeDeps or conDeps or made, &"\n# Build configuration failed - {error}\n"
result = findFile(re(lname), outdir) result = findFile(lname, outdir, regex = true)
proc getDynlibExt(): string = proc getDynlibExt(): string =
when defined(windows): when defined(windows):

View file

@ -380,9 +380,11 @@ proc getPragma*(nimState: NimState, pragmas: varargs[string]): string =
if ", cdecl" in result and dy.len != 0: if ", cdecl" in result and dy.len != 0:
result = result.replace(".}", dy & ".}") result = result.replace(".}", dy & ".}")
proc getComments*(nimState: NimState): string = proc getComments*(nimState: NimState, strip = false): string =
if not nimState.gState.nocomments and nimState.commentStr.len != 0: if not nimState.gState.nocomments and nimState.commentStr.len != 0:
result = "\n" & nimState.commentStr result = "\n" & nimState.commentStr
if strip:
result = result.replace("\n ", "\n")
nimState.commentStr = "" nimState.commentStr = ""
proc dll*(path: string): string = proc dll*(path: string): string =

View file

@ -212,7 +212,7 @@ proc initGrammar(): Grammar =
if ndname.nBl and ndname != nname: if ndname.nBl and ndname != nname:
if isEnum: if isEnum:
if nimState.addNewIdentifer(ndname): if nimState.addNewIdentifer(ndname):
nimState.enumStr &= &"{nimState.getComments()}\ntype {ndname}* = {dptr}{nname}" nimState.enumStr &= &"{nimState.getComments(true)}\ntype {ndname}* = {dptr}{nname}"
else: else:
if nimState.addNewIdentifer(ndname): if nimState.addNewIdentifer(ndname):
let let
@ -434,7 +434,7 @@ proc initGrammar(): Grammar =
nimState.getIdentifier(name, nskType) nimState.getIdentifier(name, nskType)
if nname.nBl and nimState.addNewIdentifer(nname): if nname.nBl and nimState.addNewIdentifer(nname):
nimState.enumStr &= &"{nimState.getComments()}\ndefineEnum({nname})" nimState.enumStr &= &"{nimState.getComments(true)}\ndefineEnum({nname})"
var var
i = fstart i = fstart
@ -579,9 +579,9 @@ proc initGrammar(): Grammar =
pragma = nimState.getPragma(nimState.getImportC(fname, fnname), "cdecl") pragma = nimState.getPragma(nimState.getImportC(fname, fnname), "cdecl")
if fptr.len != 0 or ftyp != "object": if fptr.len != 0 or ftyp != "object":
nimState.procStr &= &"{nimState.getComments()}\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)}{pragma}" nimState.procStr &= &"{nimState.getComments(true)}\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)}{pragma}"
else: else:
nimState.procStr &= &"{nimState.getComments()}\nproc {fnname}*({pout}){pragma}" nimState.procStr &= &"{nimState.getComments(true)}\nproc {fnname}*({pout}){pragma}"
)) ))
# // comment # // comment
@ -597,7 +597,7 @@ proc initGrammar(): Grammar =
let let
line = line.multiReplace([("//", ""), ("/*", ""), ("*/", "")]) line = line.multiReplace([("//", ""), ("/*", ""), ("*/", "")])
nimState.commentStr &= &"\n# {line.strip(leading=false)}" nimState.commentStr &= &"\n # {line.strip(leading=false)}"
)) ))
proc initRegex(ast: ref Ast) = proc initRegex(ast: ref Ast) =