nim.nim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, modulegraphs
  23. from std/browsers import openDefaultBrowser
  24. from nodejs import findNodeJs
  25. when hasTinyCBackend:
  26. import tccgen
  27. when defined(profiler) or defined(memProfiler):
  28. {.hint: "Profiling support is turned on!".}
  29. import nimprof
  30. proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
  31. var p = parseopt.initOptParser(cmd)
  32. var argsCount = 0
  33. config.commandLine.setLen 0
  34. # bugfix: otherwise, config.commandLine ends up duplicated
  35. while true:
  36. parseopt.next(p)
  37. case p.kind
  38. of cmdEnd: break
  39. of cmdLongOption, cmdShortOption:
  40. config.commandLine.add " "
  41. config.commandLine.addCmdPrefix p.kind
  42. config.commandLine.add p.key.quoteShell # quoteShell to be future proof
  43. if p.val.len > 0:
  44. config.commandLine.add ':'
  45. config.commandLine.add p.val.quoteShell
  46. if p.key == "": # `-` was passed to indicate main project is stdin
  47. p.key = "-"
  48. if processArgument(pass, p, argsCount, config): break
  49. else:
  50. processSwitch(pass, p, config)
  51. of cmdArgument:
  52. config.commandLine.add " "
  53. config.commandLine.add p.key.quoteShell
  54. if processArgument(pass, p, argsCount, config): break
  55. if pass == passCmd2:
  56. if {optRun, optWasNimscript} * config.globalOptions == {} and
  57. config.arguments.len > 0 and config.command.normalize notin ["run", "e", "r"]:
  58. rawMessage(config, errGenerated, errArgsNeedRunOption)
  59. proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
  60. let self = NimProg(
  61. supportsStdinFile: true,
  62. processCmdLine: processCmdLine
  63. )
  64. self.initDefinesProg(conf, "nim_compiler")
  65. if paramCount() == 0:
  66. writeCommandLineUsage(conf)
  67. return
  68. self.processCmdLineAndProjectPath(conf)
  69. var graph = newModuleGraph(cache, conf)
  70. if not self.loadConfigsAndProcessCmdLine(cache, conf, graph):
  71. return
  72. mainCommand(graph)
  73. if conf.hasHint(hintGCStats): echo(GC_getStatistics())
  74. #echo(GC_getStatistics())
  75. if conf.errorCounter != 0: return
  76. when hasTinyCBackend:
  77. if conf.cmd == cmdRun:
  78. tccgen.run(conf, conf.arguments)
  79. if optRun in conf.globalOptions:
  80. let output = conf.absOutFile
  81. case conf.cmd
  82. of cmdCompileToBackend:
  83. var cmdPrefix = ""
  84. case conf.backend
  85. of backendC, backendCpp, backendObjc: discard
  86. of backendJs: cmdPrefix = findNodeJs() & " "
  87. else: doAssert false, $conf.backend
  88. execExternalProgram(conf, cmdPrefix & output.quoteShell & ' ' & conf.arguments)
  89. of cmdDoc, cmdRst2html:
  90. if conf.arguments.len > 0:
  91. # reserved for future use
  92. rawMessage(conf, errGenerated, "'$1 cannot handle arguments" % [$conf.cmd])
  93. openDefaultBrowser($output)
  94. else:
  95. # support as needed
  96. rawMessage(conf, errGenerated, "'$1 cannot handle --run" % [$conf.cmd])
  97. when declared(GC_setMaxPause):
  98. GC_setMaxPause 2_000
  99. when compileOption("gc", "v2") or compileOption("gc", "refc"):
  100. # the new correct mark&sweet collector is too slow :-/
  101. GC_disableMarkAndSweep()
  102. when not defined(selftest):
  103. let conf = newConfigRef()
  104. handleCmdLine(newIdentCache(), conf)
  105. when declared(GC_setMaxPause):
  106. echo GC_getStatistics()
  107. msgQuit(int8(conf.errorCounter > 0))