kochdocs.nim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. ## Part of 'koch' responsible for the documentation generation.
  2. import std/[os, strutils, osproc, sets, pathnorm, sequtils]
  3. when defined(nimPreviewSlimSystem):
  4. import std/assertions
  5. # XXX: Remove this feature check once the csources supports it.
  6. when defined(nimHasCastPragmaBlocks):
  7. import std/pegs
  8. from std/private/globs import nativeToUnixPath, walkDirRecFilter, PathEntry
  9. import "../compiler/nimpaths"
  10. const
  11. gaCode* = " --doc.googleAnalytics:UA-48159761-1"
  12. paCode* = " --doc.plausibleAnalytics:nim-lang.org"
  13. # errormax: subsequent errors are probably consequences of 1st one; a simple
  14. # bug could cause unlimited number of errors otherwise, hard to debug in CI.
  15. docDefines = "-d:nimExperimentalLinenoiseExtra"
  16. nimArgs = "--errormax:3 --hint:Conf:off --hint:Path:off --hint:Processing:off --hint:XDeclaredButNotUsed:off --warning:UnusedImport:off -d:boot --putenv:nimversion=$# $#" % [system.NimVersion, docDefines]
  17. gitUrl = "https://github.com/nim-lang/Nim"
  18. docHtmlOutput = "doc/html"
  19. webUploadOutput = "web/upload"
  20. var nimExe*: string
  21. const allowList = ["jsbigints.nim", "jsheaders.nim", "jsformdata.nim", "jsfetch.nim", "jsutils.nim"]
  22. template isJsOnly(file: string): bool =
  23. file.isRelativeTo("lib/js") or
  24. file.extractFilename in allowList
  25. proc exe*(f: string): string =
  26. result = addFileExt(f, ExeExt)
  27. when defined(windows):
  28. result = result.replace('/','\\')
  29. proc findNimImpl*(): tuple[path: string, ok: bool] =
  30. if nimExe.len > 0: return (nimExe, true)
  31. let nim = "nim".exe
  32. result.path = "bin" / nim
  33. result.ok = true
  34. if fileExists(result.path): return
  35. for dir in split(getEnv("PATH"), PathSep):
  36. result.path = dir / nim
  37. if fileExists(result.path): return
  38. # assume there is a symlink to the exe or something:
  39. return (nim, false)
  40. proc findNim*(): string = findNimImpl().path
  41. proc exec*(cmd: string, errorcode: int = QuitFailure, additionalPath = "") =
  42. let prevPath = getEnv("PATH")
  43. if additionalPath.len > 0:
  44. var absolute = additionalPath
  45. if not absolute.isAbsolute:
  46. absolute = getCurrentDir() / absolute
  47. echo("Adding to $PATH: ", absolute)
  48. putEnv("PATH", (if prevPath.len > 0: prevPath & PathSep else: "") & absolute)
  49. echo(cmd)
  50. if execShellCmd(cmd) != 0: quit("FAILURE", errorcode)
  51. putEnv("PATH", prevPath)
  52. template inFold*(desc, body) =
  53. if existsEnv("TRAVIS"):
  54. echo "travis_fold:start:" & desc.replace(" ", "_")
  55. elif existsEnv("GITHUB_ACTIONS"):
  56. echo "::group::" & desc
  57. elif existsEnv("TF_BUILD"):
  58. echo "##[group]" & desc
  59. body
  60. if existsEnv("TRAVIS"):
  61. echo "travis_fold:end:" & desc.replace(" ", "_")
  62. elif existsEnv("GITHUB_ACTIONS"):
  63. echo "::endgroup::"
  64. elif existsEnv("TF_BUILD"):
  65. echo "##[endgroup]"
  66. proc execFold*(desc, cmd: string, errorcode: int = QuitFailure, additionalPath = "") =
  67. ## Execute shell command. Add log folding for various CI services.
  68. # https://github.com/travis-ci/travis-ci/issues/2285#issuecomment-42724719
  69. let desc = if desc.len == 0: cmd else: desc
  70. inFold(desc):
  71. exec(cmd, errorcode, additionalPath)
  72. proc execCleanPath*(cmd: string,
  73. additionalPath = ""; errorcode: int = QuitFailure) =
  74. # simulate a poor man's virtual environment
  75. let prevPath = getEnv("PATH")
  76. when defined(windows):
  77. let cleanPath = r"$1\system32;$1;$1\System32\Wbem" % getEnv"SYSTEMROOT"
  78. else:
  79. const cleanPath = r"/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin"
  80. putEnv("PATH", cleanPath & PathSep & additionalPath)
  81. echo(cmd)
  82. if execShellCmd(cmd) != 0: quit("FAILURE", errorcode)
  83. putEnv("PATH", prevPath)
  84. proc nimexec*(cmd: string) =
  85. # Consider using `nimCompile` instead
  86. exec findNim().quoteShell() & " " & cmd
  87. proc nimCompile*(input: string, outputDir = "bin", mode = "c", options = "") =
  88. let output = outputDir / input.splitFile.name.exe
  89. let cmd = findNim().quoteShell() & " " & mode & " -o:" & output & " " & options & " " & input
  90. exec cmd
  91. proc nimCompileFold*(desc, input: string, outputDir = "bin", mode = "c", options = "", outputName = "") =
  92. let outputName2 = if outputName.len == 0: input.splitFile.name.exe else: outputName.exe
  93. let output = outputDir / outputName2
  94. let cmd = findNim().quoteShell() & " " & mode & " -o:" & output & " " & options & " " & input
  95. execFold(desc, cmd)
  96. proc getMd2html(): seq[string] =
  97. for a in walkDirRecFilter("doc"):
  98. let path = a.path
  99. if a.kind == pcFile and path.splitFile.ext == ".md" and path.lastPathPart notin
  100. ["docs.md", "nimfix.md",
  101. "docstyle.md" # docstyle.md shouldn't be converted to html separately;
  102. # it's included in contributing.md.
  103. ]:
  104. # maybe we should still show nimfix, could help reviving it
  105. # `docs` is redundant with `overview`, might as well remove that file?
  106. result.add path
  107. doAssert "doc/manual/var_t_return.md".unixToNativePath in result # sanity check
  108. const
  109. mdPdfList = """
  110. manual.md
  111. lib.md
  112. tut1.md
  113. tut2.md
  114. tut3.md
  115. nimc.md
  116. niminst.md
  117. mm.md
  118. """.splitWhitespace().mapIt("doc" / it)
  119. doc0 = """
  120. lib/system/threads.nim
  121. lib/system/channels_builtin.nim
  122. """.splitWhitespace() # ran by `nim doc0` instead of `nim doc`
  123. withoutIndex = """
  124. lib/wrappers/mysql.nim
  125. lib/wrappers/sqlite3.nim
  126. lib/wrappers/postgres.nim
  127. lib/wrappers/tinyc.nim
  128. lib/wrappers/odbcsql.nim
  129. lib/wrappers/pcre.nim
  130. lib/wrappers/openssl.nim
  131. lib/posix/posix.nim
  132. lib/posix/linux.nim
  133. lib/posix/termios.nim
  134. """.splitWhitespace()
  135. # some of these are include files so shouldn't be docgen'd
  136. ignoredModules = """
  137. lib/pure/future.nim
  138. lib/pure/collections/hashcommon.nim
  139. lib/pure/collections/tableimpl.nim
  140. lib/pure/collections/setimpl.nim
  141. lib/pure/ioselects/ioselectors_kqueue.nim
  142. lib/pure/ioselects/ioselectors_select.nim
  143. lib/pure/ioselects/ioselectors_poll.nim
  144. lib/pure/ioselects/ioselectors_epoll.nim
  145. lib/posix/posix_macos_amd64.nim
  146. lib/posix/posix_other.nim
  147. lib/posix/posix_nintendoswitch.nim
  148. lib/posix/posix_nintendoswitch_consts.nim
  149. lib/posix/posix_linux_amd64.nim
  150. lib/posix/posix_linux_amd64_consts.nim
  151. lib/posix/posix_other_consts.nim
  152. lib/posix/posix_freertos_consts.nim
  153. lib/posix/posix_openbsd_amd64.nim
  154. lib/posix/posix_haiku.nim
  155. """.splitWhitespace()
  156. when (NimMajor, NimMinor) < (1, 1) or not declared(isRelativeTo):
  157. proc isRelativeTo(path, base: string): bool =
  158. let path = path.normalizedPath
  159. let base = base.normalizedPath
  160. let ret = relativePath(path, base)
  161. result = path.len > 0 and not ret.startsWith ".."
  162. proc getDocList(): seq[string] =
  163. var docIgnore: HashSet[string]
  164. for a in doc0: docIgnore.incl a
  165. for a in withoutIndex: docIgnore.incl a
  166. for a in ignoredModules: docIgnore.incl a
  167. # don't ignore these even though in lib/system (not include files)
  168. const goodSystem = """
  169. lib/system/nimscript.nim
  170. lib/system/assertions.nim
  171. lib/system/iterators.nim
  172. lib/system/exceptions.nim
  173. lib/system/dollars.nim
  174. lib/system/ctypes.nim
  175. """.splitWhitespace()
  176. proc follow(a: PathEntry): bool =
  177. result = a.path.lastPathPart notin ["nimcache", htmldocsDirname,
  178. "includes", "deprecated", "genode"] and
  179. not a.path.isRelativeTo("lib/fusion") # fusion was un-bundled but we need to keep this in case user has it installed
  180. for entry in walkDirRecFilter("lib", follow = follow):
  181. let a = entry.path
  182. if entry.kind != pcFile or a.splitFile.ext != ".nim" or
  183. (a.isRelativeTo("lib/system") and a.nativeToUnixPath notin goodSystem) or
  184. a.nativeToUnixPath in docIgnore:
  185. continue
  186. result.add a
  187. result.add normalizePath("nimsuggest/sexp.nim")
  188. let doc = getDocList()
  189. proc sexec(cmds: openArray[string]) =
  190. ## Serial queue wrapper around exec.
  191. for cmd in cmds:
  192. echo(cmd)
  193. let (outp, exitCode) = osproc.execCmdEx(cmd)
  194. if exitCode != 0: quit outp
  195. proc mexec(cmds: openArray[string]) =
  196. ## Multiprocessor version of exec
  197. let r = execProcesses(cmds, {poStdErrToStdOut, poParentStreams, poEchoCmd})
  198. if r != 0:
  199. echo "external program failed, retrying serial work queue for logs!"
  200. sexec(cmds)
  201. proc buildDocSamples(nimArgs, destPath: string) =
  202. ## Special case documentation sample proc.
  203. ##
  204. ## TODO: consider integrating into the existing generic documentation builders
  205. ## now that we have a single `doc` command.
  206. exec(findNim().quoteShell() & " doc $# -o:$# $#" %
  207. [nimArgs, destPath / "docgen_sample.html", "doc" / "docgen_sample.nim"])
  208. proc buildDocPackages(nimArgs, destPath: string) =
  209. # compiler docs; later, other packages (perhaps tools, testament etc)
  210. let nim = findNim().quoteShell()
  211. # to avoid broken links to manual from compiler dir, but a multi-package
  212. # structure could be supported later
  213. proc docProject(outdir, options, mainproj: string) =
  214. exec("$nim doc --project --outdir:$outdir $nimArgs --git.url:$gitUrl $options $mainproj" % [
  215. "nim", nim,
  216. "outdir", outdir,
  217. "nimArgs", nimArgs,
  218. "gitUrl", gitUrl,
  219. "options", options,
  220. "mainproj", mainproj,
  221. ])
  222. let extra = "-u:boot"
  223. # xxx keep in sync with what's in $nim_prs_D/config/nimdoc.cfg, or, rather,
  224. # start using nims instead of nimdoc.cfg
  225. docProject(destPath/"compiler", extra, "compiler/index.nim")
  226. proc buildDoc(nimArgs, destPath: string) =
  227. # call nim for the documentation:
  228. let rst2html = getMd2html()
  229. var
  230. commands = newSeq[string](rst2html.len + len(doc0) + len(doc) + withoutIndex.len)
  231. i = 0
  232. let nim = findNim().quoteShell()
  233. for d in items(rst2html):
  234. commands[i] = nim & " md2html $# --git.url:$# -o:$# --index:on $#" %
  235. [nimArgs, gitUrl,
  236. destPath / changeFileExt(splitFile(d).name, "html"), d]
  237. i.inc
  238. for d in items(doc0):
  239. commands[i] = nim & " doc0 $# --git.url:$# -o:$# --index:on $#" %
  240. [nimArgs, gitUrl,
  241. destPath / changeFileExt(splitFile(d).name, "html"), d]
  242. i.inc
  243. for d in items(doc):
  244. let extra = if isJsOnly(d): "--backend:js" else: ""
  245. var nimArgs2 = nimArgs
  246. if d.isRelativeTo("compiler"): doAssert false
  247. commands[i] = nim & " doc $# $# --git.url:$# --outdir:$# --index:on $#" %
  248. [extra, nimArgs2, gitUrl, destPath, d]
  249. i.inc
  250. for d in items(withoutIndex):
  251. commands[i] = nim & " doc $# --git.url:$# -o:$# $#" %
  252. [nimArgs, gitUrl,
  253. destPath / changeFileExt(splitFile(d).name, "html"), d]
  254. i.inc
  255. mexec(commands)
  256. exec(nim & " buildIndex -o:$1/theindex.html $1" % [destPath])
  257. # caveat: this works so long it's called before `buildDocPackages` which
  258. # populates `compiler/` with unrelated idx files that shouldn't be in index,
  259. # so should work in CI but you may need to remove your generated html files
  260. # locally after calling `./koch docs`. The clean fix would be for `idx` files
  261. # to be transient with `--project` (eg all in memory).
  262. proc nim2pdf(src: string, dst: string, nimArgs: string) =
  263. # xxx expose as a `nim` command or in some other reusable way.
  264. let outDir = "build" / "xelatextmp" # xxx factor pending https://github.com/timotheecour/Nim/issues/616
  265. # note: this will generate temporary files in gitignored `outDir`: aux toc log out tex
  266. exec("$# md2tex $# --outdir:$# $#" % [findNim().quoteShell(), nimArgs, outDir.quoteShell, src.quoteShell])
  267. let texFile = outDir / src.lastPathPart.changeFileExt("tex")
  268. for i in 0..<3: # call LaTeX three times to get cross references right:
  269. let xelatexLog = outDir / "xelatex.log"
  270. # `>` should work on windows, if not, we can use `execCmdEx`
  271. let cmd = "xelatex -interaction=nonstopmode -output-directory=$# $# > $#" % [outDir.quoteShell, texFile.quoteShell, xelatexLog.quoteShell]
  272. exec(cmd) # on error, user can inspect `xelatexLog`
  273. if i == 1: # build .ind file
  274. var texFileBase = texFile
  275. texFileBase.removeSuffix(".tex")
  276. let cmd = "makeindex $# > $#" % [
  277. texFileBase.quoteShell, xelatexLog.quoteShell]
  278. exec(cmd)
  279. moveFile(texFile.changeFileExt("pdf"), dst)
  280. proc buildPdfDoc*(nimArgs, destPath: string) =
  281. var pdfList: seq[string]
  282. createDir(destPath)
  283. if os.execShellCmd("xelatex -version") != 0:
  284. doAssert false, "xelatex not found" # or, raise an exception
  285. else:
  286. for src in items(mdPdfList):
  287. let dst = destPath / src.lastPathPart.changeFileExt("pdf")
  288. pdfList.add dst
  289. nim2pdf(src, dst, nimArgs)
  290. echo "\nOutput PDF files: \n ", pdfList.join(" ") # because `nim2pdf` is a bit verbose
  291. proc buildJS(): string =
  292. let nim = findNim()
  293. exec("$# js -d:release --out:$# tools/nimblepkglist.nim" %
  294. [nim.quoteShell(), webUploadOutput / "nimblepkglist.js"])
  295. # xxx deadcode? and why is it only for webUploadOutput, not for local docs?
  296. result = getDocHacksJs(nimr = getCurrentDir(), nim)
  297. proc buildDocsDir*(args: string, dir: string) =
  298. let args = nimArgs & " " & args
  299. let docHackJsSource = buildJS()
  300. createDir(dir)
  301. buildDocSamples(args, dir)
  302. buildDoc(args, dir) # bottleneck
  303. copyFile(dir / "overview.html", dir / "index.html")
  304. buildDocPackages(args, dir)
  305. copyFile(docHackJsSource, dir / docHackJsSource.lastPathPart)
  306. proc buildDocs*(args: string, localOnly = false, localOutDir = "") =
  307. let localOutDir =
  308. if localOutDir.len == 0:
  309. docHtmlOutput
  310. else:
  311. localOutDir
  312. var args = args
  313. if not localOnly:
  314. buildDocsDir(args, webUploadOutput / NimVersion)
  315. # XXX: Remove this feature check once the csources supports it.
  316. when defined(nimHasCastPragmaBlocks):
  317. let gaFilter = peg"@( y'--doc.googleAnalytics:' @(\s / $) )"
  318. args = args.replace(gaFilter)
  319. buildDocsDir(args, localOutDir)