nim.nim 5.5 KB

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