koch.nim 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. const
  12. NimbleStableCommit = "8f7af860c5ce9634af880a7081c6435e1f2a5148" # master
  13. FusionStableCommit = "319aac4d43b04113831b529f8003e82f4af6a4a5"
  14. when not defined(windows):
  15. const
  16. Z3StableCommit = "65de3f748a6812eecd7db7c478d5fc54424d368b" # the version of Z3 that DrNim uses
  17. when defined(gcc) and defined(windows):
  18. when defined(x86):
  19. {.link: "icons/koch.res".}
  20. else:
  21. {.link: "icons/koch_icon.o".}
  22. when defined(amd64) and defined(windows) and defined(vcc):
  23. {.link: "icons/koch-amd64-windows-vcc.res".}
  24. when defined(i386) and defined(windows) and defined(vcc):
  25. {.link: "icons/koch-i386-windows-vcc.res".}
  26. import std/[os, strutils, parseopt, osproc]
  27. # Using `std/os` instead of `os` to fail early if config isn't set up properly.
  28. # If this fails with: `Error: cannot open file: std/os`, see
  29. # https://github.com/nim-lang/Nim/pull/14291 for explanation + how to fix.
  30. import tools / kochdocs
  31. import tools / deps
  32. const VersionAsString = system.NimVersion
  33. const
  34. HelpText = """
  35. +-----------------------------------------------------------------+
  36. | Maintenance program for Nim |
  37. | Version $1|
  38. | (c) 2017 Andreas Rumpf |
  39. +-----------------------------------------------------------------+
  40. Build time: $2, $3
  41. Usage:
  42. koch [options] command [options for command]
  43. Options:
  44. --help, -h shows this help and quits
  45. --latest bundle the installers with bleeding edge versions of
  46. external components.
  47. --stable bundle the installers with stable versions of
  48. external components (default).
  49. --nim:path use specified path for nim binary
  50. --localdocs[:path] only build local documentations. If a path is not
  51. specified (or empty), the default is used.
  52. Possible Commands:
  53. boot [options] bootstraps with given command line options
  54. distrohelper [bindir] helper for distro packagers
  55. tools builds Nim related tools
  56. toolsNoExternal builds Nim related tools (except external tools,
  57. e.g. nimble)
  58. doesn't require network connectivity
  59. nimble builds the Nimble tool
  60. fusion clone fusion into the working tree
  61. Boot options:
  62. -d:release produce a release version of the compiler
  63. -d:nimUseLinenoise use the linenoise library for interactive mode
  64. `nim secret` (not needed on Windows)
  65. -d:leanCompiler produce a compiler without JS codegen or
  66. documentation generator in order to use less RAM
  67. for bootstrapping
  68. Commands for core developers:
  69. runCI runs continuous integration (CI), e.g. from travis
  70. docs [options] generates the full documentation
  71. csource -d:danger builds the C sources for installation
  72. pdf builds the PDF documentation
  73. zip builds the installation zip package
  74. xz builds the installation tar.xz package
  75. testinstall test tar.xz package; Unix only!
  76. installdeps [options] installs external dependency (e.g. tinyc) to dist/
  77. tests [options] run the testsuite (run a subset of tests by
  78. specifying a category, e.g. `tests cat async`)
  79. temp options creates a temporary compiler for testing
  80. pushcsource push generated C sources to its repo
  81. Web options:
  82. --googleAnalytics:UA-... add the given google analytics code to the docs. To
  83. build the official docs, use UA-48159761-1
  84. """
  85. let kochExe* = when isMainModule: os.getAppFilename() # always correct when koch is main program, even if `koch` exe renamed e.g.: `nim c -o:koch_debug koch.nim`
  86. else: getAppDir() / "koch".exe # works for winrelease
  87. proc kochExec*(cmd: string) =
  88. exec kochExe.quoteShell & " " & cmd
  89. proc kochExecFold*(desc, cmd: string) =
  90. execFold(desc, kochExe.quoteShell & " " & cmd)
  91. template withDir(dir, body) =
  92. let old = getCurrentDir()
  93. try:
  94. setCurrentDir(dir)
  95. body
  96. finally:
  97. setCurrentDir(old)
  98. let origDir = getCurrentDir()
  99. setCurrentDir(getAppDir())
  100. proc tryExec(cmd: string): bool =
  101. echo(cmd)
  102. result = execShellCmd(cmd) == 0
  103. proc safeRemove(filename: string) =
  104. if fileExists(filename): removeFile(filename)
  105. proc overwriteFile(source, dest: string) =
  106. safeRemove(dest)
  107. moveFile(source, dest)
  108. proc copyExe(source, dest: string) =
  109. safeRemove(dest)
  110. copyFile(dest=dest, source=source)
  111. inclFilePermissions(dest, {fpUserExec, fpGroupExec, fpOthersExec})
  112. const
  113. compileNimInst = "tools/niminst/niminst"
  114. distDir = "dist"
  115. proc csource(args: string) =
  116. nimexec(("cc $1 -r $3 --var:version=$2 --var:mingw=none csource " &
  117. "--main:compiler/nim.nim compiler/installer.ini $1") %
  118. [args, VersionAsString, compileNimInst])
  119. proc bundleC2nim(args: string) =
  120. cloneDependency(distDir, "https://github.com/nim-lang/c2nim.git")
  121. nimCompile("dist/c2nim/c2nim",
  122. options = "--noNimblePath --path:. " & args)
  123. proc bundleNimbleExe(latest: bool, args: string) =
  124. let commit = if latest: "HEAD" else: NimbleStableCommit
  125. cloneDependency(distDir, "https://github.com/nim-lang/nimble.git",
  126. commit = commit, allowBundled = true)
  127. # installer.ini expects it under $nim/bin
  128. nimCompile("dist/nimble/src/nimble.nim",
  129. options = "-d:release --noNimblePath " & args)
  130. proc bundleNimsuggest(args: string) =
  131. nimCompileFold("Compile nimsuggest", "nimsuggest/nimsuggest.nim",
  132. options = "-d:release -d:danger " & args)
  133. proc buildVccTool(args: string) =
  134. nimCompileFold("Compile Vcc", "tools/vccexe/vccexe.nim ", options = args)
  135. proc bundleNimpretty(args: string) =
  136. nimCompileFold("Compile nimpretty", "nimpretty/nimpretty.nim",
  137. options = "-d:release " & args)
  138. proc bundleWinTools(args: string) =
  139. nimCompile("tools/finish.nim", outputDir = "", options = args)
  140. buildVccTool(args)
  141. nimCompile("tools/nimgrab.nim", options = "-d:ssl " & args)
  142. nimCompile("tools/nimgrep.nim", options = args)
  143. nimCompile("testament/testament.nim", options = args)
  144. when false:
  145. # not yet a tool worth including
  146. nimCompile(r"tools\downloader.nim",
  147. options = r"--cc:vcc --app:gui -d:ssl --noNimblePath --path:..\ui " & args)
  148. proc bundleFusion(latest: bool) =
  149. let commit = if latest: "HEAD" else: FusionStableCommit
  150. cloneDependency(distDir, "https://github.com/nim-lang/fusion.git", commit,
  151. allowBundled = true)
  152. copyDir(distDir / "fusion" / "src" / "fusion", "lib" / "fusion")
  153. proc zip(latest: bool; args: string) =
  154. bundleFusion(latest)
  155. bundleNimbleExe(latest, args)
  156. bundleNimsuggest(args)
  157. bundleNimpretty(args)
  158. bundleWinTools(args)
  159. nimexec("cc -r $2 --var:version=$1 --var:mingw=none --main:compiler/nim.nim scripts compiler/installer.ini" %
  160. [VersionAsString, compileNimInst])
  161. exec("$# --var:version=$# --var:mingw=none --main:compiler/nim.nim zip compiler/installer.ini" %
  162. ["tools/niminst/niminst".exe, VersionAsString])
  163. proc ensureCleanGit() =
  164. let (outp, status) = osproc.execCmdEx("git diff")
  165. if outp.len != 0:
  166. quit "Not a clean git repository; 'git diff' not empty!"
  167. if status != 0:
  168. quit "Not a clean git repository; 'git diff' returned non-zero!"
  169. proc xz(latest: bool; args: string) =
  170. ensureCleanGit()
  171. nimexec("cc -r $2 --var:version=$1 --var:mingw=none --main:compiler/nim.nim scripts compiler/installer.ini" %
  172. [VersionAsString, compileNimInst])
  173. exec("$# --var:version=$# --var:mingw=none --main:compiler/nim.nim xz compiler/installer.ini" %
  174. ["tools" / "niminst" / "niminst".exe, VersionAsString])
  175. proc buildTool(toolname, args: string) =
  176. nimexec("cc $# $#" % [args, toolname])
  177. copyFile(dest="bin" / splitFile(toolname).name.exe, source=toolname.exe)
  178. proc buildTools(args: string = "") =
  179. bundleNimsuggest(args)
  180. nimCompileFold("Compile nimgrep", "tools/nimgrep.nim",
  181. options = "-d:release " & args)
  182. when defined(windows): buildVccTool(args)
  183. bundleNimpretty(args)
  184. nimCompileFold("Compile nimfind", "tools/nimfind.nim",
  185. options = "-d:release " & args)
  186. nimCompileFold("Compile testament", "testament/testament.nim",
  187. options = "-d:release " & args)
  188. proc nsis(latest: bool; args: string) =
  189. bundleFusion(latest)
  190. bundleNimbleExe(latest, args)
  191. bundleNimsuggest(args)
  192. bundleWinTools(args)
  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().quoteShell(), args, VersionAsString], additionalPATH=findNim().splitFile.dir)
  218. # -------------- boot ---------------------------------------------------------
  219. proc findStartNim: string =
  220. # we try several things before giving up:
  221. # * nimExe
  222. # * bin/nim
  223. # * $PATH/nim
  224. # If these fail, we try to build nim with the "build.(sh|bat)" script.
  225. let (nim, ok) = findNimImpl()
  226. if ok: return nim
  227. when defined(Posix):
  228. const buildScript = "build.sh"
  229. if fileExists(buildScript):
  230. if tryExec("./" & buildScript): return "bin" / nim
  231. else:
  232. const buildScript = "build.bat"
  233. if fileExists(buildScript):
  234. if tryExec(buildScript): return "bin" / nim
  235. echo("Found no nim compiler and every attempt to build one failed!")
  236. quit("FAILURE")
  237. proc thVersion(i: int): string =
  238. result = ("compiler" / "nim" & $i).exe
  239. template doUseCpp(): bool = getEnv("NIM_COMPILE_TO_CPP", "false") == "true"
  240. proc boot(args: string) =
  241. var output = "compiler" / "nim".exe
  242. var finalDest = "bin" / "nim".exe
  243. # default to use the 'c' command:
  244. let useCpp = doUseCpp()
  245. let smartNimcache = (if "release" in args or "danger" in args: "nimcache/r_" else: "nimcache/d_") &
  246. hostOS & "_" & hostCPU
  247. let nimStart = findStartNim().quoteShell()
  248. for i in 0..2:
  249. # Nim versions < (1, 1) expect Nim's exception type to have a 'raiseId' field for
  250. # C++ interop. Later Nim versions do this differently and removed the 'raiseId' field.
  251. # Thus we always bootstrap the first iteration with "c" and not with "cpp" as
  252. # a workaround.
  253. let defaultCommand = if useCpp and i > 0: "cpp" else: "c"
  254. let bootOptions = if args.len == 0 or args.startsWith("-"): defaultCommand else: ""
  255. echo "iteration: ", i+1
  256. var extraOption = ""
  257. var nimi = i.thVersion
  258. if i == 0:
  259. nimi = nimStart
  260. extraOption.add " --skipUserCfg --skipParentCfg"
  261. # The configs are skipped for bootstrap
  262. # (1st iteration) to prevent newer flags from breaking bootstrap phase.
  263. let ret = execCmdEx(nimStart & " --version")
  264. doAssert ret.exitCode == 0
  265. let version = ret.output.splitLines[0]
  266. # remove these when csources get updated
  267. template addLib() =
  268. extraOption.add " --lib:lib" # see https://github.com/nim-lang/Nim/pull/14291
  269. if version.startsWith "Nim Compiler Version 0.19.0":
  270. extraOption.add " -d:nimBoostrapCsources0_19_0"
  271. addLib()
  272. elif version.startsWith "Nim Compiler Version 0.20.0": addLib()
  273. # in order to use less memory, we split the build into two steps:
  274. # --compileOnly produces a $project.json file and does not run GCC/Clang.
  275. # jsonbuild then uses the $project.json file to build the Nim binary.
  276. exec "$# $# $# --nimcache:$# $# --compileOnly compiler" / "nim.nim" %
  277. [nimi, bootOptions, extraOption, smartNimcache, args]
  278. exec "$# jsonscript --nimcache:$# $# compiler" / "nim.nim" %
  279. [nimi, smartNimcache, args]
  280. if sameFileContent(output, i.thVersion):
  281. copyExe(output, finalDest)
  282. echo "executables are equal: SUCCESS!"
  283. return
  284. copyExe(output, (i+1).thVersion)
  285. copyExe(output, finalDest)
  286. when not defined(windows): echo "[Warning] executables are still not equal"
  287. # -------------- clean --------------------------------------------------------
  288. const
  289. cleanExt = [
  290. ".ppu", ".o", ".obj", ".dcu", ".~pas", ".~inc", ".~dsk", ".~dpr",
  291. ".map", ".tds", ".err", ".bak", ".pyc", ".exe", ".rod", ".pdb", ".idb",
  292. ".idx", ".ilk"
  293. ]
  294. ignore = [
  295. ".bzrignore", "nim", "nim.exe", "koch", "koch.exe", ".gitignore"
  296. ]
  297. proc cleanAux(dir: string) =
  298. for kind, path in walkDir(dir):
  299. case kind
  300. of pcFile:
  301. var (_, name, ext) = splitFile(path)
  302. if ext == "" or cleanExt.contains(ext):
  303. if not ignore.contains(name):
  304. echo "removing: ", path
  305. removeFile(path)
  306. of pcDir:
  307. case splitPath(path).tail
  308. of "nimcache":
  309. echo "removing dir: ", path
  310. removeDir(path)
  311. of "dist", ".git", "icons": discard
  312. else: cleanAux(path)
  313. else: discard
  314. proc removePattern(pattern: string) =
  315. for f in walkFiles(pattern):
  316. echo "removing: ", f
  317. removeFile(f)
  318. proc clean(args: string) =
  319. removePattern("web/*.html")
  320. removePattern("doc/*.html")
  321. cleanAux(getCurrentDir())
  322. for kind, path in walkDir(getCurrentDir() / "build"):
  323. if kind == pcDir:
  324. echo "removing dir: ", path
  325. removeDir(path)
  326. # -------------- builds a release ---------------------------------------------
  327. proc winReleaseArch(arch: string) =
  328. doAssert arch in ["32", "64"]
  329. let cpu = if arch == "32": "i386" else: "amd64"
  330. template withMingw(path, body) =
  331. let prevPath = getEnv("PATH")
  332. putEnv("PATH", (if path.len > 0: path & PathSep else: "") & prevPath)
  333. try:
  334. body
  335. finally:
  336. putEnv("PATH", prevPath)
  337. withMingw r"..\mingw" & arch & r"\bin":
  338. # Rebuilding koch is necessary because it uses its pointer size to
  339. # determine which mingw link to put in the NSIS installer.
  340. inFold "winrelease koch":
  341. nimexec "c --cpu:$# koch" % cpu
  342. kochExecFold("winrelease boot", "boot -d:release --cpu:$#" % cpu)
  343. kochExecFold("winrelease zip", "zip -d:release")
  344. overwriteFile r"build\nim-$#.zip" % VersionAsString,
  345. r"web\upload\download\nim-$#_x$#.zip" % [VersionAsString, arch]
  346. proc winRelease*() =
  347. # Now used from "tools/winrelease" and not directly supported by koch
  348. # anymore!
  349. # Build -docs file:
  350. when true:
  351. inFold "winrelease buildDocs":
  352. buildDocs(gaCode)
  353. withDir "web/upload/" & VersionAsString:
  354. inFold "winrelease zipdocs":
  355. exec "7z a -tzip docs-$#.zip *.html" % VersionAsString
  356. overwriteFile "web/upload/$1/docs-$1.zip" % VersionAsString,
  357. "web/upload/download/docs-$1.zip" % VersionAsString
  358. when true:
  359. inFold "winrelease csource":
  360. csource("-d:danger")
  361. when sizeof(pointer) == 4:
  362. winReleaseArch "32"
  363. when sizeof(pointer) == 8:
  364. winReleaseArch "64"
  365. # -------------- tests --------------------------------------------------------
  366. template `|`(a, b): string = (if a.len > 0: a else: b)
  367. proc tests(args: string) =
  368. nimexec "cc --opt:speed testament/testament"
  369. var testCmd = quoteShell(getCurrentDir() / "testament/testament".exe)
  370. testCmd.add " " & quoteShell("--nim:" & findNim())
  371. testCmd.add " " & (args|"all")
  372. let success = tryExec testCmd
  373. if not success:
  374. quit("tests failed", QuitFailure)
  375. proc temp(args: string) =
  376. proc splitArgs(a: string): (string, string) =
  377. # every --options before the command (indicated by starting
  378. # with not a dash) is part of the bootArgs, the rest is part
  379. # of the programArgs:
  380. let args = os.parseCmdLine a
  381. result = ("", "")
  382. var i = 0
  383. while i < args.len and args[i][0] == '-':
  384. result[0].add " " & quoteShell(args[i])
  385. inc i
  386. while i < args.len:
  387. result[1].add " " & quoteShell(args[i])
  388. inc i
  389. let d = getAppDir()
  390. var output = d / "compiler" / "nim".exe
  391. var finalDest = d / "bin" / "nim_temp".exe
  392. # 125 is the magic number to tell git bisect to skip the current commit.
  393. var (bootArgs, programArgs) = splitArgs(args)
  394. if "doc" notin programArgs and
  395. "threads" notin programArgs and
  396. "js" notin programArgs and "rst2html" notin programArgs:
  397. bootArgs.add " -d:leanCompiler"
  398. let nimexec = findNim().quoteShell()
  399. exec(nimexec & " c -d:debug --debugger:native -d:nimBetterRun " & bootArgs & " " & (d / "compiler" / "nim"), 125)
  400. copyExe(output, finalDest)
  401. setCurrentDir(origDir)
  402. if programArgs.len > 0: exec(finalDest & " " & programArgs)
  403. proc xtemp(cmd: string) =
  404. let d = getAppDir()
  405. copyExe(d / "bin" / "nim".exe, d / "bin" / "nim_backup".exe)
  406. try:
  407. withDir(d):
  408. temp""
  409. copyExe(d / "bin" / "nim_temp".exe, d / "bin" / "nim".exe)
  410. exec(cmd)
  411. finally:
  412. copyExe(d / "bin" / "nim_backup".exe, d / "bin" / "nim".exe)
  413. proc buildDrNim(args: string) =
  414. if not dirExists("dist/nimz3"):
  415. exec("git clone https://github.com/zevv/nimz3.git dist/nimz3")
  416. when defined(windows):
  417. if not dirExists("dist/dlls"):
  418. exec("git clone -q https://github.com/nim-lang/dlls.git dist/dlls")
  419. copyExe("dist/dlls/libz3.dll", "bin/libz3.dll")
  420. execFold("build drnim", "nim c -o:$1 $2 drnim/drnim" % ["bin/drnim".exe, args])
  421. else:
  422. if not dirExists("dist/z3"):
  423. exec("git clone -q https://github.com/Z3Prover/z3.git dist/z3")
  424. withDir("dist/z3"):
  425. exec("git fetch")
  426. exec("git checkout " & Z3StableCommit)
  427. createDir("build")
  428. withDir("build"):
  429. exec("""cmake -DZ3_BUILD_LIBZ3_SHARED=FALSE -G "Unix Makefiles" ../""")
  430. exec("make -j4")
  431. execFold("build drnim", "nim cpp --dynlibOverride=libz3 -o:$1 $2 drnim/drnim" % ["bin/drnim".exe, args])
  432. # always run the tests for now:
  433. exec("testament/testament".exe & " --nim:" & "drnim".exe & " pat drnim/tests")
  434. proc hostInfo(): string =
  435. "hostOS: $1, hostCPU: $2, int: $3, float: $4, cpuEndian: $5, cwd: $6" %
  436. [hostOS, hostCPU, $int.sizeof, $float.sizeof, $cpuEndian, getCurrentDir()]
  437. proc installDeps(dep: string, commit = "") =
  438. # the hashes/urls are version controlled here, so can be changed seamlessly
  439. # and tied to a nim release (mimicking git submodules)
  440. var commit = commit
  441. case dep
  442. of "tinyc":
  443. if commit.len == 0: commit = "916cc2f94818a8a382dd8d4b8420978816c1dfb3"
  444. cloneDependency(distDir, "https://github.com/timotheecour/nim-tinyc-archive", commit)
  445. else: doAssert false, "unsupported: " & dep
  446. # xxx: also add linenoise, niminst etc, refs https://github.com/nim-lang/RFCs/issues/206
  447. proc runCI(cmd: string) =
  448. doAssert cmd.len == 0, cmd # avoid silently ignoring
  449. echo "runCI: ", cmd
  450. echo hostInfo()
  451. # boot without -d:nimHasLibFFI to make sure this still works
  452. kochExecFold("Boot in release mode", "boot -d:release")
  453. ## build nimble early on to enable remainder to depend on it if needed
  454. kochExecFold("Build Nimble", "nimble")
  455. if getEnv("NIM_TEST_PACKAGES", "0") == "1":
  456. execFold("Test selected Nimble packages (1)", "nim c -r testament/testament cat nimble-packages-1")
  457. elif getEnv("NIM_TEST_PACKAGES", "0") == "2":
  458. execFold("Test selected Nimble packages (2)", "nim c -r testament/testament cat nimble-packages-2")
  459. else:
  460. buildTools()
  461. ## run tests
  462. execFold("Test nimscript", "nim e tests/test_nimscript.nims")
  463. when defined(windows):
  464. # note: will be over-written below
  465. execFold("Compile tester", "nim c -d:nimCoroutines --os:genode -d:posix --compileOnly testament/testament")
  466. # main bottleneck here
  467. # xxx: even though this is the main bottlneck, we could use same code to batch the other tests
  468. #[
  469. BUG: with initOptParser, `--batch:'' all` interprets `all` as the argument of --batch
  470. ]#
  471. execFold("Run tester", "nim c -r -d:nimCoroutines testament/testament --pedantic --batch:$1 all -d:nimCoroutines" % ["NIM_TESTAMENT_BATCH".getEnv("_")])
  472. block CT_FFI:
  473. when defined(posix): # windows can be handled in future PR's
  474. execFold("nimble install -y libffi", "nimble install -y libffi")
  475. const nimFFI = "./bin/nim.ctffi"
  476. # no need to bootstrap with koch boot (would be slower)
  477. let backend = if doUseCpp(): "cpp" else: "c"
  478. execFold("build with -d:nimHasLibFFI", "nim $1 -d:release -d:nimHasLibFFI -o:$2 compiler/nim.nim" % [backend, nimFFI])
  479. execFold("test with -d:nimHasLibFFI", "$1 $2 -r testament/testament --nim:$1 r tests/misc/trunner.nim -d:nimTrunnerFfi" % [nimFFI, backend])
  480. execFold("Run nimdoc tests", "nim c -r nimdoc/tester")
  481. execFold("Run rst2html tests", "nim c -r nimdoc/rsttester")
  482. execFold("Run nimpretty tests", "nim c -r nimpretty/tester.nim")
  483. when defined(posix):
  484. execFold("Run nimsuggest tests", "nim c -r nimsuggest/tester")
  485. proc pushCsources() =
  486. if not dirExists("../csources/.git"):
  487. quit "[Error] no csources git repository found"
  488. csource("-d:danger")
  489. let cwd = getCurrentDir()
  490. try:
  491. copyDir("build/c_code", "../csources/c_code")
  492. copyFile("build/build.sh", "../csources/build.sh")
  493. copyFile("build/build.bat", "../csources/build.bat")
  494. copyFile("build/build64.bat", "../csources/build64.bat")
  495. copyFile("build/makefile", "../csources/makefile")
  496. setCurrentDir("../csources")
  497. for kind, path in walkDir("c_code"):
  498. if kind == pcDir:
  499. exec("git add " & path / "*.c")
  500. exec("git commit -am \"updated csources to version " & NimVersion & "\"")
  501. exec("git push origin master")
  502. exec("git tag -am \"Version $1\" v$1" % NimVersion)
  503. exec("git push origin v$1" % NimVersion)
  504. finally:
  505. setCurrentDir(cwd)
  506. proc testUnixInstall(cmdLineRest: string) =
  507. csource("-d:danger" & cmdLineRest)
  508. xz(false, cmdLineRest)
  509. let oldCurrentDir = getCurrentDir()
  510. try:
  511. let destDir = getTempDir()
  512. copyFile("build/nim-$1.tar.xz" % VersionAsString,
  513. destDir / "nim-$1.tar.xz" % VersionAsString)
  514. setCurrentDir(destDir)
  515. execCleanPath("tar -xJf nim-$1.tar.xz" % VersionAsString)
  516. setCurrentDir("nim-$1" % VersionAsString)
  517. execCleanPath("sh build.sh")
  518. # first test: try if './bin/nim --version' outputs something sane:
  519. let output = execProcess("./bin/nim --version").splitLines
  520. if output.len > 0 and output[0].contains(VersionAsString):
  521. echo "Version check: success"
  522. execCleanPath("./bin/nim c koch.nim")
  523. execCleanPath("./koch boot -d:release", destDir / "bin")
  524. # check the docs build:
  525. execCleanPath("./koch docs", destDir / "bin")
  526. # check nimble builds:
  527. execCleanPath("./koch tools")
  528. # check the tests work:
  529. putEnv("NIM_EXE_NOT_IN_PATH", "NOT_IN_PATH")
  530. execCleanPath("./koch tests --nim:./bin/nim cat megatest", destDir / "bin")
  531. else:
  532. echo "Version check: failure"
  533. finally:
  534. setCurrentDir oldCurrentDir
  535. proc valgrind(cmd: string) =
  536. # somewhat hacky: '=' sign means "pass to valgrind" else "pass to Nim"
  537. let args = parseCmdLine(cmd)
  538. var nimcmd = ""
  539. var valcmd = ""
  540. for i, a in args:
  541. if i == args.len-1:
  542. # last element is the filename:
  543. valcmd.add ' '
  544. valcmd.add changeFileExt(a, ExeExt)
  545. nimcmd.add ' '
  546. nimcmd.add a
  547. elif '=' in a:
  548. valcmd.add ' '
  549. valcmd.add a
  550. else:
  551. nimcmd.add ' '
  552. nimcmd.add a
  553. exec("nim c" & nimcmd)
  554. let supp = getAppDir() / "tools" / "nimgrind.supp"
  555. exec("valgrind --suppressions=" & supp & valcmd)
  556. proc showHelp() =
  557. quit(HelpText % [VersionAsString & spaces(44-len(VersionAsString)),
  558. CompileDate, CompileTime], QuitSuccess)
  559. when isMainModule:
  560. var op = initOptParser()
  561. var
  562. latest = false
  563. localDocsOnly = false
  564. localDocsOut = ""
  565. while true:
  566. op.next()
  567. case op.kind
  568. of cmdLongOption, cmdShortOption:
  569. case normalize(op.key)
  570. of "latest": latest = true
  571. of "stable": latest = false
  572. of "nim": nimExe = op.val.absolutePath # absolute so still works with changeDir
  573. of "localdocs":
  574. localDocsOnly = true
  575. if op.val.len > 0:
  576. localDocsOut = op.val.absolutePath
  577. else: showHelp()
  578. of cmdArgument:
  579. case normalize(op.key)
  580. of "boot": boot(op.cmdLineRest)
  581. of "clean": clean(op.cmdLineRest)
  582. of "doc", "docs": buildDocs(op.cmdLineRest, localDocsOnly, localDocsOut)
  583. of "doc0", "docs0":
  584. # undocumented command for Araq-the-merciful:
  585. buildDocs(op.cmdLineRest & gaCode)
  586. of "pdf": buildPdfDoc(op.cmdLineRest, "doc/pdf")
  587. of "csource", "csources": csource(op.cmdLineRest)
  588. of "zip": zip(latest, op.cmdLineRest)
  589. of "xz": xz(latest, op.cmdLineRest)
  590. of "nsis": nsis(latest, op.cmdLineRest)
  591. of "geninstall": geninstall(op.cmdLineRest)
  592. of "distrohelper": geninstall()
  593. of "install": install(op.cmdLineRest)
  594. of "testinstall": testUnixInstall(op.cmdLineRest)
  595. of "installdeps": installDeps(op.cmdLineRest)
  596. of "runci": runCI(op.cmdLineRest)
  597. of "test", "tests": tests(op.cmdLineRest)
  598. of "temp": temp(op.cmdLineRest)
  599. of "xtemp": xtemp(op.cmdLineRest)
  600. of "wintools": bundleWinTools(op.cmdLineRest)
  601. of "nimble": bundleNimbleExe(latest, op.cmdLineRest)
  602. of "nimsuggest": bundleNimsuggest(op.cmdLineRest)
  603. # toolsNoNimble is kept for backward compatibility with build scripts
  604. of "toolsnonimble", "toolsnoexternal":
  605. buildTools(op.cmdLineRest)
  606. of "tools":
  607. buildTools(op.cmdLineRest)
  608. bundleNimbleExe(latest, op.cmdLineRest)
  609. bundleFusion(latest)
  610. of "pushcsource", "pushcsources": pushCsources()
  611. of "valgrind": valgrind(op.cmdLineRest)
  612. of "c2nim": bundleC2nim(op.cmdLineRest)
  613. of "drnim": buildDrNim(op.cmdLineRest)
  614. of "fusion": bundleFusion(latest)
  615. else: showHelp()
  616. break
  617. of cmdEnd: break