nim.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. when defined(gcc) and defined(windows):
  10. when defined(x86):
  11. {.link: "icons/nim.res".}
  12. else:
  13. {.link: "icons/nim_icon.o".}
  14. when defined(amd64) and defined(windows) and defined(vcc):
  15. {.link: "icons/nim-amd64-windows-vcc.res".}
  16. when defined(i386) and defined(windows) and defined(vcc):
  17. {.link: "icons/nim-i386-windows-vcc.res".}
  18. import
  19. commands, lexer, condsyms, options, msgs, nversion, nimconf, ropes,
  20. extccomp, strutils, os, osproc, platform, main, parseopt,
  21. nodejs, scriptconfig, idents, modulegraphs, lineinfos, cmdlinehelper,
  22. pathutils
  23. when hasTinyCBackend:
  24. import tccgen
  25. when defined(profiler) or defined(memProfiler):
  26. {.hint: "Profiling support is turned on!".}
  27. import nimprof
  28. proc prependCurDir(f: AbsoluteFile): AbsoluteFile =
  29. when defined(unix):
  30. if os.isAbsolute(f.string): result = f
  31. else: result = AbsoluteFile("./" & f.string)
  32. else:
  33. result = f
  34. proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
  35. var p = parseopt.initOptParser(cmd)
  36. var argsCount = 0
  37. while true:
  38. parseopt.next(p)
  39. case p.kind
  40. of cmdEnd: break
  41. of cmdLongoption, cmdShortOption:
  42. if p.key == " ":
  43. p.key = "-"
  44. if processArgument(pass, p, argsCount, config): break
  45. else:
  46. processSwitch(pass, p, config)
  47. of cmdArgument:
  48. if processArgument(pass, p, argsCount, config): break
  49. if pass == passCmd2:
  50. if optRun notin config.globalOptions and config.arguments.len > 0 and config.command.normalize != "run":
  51. rawMessage(config, errGenerated, errArgsNeedRunOption)
  52. proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
  53. let self = NimProg(
  54. supportsStdinFile: true,
  55. processCmdLine: processCmdLine,
  56. mainCommand: mainCommand
  57. )
  58. self.initDefinesProg(conf, "nim_compiler")
  59. if paramCount() == 0:
  60. writeCommandLineUsage(conf, conf.helpWritten)
  61. return
  62. self.processCmdLineAndProjectPath(conf)
  63. if not self.loadConfigsAndRunMainCommand(cache, conf): return
  64. if optHints in conf.options and hintGCStats in conf.notes: echo(GC_getStatistics())
  65. #echo(GC_getStatistics())
  66. if conf.errorCounter != 0: return
  67. when hasTinyCBackend:
  68. if conf.cmd == cmdRun:
  69. tccgen.run(conf.arguments)
  70. if optRun in conf.globalOptions:
  71. if conf.cmd == cmdCompileToJS:
  72. var ex: string
  73. if not conf.outFile.isEmpty:
  74. ex = conf.outFile.prependCurDir.quoteShell
  75. else:
  76. ex = quoteShell(
  77. completeCFilePath(conf, changeFileExt(conf.projectFull, "js").prependCurDir))
  78. execExternalProgram(conf, findNodeJs() & " " & ex & ' ' & conf.arguments)
  79. else:
  80. var binPath: AbsoluteFile
  81. if not conf.outFile.isEmpty:
  82. # If the user specified an outFile path, use that directly.
  83. binPath = conf.outFile.prependCurDir
  84. else:
  85. # Figure out ourselves a valid binary name.
  86. binPath = changeFileExt(conf.projectFull, ExeExt).prependCurDir
  87. var ex = quoteShell(binPath)
  88. execExternalProgram(conf, ex & ' ' & conf.arguments)
  89. when declared(GC_setMaxPause):
  90. GC_setMaxPause 2_000
  91. when compileOption("gc", "v2") or compileOption("gc", "refc"):
  92. # the new correct mark&sweet collector is too slow :-/
  93. GC_disableMarkAndSweep()
  94. when not defined(selftest):
  95. let conf = newConfigRef()
  96. handleCmdLine(newIdentCache(), conf)
  97. when declared(GC_setMaxPause):
  98. echo GC_getStatistics()
  99. msgQuit(int8(conf.errorCounter > 0))