From a0413f1595be040c57bc3d75cea1679e5d671d71 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Wed, 24 Jun 2020 13:17:25 -0500 Subject: [PATCH] Use g/decho in build --- nimterop/build/conan.nim | 10 +++--- nimterop/build/getheader.nim | 10 +++--- nimterop/build/jbb.nim | 3 +- nimterop/build/misc.nim | 62 ++++++++++++++++++++++++++++++++++++ nimterop/build/nimconf.nim | 3 +- nimterop/build/shell.nim | 23 ++++++------- nimterop/build/tools.nim | 37 +++++++++------------ nimterop/cimport.nim | 18 +++++------ nimterop/globals.nim | 29 +++++++++++++---- nimterop/toast.nim | 3 +- nimterop/toastlib/tshelp.nim | 3 -- 11 files changed, 135 insertions(+), 66 deletions(-) create mode 100644 nimterop/build/misc.nim diff --git a/nimterop/build/conan.nim b/nimterop/build/conan.nim index 31e7f26..792de01 100644 --- a/nimterop/build/conan.nim +++ b/nimterop/build/conan.nim @@ -1,11 +1,11 @@ -import os, strformat, strutils, tables +import json, os, strformat, strutils, tables + +import ".."/globals import "."/[ccompiler, misc, nimconf, shell] when (NimMajor, NimMinor, NimPatch) < (1, 2, 0): import marshal -else: - import json type ConanPackage* = ref object @@ -158,7 +158,7 @@ proc searchConan*(name: string, version = "", user = "", channel = ""): ConanPac if channel.len != 0: query &= "/" & channel - echo &"# Searching Conan.io for latest version of {name}" + gecho &"# Searching Conan.io for latest version of {name}" let j1 = jsonGet(conanSearchUrl % ["query", query]) @@ -397,7 +397,7 @@ proc downloadConan*(pkg: ConanPackage, outdir: string, main = true) = doAssert pkg.recipes.len != 0, &"Failed to download {pkg.name} v{pkg.version} from Conan - check https://conan.io/center" - echo &"# Downloading {pkg.name} v{pkg.version} from Conan.io" + gecho &"# Downloading {pkg.name} v{pkg.version} from Conan.io" for recipe, builds in pkg.recipes: for build in builds: if pkg.bhash.len == 0 or pkg.bhash == build.bhash: diff --git a/nimterop/build/getheader.nim b/nimterop/build/getheader.nim index 8bb13fd..6972ccc 100644 --- a/nimterop/build/getheader.nim +++ b/nimterop/build/getheader.nim @@ -476,9 +476,9 @@ macro getHeader*( {.passL: `ldeps`.join(" ").} static: - echo "# Including library " & lpath + gecho "# Including library " & lpath if `ldeps`.len != 0: - echo "# Including dependencies " & `ldeps`.join(" ") + gecho "# Including dependencies " & `ldeps`.join(" ") else: const `lpath`* = when not useStd: `libdir` / lpath.extractFilename() else: lpath @@ -498,7 +498,7 @@ macro getHeader*( ldeps[i] = ldeptgt # Copy downloaded dependencies to `libdir` if copied.len != 0: - echo "# Copying dependencies: " & copied.join(" ") & "\n# to " & `libdir` + gecho "# Copying dependencies: " & copied.join(" ") & "\n# to " & `libdir` ldeps else: ldeps @@ -507,8 +507,8 @@ macro getHeader*( when not useStd: # Copy downloaded shared libraries to `libdir` if not fileExists(`lpath`) or getFileDate(lpath) != getFileDate(`lpath`): - echo "# Copying " & `lpath`.extractFilename() & " to " & `libdir` + gecho "# Copying " & `lpath`.extractFilename() & " to " & `libdir` cpFile(lpath, `lpath`) - echo "# Including library " & `lpath` + gecho "# Including library " & `lpath` ) diff --git a/nimterop/build/jbb.nim b/nimterop/build/jbb.nim index cb48a27..0fb163f 100644 --- a/nimterop/build/jbb.nim +++ b/nimterop/build/jbb.nim @@ -1,5 +1,6 @@ import json, os, strformat, strutils, tables +import ".."/globals import "."/[ccompiler, nimconf, shell] when (NimMajor, NimMinor, NimPatch) < (1, 2, 0): @@ -198,7 +199,7 @@ proc downloadJBB*(pkg: JBBPackage, outdir: string, main = true) = else: "" path = outdir / pkg.name - echo &"# Downloading {pkg.name}{vstr} from BinaryBuilder.org" + gecho &"# Downloading {pkg.name}{vstr} from BinaryBuilder.org" downloadUrl(pkg.url, path, quiet = true) pkg.findJBBLibs(path) diff --git a/nimterop/build/misc.nim b/nimterop/build/misc.nim new file mode 100644 index 0000000..c6c0e48 --- /dev/null +++ b/nimterop/build/misc.nim @@ -0,0 +1,62 @@ +import os, strutils + +when defined(Windows): + import strformat + +import ".."/globals + +proc sanitizePath*(path: string, noQuote = false, sep = $DirSep): string = + result = path.multiReplace([("\\\\", sep), ("\\", sep), ("/", sep)]) + if not noQuote: + result = result.quoteShell + +proc getCurrentNimCompiler*(): string = + when nimvm: + result = getCurrentCompilerExe() + when defined(nimsuggest): + result = result.replace("nimsuggest", "nim") + else: + result = gState.nim + +proc compareVersions*(ver1, ver2: string): int = + ## Compare two version strings x.y.z and return -1, 0, 1 + ## + ## ver1 < ver2 = -1 + ## ver1 = ver2 = 0 + ## ver1 > ver2 = 1 + let + ver1seq = ver1.replace("-", "").split('.') + ver2seq = ver2.replace("-", "").split('.') + for i in 0 ..< ver1seq.len: + let + p1 = ver1seq[i] + p2 = if i < ver2seq.len: ver2seq[i] else: "0" + + try: + let + h1 = p1.parseHexInt() + h2 = p2.parseHexInt() + + if h1 < h2: return -1 + elif h1 > h2: return 1 + except ValueError: + if p1 < p2: return -1 + elif p1 > p2: return 1 + +proc fixCmd*(cmd: string): string = + when defined(Windows): + # Replace 'cd d:\abc' with 'd: && cd d:\abc` + var filteredCmd = cmd + if cmd.toLower().startsWith("cd"): + var + colonIndex = cmd.find(":") + driveLetter = cmd.substr(colonIndex-1, colonIndex) + if (driveLetter[0].isAlphaAscii() and + driveLetter[1] == ':' and + colonIndex == 4): + filteredCmd = &"{driveLetter} && {cmd}" + result = "cmd /c " & filteredCmd + elif defined(posix): + result = cmd + else: + doAssert false diff --git a/nimterop/build/nimconf.nim b/nimterop/build/nimconf.nim index 70e47a3..3bbe521 100644 --- a/nimterop/build/nimconf.nim +++ b/nimterop/build/nimconf.nim @@ -1,5 +1,6 @@ import json, os, osproc, sets, strformat, strutils +import ".."/globals import "."/misc when nimvm: @@ -42,7 +43,7 @@ proc getJson(projectDir: string): JsonNode = try: result = parseJson(dump) except JsonParsingError as e: - echo "# Failed to parse `nim dump` output: " & e.msg + gecho "# Failed to parse `nim dump` output: " & e.msg proc getOsCacheDir(): string = # OS default cache directory diff --git a/nimterop/build/shell.nim b/nimterop/build/shell.nim index 7f84db3..a5d49de 100644 --- a/nimterop/build/shell.nim +++ b/nimterop/build/shell.nim @@ -5,6 +5,7 @@ when not defined(TOAST): else: import os +import ".."/globals import "."/[misc, nimconf] when not defined(TOAST): @@ -228,7 +229,7 @@ proc extractZip*(zipfile, outdir: string, quiet = false) = "[IO.Compression.ZipFile]::ExtractToDirectory('$#', '.'); }\"" if not quiet: - echo "# Extracting " & zipfile + gecho "# Extracting " & zipfile discard execAction(&"cd {outdir.sanitizePath} && {cmd % zipfile}") proc extractTar*(tarfile, outdir: string, quiet = false) = @@ -262,7 +263,7 @@ proc extractTar*(tarfile, outdir: string, quiet = false) = doAssert cmd.len != 0, "No extraction tool - tar, 7z, 7za - available for " & tarfile.sanitizePath if not quiet: - echo "# Extracting " & tarfile + gecho "# Extracting " & tarfile discard execAction(&"cd {outdir.sanitizePath} && {cmd}") if name.len != 0: rmFile(outdir / name) @@ -279,7 +280,7 @@ proc downloadUrl*(url, outdir: string, quiet = false, retry = 1) = if not (ext in archives and fileExists(filePath)): if not quiet: - echo "# Downloading " & file + gecho "# Downloading " & file mkDir(outdir) var cmd = findExe("curl") if cmd.len != 0: @@ -302,12 +303,12 @@ proc downloadUrl*(url, outdir: string, quiet = false, retry = 1) = proc gitReset*(outdir: string) = ## Hard reset the git repository at the specified directory - echo "# Resetting " & outdir + gecho "# Resetting " & outdir let cmd = &"cd {outdir.sanitizePath} && git reset --hard" while execAction(cmd).output.contains("Permission denied"): sleep(1000) - echo "# Retrying ..." + gecho "# Retrying ..." proc gitCheckout*(file, outdir: string) = ## Checkout the specified `file` in the git repository at `outdir` @@ -315,12 +316,12 @@ proc gitCheckout*(file, outdir: string) = ## This effectively resets all changes in the file and can be ## used to undo any changes that were made to source files to enable ## successful wrapping with `cImport()` or `c2nImport()`. - echo "# Resetting " & file + gecho "# Resetting " & file let file2 = file.relativePath outdir let cmd = &"cd {outdir.sanitizePath} && git checkout {file2.sanitizePath}" while execAction(cmd).output.contains("Permission denied"): sleep(500) - echo "# Retrying ..." + gecho "# Retrying ..." proc gitPull*(url: string, outdir = "", plist = "", checkout = "", quiet = false) = ## Pull the specified git repository to the output directory @@ -343,7 +344,7 @@ proc gitPull*(url: string, outdir = "", plist = "", checkout = "", quiet = false mkDir(outdir) if not quiet: - echo "# Setting up Git repo: " & url + gecho "# Setting up Git repo: " & url discard execAction(&"cd {outdirQ} && git init .") discard execAction(&"cd {outdirQ} && git remote add origin {url}") @@ -360,12 +361,12 @@ proc gitPull*(url: string, outdir = "", plist = "", checkout = "", quiet = false if checkout.len != 0: if not quiet: - echo "# Checking out " & checkout + gecho "# Checking out " & checkout discard execAction(&"cd {outdirQ} && git fetch", retry = 3) discard execAction(&"cd {outdirQ} && git checkout {checkout}") else: if not quiet: - echo "# Pulling repository" + gecho "# Pulling repository" discard execAction(&"cd {outdirQ} && git pull --depth=1 origin master", retry = 3) proc gitTags*(outdir: string): seq[string] = @@ -504,5 +505,5 @@ proc getProjectCacheDir*(name: string, forceClean = true): string = result = getNimteropCacheDir() / name if forceClean and compileOption("forceBuild"): - echo "# Removing " & result + gecho "# Removing " & result rmDir(result) diff --git a/nimterop/build/tools.nim b/nimterop/build/tools.nim index 5be1f4b..c164ffd 100644 --- a/nimterop/build/tools.nim +++ b/nimterop/build/tools.nim @@ -5,13 +5,6 @@ import os except findExe import ".."/globals import "."/[misc, shell] -proc echoDebug(str: string) = - let str = "\n# " & str.strip().replace("\n", "\n# ") - when defined(TOAST): - if gState.debug: echo str - else: - if gStateCT.debug: echo str - proc configure*(path, check: string, flags = "") = ## Run the GNU `configure` command to generate all Makefiles or other ## build scripts in the specified path @@ -30,18 +23,18 @@ proc configure*(path, check: string, flags = "") = if (path / check).fileExists(): return - echo "# Configuring " & path + gecho "# Configuring " & path if not fileExists(path / "configure"): for i in @["autogen.sh", "build" / "autogen.sh"]: if fileExists(path / i): - echo "# Running autogen.sh" + gecho "# Running autogen.sh" when defined(unix): - echoDebug execAction( + decho execAction( &"cd {(path / i).parentDir().sanitizePath} && ./autogen.sh").output else: - echoDebug execAction( + decho execAction( &"cd {(path / i).parentDir().sanitizePath} && bash ./autogen.sh").output break @@ -49,14 +42,14 @@ proc configure*(path, check: string, flags = "") = if not fileExists(path / "configure"): for i in @["configure.ac", "configure.in"]: if fileExists(path / i): - echo "# Running autoreconf" + gecho "# Running autoreconf" - echoDebug execAction(&"cd {path.sanitizePath} && autoreconf -fi").output + decho execAction(&"cd {path.sanitizePath} && autoreconf -fi").output break if fileExists(path / "configure"): - echo "# Running configure " & flags + gecho "# Running configure " & flags when defined(unix): var @@ -67,7 +60,7 @@ proc configure*(path, check: string, flags = "") = if flags.len != 0: cmd &= &" {flags}" - echoDebug execAction(cmd).output + decho execAction(cmd).output doAssert (path / check).fileExists(), "Configure failed" @@ -156,15 +149,15 @@ proc cmake*(path, check, flags: string) = if (path / check).fileExists(): return - echo "# Running cmake " & flags - echo "# Path: " & path + gecho "# Running cmake " & flags + gecho "# Path: " & path mkDir(path) let cmd = &"cd {path.sanitizePath} && cmake {flags}" - echoDebug execAction(cmd).output + decho execAction(cmd).output doAssert (path / check).fileExists(), "cmake failed" @@ -184,8 +177,8 @@ proc make*(path, check: string, flags = "", regex = false) = if findFile(check, path, regex = regex).len != 0: return - echo "# Running make " & flags - echo "# Path: " & path + gecho "# Running make " & flags + gecho "# Path: " & path var cmd = findExe("make") @@ -200,7 +193,7 @@ proc make*(path, check: string, flags = "", regex = false) = if flags.len != 0: cmd &= &" {flags}" - echoDebug execAction(cmd).output + decho execAction(cmd).output doAssert findFile(check, path, regex = regex).len != 0, "make failed" @@ -219,7 +212,7 @@ proc buildWithCmake*(outdir, flags: string): BuildStatus = elif uname.contains("mingw"): gen = "MinGW Makefiles".quoteShell & " -DCMAKE_SH=\"CMAKE_SH-NOTFOUND\"" else: - echo "Unsupported system: " & uname + gecho "Unsupported system: " & uname else: gen = "MinGW Makefiles".quoteShell else: diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim index 38a75e7..edfdeb9 100644 --- a/nimterop/cimport.nim +++ b/nimterop/cimport.nim @@ -246,8 +246,8 @@ proc onSymbolOverride*(sym: var Symbol) {.exportc, dynlib.} = gStateCT.symOverride.add name - if gStateCT.debug and names.nBl: - echo "# Overriding " & names.join(" ") + if names.nBl: + decho "Overriding " & names.join(" ") proc cSkipSymbol*(skips: seq[string]) {.compileTime.} = ## Similar to `cOverride() `_, this macro allows @@ -412,7 +412,7 @@ macro cDefine*(name: static string, val: static string = ""): untyped = {.passC: `str`.} if gStateCT.debug: - echo result.repr & "\n" + gecho result.repr & "\n" proc cAddSearchDir*(dir: string) {.compileTime.} = ## Add directory `dir` to the search path used in calls to @@ -442,7 +442,7 @@ macro cIncludeDir*(dir: static string): untyped = result.add quote do: {.passC: `str`.} if gStateCT.debug: - echo result.repr + gecho result.repr proc cAddStdDir*(mode = "c") {.compileTime.} = ## Add the standard `c` [default] or `cpp` include paths to search @@ -545,7 +545,7 @@ macro cCompile*(path: static string, mode = "c", exclude = ""): untyped = result.add stmt.parseStmt() if gStateCT.debug: - echo result.repr + gecho result.repr macro cImport*(filenames: static seq[string], recurse: static bool = false, dynlib: static string = "", mode: static string = "c", flags: static string = ""): untyped = @@ -565,7 +565,7 @@ macro cImport*(filenames: static seq[string], recurse: static bool = false, dynl if gStateCT.pluginSourcePath.Bl: cPluginHelper(gStateCT.pluginSource) - echo "# Importing " & fullpaths.join(", ").sanitizePath + gecho "# Importing " & fullpaths.join(", ").sanitizePath let output = getToast(fullpaths, recurse, dynlib, mode, flags) @@ -576,7 +576,7 @@ macro cImport*(filenames: static seq[string], recurse: static bool = false, dynl gStateCT.overrides = "" if gStateCT.debug: - echo output + gecho output try: let body = parseStmt(output) @@ -661,7 +661,7 @@ macro c2nImport*(filename: static string, recurse: static bool = false, dynlib: let fullpath = findPath(filename) - echo "# Importing " & fullpath & " with c2nim" + gecho "# Importing " & fullpath & " with c2nim" let output = getToast(@[fullpath], recurse, dynlib, mode, noNimout = true) @@ -700,7 +700,7 @@ macro c2nImport*(filename: static string, recurse: static bool = false, dynlib: nimout = &"const {header} = \"{fullpath}\"\n\n" & readFile(npath) if gStateCT.debug: - echo nimout + gecho nimout try: let body = parseStmt(nimout) diff --git a/nimterop/globals.nim b/nimterop/globals.nim index e8117c8..a89bac4 100644 --- a/nimterop/globals.nim +++ b/nimterop/globals.nim @@ -1,4 +1,4 @@ -import tables +import strutils, tables when defined(TOAST): import sets, sequtils, strutils @@ -124,16 +124,31 @@ when defined(TOAST): Status* = enum success, unknown, error - # Redirect output to file when required - template gecho*(args: string) = - if gState.outputHandle.isNil: +proc getCommented*(str: string): string = + "\n# " & str.strip().replace("\n", "\n# ") + +# Redirect output to file when required +template gecho*(args: string) = + when defined(TOAST): + when nimvm: echo args else: - gState.outputHandle.writeLine(args) + if gState.outputHandle.isNil: + echo args + else: + gState.outputHandle.writeLine(args) + else: + echo args - template decho*(args: varargs[string, `$`]): untyped = +template decho*(args: varargs[string, `$`]): untyped = + let + str = join(args, "").getCommented() + when defined(TOAST): if gState.debug: - gecho join(args, "").getCommented() + gecho str + else: + if gStateCT.debug: + echo str template nBl*(s: typed): untyped {.used.} = (s.len != 0) diff --git a/nimterop/toast.nim b/nimterop/toast.nim index e888e30..6fe5180 100644 --- a/nimterop/toast.nim +++ b/nimterop/toast.nim @@ -124,8 +124,7 @@ proc main( doAssert gState.outputHandle.open(outputFile, fmWrite), &"Failed to write to {outputFile}" - if gState.debug: - echo &"# Writing output to {outputFile}\n" + decho &"# Writing output to {outputFile}\n" if source.nBl: # Print source after preprocess or Nim output diff --git a/nimterop/toastlib/tshelp.nim b/nimterop/toastlib/tshelp.nim index 9310cf7..3b7c2cc 100644 --- a/nimterop/toastlib/tshelp.nim +++ b/nimterop/toastlib/tshelp.nim @@ -30,9 +30,6 @@ template withCodeAst*(code: string, mode: string, body: untyped): untyped = defer: tree.tsTreeDelete() -proc getCommented*(str: string): string = - "\n# " & str.strip().replace("\n", "\n# ") - proc isNil*(node: TSNode): bool = node.tsNodeIsNull()