diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2020-01-14 14:23:05 -0600 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2020-01-14 14:23:05 -0600 |
| commit | 359bc6e38a90696fe40b9265c62549af8200861d (patch) | |
| tree | bd61412c9f63dea2c47c4d908ce827b7cf4b8ebb | |
| parent | 972297c50957000c693a40ed9f0846ee3f79ca81 (diff) | |
| download | nimterop-359bc6e38a90696fe40b9265c62549af8200861d.tar.gz nimterop-359bc6e38a90696fe40b9265c62549af8200861d.zip | |
Fix stdout reopen issue
| -rw-r--r-- | nimterop/ast.nim | 26 | ||||
| -rw-r--r-- | nimterop/build.nim | 4 | ||||
| -rw-r--r-- | nimterop/globals.nim | 13 | ||||
| -rw-r--r-- | nimterop/grammar.nim | 4 | ||||
| -rw-r--r-- | nimterop/toast.nim | 52 |
5 files changed, 48 insertions, 51 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim index fcd9411..8ffb850 100644 --- a/nimterop/ast.nim +++ b/nimterop/ast.nim @@ -83,7 +83,7 @@ proc searchAstForNode(ast: ref Ast, node: TSNode, nimState: NimState): bool = if nimState.gState.debug: nimState.nodeBranch.add $node.tsNodeType() - echo "#" & spaces(nimState.nodeBranch.len * 2) & nimState.nodeBranch[^1] + necho "#" & spaces(nimState.nodeBranch.len * 2) & nimState.nodeBranch[^1] if ast.children.nBl: if childNames.contains(ast.regex) or @@ -111,14 +111,14 @@ proc searchAstForNode(ast: ref Ast, node: TSNode, nimState: NimState): bool = result = node.saveNodeData(nimState) else: if nimState.gState.debug: - echo "#" & spaces(nimState.nodeBranch.len * 2) & &" {ast.getRegexForAstChildren()} !=~ {childNames}" + necho "#" & spaces(nimState.nodeBranch.len * 2) & &" {ast.getRegexForAstChildren()} !=~ {childNames}" elif node.getTSNodeNamedChildCountSansComments() == 0: result = node.saveNodeData(nimState) if nimState.gState.debug: discard nimState.nodeBranch.pop() if nimstate.nodeBranch.Bl: - echo "" + necho "" proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) = var @@ -133,7 +133,7 @@ proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) = if name in astTable: for ast in astTable[name]: if nimState.gState.debug: - echo "\n# " & nimState.getNodeVal(node).replace("\n", "\n# ") & "\n" + necho "\n# " & nimState.getNodeVal(node).replace("\n", "\n# ") & "\n" if searchAstForNode(ast, node, nimState): ast.tonim(ast, node, nimState) if nimState.gState.debug: @@ -166,8 +166,8 @@ proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) = if node == root: break -proc printNimHeader*() = - echo """# Generated at $1 +proc printNimHeader*(gState: State) = + gecho """# Generated at $1 # Command line: # $2 $3 @@ -194,32 +194,32 @@ proc printNim*(gState: State, fullpath: string, root: TSNode, astTable: AstTable root.searchAst(astTable, nimState) if nimState.enumStr.nBl: - echo &"{nimState.enumStr}\n" + necho &"{nimState.enumStr}\n" nimState.constStr = nimState.getOverrideFinal(nskConst) & nimState.constStr if nimState.constStr.nBl: - echo &"const{nimState.constStr}\n" + necho &"const{nimState.constStr}\n" - echo &""" + necho &""" {{.pragma: {nimState.impShort}, importc{nimState.getHeader()}.}} {{.pragma: {nimState.impShort}C, {nimState.impShort}, cdecl{nimState.getDynlib()}.}} """ nimState.typeStr = nimState.getOverrideFinal(nskType) & nimState.typeStr if nimState.typeStr.nBl: - echo &"type{nimState.typeStr}\n" + necho &"type{nimState.typeStr}\n" nimState.procStr = nimState.getOverrideFinal(nskProc) & nimState.procStr if nimState.procStr.nBl: - echo &"{nimState.procStr}\n" + necho &"{nimState.procStr}\n" if nimState.gState.debug: if nimState.debugStr.nBl: - echo nimState.debugStr + necho nimState.debugStr if nimState.skipStr.nBl: let hash = nimState.skipStr.hash().abs() sname = getTempDir() / &"nimterop_{$hash}.h" - echo &"# Writing skipped definitions to {sname}\n" + necho &"# Writing skipped definitions to {sname}\n" writeFile(sname, nimState.skipStr) diff --git a/nimterop/build.nim b/nimterop/build.nim index 6ac21e9..4b061cc 100644 --- a/nimterop/build.nim +++ b/nimterop/build.nim @@ -462,7 +462,7 @@ proc configure*(path, check: string, flags = "") = echo "# Running autogen.sh" echo execAction( - &"cd {(path / i).parentDir().sanitizePath} && bash autogen.sh").output + &"cd {(path / i).parentDir().sanitizePath} && bash ./autogen.sh").output break @@ -479,7 +479,7 @@ proc configure*(path, check: string, flags = "") = echo "# Running configure " & flags var - cmd = &"cd {path.sanitizePath} && bash configure" + cmd = &"cd {path.sanitizePath} && bash ./configure" if flags.len != 0: cmd &= &" {flags}" diff --git a/nimterop/globals.nim b/nimterop/globals.nim index 7c512b1..a868460 100644 --- a/nimterop/globals.nim +++ b/nimterop/globals.nim @@ -62,6 +62,8 @@ type onSymbol*, onSymbolOverride*: OnSymbol onSymbolOverrideFinal*: OnSymbolOverrideFinal + outputHandle*: File + NimState {.used.} = ref object identifiers*: TableRef[string, string] @@ -94,3 +96,14 @@ const modeDefault {.used.} = $cpp # TODO: USE this everywhere relevant when not declared(CIMPORT): export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, NimState, nBl, Bl, CompileMode, modeDefault + + # Redirect output to file when required + template gecho*(args: string) {.dirty.} = + if gState.outputHandle.isNil: + echo args + else: + gState.outputHandle.writeLine(args) + + template necho*(args: string) {.dirty.} = + let gState = nimState.gState + gecho args diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index 9f50ba4..ba0e9fc 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -735,7 +735,7 @@ proc parseGrammar*(): AstTable = else: result[n].add(ast) -proc printGrammar*(astTable: AstTable) = +proc printGrammar*(gState: State, astTable: AstTable) = for name in astTable.keys(): for ast in astTable[name]: - echo ast.printAst() + gecho ast.printAst() diff --git a/nimterop/toast.nim b/nimterop/toast.nim index c26b460..76604f1 100644 --- a/nimterop/toast.nim +++ b/nimterop/toast.nim @@ -22,12 +22,12 @@ proc printLisp(gState: State, root: TSNode) = if node.tsNodeNamedChildCount() != 0: if gState.pretty: - echo "" + gecho "" nextnode = node.tsNodeNamedChild(0) depth += 1 else: if gState.pretty: - echo ")" + gecho ")" else: stdout.write ")" nextnode = node.tsNodeNextNamedSibling() @@ -39,7 +39,7 @@ proc printLisp(gState: State, root: TSNode) = if depth == -1: break if gState.pretty: - echo spaces(depth) & ")" + gecho spaces(depth) & ")" else: stdout.write ")" if node == root: @@ -95,7 +95,7 @@ proc process(gState: State, path: string, astTable: AstTable) = elif gState.pnim: gState.printNim(path, root, astTable) elif gState.preprocess: - echo gState.code + gecho gState.code # CLI processing with default values proc main( @@ -149,32 +149,23 @@ proc main( if pluginSourcePath.nBl: gState.loadPlugin(pluginSourcePath) - # Backup stdout var outputFile = output - outputHandle: File - stdoutBackup = stdout check = check or stub - # Fix output file extention - if outputFile.len != 0: + # Fix output file extention for Nim mode + if outputFile.len != 0 and pnim: if outputFile.splitFile().ext != ".nim": outputFile = outputFile & ".nim" # Check needs a file if check and outputFile.len == 0: outputFile = getTempDir() / "toast_" & ($getTime().toUnix()).addFileExt("nim") - when defined(windows): - # https://github.com/nim-lang/Nim/issues/12939 - echo &"Cannot print wrapper with check on Windows, review {outputFile}\n" # Redirect output to file if outputFile.len != 0: - when defined(windows): - doAssert stdout.reopen(outputFile, fmWrite), &"Failed to write to {outputFile}" - else: - doAssert outputHandle.open(outputFile, fmWrite), &"Failed to write to {outputFile}" - stdout = outputHandle + doAssert gState.outputHandle.open(outputFile, fmWrite), + &"Failed to write to {outputFile}" # Process grammar into AST let @@ -182,17 +173,17 @@ proc main( if pgrammar: # Print AST of grammar - astTable.printGrammar() + gState.printGrammar(astTable) elif source.nBl: # Print source after preprocess or Nim output if gState.pnim: - printNimHeader() + gState.printNimHeader() for src in source: gState.process(src.expandSymlinkAbs(), astTable) - when not defined(windows): - # Restore stdout - stdout = stdoutBackup + # Close outputFile + if outputFile.len != 0: + gState.outputHandle.close() # Check Nim output if gState.pnim and check: @@ -202,12 +193,6 @@ proc main( if err != 0: # Failed check so try stubbing if stub: - # Close output file to prepend stubs - when not defined(windows): - outputHandle.close() - else: - stdout.close() - # Find undeclared identifiers in error var data = "" @@ -234,14 +219,13 @@ proc main( # Rerun nim check on stubbed wrapper (check, err) = execCmdEx(&"{gState.nim} check {outputFile}") - doAssert err == 0, "# Nim check with stub failed:\n\n" & check + doAssert err == 0, data & "# Nim check with stub failed:\n\n" & check else: - doAssert err == 0, "# Nim check failed:\n\n" & check + doAssert err == 0, outputFile.readFile() & "# Nim check failed:\n\n" & check - when not defined(windows): - # Print wrapper if temporarily redirected to file - if check and output.len == 0: - stdout.write outputFile.readFile() + # Print wrapper if temporarily redirected to file + if check and output.len == 0: + stdout.write outputFile.readFile() when isMainModule: # Setup cligen command line help and short flags |
