gorgeimpl.nim 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, os, osproc, streams, options,
  11. lineinfos, pathutils
  12. when defined(nimPreviewSlimSystem):
  13. import std/syncio
  14. import ../dist/checksums/src/checksums/sha1
  15. proc readOutput(p: Process): (string, int) =
  16. result[0] = ""
  17. var output = p.outputStream
  18. while not output.atEnd:
  19. result[0].add(output.readLine)
  20. result[0].add("\n")
  21. if result[0].len > 0:
  22. result[0].setLen(result[0].len - "\n".len)
  23. result[1] = p.waitForExit
  24. proc opGorge*(cmd, input, cache: string, info: TLineInfo; conf: ConfigRef): (string, int) =
  25. let workingDir = parentDir(toFullPath(conf, info))
  26. if cache.len > 0:
  27. let h = secureHash(cmd & "\t" & input & "\t" & cache)
  28. let filename = toGeneratedFile(conf, AbsoluteFile("gorge_" & $h), "txt").string
  29. var f: File
  30. if optForceFullMake notin conf.globalOptions and open(f, filename):
  31. result = (f.readAll, 0)
  32. f.close
  33. return
  34. var readSuccessful = false
  35. try:
  36. var p = startProcess(cmd, workingDir,
  37. options={poEvalCommand, poStdErrToStdOut})
  38. if input.len != 0:
  39. p.inputStream.write(input)
  40. p.inputStream.close()
  41. result = p.readOutput
  42. p.close()
  43. readSuccessful = true
  44. # only cache successful runs:
  45. if result[1] == 0:
  46. writeFile(filename, result[0])
  47. except IOError, OSError:
  48. if not readSuccessful:
  49. when defined(nimLegacyGorgeErrors):
  50. result = ("", -1)
  51. else:
  52. result = ("Error running startProcess: " & getCurrentExceptionMsg(), -1)
  53. else:
  54. try:
  55. var p = startProcess(cmd, workingDir,
  56. options={poEvalCommand, poStdErrToStdOut})
  57. if input.len != 0:
  58. p.inputStream.write(input)
  59. p.inputStream.close()
  60. result = p.readOutput
  61. p.close()
  62. except IOError, OSError:
  63. when defined(nimLegacyGorgeErrors):
  64. result = ("", -1)
  65. else:
  66. result = ("Error running startProcess: " & getCurrentExceptionMsg(), -1)