nim.nim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. scriptconfig, idents, modulegraphs, lineinfos, cmdlinehelper,
  22. pathutils
  23. include nodejs
  24. when hasTinyCBackend:
  25. import tccgen
  26. when defined(profiler) or defined(memProfiler):
  27. {.hint: "Profiling support is turned on!".}
  28. import nimprof
  29. proc prependCurDir(f: AbsoluteFile): AbsoluteFile =
  30. when defined(unix):
  31. if os.isAbsolute(f.string): result = f
  32. else: result = AbsoluteFile("./" & f.string)
  33. else:
  34. result = f
  35. proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
  36. var p = parseopt.initOptParser(cmd)
  37. var argsCount = 0
  38. while true:
  39. parseopt.next(p)
  40. case p.kind
  41. of cmdEnd: break
  42. of cmdLongoption, cmdShortOption:
  43. if p.key == " ":
  44. p.key = "-"
  45. if processArgument(pass, p, argsCount, config): break
  46. else:
  47. processSwitch(pass, p, config)
  48. of cmdArgument:
  49. if processArgument(pass, p, argsCount, config): break
  50. if pass == passCmd2:
  51. if {optRun, optWasNimscript} * config.globalOptions == {} and
  52. config.arguments.len > 0 and config.command.normalize notin ["run", "e"]:
  53. rawMessage(config, errGenerated, errArgsNeedRunOption)
  54. proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
  55. let self = NimProg(
  56. supportsStdinFile: true,
  57. processCmdLine: processCmdLine,
  58. mainCommand: mainCommand
  59. )
  60. self.initDefinesProg(conf, "nim_compiler")
  61. if paramCount() == 0:
  62. writeCommandLineUsage(conf)
  63. return
  64. self.processCmdLineAndProjectPath(conf)
  65. if not self.loadConfigsAndRunMainCommand(cache, conf): return
  66. if optHints in conf.options and hintGCStats in conf.notes: echo(GC_getStatistics())
  67. #echo(GC_getStatistics())
  68. if conf.errorCounter != 0: return
  69. when hasTinyCBackend:
  70. if conf.cmd == cmdRun:
  71. tccgen.run(conf.arguments)
  72. if optRun in conf.globalOptions:
  73. var ex = quoteShell conf.absOutFile
  74. if conf.cmd == cmdCompileToJS:
  75. execExternalProgram(conf, findNodeJs() & " " & ex & ' ' & conf.arguments)
  76. else:
  77. execExternalProgram(conf, ex & ' ' & conf.arguments)
  78. when declared(GC_setMaxPause):
  79. GC_setMaxPause 2_000
  80. when compileOption("gc", "v2") or compileOption("gc", "refc"):
  81. # the new correct mark&sweet collector is too slow :-/
  82. GC_disableMarkAndSweep()
  83. when not defined(selftest):
  84. let conf = newConfigRef()
  85. handleCmdLine(newIdentCache(), conf)
  86. when declared(GC_setMaxPause):
  87. echo GC_getStatistics()
  88. msgQuit(int8(conf.errorCounter > 0))