Use g/decho in build

This commit is contained in:
Ganesh Viswanathan 2020-06-24 13:17:25 -05:00
commit a0413f1595
11 changed files with 135 additions and 66 deletions

View file

@ -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:

View file

@ -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`
)

View file

@ -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)

62
nimterop/build/misc.nim Normal file
View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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:

View file

@ -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() <cimport.html#cOverride.m>`_, 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)

View file

@ -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)

View file

@ -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

View file

@ -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()