nim.nim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. 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. mainCommand: mainCommand
  64. )
  65. self.initDefinesProg(conf, "nim_compiler")
  66. if paramCount() == 0:
  67. writeCommandLineUsage(conf)
  68. return
  69. self.processCmdLineAndProjectPath(conf)
  70. if not self.loadConfigsAndRunMainCommand(cache, conf): return
  71. if conf.hasHint(hintGCStats): echo(GC_getStatistics())
  72. #echo(GC_getStatistics())
  73. if conf.errorCounter != 0: return
  74. when hasTinyCBackend:
  75. if conf.cmd == cmdRun:
  76. tccgen.run(conf, conf.arguments)
  77. if optRun in conf.globalOptions:
  78. let output = conf.absOutFile
  79. case conf.cmd
  80. of cmdCompileToBackend:
  81. var cmdPrefix = ""
  82. case conf.backend
  83. of backendC, backendCpp, backendObjc: discard
  84. of backendJs: cmdPrefix = findNodeJs() & " "
  85. else: doAssert false, $conf.backend
  86. execExternalProgram(conf, cmdPrefix & output.quoteShell & ' ' & conf.arguments)
  87. of cmdDoc, cmdRst2html:
  88. if conf.arguments.len > 0:
  89. # reserved for future use
  90. rawMessage(conf, errGenerated, "'$1 cannot handle arguments" % [$conf.cmd])
  91. openDefaultBrowser($output)
  92. else:
  93. # support as needed
  94. rawMessage(conf, errGenerated, "'$1 cannot handle --run" % [$conf.cmd])
  95. when declared(GC_setMaxPause):
  96. GC_setMaxPause 2_000
  97. when compileOption("gc", "v2") or compileOption("gc", "refc"):
  98. # the new correct mark&sweet collector is too slow :-/
  99. GC_disableMarkAndSweep()
  100. when not defined(selftest):
  101. let conf = newConfigRef()
  102. handleCmdLine(newIdentCache(), conf)
  103. when declared(GC_setMaxPause):
  104. echo GC_getStatistics()
  105. msgQuit(int8(conf.errorCounter > 0))