added 'nim jsonscript' feature

This commit is contained in:
Andreas Rumpf 2017-09-23 16:29:05 +02:00
commit 7aad0d6544
4 changed files with 76 additions and 33 deletions

View file

@ -701,26 +701,18 @@ template tryExceptOSErrorMessage(errorPrefix: string = "", body: untyped): typed
rawMessage(errExecutionOfProgramFailed, ose.msg & " " & $ose.errorCode) rawMessage(errExecutionOfProgramFailed, ose.msg & " " & $ose.errorCode)
raise raise
proc callCCompiler*(projectfile: string) = proc execLinkCmd(linkCmd: string) =
var tryExceptOSErrorMessage("invocation of external linker program failed."):
linkCmd: string execExternalProgram(linkCmd,
if gGlobalOptions * {optCompileOnly, optGenScript} == {optCompileOnly}: if optListCmd in gGlobalOptions or gVerbosity > 1: hintExecuting else: hintLinking)
return # speed up that call if only compiling and no script shall be
# generated proc execCmdsInParallel(cmds: seq[string]; prettyCb: proc (idx: int)) =
#var c = cCompiler
var script: Rope = nil
var cmds: TStringSeq = @[]
var prettyCmds: TStringSeq = @[]
let prettyCb = proc (idx: int) =
echo prettyCmds[idx]
let runCb = proc (idx: int, p: Process) = let runCb = proc (idx: int, p: Process) =
let exitCode = p.peekExitCode let exitCode = p.peekExitCode
if exitCode != 0: if exitCode != 0:
rawMessage(errGenerated, "execution of an external compiler program '" & rawMessage(errGenerated, "execution of an external compiler program '" &
cmds[idx] & "' failed with exit code: " & $exitCode & "\n\n" & cmds[idx] & "' failed with exit code: " & $exitCode & "\n\n" &
p.outputStream.readAll.strip) p.outputStream.readAll.strip)
compileCFile(toCompile, script, cmds, prettyCmds)
if optCompileOnly notin gGlobalOptions:
if gNumberOfProcessors == 0: gNumberOfProcessors = countProcessors() if gNumberOfProcessors == 0: gNumberOfProcessors = countProcessors()
var res = 0 var res = 0
if gNumberOfProcessors <= 1: if gNumberOfProcessors <= 1:
@ -742,6 +734,22 @@ proc callCCompiler*(projectfile: string) =
if res != 0: if res != 0:
if gNumberOfProcessors <= 1: if gNumberOfProcessors <= 1:
rawMessage(errExecutionOfProgramFailed, cmds.join()) rawMessage(errExecutionOfProgramFailed, cmds.join())
proc callCCompiler*(projectfile: string) =
var
linkCmd: string
if gGlobalOptions * {optCompileOnly, optGenScript} == {optCompileOnly}:
return # speed up that call if only compiling and no script shall be
# generated
#var c = cCompiler
var script: Rope = nil
var cmds: TStringSeq = @[]
var prettyCmds: TStringSeq = @[]
let prettyCb = proc (idx: int) =
echo prettyCmds[idx]
compileCFile(toCompile, script, cmds, prettyCmds)
if optCompileOnly notin gGlobalOptions:
execCmdsInParallel(cmds, prettyCb)
if optNoLinking notin gGlobalOptions: if optNoLinking notin gGlobalOptions:
# call the linker: # call the linker:
var objfiles = "" var objfiles = ""
@ -756,9 +764,7 @@ proc callCCompiler*(projectfile: string) =
linkCmd = getLinkCmd(projectfile, objfiles) linkCmd = getLinkCmd(projectfile, objfiles)
if optCompileOnly notin gGlobalOptions: if optCompileOnly notin gGlobalOptions:
tryExceptOSErrorMessage("invocation of external linker program failed."): execLinkCmd(linkCmd)
execExternalProgram(linkCmd,
if optListCmd in gGlobalOptions or gVerbosity > 1: hintExecuting else: hintLinking)
else: else:
linkCmd = "" linkCmd = ""
if optGenScript in gGlobalOptions: if optGenScript in gGlobalOptions:
@ -766,7 +772,8 @@ proc callCCompiler*(projectfile: string) =
add(script, tnl) add(script, tnl)
generateScript(projectfile, script) generateScript(projectfile, script)
from json import escapeJson #from json import escapeJson
import json
proc writeJsonBuildInstructions*(projectfile: string) = proc writeJsonBuildInstructions*(projectfile: string) =
template lit(x: untyped) = f.write x template lit(x: untyped) = f.write x
@ -834,6 +841,34 @@ proc writeJsonBuildInstructions*(projectfile: string) =
lit "\L}\L" lit "\L}\L"
close(f) close(f)
proc runJsonBuildInstructions*(projectfile: string) =
let file = projectfile.splitFile.name
let jsonFile = toGeneratedFile(file, "json")
try:
let data = json.parseFile(jsonFile)
let toCompile = data["compile"]
doAssert toCompile.kind == JArray
var cmds: TStringSeq = @[]
var prettyCmds: TStringSeq = @[]
for c in toCompile:
doAssert c.kind == JArray
doAssert c.len >= 2
add(cmds, c[1].getStr)
let (_, name, _) = splitFile(c[0].getStr)
add(prettyCmds, "CC: " & name)
let prettyCb = proc (idx: int) =
echo prettyCmds[idx]
execCmdsInParallel(cmds, prettyCb)
let linkCmd = data["linkcmd"]
doAssert linkCmd.kind == JString
execLinkCmd(linkCmd.getStr)
except:
echo getCurrentException().getStackTrace()
quit "error evaluating JSON file: " & jsonFile
proc genMappingFiles(list: CFileList): Rope = proc genMappingFiles(list: CFileList): Rope =
for it in list: for it in list:
addf(result, "--file:r\"$1\"$N", [rope(it.cname)]) addf(result, "--file:r\"$1\"$N", [rope(it.cname)])

View file

@ -78,6 +78,10 @@ proc commandCompileToC(graph: ModuleGraph; cache: IdentCache) =
extccomp.callCCompiler(proj) extccomp.callCCompiler(proj)
extccomp.writeJsonBuildInstructions(proj) extccomp.writeJsonBuildInstructions(proj)
proc commandJsonScript(graph: ModuleGraph; cache: IdentCache) =
let proj = changeFileExt(gProjectFull, "")
extccomp.runJsonBuildInstructions(proj)
proc commandCompileToJS(graph: ModuleGraph; cache: IdentCache) = proc commandCompileToJS(graph: ModuleGraph; cache: IdentCache) =
#incl(gGlobalOptions, optSafeCode) #incl(gGlobalOptions, optSafeCode)
setTarget(osJS, cpuJS) setTarget(osJS, cpuJS)
@ -266,6 +270,9 @@ proc mainCommand*(graph: ModuleGraph; cache: IdentCache) =
of "nop", "help": of "nop", "help":
# prevent the "success" message: # prevent the "success" message:
gCmd = cmdDump gCmd = cmdDump
of "jsonscript":
gCmd = cmdJsonScript
commandJsonScript(graph, cache)
else: else:
rawMessage(errInvalidCommandX, command) rawMessage(errInvalidCommandX, command)

View file

@ -91,7 +91,8 @@ type
cmdRst2html, # convert a reStructuredText file to HTML cmdRst2html, # convert a reStructuredText file to HTML
cmdRst2tex, # convert a reStructuredText file to TeX cmdRst2tex, # convert a reStructuredText file to TeX
cmdInteractive, # start interactive session cmdInteractive, # start interactive session
cmdRun # run the project via TCC backend cmdRun, # run the project via TCC backend
cmdJsonScript # compile a .json build file
TStringSeq* = seq[string] TStringSeq* = seq[string]
TGCMode* = enum # the selected GC TGCMode* = enum # the selected GC
gcNone, gcBoehm, gcGo, gcRegions, gcMarkAndSweep, gcRefc, gcNone, gcBoehm, gcGo, gcRegions, gcMarkAndSweep, gcRefc,

View file

@ -589,7 +589,7 @@ proc cmdChangeTriggersRecompilation(old, new: TCommands): bool =
return false return false
of cmdNone, cmdDoc, cmdInterpret, cmdPretty, cmdGenDepend, cmdDump, of cmdNone, cmdDoc, cmdInterpret, cmdPretty, cmdGenDepend, cmdDump,
cmdCheck, cmdParse, cmdScan, cmdIdeTools, cmdDef, cmdCheck, cmdParse, cmdScan, cmdIdeTools, cmdDef,
cmdRst2html, cmdRst2tex, cmdInteractive, cmdRun: cmdRst2html, cmdRst2tex, cmdInteractive, cmdRun, cmdJsonScript:
discard discard
# else: trigger recompilation: # else: trigger recompilation:
result = true result = true