nim.nim 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. import std/[os, strutils, parseopt]
  10. when defined(nimPreviewSlimSystem):
  11. import std/assertions
  12. when defined(windows) and not defined(nimKochBootstrap):
  13. # remove workaround pending bootstrap >= 1.5.1
  14. # refs https://github.com/nim-lang/Nim/issues/18334#issuecomment-867114536
  15. # alternative would be to prepend `currentSourcePath.parentDir.quoteShell`
  16. when defined(gcc):
  17. when defined(x86):
  18. {.link: "../icons/nim.res".}
  19. else:
  20. {.link: "../icons/nim_icon.o".}
  21. when defined(amd64) and defined(vcc):
  22. {.link: "../icons/nim-amd64-windows-vcc.res".}
  23. when defined(i386) and defined(vcc):
  24. {.link: "../icons/nim-i386-windows-vcc.res".}
  25. import
  26. commands, options, msgs, extccomp, main, idents, lineinfos, cmdlinehelper,
  27. pathutils, modulegraphs
  28. from browsers import openDefaultBrowser
  29. from nodejs import findNodeJs
  30. when hasTinyCBackend:
  31. import tccgen
  32. when defined(profiler) or defined(memProfiler):
  33. {.hint: "Profiling support is turned on!".}
  34. import nimprof
  35. proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
  36. var p = parseopt.initOptParser(cmd)
  37. var argsCount = 0
  38. config.commandLine.setLen 0
  39. # bugfix: otherwise, config.commandLine ends up duplicated
  40. while true:
  41. parseopt.next(p)
  42. case p.kind
  43. of cmdEnd: break
  44. of cmdLongOption, cmdShortOption:
  45. config.commandLine.add " "
  46. config.commandLine.addCmdPrefix p.kind
  47. config.commandLine.add p.key.quoteShell # quoteShell to be future proof
  48. if p.val.len > 0:
  49. config.commandLine.add ':'
  50. config.commandLine.add p.val.quoteShell
  51. if p.key == "": # `-` was passed to indicate main project is stdin
  52. p.key = "-"
  53. if processArgument(pass, p, argsCount, config): break
  54. else:
  55. processSwitch(pass, p, config)
  56. of cmdArgument:
  57. config.commandLine.add " "
  58. config.commandLine.add p.key.quoteShell
  59. if processArgument(pass, p, argsCount, config): break
  60. if pass == passCmd2:
  61. if {optRun, optWasNimscript} * config.globalOptions == {} and
  62. config.arguments.len > 0 and config.cmd notin {cmdTcc, cmdNimscript, cmdCrun}:
  63. rawMessage(config, errGenerated, errArgsNeedRunOption)
  64. proc getNimRunExe(conf: ConfigRef): string =
  65. # xxx consider defining `conf.getConfigVar("nimrun.exe")` to allow users to
  66. # customize the binary to run the command with, e.g. for custom `nodejs` or `wine`.
  67. if conf.isDefined("mingw"):
  68. if conf.isDefined("i386"): result = "wine"
  69. elif conf.isDefined("amd64"): result = "wine64"
  70. proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
  71. let self = NimProg(
  72. supportsStdinFile: true,
  73. processCmdLine: processCmdLine
  74. )
  75. self.initDefinesProg(conf, "nim_compiler")
  76. if paramCount() == 0:
  77. writeCommandLineUsage(conf)
  78. return
  79. self.processCmdLineAndProjectPath(conf)
  80. var graph = newModuleGraph(cache, conf)
  81. if not self.loadConfigsAndProcessCmdLine(cache, conf, graph):
  82. return
  83. if conf.cmd == cmdCheck and optWasNimscript notin conf.globalOptions and
  84. conf.backend == backendInvalid:
  85. conf.backend = backendC
  86. if conf.selectedGC == gcUnselected:
  87. if conf.backend in {backendC, backendCpp, backendObjc}:
  88. initOrcDefines(conf)
  89. mainCommand(graph)
  90. if conf.hasHint(hintGCStats): echo(GC_getStatistics())
  91. #echo(GC_getStatistics())
  92. if conf.errorCounter != 0: return
  93. when hasTinyCBackend:
  94. if conf.cmd == cmdTcc:
  95. tccgen.run(conf, conf.arguments)
  96. if optRun in conf.globalOptions:
  97. let output = conf.absOutFile
  98. case conf.cmd
  99. of cmdBackends, cmdTcc:
  100. let nimRunExe = getNimRunExe(conf)
  101. var cmdPrefix = ""
  102. if nimRunExe.len > 0: cmdPrefix.add nimRunExe.quoteShell
  103. case conf.backend
  104. of backendC, backendCpp, backendObjc: discard
  105. of backendJs:
  106. # D20210217T215950:here this flag is needed for node < v15.0.0, otherwise
  107. # tasyncjs_fail` would fail, refs https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode
  108. if cmdPrefix.len == 0: cmdPrefix = findNodeJs().quoteShell
  109. cmdPrefix.add " --unhandled-rejections=strict"
  110. else: doAssert false, $conf.backend
  111. if cmdPrefix.len > 0: cmdPrefix.add " "
  112. # without the `cmdPrefix.len > 0` check, on windows you'd get a cryptic:
  113. # `The parameter is incorrect`
  114. let cmd = cmdPrefix & output.quoteShell & ' ' & conf.arguments
  115. execExternalProgram(conf, cmd.strip(leading=false,trailing=true))
  116. of cmdDocLike, cmdRst2html, cmdRst2tex, cmdMd2html, cmdMd2tex: # bugfix(cmdRst2tex was missing)
  117. if conf.arguments.len > 0:
  118. # reserved for future use
  119. rawMessage(conf, errGenerated, "'$1 cannot handle arguments" % [$conf.cmd])
  120. openDefaultBrowser($output)
  121. else:
  122. # support as needed
  123. rawMessage(conf, errGenerated, "'$1 cannot handle --run" % [$conf.cmd])
  124. when declared(GC_setMaxPause):
  125. GC_setMaxPause 2_000
  126. when compileOption("gc", "refc"):
  127. # the new correct mark&sweet collector is too slow :-/
  128. GC_disableMarkAndSweep()
  129. when not defined(selftest):
  130. let conf = newConfigRef()
  131. handleCmdLine(newIdentCache(), conf)
  132. when declared(GC_setMaxPause):
  133. echo GC_getStatistics()
  134. msgQuit(int8(conf.errorCounter > 0))