nim.nim 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 nimbleLockExists(config: ConfigRef): bool =
  36. const nimbleLock = "nimble.lock"
  37. let pd = if not config.projectPath.isEmpty: config.projectPath else: AbsoluteDir(getCurrentDir())
  38. if optSkipParentConfigFiles notin config.globalOptions:
  39. for dir in parentDirs(pd.string, fromRoot=true, inclusive=false):
  40. if fileExists(dir / nimbleLock):
  41. return true
  42. return fileExists(pd.string / nimbleLock)
  43. proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
  44. var p = parseopt.initOptParser(cmd)
  45. var argsCount = 0
  46. config.commandLine.setLen 0
  47. # bugfix: otherwise, config.commandLine ends up duplicated
  48. while true:
  49. parseopt.next(p)
  50. case p.kind
  51. of cmdEnd: break
  52. of cmdLongOption, cmdShortOption:
  53. config.commandLine.add " "
  54. config.commandLine.addCmdPrefix p.kind
  55. config.commandLine.add p.key.quoteShell # quoteShell to be future proof
  56. if p.val.len > 0:
  57. config.commandLine.add ':'
  58. config.commandLine.add p.val.quoteShell
  59. if p.key == "": # `-` was passed to indicate main project is stdin
  60. p.key = "-"
  61. if processArgument(pass, p, argsCount, config): break
  62. else:
  63. processSwitch(pass, p, config)
  64. of cmdArgument:
  65. config.commandLine.add " "
  66. config.commandLine.add p.key.quoteShell
  67. if processArgument(pass, p, argsCount, config): break
  68. if pass == passCmd2:
  69. if {optRun, optWasNimscript} * config.globalOptions == {} and
  70. config.arguments.len > 0 and config.cmd notin {cmdTcc, cmdNimscript, cmdCrun}:
  71. rawMessage(config, errGenerated, errArgsNeedRunOption)
  72. if config.nimbleLockExists:
  73. # disable nimble path if nimble.lock is present.
  74. # see https://github.com/nim-lang/nimble/issues/1004
  75. disableNimblePath(config)
  76. proc getNimRunExe(conf: ConfigRef): string =
  77. # xxx consider defining `conf.getConfigVar("nimrun.exe")` to allow users to
  78. # customize the binary to run the command with, e.g. for custom `nodejs` or `wine`.
  79. if conf.isDefined("mingw"):
  80. if conf.isDefined("i386"): result = "wine"
  81. elif conf.isDefined("amd64"): result = "wine64"
  82. proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
  83. let self = NimProg(
  84. supportsStdinFile: true,
  85. processCmdLine: processCmdLine
  86. )
  87. self.initDefinesProg(conf, "nim_compiler")
  88. if paramCount() == 0:
  89. writeCommandLineUsage(conf)
  90. return
  91. self.processCmdLineAndProjectPath(conf)
  92. var graph = newModuleGraph(cache, conf)
  93. if not self.loadConfigsAndProcessCmdLine(cache, conf, graph):
  94. return
  95. if conf.cmd == cmdCheck and optWasNimscript notin conf.globalOptions and
  96. conf.backend == backendInvalid:
  97. conf.backend = backendC
  98. if conf.selectedGC == gcUnselected:
  99. if conf.backend in {backendC, backendCpp, backendObjc}:
  100. initOrcDefines(conf)
  101. mainCommand(graph)
  102. if conf.hasHint(hintGCStats): echo(GC_getStatistics())
  103. #echo(GC_getStatistics())
  104. if conf.errorCounter != 0: return
  105. when hasTinyCBackend:
  106. if conf.cmd == cmdTcc:
  107. tccgen.run(conf, conf.arguments)
  108. if optRun in conf.globalOptions:
  109. let output = conf.absOutFile
  110. case conf.cmd
  111. of cmdBackends, cmdTcc:
  112. let nimRunExe = getNimRunExe(conf)
  113. var cmdPrefix = ""
  114. if nimRunExe.len > 0: cmdPrefix.add nimRunExe.quoteShell
  115. case conf.backend
  116. of backendC, backendCpp, backendObjc: discard
  117. of backendJs:
  118. # D20210217T215950:here this flag is needed for node < v15.0.0, otherwise
  119. # tasyncjs_fail` would fail, refs https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode
  120. if cmdPrefix.len == 0: cmdPrefix = findNodeJs().quoteShell
  121. cmdPrefix.add " --unhandled-rejections=strict"
  122. else: doAssert false, $conf.backend
  123. if cmdPrefix.len > 0: cmdPrefix.add " "
  124. # without the `cmdPrefix.len > 0` check, on windows you'd get a cryptic:
  125. # `The parameter is incorrect`
  126. let cmd = cmdPrefix & output.quoteShell & ' ' & conf.arguments
  127. execExternalProgram(conf, cmd.strip(leading=false,trailing=true))
  128. of cmdDocLike, cmdRst2html, cmdRst2tex, cmdMd2html, cmdMd2tex: # bugfix(cmdRst2tex was missing)
  129. if conf.arguments.len > 0:
  130. # reserved for future use
  131. rawMessage(conf, errGenerated, "'$1 cannot handle arguments" % [$conf.cmd])
  132. openDefaultBrowser($output)
  133. else:
  134. # support as needed
  135. rawMessage(conf, errGenerated, "'$1 cannot handle --run" % [$conf.cmd])
  136. when declared(GC_setMaxPause):
  137. GC_setMaxPause 2_000
  138. when compileOption("gc", "refc"):
  139. # the new correct mark&sweet collector is too slow :-/
  140. GC_disableMarkAndSweep()
  141. when not defined(selftest):
  142. let conf = newConfigRef()
  143. handleCmdLine(newIdentCache(), conf)
  144. when declared(GC_setMaxPause):
  145. echo GC_getStatistics()
  146. msgQuit(int8(conf.errorCounter > 0))