nim.nim 5.7 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):
  13. when defined(gcc):
  14. when defined(x86):
  15. {.link: "../icons/nim.res".}
  16. else:
  17. {.link: "../icons/nim_icon.o".}
  18. when defined(amd64) and defined(vcc):
  19. {.link: "../icons/nim-amd64-windows-vcc.res".}
  20. when defined(i386) and defined(vcc):
  21. {.link: "../icons/nim-i386-windows-vcc.res".}
  22. import
  23. commands, options, msgs, extccomp, main, idents, lineinfos, cmdlinehelper,
  24. pathutils, modulegraphs
  25. from browsers import openDefaultBrowser
  26. from nodejs import findNodeJs
  27. when hasTinyCBackend:
  28. import tccgen
  29. when defined(profiler) or defined(memProfiler):
  30. {.hint: "Profiling support is turned on!".}
  31. import nimprof
  32. proc nimbleLockExists(config: ConfigRef): bool =
  33. const nimbleLock = "nimble.lock"
  34. let pd = if not config.projectPath.isEmpty: config.projectPath else: AbsoluteDir(getCurrentDir())
  35. if optSkipParentConfigFiles notin config.globalOptions:
  36. for dir in parentDirs(pd.string, fromRoot=true, inclusive=false):
  37. if fileExists(dir / nimbleLock):
  38. return true
  39. return fileExists(pd.string / nimbleLock)
  40. proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
  41. var p = parseopt.initOptParser(cmd)
  42. var argsCount = 0
  43. config.commandLine.setLen 0
  44. # bugfix: otherwise, config.commandLine ends up duplicated
  45. while true:
  46. parseopt.next(p)
  47. case p.kind
  48. of cmdEnd: break
  49. of cmdLongOption, cmdShortOption:
  50. config.commandLine.add " "
  51. config.commandLine.addCmdPrefix p.kind
  52. config.commandLine.add p.key.quoteShell # quoteShell to be future proof
  53. if p.val.len > 0:
  54. config.commandLine.add ':'
  55. config.commandLine.add p.val.quoteShell
  56. if p.key == "": # `-` was passed to indicate main project is stdin
  57. p.key = "-"
  58. if processArgument(pass, p, argsCount, config): break
  59. else:
  60. processSwitch(pass, p, config)
  61. of cmdArgument:
  62. config.commandLine.add " "
  63. config.commandLine.add p.key.quoteShell
  64. if processArgument(pass, p, argsCount, config): break
  65. if pass == passCmd2:
  66. if {optRun, optWasNimscript} * config.globalOptions == {} and
  67. config.arguments.len > 0 and config.cmd notin {cmdTcc, cmdNimscript, cmdCrun}:
  68. rawMessage(config, errGenerated, errArgsNeedRunOption)
  69. if config.nimbleLockExists:
  70. # disable nimble path if nimble.lock is present.
  71. # see https://github.com/nim-lang/nimble/issues/1004
  72. disableNimblePath(config)
  73. proc getNimRunExe(conf: ConfigRef): string =
  74. # xxx consider defining `conf.getConfigVar("nimrun.exe")` to allow users to
  75. # customize the binary to run the command with, e.g. for custom `nodejs` or `wine`.
  76. if conf.isDefined("mingw"):
  77. if conf.isDefined("i386"): result = "wine"
  78. elif conf.isDefined("amd64"): result = "wine64"
  79. else: result = ""
  80. else:
  81. result = ""
  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: raiseAssert $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))