nim.nim 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 std/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} or
  100. (conf.cmd in cmdDocLike and conf.backend != backendJs) or
  101. conf.cmd == cmdGendepend:
  102. initOrcDefines(conf)
  103. mainCommand(graph)
  104. if conf.hasHint(hintGCStats): echo(GC_getStatistics())
  105. #echo(GC_getStatistics())
  106. if conf.errorCounter != 0: return
  107. when hasTinyCBackend:
  108. if conf.cmd == cmdTcc:
  109. tccgen.run(conf, conf.arguments)
  110. if optRun in conf.globalOptions:
  111. let output = conf.absOutFile
  112. case conf.cmd
  113. of cmdBackends, cmdTcc:
  114. let nimRunExe = getNimRunExe(conf)
  115. var cmdPrefix = ""
  116. if nimRunExe.len > 0: cmdPrefix.add nimRunExe.quoteShell
  117. case conf.backend
  118. of backendC, backendCpp, backendObjc: discard
  119. of backendJs:
  120. # D20210217T215950:here this flag is needed for node < v15.0.0, otherwise
  121. # tasyncjs_fail` would fail, refs https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode
  122. if cmdPrefix.len == 0: cmdPrefix = findNodeJs().quoteShell
  123. cmdPrefix.add " --unhandled-rejections=strict"
  124. else: raiseAssert $conf.backend
  125. if cmdPrefix.len > 0: cmdPrefix.add " "
  126. # without the `cmdPrefix.len > 0` check, on windows you'd get a cryptic:
  127. # `The parameter is incorrect`
  128. let cmd = cmdPrefix & output.quoteShell & ' ' & conf.arguments
  129. execExternalProgram(conf, cmd.strip(leading=false,trailing=true))
  130. of cmdDocLike, cmdRst2html, cmdRst2tex, cmdMd2html, cmdMd2tex: # bugfix(cmdRst2tex was missing)
  131. if conf.arguments.len > 0:
  132. # reserved for future use
  133. rawMessage(conf, errGenerated, "'$1 cannot handle arguments" % [$conf.cmd])
  134. openDefaultBrowser($output)
  135. else:
  136. # support as needed
  137. rawMessage(conf, errGenerated, "'$1 cannot handle --run" % [$conf.cmd])
  138. when declared(GC_setMaxPause):
  139. GC_setMaxPause 2_000
  140. when compileOption("gc", "refc"):
  141. # the new correct mark&sweet collector is too slow :-/
  142. GC_disableMarkAndSweep()
  143. when not defined(selftest):
  144. let conf = newConfigRef()
  145. handleCmdLine(newIdentCache(), conf)
  146. when declared(GC_setMaxPause):
  147. echo GC_getStatistics()
  148. msgQuit(int8(conf.errorCounter > 0))