koch.nim 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. #
  2. #
  3. # Maintenance program for Nim
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # See doc/koch.txt for documentation.
  10. #
  11. when defined(gcc) and defined(windows):
  12. when defined(x86):
  13. {.link: "icons/koch.res".}
  14. else:
  15. {.link: "icons/koch_icon.o".}
  16. when defined(amd64) and defined(windows) and defined(vcc):
  17. {.link: "icons/koch-amd64-windows-vcc.res".}
  18. when defined(i386) and defined(windows) and defined(vcc):
  19. {.link: "icons/koch-i386-windows-vcc.res".}
  20. import
  21. os, strutils, parseopt, osproc, streams
  22. import tools / kochdocs
  23. const VersionAsString = system.NimVersion
  24. const
  25. HelpText = """
  26. +-----------------------------------------------------------------+
  27. | Maintenance program for Nim |
  28. | Version $1|
  29. | (c) 2017 Andreas Rumpf |
  30. +-----------------------------------------------------------------+
  31. Build time: $2, $3
  32. Usage:
  33. koch [options] command [options for command]
  34. Options:
  35. --help, -h shows this help and quits
  36. --latest bundle the installers with a bleeding edge Nimble
  37. --stable bundle the installers with a stable Nimble
  38. Possible Commands:
  39. boot [options] bootstraps with given command line options
  40. distrohelper [bindir] helper for distro packagers
  41. tools builds Nim related tools
  42. toolsNoNimble builds Nim related tools (except nimble)
  43. doesn't require network connectivity
  44. nimble builds the Nimble tool
  45. Boot options:
  46. -d:release produce a release version of the compiler
  47. -d:useLinenoise use the linenoise library for interactive mode
  48. (not needed on Windows)
  49. -d:leanCompiler produce a compiler without JS codegen or
  50. documentation generator in order to use less RAM
  51. for bootstrapping
  52. Commands for core developers:
  53. runCI runs continuous integration (CI), eg from travis
  54. docs [options] generates the full documentation
  55. csource -d:release builds the C sources for installation
  56. pdf builds the PDF documentation
  57. zip builds the installation zip package
  58. xz builds the installation tar.xz package
  59. testinstall test tar.xz package; Unix only!
  60. tests [options] run the testsuite (run a subset of tests by
  61. specifying a category, e.g. `tests cat async`)
  62. temp options creates a temporary compiler for testing
  63. pushcsource push generated C sources to its repo
  64. Web options:
  65. --googleAnalytics:UA-... add the given google analytics code to the docs. To
  66. build the official docs, use UA-48159761-1
  67. """
  68. let kochExe* = when isMainModule: os.getAppFilename() # always correct when koch is main program, even if `koch` exe renamed eg: `nim c -o:koch_debug koch.nim`
  69. else: getAppDir() / "koch".exe # works for winrelease
  70. proc kochExec*(cmd: string) =
  71. exec kochExe.quoteShell & " " & cmd
  72. proc kochExecFold*(desc, cmd: string) =
  73. execFold(desc, kochExe.quoteShell & " " & cmd)
  74. template withDir(dir, body) =
  75. let old = getCurrentDir()
  76. try:
  77. setCurrentDir(dir)
  78. body
  79. finally:
  80. setCurrentDir(old)
  81. let origDir = getCurrentDir()
  82. setCurrentDir(getAppDir())
  83. proc tryExec(cmd: string): bool =
  84. echo(cmd)
  85. result = execShellCmd(cmd) == 0
  86. proc safeRemove(filename: string) =
  87. if existsFile(filename): removeFile(filename)
  88. proc overwriteFile(source, dest: string) =
  89. safeRemove(dest)
  90. moveFile(source, dest)
  91. proc copyExe(source, dest: string) =
  92. safeRemove(dest)
  93. copyFile(dest=dest, source=source)
  94. inclFilePermissions(dest, {fpUserExec})
  95. const
  96. compileNimInst = "tools/niminst/niminst"
  97. proc csource(args: string) =
  98. nimexec(("cc $1 -r $3 --var:version=$2 --var:mingw=none csource " &
  99. "--main:compiler/nim.nim compiler/installer.ini $1") %
  100. [args, VersionAsString, compileNimInst])
  101. proc bundleNimbleSrc(latest: bool) =
  102. ## bunldeNimbleSrc() bundles a specific Nimble commit with the tarball. We
  103. ## always bundle the latest official release.
  104. if not dirExists("dist/nimble/.git"):
  105. exec("git clone https://github.com/nim-lang/nimble.git dist/nimble")
  106. if not latest:
  107. withDir("dist/nimble"):
  108. exec("git checkout -f stable")
  109. exec("git pull")
  110. proc bundleC2nim() =
  111. if not dirExists("dist/c2nim/.git"):
  112. exec("git clone https://github.com/nim-lang/c2nim.git dist/c2nim")
  113. nimCompile("dist/c2nim/c2nim", options = "--noNimblePath --path:.")
  114. proc bundleNimbleExe(latest: bool) =
  115. bundleNimbleSrc(latest)
  116. # installer.ini expects it under $nim/bin
  117. nimCompile("dist/nimble/src/nimble.nim", options = "-d:release --nilseqs:on")
  118. proc buildNimble(latest: bool) =
  119. # old installations created nim/nimblepkg/*.nim files. We remove these
  120. # here so that it cannot cause problems (nimble bug #306):
  121. if dirExists("bin/nimblepkg"):
  122. removeDir("bin/nimblepkg")
  123. # if koch is used for a tar.xz, build the dist/nimble we shipped
  124. # with the tarball:
  125. var installDir = "dist/nimble"
  126. if not latest and dirExists(installDir) and not dirExists("dist/nimble/.git"):
  127. discard "don't do the git dance"
  128. else:
  129. if not dirExists("dist/nimble/.git"):
  130. if dirExists(installDir):
  131. var id = 0
  132. while dirExists("dist/nimble" & $id):
  133. inc id
  134. installDir = "dist/nimble" & $id
  135. exec("git clone https://github.com/nim-lang/nimble.git " & installDir)
  136. withDir(installDir):
  137. if latest:
  138. exec("git checkout -f master")
  139. else:
  140. exec("git checkout -f stable")
  141. exec("git pull")
  142. nimCompile(installDir / "src/nimble.nim", options = "--noNimblePath --nilseqs:on -d:release")
  143. proc bundleNimsuggest() =
  144. nimCompile("nimsuggest/nimsuggest.nim", options = "-d:release")
  145. proc buildVccTool() =
  146. nimCompileFold("Compile Vcc", "tools/vccexe/vccexe.nim")
  147. proc bundleWinTools() =
  148. # TODO: consider building under `bin` instead of `.`
  149. nimCompile("tools/finish.nim", outputDir = "")
  150. buildVccTool()
  151. nimCompile("tools/nimgrab.nim", options = "-d:ssl")
  152. nimCompile("tools/nimgrep.nim")
  153. bundleC2nim()
  154. when false:
  155. # not yet a tool worth including
  156. nimCompile(r"tools\downloader.nim", options = r"--cc:vcc --app:gui -d:ssl --noNimblePath --path:..\ui")
  157. proc zip(latest: bool; args: string) =
  158. bundleNimbleExe(latest)
  159. bundleNimsuggest()
  160. bundleWinTools()
  161. nimexec("cc -r $2 --var:version=$1 --var:mingw=none --main:compiler/nim.nim scripts compiler/installer.ini" %
  162. [VersionAsString, compileNimInst])
  163. exec("$# --var:version=$# --var:mingw=none --main:compiler/nim.nim zip compiler/installer.ini" %
  164. ["tools/niminst/niminst".exe, VersionAsString])
  165. proc ensureCleanGit() =
  166. let (outp, status) = osproc.execCmdEx("git diff")
  167. if outp.len != 0:
  168. quit "Not a clean git repository; 'git diff' not empty!"
  169. if status != 0:
  170. quit "Not a clean git repository; 'git diff' returned non-zero!"
  171. proc xz(latest: bool; args: string) =
  172. ensureCleanGit()
  173. bundleNimbleSrc(latest)
  174. when false:
  175. bundleNimsuggest()
  176. nimexec("cc -r $2 --var:version=$1 --var:mingw=none --main:compiler/nim.nim scripts compiler/installer.ini" %
  177. [VersionAsString, compileNimInst])
  178. exec("$# --var:version=$# --var:mingw=none --main:compiler/nim.nim xz compiler/installer.ini" %
  179. ["tools" / "niminst" / "niminst".exe, VersionAsString])
  180. proc buildTool(toolname, args: string) =
  181. nimexec("cc $# $#" % [args, toolname])
  182. copyFile(dest="bin" / splitFile(toolname).name.exe, source=toolname.exe)
  183. proc buildTools() =
  184. bundleNimsuggest()
  185. nimCompileFold("Compile nimgrep", "tools/nimgrep.nim", options = "-d:release")
  186. when defined(windows): buildVccTool()
  187. nimCompileFold("Compile nimpretty", "nimpretty/nimpretty.nim", options = "-d:release")
  188. nimCompileFold("Compile nimfind", "tools/nimfind.nim", options = "-d:release")
  189. proc nsis(latest: bool; args: string) =
  190. bundleNimbleExe(latest)
  191. bundleNimsuggest()
  192. bundleWinTools()
  193. # make sure we have generated the niminst executables:
  194. buildTool("tools/niminst/niminst", args)
  195. #buildTool("tools/nimgrep", args)
  196. # produce 'nim_debug.exe':
  197. #exec "nim c compiler" / "nim.nim"
  198. #copyExe("compiler/nim".exe, "bin/nim_debug".exe)
  199. exec(("tools" / "niminst" / "niminst --var:version=$# --var:mingw=mingw$#" &
  200. " nsis compiler/installer.ini") % [VersionAsString, $(sizeof(pointer)*8)])
  201. proc geninstall(args="") =
  202. nimexec("cc -r $# --var:version=$# --var:mingw=none --main:compiler/nim.nim scripts compiler/installer.ini $#" %
  203. [compileNimInst, VersionAsString, args])
  204. proc install(args: string) =
  205. geninstall()
  206. exec("sh ./install.sh $#" % args)
  207. when false:
  208. proc web(args: string) =
  209. nimexec("js tools/dochack/dochack.nim")
  210. nimexec("cc -r tools/nimweb.nim $# web/website.ini --putenv:nimversion=$#" %
  211. [args, VersionAsString])
  212. proc website(args: string) =
  213. nimexec("cc -r tools/nimweb.nim $# --website web/website.ini --putenv:nimversion=$#" %
  214. [args, VersionAsString])
  215. proc pdf(args="") =
  216. exec("$# cc -r tools/nimweb.nim $# --pdf web/website.ini --putenv:nimversion=$#" %
  217. [findNim(), args, VersionAsString], additionalPATH=findNim().splitFile.dir)
  218. # -------------- boot ---------------------------------------------------------
  219. proc findStartNim: string =
  220. # we try several things before giving up:
  221. # * bin/nim
  222. # * $PATH/nim
  223. # If these fail, we try to build nim with the "build.(sh|bat)" script.
  224. var nim = "nim".exe
  225. result = "bin" / nim
  226. if existsFile(result): return
  227. for dir in split(getEnv("PATH"), PathSep):
  228. if existsFile(dir / nim): return dir / nim
  229. when defined(Posix):
  230. const buildScript = "build.sh"
  231. if existsFile(buildScript):
  232. if tryExec("./" & buildScript): return "bin" / nim
  233. else:
  234. const buildScript = "build.bat"
  235. if existsFile(buildScript):
  236. if tryExec(buildScript): return "bin" / nim
  237. echo("Found no nim compiler and every attempt to build one failed!")
  238. quit("FAILURE")
  239. proc thVersion(i: int): string =
  240. result = ("compiler" / "nim" & $i).exe
  241. proc boot(args: string) =
  242. var output = "compiler" / "nim".exe
  243. var finalDest = "bin" / "nim".exe
  244. # default to use the 'c' command:
  245. let useCpp = getEnv("NIM_COMPILE_TO_CPP", "false") == "true"
  246. let smartNimcache = (if "release" in args: "nimcache/r_" else: "nimcache/d_") &
  247. hostOs & "_" & hostCpu
  248. let nimStart = findStartNim()
  249. for i in 0..2:
  250. let defaultCommand = if useCpp: "cpp" else: "c"
  251. let bootOptions = if args.len == 0 or args.startsWith("-"): defaultCommand else: ""
  252. echo "iteration: ", i+1
  253. var extraOption = ""
  254. var nimi = i.thVersion
  255. if i == 0:
  256. nimi = nimStart
  257. extraOption.add " --skipUserCfg --skipParentCfg"
  258. # The configs are skipped for bootstrap
  259. # (1st iteration) to prevent newer flags from breaking bootstrap phase.
  260. let ret = execCmdEx(nimStart & " --version")
  261. doAssert ret.exitCode == 0
  262. let version = ret.output.splitLines[0]
  263. if version.startsWith "Nim Compiler Version 0.19.0":
  264. extraOption.add " -d:nimBoostrapCsources0_19_0"
  265. # remove this when csources get updated
  266. exec "$# $# $# $# --nimcache:$# compiler" / "nim.nim" %
  267. [nimi, bootOptions, extraOption, args, smartNimcache]
  268. if sameFileContent(output, i.thVersion):
  269. copyExe(output, finalDest)
  270. echo "executables are equal: SUCCESS!"
  271. return
  272. copyExe(output, (i+1).thVersion)
  273. copyExe(output, finalDest)
  274. when not defined(windows): echo "[Warning] executables are still not equal"
  275. # -------------- clean --------------------------------------------------------
  276. const
  277. cleanExt = [
  278. ".ppu", ".o", ".obj", ".dcu", ".~pas", ".~inc", ".~dsk", ".~dpr",
  279. ".map", ".tds", ".err", ".bak", ".pyc", ".exe", ".rod", ".pdb", ".idb",
  280. ".idx", ".ilk"
  281. ]
  282. ignore = [
  283. ".bzrignore", "nim", "nim.exe", "koch", "koch.exe", ".gitignore"
  284. ]
  285. proc cleanAux(dir: string) =
  286. for kind, path in walkDir(dir):
  287. case kind
  288. of pcFile:
  289. var (_, name, ext) = splitFile(path)
  290. if ext == "" or cleanExt.contains(ext):
  291. if not ignore.contains(name):
  292. echo "removing: ", path
  293. removeFile(path)
  294. of pcDir:
  295. case splitPath(path).tail
  296. of "nimcache":
  297. echo "removing dir: ", path
  298. removeDir(path)
  299. of "dist", ".git", "icons": discard
  300. else: cleanAux(path)
  301. else: discard
  302. proc removePattern(pattern: string) =
  303. for f in walkFiles(pattern):
  304. echo "removing: ", f
  305. removeFile(f)
  306. proc clean(args: string) =
  307. removePattern("web/*.html")
  308. removePattern("doc/*.html")
  309. cleanAux(getCurrentDir())
  310. for kind, path in walkDir(getCurrentDir() / "build"):
  311. if kind == pcDir:
  312. echo "removing dir: ", path
  313. removeDir(path)
  314. # -------------- builds a release ---------------------------------------------
  315. proc winReleaseArch(arch: string) =
  316. doAssert arch in ["32", "64"]
  317. let cpu = if arch == "32": "i386" else: "amd64"
  318. template withMingw(path, body) =
  319. let prevPath = getEnv("PATH")
  320. putEnv("PATH", (if path.len > 0: path & PathSep else: "") & prevPath)
  321. try:
  322. body
  323. finally:
  324. putEnv("PATH", prevPath)
  325. withMingw r"..\mingw" & arch & r"\bin":
  326. # Rebuilding koch is necessary because it uses its pointer size to
  327. # determine which mingw link to put in the NSIS installer.
  328. inFold "winrelease koch":
  329. nimexec "c --cpu:$# koch" % cpu
  330. kochExecFold("winrelease boot", "boot -d:release --cpu:$#" % cpu)
  331. kochExecFold("winrelease zip", "--latest zip -d:release")
  332. overwriteFile r"build\nim-$#.zip" % VersionAsString,
  333. r"web\upload\download\nim-$#_x$#.zip" % [VersionAsString, arch]
  334. proc winRelease*() =
  335. # Now used from "tools/winrelease" and not directly supported by koch
  336. # anymore!
  337. # Build -docs file:
  338. when true:
  339. inFold "winrelease buildDocs":
  340. buildDocs(gaCode)
  341. withDir "web/upload/" & VersionAsString:
  342. inFold "winrelease zipdocs":
  343. exec "7z a -tzip docs-$#.zip *.html" % VersionAsString
  344. overwriteFile "web/upload/$1/docs-$1.zip" % VersionAsString,
  345. "web/upload/download/docs-$1.zip" % VersionAsString
  346. when true:
  347. inFold "winrelease csource":
  348. csource("-d:release")
  349. when sizeof(pointer) == 4:
  350. winReleaseArch "32"
  351. when sizeof(pointer) == 8:
  352. winReleaseArch "64"
  353. # -------------- tests --------------------------------------------------------
  354. template `|`(a, b): string = (if a.len > 0: a else: b)
  355. proc tests(args: string) =
  356. nimexec "cc --opt:speed testament/tester"
  357. let tester = quoteShell(getCurrentDir() / "testament/tester".exe)
  358. let success = tryExec tester & " " & (args|"all")
  359. if not success:
  360. quit("tests failed", QuitFailure)
  361. proc temp(args: string) =
  362. proc splitArgs(a: string): (string, string) =
  363. # every --options before the command (indicated by starting
  364. # with not a dash) is part of the bootArgs, the rest is part
  365. # of the programArgs:
  366. let args = os.parseCmdLine a
  367. result = ("", "")
  368. var i = 0
  369. while i < args.len and args[i][0] == '-':
  370. result[0].add " " & quoteShell(args[i])
  371. inc i
  372. while i < args.len:
  373. result[1].add " " & quoteShell(args[i])
  374. inc i
  375. let d = getAppDir()
  376. var output = d / "compiler" / "nim".exe
  377. var finalDest = d / "bin" / "nim_temp".exe
  378. # 125 is the magic number to tell git bisect to skip the current
  379. # commit.
  380. let (bootArgs, programArgs) = splitArgs(args)
  381. let nimexec = findNim()
  382. exec(nimexec & " c -d:debug --debugger:native " & bootArgs & " " & (d / "compiler" / "nim"), 125)
  383. copyExe(output, finalDest)
  384. setCurrentDir(origDir)
  385. if programArgs.len > 0: exec(finalDest & " " & programArgs)
  386. proc xtemp(cmd: string) =
  387. let d = getAppDir()
  388. copyExe(d / "bin" / "nim".exe, d / "bin" / "nim_backup".exe)
  389. try:
  390. withDir(d):
  391. temp""
  392. copyExe(d / "bin" / "nim_temp".exe, d / "bin" / "nim".exe)
  393. exec(cmd)
  394. finally:
  395. copyExe(d / "bin" / "nim_backup".exe, d / "bin" / "nim".exe)
  396. proc runCI(cmd: string) =
  397. doAssert cmd.len == 0, cmd # avoid silently ignoring
  398. echo "runCI:", cmd
  399. # note(@araq): Do not replace these commands with direct calls (eg boot())
  400. # as that would weaken our testing efforts.
  401. when defined(posix): # appveyor (on windows) didn't run this
  402. kochExecFold("Boot", "boot")
  403. # boot without -d:nimHasLibFFI to make sure this still works
  404. kochExecFold("Boot in release mode", "boot -d:release")
  405. ## build nimble early on to enable remainder to depend on it if needed
  406. kochExecFold("Build Nimble", "nimble")
  407. when false:
  408. execFold("nimble install -y libffi", "nimble install -y libffi")
  409. kochExecFold("boot -d:release -d:nimHasLibFFI", "boot -d:release -d:nimHasLibFFI")
  410. if getEnv("NIM_TEST_PACKAGES", "false") == "true":
  411. execFold("Test selected Nimble packages", "nim c -r testament/tester cat nimble-packages")
  412. else:
  413. buildTools() # altenatively, kochExec "tools --toolsNoNimble"
  414. ## run tests
  415. execFold("Test nimscript", "nim e tests/test_nimscript.nims")
  416. when defined(windows):
  417. # note: will be over-written below
  418. execFold("Compile tester", "nim c -d:nimCoroutines --os:genode -d:posix --compileOnly testament/tester")
  419. # main bottleneck here
  420. execFold("Run tester", "nim c -r -d:nimCoroutines testament/tester --pedantic all -d:nimCoroutines")
  421. execFold("Run nimdoc tests", "nim c -r nimdoc/tester")
  422. execFold("Run nimpretty tests", "nim c -r nimpretty/tester.nim")
  423. when defined(posix):
  424. execFold("Run nimsuggest tests", "nim c -r nimsuggest/tester")
  425. ## remaining actions
  426. when defined(posix):
  427. kochExecFold("Docs", "docs --git.commit:devel")
  428. kochExecFold("C sources", "csource")
  429. elif defined(windows):
  430. when false:
  431. kochExec "csource"
  432. kochExec "zip"
  433. proc pushCsources() =
  434. if not dirExists("../csources/.git"):
  435. quit "[Error] no csources git repository found"
  436. csource("-d:release")
  437. let cwd = getCurrentDir()
  438. try:
  439. copyDir("build/c_code", "../csources/c_code")
  440. copyFile("build/build.sh", "../csources/build.sh")
  441. copyFile("build/build.bat", "../csources/build.bat")
  442. copyFile("build/build64.bat", "../csources/build64.bat")
  443. copyFile("build/makefile", "../csources/makefile")
  444. setCurrentDir("../csources")
  445. for kind, path in walkDir("c_code"):
  446. if kind == pcDir:
  447. exec("git add " & path / "*.c")
  448. exec("git commit -am \"updated csources to version " & NimVersion & "\"")
  449. exec("git push origin master")
  450. exec("git tag -am \"Version $1\" v$1" % NimVersion)
  451. exec("git push origin v$1" % NimVersion)
  452. finally:
  453. setCurrentDir(cwd)
  454. proc testUnixInstall(cmdLineRest: string) =
  455. csource("-d:release " & cmdLineRest)
  456. xz(false, cmdLineRest)
  457. let oldCurrentDir = getCurrentDir()
  458. try:
  459. let destDir = getTempDir()
  460. copyFile("build/nim-$1.tar.xz" % VersionAsString,
  461. destDir / "nim-$1.tar.xz" % VersionAsString)
  462. setCurrentDir(destDir)
  463. execCleanPath("tar -xJf nim-$1.tar.xz" % VersionAsString)
  464. setCurrentDir("nim-$1" % VersionAsString)
  465. execCleanPath("sh build.sh")
  466. # first test: try if './bin/nim --version' outputs something sane:
  467. let output = execProcess("./bin/nim --version").splitLines
  468. if output.len > 0 and output[0].contains(VersionAsString):
  469. echo "Version check: success"
  470. execCleanPath("./bin/nim c koch.nim")
  471. execCleanPath("./koch boot -d:release", destDir / "bin")
  472. # check the docs build:
  473. execCleanPath("./koch docs", destDir / "bin")
  474. # check nimble builds:
  475. execCleanPath("./koch --latest tools")
  476. # check the tests work:
  477. putEnv("NIM_EXE_NOT_IN_PATH", "NOT_IN_PATH")
  478. execCleanPath("./koch tests --nim:./bin/nim cat megatest", destDir / "bin")
  479. else:
  480. echo "Version check: failure"
  481. finally:
  482. setCurrentDir oldCurrentDir
  483. proc valgrind(cmd: string) =
  484. # somewhat hacky: '=' sign means "pass to valgrind" else "pass to Nim"
  485. let args = parseCmdLine(cmd)
  486. var nimcmd = ""
  487. var valcmd = ""
  488. for i, a in args:
  489. if i == args.len-1:
  490. # last element is the filename:
  491. valcmd.add ' '
  492. valcmd.add changeFileExt(a, ExeExt)
  493. nimcmd.add ' '
  494. nimcmd.add a
  495. elif '=' in a:
  496. valcmd.add ' '
  497. valcmd.add a
  498. else:
  499. nimcmd.add ' '
  500. nimcmd.add a
  501. exec("nim c" & nimcmd)
  502. let supp = getAppDir() / "tools" / "nimgrind.supp"
  503. exec("valgrind --suppressions=" & supp & valcmd)
  504. proc showHelp() =
  505. quit(HelpText % [VersionAsString & spaces(44-len(VersionAsString)),
  506. CompileDate, CompileTime], QuitSuccess)
  507. when isMainModule:
  508. var op = initOptParser()
  509. var latest = false
  510. var stable = false
  511. template isLatest(): bool =
  512. if stable: false
  513. else:
  514. existsDir(".git") or latest
  515. while true:
  516. op.next()
  517. case op.kind
  518. of cmdLongOption, cmdShortOption:
  519. case normalize(op.key)
  520. of "latest": latest = true
  521. of "stable": stable = true
  522. else: showHelp()
  523. of cmdArgument:
  524. case normalize(op.key)
  525. of "boot": boot(op.cmdLineRest)
  526. of "clean": clean(op.cmdLineRest)
  527. of "doc", "docs": buildDocs(op.cmdLineRest)
  528. of "doc0", "docs0":
  529. # undocumented command for Araq-the-merciful:
  530. buildDocs(op.cmdLineRest & gaCode)
  531. of "pdf": buildPdfDoc(op.cmdLineRest, "doc/pdf")
  532. of "csource", "csources": csource(op.cmdLineRest)
  533. of "zip": zip(latest, op.cmdLineRest)
  534. of "xz": xz(latest, op.cmdLineRest)
  535. of "nsis": nsis(latest, op.cmdLineRest)
  536. of "geninstall": geninstall(op.cmdLineRest)
  537. of "distrohelper": geninstall()
  538. of "install": install(op.cmdLineRest)
  539. of "testinstall": testUnixInstall(op.cmdLineRest)
  540. of "runci": runCI(op.cmdLineRest)
  541. of "test", "tests": tests(op.cmdLineRest)
  542. of "temp": temp(op.cmdLineRest)
  543. of "xtemp": xtemp(op.cmdLineRest)
  544. of "wintools": bundleWinTools()
  545. of "nimble": buildNimble(isLatest())
  546. of "nimsuggest": bundleNimsuggest()
  547. of "toolsnonimble":
  548. buildTools()
  549. of "tools":
  550. buildTools()
  551. buildNimble(isLatest())
  552. of "pushcsource", "pushcsources": pushCsources()
  553. of "valgrind": valgrind(op.cmdLineRest)
  554. of "c2nim": bundleC2nim()
  555. else: showHelp()
  556. break
  557. of cmdEnd: break