nim.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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, options, msgs,
  20. extccomp, strutils, os, main, parseopt,
  21. idents, 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 addCmdPrefix*(result: var string, kind: CmdLineKind) =
  36. # consider moving this to std/parseopt
  37. case kind
  38. of cmdLongOption: result.add "--"
  39. of cmdShortOption: result.add "-"
  40. of cmdArgument, cmdEnd: discard
  41. proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
  42. var p = parseopt.initOptParser(cmd)
  43. var argsCount = 0
  44. config.commandLine.setLen 0
  45. # bugfix: otherwise, config.commandLine ends up duplicated
  46. while true:
  47. parseopt.next(p)
  48. case p.kind
  49. of cmdEnd: break
  50. of cmdLongOption, cmdShortOption:
  51. config.commandLine.add " "
  52. config.commandLine.addCmdPrefix p.kind
  53. config.commandLine.add p.key.quoteShell # quoteShell to be future proof
  54. if p.val.len > 0:
  55. config.commandLine.add ':'
  56. config.commandLine.add p.val.quoteShell
  57. if p.key == " ":
  58. p.key = "-"
  59. if processArgument(pass, p, argsCount, config): break
  60. else:
  61. processSwitch(pass, p, config)
  62. of cmdArgument:
  63. config.commandLine.add " "
  64. config.commandLine.add p.key.quoteShell
  65. if processArgument(pass, p, argsCount, config): break
  66. if pass == passCmd2:
  67. if {optRun, optWasNimscript} * config.globalOptions == {} and
  68. config.arguments.len > 0 and config.command.normalize notin ["run", "e"]:
  69. rawMessage(config, errGenerated, errArgsNeedRunOption)
  70. proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
  71. let self = NimProg(
  72. supportsStdinFile: true,
  73. processCmdLine: processCmdLine,
  74. mainCommand: mainCommand
  75. )
  76. self.initDefinesProg(conf, "nim_compiler")
  77. if paramCount() == 0:
  78. writeCommandLineUsage(conf)
  79. return
  80. self.processCmdLineAndProjectPath(conf)
  81. if not self.loadConfigsAndRunMainCommand(cache, conf): return
  82. if optHints in conf.options and hintGCStats in conf.notes: echo(GC_getStatistics())
  83. #echo(GC_getStatistics())
  84. if conf.errorCounter != 0: return
  85. when hasTinyCBackend:
  86. if conf.cmd == cmdRun:
  87. tccgen.run(conf.arguments)
  88. if optRun in conf.globalOptions:
  89. var ex = quoteShell conf.absOutFile
  90. if conf.cmd == cmdCompileToJS:
  91. execExternalProgram(conf, findNodeJs() & " " & ex & ' ' & conf.arguments)
  92. else:
  93. execExternalProgram(conf, ex & ' ' & conf.arguments)
  94. when declared(GC_setMaxPause):
  95. GC_setMaxPause 2_000
  96. when compileOption("gc", "v2") or compileOption("gc", "refc"):
  97. # the new correct mark&sweet collector is too slow :-/
  98. GC_disableMarkAndSweep()
  99. when not defined(selftest):
  100. let conf = newConfigRef()
  101. handleCmdLine(newIdentCache(), conf)
  102. when declared(GC_setMaxPause):
  103. echo GC_getStatistics()
  104. msgQuit(int8(conf.errorCounter > 0))