nim.nim 4.9 KB

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