gorgeimpl.nim 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Module that implements ``gorge`` for the compiler.
  10. import msgs, std / sha1, os, osproc, streams, options,
  11. lineinfos, pathutils
  12. when defined(nimPreviewSlimSystem):
  13. import std/syncio
  14. proc readOutput(p: Process): (string, int) =
  15. result[0] = ""
  16. var output = p.outputStream
  17. while not output.atEnd:
  18. result[0].add(output.readLine)
  19. result[0].add("\n")
  20. if result[0].len > 0:
  21. result[0].setLen(result[0].len - "\n".len)
  22. result[1] = p.waitForExit
  23. proc opGorge*(cmd, input, cache: string, info: TLineInfo; conf: ConfigRef): (string, int) =
  24. let workingDir = parentDir(toFullPath(conf, info))
  25. if cache.len > 0:
  26. let h = secureHash(cmd & "\t" & input & "\t" & cache)
  27. let filename = toGeneratedFile(conf, AbsoluteFile("gorge_" & $h), "txt").string
  28. var f: File
  29. if optForceFullMake notin conf.globalOptions and open(f, filename):
  30. result = (f.readAll, 0)
  31. f.close
  32. return
  33. var readSuccessful = false
  34. try:
  35. var p = startProcess(cmd, workingDir,
  36. options={poEvalCommand, poStdErrToStdOut})
  37. if input.len != 0:
  38. p.inputStream.write(input)
  39. p.inputStream.close()
  40. result = p.readOutput
  41. p.close()
  42. readSuccessful = true
  43. # only cache successful runs:
  44. if result[1] == 0:
  45. writeFile(filename, result[0])
  46. except IOError, OSError:
  47. if not readSuccessful:
  48. when defined(nimLegacyGorgeErrors):
  49. result = ("", -1)
  50. else:
  51. result = ("Error running startProcess: " & getCurrentExceptionMsg(), -1)
  52. else:
  53. try:
  54. var p = startProcess(cmd, workingDir,
  55. options={poEvalCommand, poStdErrToStdOut})
  56. if input.len != 0:
  57. p.inputStream.write(input)
  58. p.inputStream.close()
  59. result = p.readOutput
  60. p.close()
  61. except IOError, OSError:
  62. when defined(nimLegacyGorgeErrors):
  63. result = ("", -1)
  64. else:
  65. result = ("Error running startProcess: " & getCurrentExceptionMsg(), -1)