Fix #160 - toast should check wrapper

This commit is contained in:
Ganesh Viswanathan 2019-12-19 21:20:27 -06:00
commit ea6785500a
4 changed files with 47 additions and 13 deletions

View file

@ -96,6 +96,7 @@ Usage:
Options(opt-arg sep :|=|spc):
-h, --help print this cligen-erated help
--help-syntax advanced: prepend,plurals,..
-k, --check bool false check generated wrapper with compiler
-d, --debug bool false enable debug output
-D=, --defines= strings {} definitions to pass to preprocessor
-l=, --dynlib= string "" Import symbols from library in specified Nim string
@ -103,6 +104,7 @@ Options(opt-arg sep :|=|spc):
-m=, --mode= string "cpp" language parser: c or cpp
--nim= string "nim" use a particular Nim executable (default: $PATH/nim)
-c, --nocomments bool false exclude top-level comments from output
-o=, --output= string "" file to output content - default stdout
-a, --past bool false print AST output
-g, --pgrammar bool false print grammar
--pluginSourcePath= string "" Nim file to build and load as a plugin

View file

@ -21,7 +21,7 @@ const CIMPORT {.used.} = 1
include "."/globals
import "."/[build, paths, types]
import "."/[build, compat, paths, types]
export types
proc interpPath(dir: string): string=
@ -116,12 +116,7 @@ proc getNimCheckError(output: string): tuple[tmpFile, errors: string] =
doAssert fileExists(result.tmpFile), "Failed to write to cache dir: " & result.tmpFile
let
nim =
when (NimMajor, NimMinor, NimPatch) >= (0, 19, 9):
getCurrentCompilerExe()
else:
"nim"
(check, _) = gorgeEx(&"{nim} check {result.tmpFile.sanitizePath}")
(check, _) = gorgeEx(&"{getCurrentCompilerExe()} check {result.tmpFile.sanitizePath}")
result.errors = "\n\n" & check
@ -157,8 +152,7 @@ proc getToast(fullpath: string, recurse: bool = false, dynlib: string = "",
if gStateCT.symOverride.nBl:
cmd.add &" --symOverride={gStateCT.symOverride.join(\",\")}"
when (NimMajor, NimMinor, NimPatch) >= (0, 19, 9):
cmd.add &" --nim:{getCurrentCompilerExe().sanitizePath}"
cmd.add &" --nim:{getCurrentCompilerExe().sanitizePath}"
if gStateCT.pluginSourcePath.nBl:
cmd.add &" --pluginSourcePath={gStateCT.pluginSourcePath.sanitizePath}"

View file

@ -30,3 +30,5 @@ else:
if not base.endsWith DirSep: base.add DirSep
doAssert file.startsWith base
result = file[base.len .. ^1]
proc getCurrentCompilerExe*(): string = "nim"

View file

@ -1,8 +1,8 @@
import os, strformat, strutils
import os, strformat, strutils, times
import "."/treesitter/[api, c, cpp]
import "."/[ast, globals, getters, grammar]
import "."/[ast, compat, globals, getters, grammar]
proc printLisp(gState: State, root: TSNode) =
var
@ -99,6 +99,7 @@ proc process(gState: State, path: string, astTable: AstTable) =
# CLI processing with default values
proc main(
check = false,
debug = false,
defines: seq[string] = @[],
dynlib: string = "",
@ -147,23 +148,57 @@ proc main(
if pluginSourcePath.nBl:
gState.loadPlugin(pluginSourcePath)
if output.len != 0:
doAssert reopen(stdout, output, fmWrite), "Failed to write to " & output
# Backup stdout
var
outputFile = output
outputHandle: File
stdoutBackup = stdout
# Check needs a file
if check and outputFile.len == 0:
outputFile = getTempDir() / "toast_" & ($getTime().toUnix()).addFileExt("nim")
# Redirect output to file
if outputFile.len != 0:
doAssert outputHandle.open(outputFile, fmWrite), "Failed to write to " & outputFile
stdout = outputHandle
# Process grammar into AST
let
astTable = parseGrammar()
if pgrammar:
# Print AST of grammar
astTable.printGrammar()
elif source.nBl:
# Print source after preprocess or Nim output
if gState.pnim:
printNimHeader()
for src in source:
gState.process(src.expandSymlinkAbs(), astTable)
# Restore stdout
stdout = stdoutBackup
# Print wrapper if temporarily redirected to file
if check and output.len == 0:
stdout.write outputFile.readFile()
discard outputFile.tryRemoveFile()
# Check Nim output
if gState.pnim and check:
var
(check, err) = gorgeEx(&"{getCurrentCompilerExe()} check {outputFile}")
if err == 0:
echo "# Checked wrapper successfully"
else:
doAssert err == 0, "# Nim check failed:\n\n" & check
when isMainModule:
# Setup cligen command line help and short flags
import cligen
dispatch(main, help = {
"check": "check generated wrapper with compiler",
"debug": "enable debug output",
"defines": "definitions to pass to preprocessor",
"dynlib": "Import symbols from library in specified Nim string",
@ -183,6 +218,7 @@ when isMainModule:
"suffix": "Strip suffix from identifiers",
"symOverride": "skip generating specified symbols"
}, short = {
"check": 'k',
"debug": 'd',
"defines": 'D',
"dynlib": 'l',