main.nim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. # implements the command dispatcher and several commands
  10. when not defined(nimcore):
  11. {.error: "nimcore MUST be defined for Nim's core tooling".}
  12. import
  13. llstream, strutils, os, ast, lexer, syntaxes, options, msgs,
  14. condsyms, times,
  15. sem, idents, passes, extccomp,
  16. cgen, json, nversion,
  17. platform, nimconf, passaux, depends, vm, idgen,
  18. modules,
  19. modulegraphs, tables, rod, lineinfos, pathutils
  20. when not defined(leanCompiler):
  21. import jsgen, docgen, docgen2
  22. proc semanticPasses(g: ModuleGraph) =
  23. registerPass g, verbosePass
  24. registerPass g, semPass
  25. proc writeDepsFile(g: ModuleGraph) =
  26. let fname = g.config.nimcacheDir / RelativeFile(g.config.projectName & ".deps")
  27. let f = open(fname.string, fmWrite)
  28. for m in g.modules:
  29. if m != nil:
  30. f.writeLine(toFullPath(g.config, m.position.FileIndex))
  31. for k in g.inclToMod.keys:
  32. if g.getModule(k).isNil: # don't repeat includes which are also modules
  33. f.writeLine(toFullPath(g.config, k))
  34. f.close()
  35. proc commandGenDepend(graph: ModuleGraph) =
  36. semanticPasses(graph)
  37. registerPass(graph, gendependPass)
  38. compileProject(graph)
  39. let project = graph.config.projectFull
  40. writeDepsFile(graph)
  41. generateDot(graph, project)
  42. execExternalProgram(graph.config, "dot -Tpng -o" &
  43. changeFileExt(project, "png").string &
  44. ' ' & changeFileExt(project, "dot").string)
  45. proc commandCheck(graph: ModuleGraph) =
  46. graph.config.setErrorMaxHighMaybe
  47. defineSymbol(graph.config.symbols, "nimcheck")
  48. semanticPasses(graph) # use an empty backend for semantic checking only
  49. compileProject(graph)
  50. when not defined(leanCompiler):
  51. proc commandDoc2(graph: ModuleGraph; json: bool) =
  52. handleDocOutputOptions graph.config
  53. graph.config.setErrorMaxHighMaybe
  54. semanticPasses(graph)
  55. if json: registerPass(graph, docgen2JsonPass)
  56. else: registerPass(graph, docgen2Pass)
  57. compileProject(graph)
  58. finishDoc2Pass(graph.config.projectName)
  59. proc setOutFile(conf: ConfigRef) =
  60. proc libNameTmpl(conf: ConfigRef): string {.inline.} =
  61. result = if conf.target.targetOS == osWindows: "$1.lib" else: "lib$1.a"
  62. if conf.outFile.isEmpty:
  63. let base = conf.projectName
  64. let targetName = if optGenDynLib in conf.globalOptions:
  65. platform.OS[conf.target.targetOS].dllFrmt % base
  66. elif optGenStaticLib in conf.globalOptions:
  67. libNameTmpl(conf) % base
  68. else:
  69. base & platform.OS[conf.target.targetOS].exeExt
  70. conf.outFile = RelativeFile targetName
  71. proc commandCompileToC(graph: ModuleGraph) =
  72. let conf = graph.config
  73. setOutFile(conf)
  74. extccomp.initVars(conf)
  75. semanticPasses(graph)
  76. registerPass(graph, cgenPass)
  77. if {optRun, optForceFullMake} * conf.globalOptions == {optRun} or isDefined(conf, "nimBetterRun"):
  78. let proj = changeFileExt(conf.projectFull, "")
  79. if not changeDetectedViaJsonBuildInstructions(conf, proj):
  80. # nothing changed
  81. graph.config.notes = graph.config.mainPackageNotes
  82. return
  83. if not extccomp.ccHasSaneOverflow(conf):
  84. conf.symbols.defineSymbol("nimEmulateOverflowChecks")
  85. compileProject(graph)
  86. if graph.config.errorCounter > 0:
  87. return # issue #9933
  88. cgenWriteModules(graph.backend, conf)
  89. if conf.cmd != cmdRun:
  90. extccomp.callCCompiler(conf)
  91. # for now we do not support writing out a .json file with the build instructions when HCR is on
  92. if not conf.hcrOn:
  93. extccomp.writeJsonBuildInstructions(conf)
  94. if optGenScript in graph.config.globalOptions:
  95. writeDepsFile(graph)
  96. proc commandJsonScript(graph: ModuleGraph) =
  97. let proj = changeFileExt(graph.config.projectFull, "")
  98. extccomp.runJsonBuildInstructions(graph.config, proj)
  99. proc commandCompileToJS(graph: ModuleGraph) =
  100. when defined(leanCompiler):
  101. globalError(graph.config, unknownLineInfo, "compiler wasn't built with JS code generator")
  102. else:
  103. let conf = graph.config
  104. conf.exc = excCpp
  105. if conf.outFile.isEmpty:
  106. conf.outFile = RelativeFile(conf.projectName & ".js")
  107. #incl(gGlobalOptions, optSafeCode)
  108. setTarget(graph.config.target, osJS, cpuJS)
  109. #initDefines()
  110. defineSymbol(graph.config.symbols, "ecmascript") # For backward compatibility
  111. semanticPasses(graph)
  112. registerPass(graph, JSgenPass)
  113. compileProject(graph)
  114. if optGenScript in graph.config.globalOptions:
  115. writeDepsFile(graph)
  116. proc interactivePasses(graph: ModuleGraph) =
  117. initDefines(graph.config.symbols)
  118. defineSymbol(graph.config.symbols, "nimscript")
  119. # note: seems redundant with -d:nimHasLibFFI
  120. when hasFFI: defineSymbol(graph.config.symbols, "nimffi")
  121. registerPass(graph, verbosePass)
  122. registerPass(graph, semPass)
  123. registerPass(graph, evalPass)
  124. proc commandInteractive(graph: ModuleGraph) =
  125. graph.config.setErrorMaxHighMaybe
  126. interactivePasses(graph)
  127. compileSystemModule(graph)
  128. if graph.config.commandArgs.len > 0:
  129. discard graph.compileModule(fileInfoIdx(graph.config, graph.config.projectFull), {})
  130. else:
  131. var m = graph.makeStdinModule()
  132. incl(m.flags, sfMainModule)
  133. processModule(graph, m, llStreamOpenStdIn())
  134. const evalPasses = [verbosePass, semPass, evalPass]
  135. proc evalNim(graph: ModuleGraph; nodes: PNode, module: PSym) =
  136. carryPasses(graph, nodes, module, evalPasses)
  137. proc commandScan(cache: IdentCache, config: ConfigRef) =
  138. var f = addFileExt(AbsoluteFile mainCommandArg(config), NimExt)
  139. var stream = llStreamOpen(f, fmRead)
  140. if stream != nil:
  141. var
  142. L: TLexer
  143. tok: TToken
  144. initToken(tok)
  145. openLexer(L, f, stream, cache, config)
  146. while true:
  147. rawGetTok(L, tok)
  148. printTok(config, tok)
  149. if tok.tokType == tkEof: break
  150. closeLexer(L)
  151. else:
  152. rawMessage(config, errGenerated, "cannot open file: " & f.string)
  153. const
  154. PrintRopeCacheStats = false
  155. proc mainCommand*(graph: ModuleGraph) =
  156. let conf = graph.config
  157. let cache = graph.cache
  158. setupModuleCache(graph)
  159. # In "nim serve" scenario, each command must reset the registered passes
  160. clearPasses(graph)
  161. conf.lastCmdTime = epochTime()
  162. conf.searchPaths.add(conf.libpath)
  163. setId(100)
  164. proc customizeForBackend(backend: TBackend) =
  165. ## Sets backend specific options but don't compile to backend yet in
  166. ## case command doesn't require it. This must be called by all commands.
  167. if conf.backend == backendInvalid:
  168. # only set if wasn't already set, to allow override via `nim c -b:cpp`
  169. conf.backend = backend
  170. defineSymbol(graph.config.symbols, $conf.backend)
  171. case conf.backend
  172. of backendC:
  173. if conf.exc == excNone: conf.exc = excSetjmp
  174. of backendCpp:
  175. if conf.exc == excNone: conf.exc = excCpp
  176. of backendObjc: discard
  177. of backendJs:
  178. if conf.hcrOn:
  179. # XXX: At the moment, system.nim cannot be compiled in JS mode
  180. # with "-d:useNimRtl". The HCR option has been processed earlier
  181. # and it has added this define implictly, so we must undo that here.
  182. # A better solution might be to fix system.nim
  183. undefSymbol(conf.symbols, "useNimRtl")
  184. of backendInvalid: doAssert false
  185. if conf.selectedGC in {gcArc, gcOrc} and conf.backend != backendCpp:
  186. conf.exc = excGoto
  187. var commandAlreadyProcessed = false
  188. proc compileToBackend(backend: TBackend, cmd = cmdCompileToBackend) =
  189. commandAlreadyProcessed = true
  190. conf.cmd = cmd
  191. customizeForBackend(backend)
  192. case conf.backend
  193. of backendC: commandCompileToC(graph)
  194. of backendCpp: commandCompileToC(graph)
  195. of backendObjc: commandCompileToC(graph)
  196. of backendJs: commandCompileToJS(graph)
  197. of backendInvalid: doAssert false
  198. template docLikeCmd(body) =
  199. when defined(leanCompiler):
  200. quit "compiler wasn't built with documentation generator"
  201. else:
  202. wantMainModule(conf)
  203. conf.cmd = cmdDoc
  204. loadConfigs(DocConfig, cache, conf)
  205. defineSymbol(conf.symbols, "nimdoc")
  206. body
  207. block: ## command prepass
  208. var docLikeCmd2 = false # includes what calls `docLikeCmd` + some more
  209. case conf.command.normalize
  210. of "r": conf.globalOptions.incl {optRun, optUseNimcache}
  211. of "doc0", "doc2", "doc", "rst2html", "rst2tex", "jsondoc0", "jsondoc2",
  212. "jsondoc", "ctags", "buildindex": docLikeCmd2 = true
  213. else: discard
  214. if conf.outDir.isEmpty:
  215. # doc like commands can generate a lot of files (especially with --project)
  216. # so by default should not end up in $PWD nor in $projectPath.
  217. conf.outDir = block:
  218. var ret = if optUseNimcache in conf.globalOptions: getNimcacheDir(conf)
  219. else: conf.projectPath
  220. doAssert ret.string.isAbsolute # `AbsoluteDir` is not a real guarantee
  221. if docLikeCmd2: ret = ret / htmldocsDir
  222. ret
  223. ## process all backend commands
  224. case conf.command.normalize
  225. of "c", "cc", "compile", "compiletoc": compileToBackend(backendC) # compile means compileToC currently
  226. of "cpp", "compiletocpp": compileToBackend(backendCpp)
  227. of "objc", "compiletooc": compileToBackend(backendObjc)
  228. of "js", "compiletojs": compileToBackend(backendJs)
  229. of "r": compileToBackend(backendC) # different from `"run"`!
  230. of "run":
  231. when hasTinyCBackend:
  232. extccomp.setCC(conf, "tcc", unknownLineInfo)
  233. if conf.backend notin {backendC, backendInvalid}:
  234. rawMessage(conf, errGenerated, "'run' requires c backend, got: '$1'" % $conf.backend)
  235. compileToBackend(backendC, cmd = cmdRun)
  236. else:
  237. rawMessage(conf, errGenerated, "'run' command not available; rebuild with -d:tinyc")
  238. else: customizeForBackend(backendC) # fallback for other commands
  239. ## process all other commands
  240. case conf.command.normalize # synchronize with `cmdUsingHtmlDocs`
  241. of "doc0": docLikeCmd commandDoc(cache, conf)
  242. of "doc2", "doc":
  243. docLikeCmd():
  244. conf.setNoteDefaults(warnLockLevel, false) # issue #13218
  245. conf.setNoteDefaults(warnRedefinitionOfLabel, false) # issue #13218
  246. # because currently generates lots of false positives due to conflation
  247. # of labels links in doc comments, eg for random.rand:
  248. # ## * `rand proc<#rand,Rand,Natural>`_ that returns an integer
  249. # ## * `rand proc<#rand,Rand,range[]>`_ that returns a float
  250. commandDoc2(graph, false)
  251. if optGenIndex in conf.globalOptions and optWholeProject in conf.globalOptions:
  252. commandBuildIndex(conf, $conf.outDir)
  253. of "rst2html":
  254. conf.setNoteDefaults(warnRedefinitionOfLabel, false) # similar to issue #13218
  255. when defined(leanCompiler):
  256. quit "compiler wasn't built with documentation generator"
  257. else:
  258. conf.cmd = cmdRst2html
  259. loadConfigs(DocConfig, cache, conf)
  260. commandRst2Html(cache, conf)
  261. of "rst2tex":
  262. when defined(leanCompiler):
  263. quit "compiler wasn't built with documentation generator"
  264. else:
  265. conf.cmd = cmdRst2tex
  266. loadConfigs(DocTexConfig, cache, conf)
  267. commandRst2TeX(cache, conf)
  268. of "jsondoc0": docLikeCmd commandJson(cache, conf)
  269. of "jsondoc2", "jsondoc": docLikeCmd commandDoc2(graph, true)
  270. of "ctags": docLikeCmd commandTags(cache, conf)
  271. of "buildindex": docLikeCmd commandBuildIndex(conf, $conf.projectFull, conf.outFile)
  272. of "gendepend":
  273. conf.cmd = cmdGenDepend
  274. commandGenDepend(graph)
  275. of "dump":
  276. conf.cmd = cmdDump
  277. if getConfigVar(conf, "dump.format") == "json":
  278. wantMainModule(conf)
  279. var definedSymbols = newJArray()
  280. for s in definedSymbolNames(conf.symbols): definedSymbols.elems.add(%s)
  281. var libpaths = newJArray()
  282. var lazyPaths = newJArray()
  283. for dir in conf.searchPaths: libpaths.elems.add(%dir.string)
  284. for dir in conf.lazyPaths: lazyPaths.elems.add(%dir.string)
  285. var hints = newJObject() # consider factoring with `listHints`
  286. for a in hintMin..hintMax:
  287. hints[a.msgToStr] = %(a in conf.notes)
  288. var warnings = newJObject()
  289. for a in warnMin..warnMax:
  290. warnings[a.msgToStr] = %(a in conf.notes)
  291. var dumpdata = %[
  292. (key: "version", val: %VersionAsString),
  293. (key: "nimExe", val: %(getAppFilename())),
  294. (key: "prefixdir", val: %conf.getPrefixDir().string),
  295. (key: "libpath", val: %conf.libpath.string),
  296. (key: "project_path", val: %conf.projectFull.string),
  297. (key: "defined_symbols", val: definedSymbols),
  298. (key: "lib_paths", val: %libpaths),
  299. (key: "lazyPaths", val: %lazyPaths),
  300. (key: "outdir", val: %conf.outDir.string),
  301. (key: "out", val: %conf.outFile.string),
  302. (key: "nimcache", val: %getNimcacheDir(conf).string),
  303. (key: "hints", val: hints),
  304. (key: "warnings", val: warnings),
  305. ]
  306. msgWriteln(conf, $dumpdata, {msgStdout, msgSkipHook})
  307. else:
  308. msgWriteln(conf, "-- list of currently defined symbols --",
  309. {msgStdout, msgSkipHook})
  310. for s in definedSymbolNames(conf.symbols): msgWriteln(conf, s, {msgStdout, msgSkipHook})
  311. msgWriteln(conf, "-- end of list --", {msgStdout, msgSkipHook})
  312. for it in conf.searchPaths: msgWriteln(conf, it.string)
  313. of "check":
  314. conf.cmd = cmdCheck
  315. commandCheck(graph)
  316. of "parse":
  317. conf.cmd = cmdParse
  318. wantMainModule(conf)
  319. discard parseFile(conf.projectMainIdx, cache, conf)
  320. of "scan":
  321. conf.cmd = cmdScan
  322. wantMainModule(conf)
  323. commandScan(cache, conf)
  324. msgWriteln(conf, "Beware: Indentation tokens depend on the parser's state!")
  325. of "secret":
  326. conf.cmd = cmdInteractive
  327. commandInteractive(graph)
  328. of "e":
  329. if not fileExists(conf.projectFull):
  330. rawMessage(conf, errGenerated, "NimScript file does not exist: " & conf.projectFull.string)
  331. elif not conf.projectFull.string.endsWith(".nims"):
  332. rawMessage(conf, errGenerated, "not a NimScript file: " & conf.projectFull.string)
  333. # main NimScript logic handled in cmdlinehelper.nim.
  334. of "nop", "help":
  335. # prevent the "success" message:
  336. conf.cmd = cmdDump
  337. of "jsonscript":
  338. conf.cmd = cmdJsonScript
  339. setOutFile(graph.config)
  340. commandJsonScript(graph)
  341. elif commandAlreadyProcessed: discard # already handled
  342. else:
  343. rawMessage(conf, errGenerated, "invalid command: " & conf.command)
  344. if conf.errorCounter == 0 and
  345. conf.cmd notin {cmdInterpret, cmdRun, cmdDump}:
  346. let mem =
  347. when declared(system.getMaxMem): formatSize(getMaxMem()) & " peakmem"
  348. else: formatSize(getTotalMem()) & " totmem"
  349. let loc = $conf.linesCompiled
  350. let build = if isDefined(conf, "danger"): "Dangerous Release"
  351. elif isDefined(conf, "release"): "Release"
  352. else: "Debug"
  353. let sec = formatFloat(epochTime() - conf.lastCmdTime, ffDecimal, 3)
  354. let project = if optListFullPaths in conf.globalOptions: $conf.projectFull else: $conf.projectName
  355. var output: string
  356. if optCompileOnly in conf.globalOptions and conf.cmd != cmdJsonScript:
  357. output = $conf.jsonBuildFile
  358. elif conf.outFile.isEmpty and conf.cmd notin {cmdJsonScript, cmdCompileToBackend, cmdDoc}:
  359. # for some cmd we expect a valid absOutFile
  360. output = "unknownOutput"
  361. else:
  362. output = $conf.absOutFile
  363. if optListFullPaths notin conf.globalOptions: output = output.AbsoluteFile.extractFilename
  364. rawMessage(conf, hintSuccessX, [
  365. "loc", loc,
  366. "sec", sec,
  367. "mem", mem,
  368. "build", build,
  369. "project", project,
  370. "output", output,
  371. ])
  372. when PrintRopeCacheStats:
  373. echo "rope cache stats: "
  374. echo " tries : ", gCacheTries
  375. echo " misses: ", gCacheMisses
  376. echo " int tries: ", gCacheIntTries
  377. echo " efficiency: ", formatFloat(1-(gCacheMisses.float/gCacheTries.float),
  378. ffDecimal, 3)