nim.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, optWasNimscript} * config.globalOptions == {} and
  51. config.arguments.len > 0 and config.command.normalize notin ["run", "e"]:
  52. rawMessage(config, errGenerated, errArgsNeedRunOption)
  53. proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
  54. let self = NimProg(
  55. supportsStdinFile: true,
  56. processCmdLine: processCmdLine,
  57. mainCommand: mainCommand
  58. )
  59. self.initDefinesProg(conf, "nim_compiler")
  60. if paramCount() == 0:
  61. writeCommandLineUsage(conf, conf.helpWritten)
  62. return
  63. self.processCmdLineAndProjectPath(conf)
  64. if not self.loadConfigsAndRunMainCommand(cache, conf): return
  65. if optHints in conf.options and hintGCStats in conf.notes: echo(GC_getStatistics())
  66. #echo(GC_getStatistics())
  67. if conf.errorCounter != 0: return
  68. when hasTinyCBackend:
  69. if conf.cmd == cmdRun:
  70. tccgen.run(conf.arguments)
  71. if optRun in conf.globalOptions:
  72. if conf.cmd == cmdCompileToJS:
  73. var ex: string
  74. if not conf.outFile.isEmpty:
  75. ex = conf.outFile.prependCurDir.quoteShell
  76. else:
  77. ex = quoteShell(
  78. completeCFilePath(conf, changeFileExt(conf.projectFull, "js").prependCurDir))
  79. execExternalProgram(conf, findNodeJs() & " " & ex & ' ' & conf.arguments)
  80. else:
  81. var binPath: AbsoluteFile
  82. if not conf.outFile.isEmpty:
  83. # If the user specified an outFile path, use that directly.
  84. binPath = conf.outFile.prependCurDir
  85. else:
  86. # Figure out ourselves a valid binary name.
  87. binPath = changeFileExt(conf.projectFull, ExeExt).prependCurDir
  88. var ex = quoteShell(binPath)
  89. execExternalProgram(conf, ex & ' ' & conf.arguments)
  90. when declared(GC_setMaxPause):
  91. GC_setMaxPause 2_000
  92. when compileOption("gc", "v2") or compileOption("gc", "refc"):
  93. # the new correct mark&sweet collector is too slow :-/
  94. GC_disableMarkAndSweep()
  95. when not defined(selftest):
  96. let conf = newConfigRef()
  97. handleCmdLine(newIdentCache(), conf)
  98. when declared(GC_setMaxPause):
  99. echo GC_getStatistics()
  100. msgQuit(int8(conf.errorCounter > 0))