koch.nim 25 KB

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