koch.nim 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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 = "372ee4313827ef9f2ea388840f7d6b46c2b1b014"
  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. let input = "tools/vccexe/vccexe.nim"
  135. if contains(args, "--cc:vcc"):
  136. nimCompileFold("Compile Vcc", input, "build", options = args)
  137. let fileName = input.splitFile.name
  138. moveFile(exe("build" / fileName), exe("bin" / fileName))
  139. else:
  140. nimCompileFold("Compile Vcc", input, options = args)
  141. proc bundleNimpretty(args: string) =
  142. nimCompileFold("Compile nimpretty", "nimpretty/nimpretty.nim",
  143. options = "-d:release " & args)
  144. proc bundleWinTools(args: string) =
  145. nimCompile("tools/finish.nim", outputDir = "", options = args)
  146. buildVccTool(args)
  147. nimCompile("tools/nimgrab.nim", options = "-d:ssl " & args)
  148. nimCompile("tools/nimgrep.nim", options = args)
  149. nimCompile("testament/testament.nim", options = args)
  150. when false:
  151. # not yet a tool worth including
  152. nimCompile(r"tools\downloader.nim",
  153. options = r"--cc:vcc --app:gui -d:ssl --noNimblePath --path:..\ui " & args)
  154. proc bundleFusion(latest: bool) =
  155. let commit = if latest: "HEAD" else: FusionStableCommit
  156. cloneDependency(distDir, "https://github.com/nim-lang/fusion.git", commit,
  157. allowBundled = true)
  158. copyDir(distDir / "fusion" / "src" / "fusion", "lib" / "fusion")
  159. proc zip(latest: bool; args: string) =
  160. bundleFusion(latest)
  161. bundleNimbleExe(latest, args)
  162. bundleNimsuggest(args)
  163. bundleNimpretty(args)
  164. bundleWinTools(args)
  165. nimexec("cc -r $2 --var:version=$1 --var:mingw=none --main:compiler/nim.nim scripts compiler/installer.ini" %
  166. [VersionAsString, compileNimInst])
  167. exec("$# --var:version=$# --var:mingw=none --main:compiler/nim.nim zip compiler/installer.ini" %
  168. ["tools/niminst/niminst".exe, VersionAsString])
  169. proc ensureCleanGit() =
  170. let (outp, status) = osproc.execCmdEx("git diff")
  171. if outp.len != 0:
  172. quit "Not a clean git repository; 'git diff' not empty!"
  173. if status != 0:
  174. quit "Not a clean git repository; 'git diff' returned non-zero!"
  175. proc xz(latest: bool; args: string) =
  176. ensureCleanGit()
  177. nimexec("cc -r $2 --var:version=$1 --var:mingw=none --main:compiler/nim.nim scripts compiler/installer.ini" %
  178. [VersionAsString, compileNimInst])
  179. exec("$# --var:version=$# --var:mingw=none --main:compiler/nim.nim xz compiler/installer.ini" %
  180. ["tools" / "niminst" / "niminst".exe, VersionAsString])
  181. proc buildTool(toolname, args: string) =
  182. nimexec("cc $# $#" % [args, toolname])
  183. copyFile(dest="bin" / splitFile(toolname).name.exe, source=toolname.exe)
  184. proc buildTools(args: string = "") =
  185. bundleNimsuggest(args)
  186. nimCompileFold("Compile nimgrep", "tools/nimgrep.nim",
  187. options = "-d:release " & args)
  188. when defined(windows): buildVccTool(args)
  189. bundleNimpretty(args)
  190. nimCompileFold("Compile nimfind", "tools/nimfind.nim",
  191. options = "-d:release " & args)
  192. nimCompileFold("Compile testament", "testament/testament.nim",
  193. options = "-d:release " & args)
  194. proc nsis(latest: bool; args: string) =
  195. bundleFusion(latest)
  196. bundleNimbleExe(latest, args)
  197. bundleNimsuggest(args)
  198. bundleWinTools(args)
  199. # make sure we have generated the niminst executables:
  200. buildTool("tools/niminst/niminst", args)
  201. #buildTool("tools/nimgrep", args)
  202. # produce 'nim_debug.exe':
  203. #exec "nim c compiler" / "nim.nim"
  204. #copyExe("compiler/nim".exe, "bin/nim_debug".exe)
  205. exec(("tools" / "niminst" / "niminst --var:version=$# --var:mingw=mingw$#" &
  206. " nsis compiler/installer.ini") % [VersionAsString, $(sizeof(pointer)*8)])
  207. proc geninstall(args="") =
  208. nimexec("cc -r $# --var:version=$# --var:mingw=none --main:compiler/nim.nim scripts compiler/installer.ini $#" %
  209. [compileNimInst, VersionAsString, args])
  210. proc install(args: string) =
  211. geninstall()
  212. exec("sh ./install.sh $#" % args)
  213. when false:
  214. proc web(args: string) =
  215. nimexec("js tools/dochack/dochack.nim")
  216. nimexec("cc -r tools/nimweb.nim $# web/website.ini --putenv:nimversion=$#" %
  217. [args, VersionAsString])
  218. proc website(args: string) =
  219. nimexec("cc -r tools/nimweb.nim $# --website web/website.ini --putenv:nimversion=$#" %
  220. [args, VersionAsString])
  221. proc pdf(args="") =
  222. exec("$# cc -r tools/nimweb.nim $# --pdf web/website.ini --putenv:nimversion=$#" %
  223. [findNim().quoteShell(), args, VersionAsString], additionalPATH=findNim().splitFile.dir)
  224. # -------------- boot ---------------------------------------------------------
  225. proc findStartNim: string =
  226. # we try several things before giving up:
  227. # * nimExe
  228. # * bin/nim
  229. # * $PATH/nim
  230. # If these fail, we try to build nim with the "build.(sh|bat)" script.
  231. let (nim, ok) = findNimImpl()
  232. if ok: return nim
  233. when defined(Posix):
  234. const buildScript = "build.sh"
  235. if fileExists(buildScript):
  236. if tryExec("./" & buildScript): return "bin" / nim
  237. else:
  238. const buildScript = "build.bat"
  239. if fileExists(buildScript):
  240. if tryExec(buildScript): return "bin" / nim
  241. echo("Found no nim compiler and every attempt to build one failed!")
  242. quit("FAILURE")
  243. proc thVersion(i: int): string =
  244. result = ("compiler" / "nim" & $i).exe
  245. template doUseCpp(): bool = getEnv("NIM_COMPILE_TO_CPP", "false") == "true"
  246. proc boot(args: string) =
  247. var output = "compiler" / "nim".exe
  248. var finalDest = "bin" / "nim".exe
  249. # default to use the 'c' command:
  250. let useCpp = doUseCpp()
  251. let smartNimcache = (if "release" in args or "danger" in args: "nimcache/r_" else: "nimcache/d_") &
  252. hostOS & "_" & hostCPU
  253. let nimStart = findStartNim().quoteShell()
  254. for i in 0..2:
  255. # Nim versions < (1, 1) expect Nim's exception type to have a 'raiseId' field for
  256. # C++ interop. Later Nim versions do this differently and removed the 'raiseId' field.
  257. # Thus we always bootstrap the first iteration with "c" and not with "cpp" as
  258. # a workaround.
  259. let defaultCommand = if useCpp and i > 0: "cpp" else: "c"
  260. let bootOptions = if args.len == 0 or args.startsWith("-"): defaultCommand else: ""
  261. echo "iteration: ", i+1
  262. var extraOption = ""
  263. var nimi = i.thVersion
  264. if i == 0:
  265. nimi = nimStart
  266. extraOption.add " --skipUserCfg --skipParentCfg"
  267. # The configs are skipped for bootstrap
  268. # (1st iteration) to prevent newer flags from breaking bootstrap phase.
  269. let ret = execCmdEx(nimStart & " --version")
  270. doAssert ret.exitCode == 0
  271. let version = ret.output.splitLines[0]
  272. # remove these when csources get updated
  273. template addLib() =
  274. extraOption.add " --lib:lib" # see https://github.com/nim-lang/Nim/pull/14291
  275. if version.startsWith "Nim Compiler Version 0.19.0":
  276. extraOption.add " -d:nimBoostrapCsources0_19_0"
  277. addLib()
  278. elif version.startsWith "Nim Compiler Version 0.20.0": addLib()
  279. # in order to use less memory, we split the build into two steps:
  280. # --compileOnly produces a $project.json file and does not run GCC/Clang.
  281. # jsonbuild then uses the $project.json file to build the Nim binary.
  282. exec "$# $# $# --nimcache:$# $# --compileOnly compiler" / "nim.nim" %
  283. [nimi, bootOptions, extraOption, smartNimcache, args]
  284. exec "$# jsonscript --nimcache:$# $# compiler" / "nim.nim" %
  285. [nimi, smartNimcache, args]
  286. if sameFileContent(output, i.thVersion):
  287. copyExe(output, finalDest)
  288. echo "executables are equal: SUCCESS!"
  289. return
  290. copyExe(output, (i+1).thVersion)
  291. copyExe(output, finalDest)
  292. when not defined(windows): echo "[Warning] executables are still not equal"
  293. # -------------- clean --------------------------------------------------------
  294. const
  295. cleanExt = [
  296. ".ppu", ".o", ".obj", ".dcu", ".~pas", ".~inc", ".~dsk", ".~dpr",
  297. ".map", ".tds", ".err", ".bak", ".pyc", ".exe", ".rod", ".pdb", ".idb",
  298. ".idx", ".ilk"
  299. ]
  300. ignore = [
  301. ".bzrignore", "nim", "nim.exe", "koch", "koch.exe", ".gitignore"
  302. ]
  303. proc cleanAux(dir: string) =
  304. for kind, path in walkDir(dir):
  305. case kind
  306. of pcFile:
  307. var (_, name, ext) = splitFile(path)
  308. if ext == "" or cleanExt.contains(ext):
  309. if not ignore.contains(name):
  310. echo "removing: ", path
  311. removeFile(path)
  312. of pcDir:
  313. case splitPath(path).tail
  314. of "nimcache":
  315. echo "removing dir: ", path
  316. removeDir(path)
  317. of "dist", ".git", "icons": discard
  318. else: cleanAux(path)
  319. else: discard
  320. proc removePattern(pattern: string) =
  321. for f in walkFiles(pattern):
  322. echo "removing: ", f
  323. removeFile(f)
  324. proc clean(args: string) =
  325. removePattern("web/*.html")
  326. removePattern("doc/*.html")
  327. cleanAux(getCurrentDir())
  328. for kind, path in walkDir(getCurrentDir() / "build"):
  329. if kind == pcDir:
  330. echo "removing dir: ", path
  331. removeDir(path)
  332. # -------------- builds a release ---------------------------------------------
  333. proc winReleaseArch(arch: string) =
  334. doAssert arch in ["32", "64"]
  335. let cpu = if arch == "32": "i386" else: "amd64"
  336. template withMingw(path, body) =
  337. let prevPath = getEnv("PATH")
  338. putEnv("PATH", (if path.len > 0: path & PathSep else: "") & prevPath)
  339. try:
  340. body
  341. finally:
  342. putEnv("PATH", prevPath)
  343. withMingw r"..\mingw" & arch & r"\bin":
  344. # Rebuilding koch is necessary because it uses its pointer size to
  345. # determine which mingw link to put in the NSIS installer.
  346. inFold "winrelease koch":
  347. nimexec "c --cpu:$# koch" % cpu
  348. kochExecFold("winrelease boot", "boot -d:release --cpu:$#" % cpu)
  349. kochExecFold("winrelease zip", "zip -d:release")
  350. overwriteFile r"build\nim-$#.zip" % VersionAsString,
  351. r"web\upload\download\nim-$#_x$#.zip" % [VersionAsString, arch]
  352. proc winRelease*() =
  353. # Now used from "tools/winrelease" and not directly supported by koch
  354. # anymore!
  355. # Build -docs file:
  356. when true:
  357. inFold "winrelease buildDocs":
  358. buildDocs(gaCode)
  359. withDir "web/upload/" & VersionAsString:
  360. inFold "winrelease zipdocs":
  361. exec "7z a -tzip docs-$#.zip *.html" % VersionAsString
  362. overwriteFile "web/upload/$1/docs-$1.zip" % VersionAsString,
  363. "web/upload/download/docs-$1.zip" % VersionAsString
  364. when true:
  365. inFold "winrelease csource":
  366. csource("-d:danger")
  367. when sizeof(pointer) == 4:
  368. winReleaseArch "32"
  369. when sizeof(pointer) == 8:
  370. winReleaseArch "64"
  371. # -------------- tests --------------------------------------------------------
  372. template `|`(a, b): string = (if a.len > 0: a else: b)
  373. proc tests(args: string) =
  374. nimexec "cc --opt:speed testament/testament"
  375. var testCmd = quoteShell(getCurrentDir() / "testament/testament".exe)
  376. testCmd.add " " & quoteShell("--nim:" & findNim())
  377. testCmd.add " " & (args|"all")
  378. let success = tryExec testCmd
  379. if not success:
  380. quit("tests failed", QuitFailure)
  381. proc temp(args: string) =
  382. proc splitArgs(a: string): (string, string) =
  383. # every --options before the command (indicated by starting
  384. # with not a dash) is part of the bootArgs, the rest is part
  385. # of the programArgs:
  386. let args = os.parseCmdLine a
  387. result = ("", "")
  388. var i = 0
  389. while i < args.len and args[i][0] == '-':
  390. result[0].add " " & quoteShell(args[i])
  391. inc i
  392. while i < args.len:
  393. result[1].add " " & quoteShell(args[i])
  394. inc i
  395. let d = getAppDir()
  396. var output = d / "compiler" / "nim".exe
  397. var finalDest = d / "bin" / "nim_temp".exe
  398. # 125 is the magic number to tell git bisect to skip the current commit.
  399. var (bootArgs, programArgs) = splitArgs(args)
  400. if "doc" notin programArgs and
  401. "threads" notin programArgs and
  402. "js" notin programArgs and "rst2html" notin programArgs:
  403. bootArgs.add " -d:leanCompiler"
  404. let nimexec = findNim().quoteShell()
  405. exec(nimexec & " c -d:debug --debugger:native -d:nimBetterRun " & bootArgs & " " & (d / "compiler" / "nim"), 125)
  406. copyExe(output, finalDest)
  407. setCurrentDir(origDir)
  408. if programArgs.len > 0: exec(finalDest & " " & programArgs)
  409. proc xtemp(cmd: string) =
  410. let d = getAppDir()
  411. copyExe(d / "bin" / "nim".exe, d / "bin" / "nim_backup".exe)
  412. try:
  413. withDir(d):
  414. temp""
  415. copyExe(d / "bin" / "nim_temp".exe, d / "bin" / "nim".exe)
  416. exec(cmd)
  417. finally:
  418. copyExe(d / "bin" / "nim_backup".exe, d / "bin" / "nim".exe)
  419. proc buildDrNim(args: string) =
  420. if not dirExists("dist/nimz3"):
  421. exec("git clone https://github.com/zevv/nimz3.git dist/nimz3")
  422. when defined(windows):
  423. if not dirExists("dist/dlls"):
  424. exec("git clone -q https://github.com/nim-lang/dlls.git dist/dlls")
  425. copyExe("dist/dlls/libz3.dll", "bin/libz3.dll")
  426. execFold("build drnim", "nim c -o:$1 $2 drnim/drnim" % ["bin/drnim".exe, args])
  427. else:
  428. if not dirExists("dist/z3"):
  429. exec("git clone -q https://github.com/Z3Prover/z3.git dist/z3")
  430. withDir("dist/z3"):
  431. exec("git fetch")
  432. exec("git checkout " & Z3StableCommit)
  433. createDir("build")
  434. withDir("build"):
  435. exec("""cmake -DZ3_BUILD_LIBZ3_SHARED=FALSE -G "Unix Makefiles" ../""")
  436. exec("make -j4")
  437. execFold("build drnim", "nim cpp --dynlibOverride=libz3 -o:$1 $2 drnim/drnim" % ["bin/drnim".exe, args])
  438. # always run the tests for now:
  439. exec("testament/testament".exe & " --nim:" & "drnim".exe & " pat drnim/tests")
  440. proc hostInfo(): string =
  441. "hostOS: $1, hostCPU: $2, int: $3, float: $4, cpuEndian: $5, cwd: $6" %
  442. [hostOS, hostCPU, $int.sizeof, $float.sizeof, $cpuEndian, getCurrentDir()]
  443. proc installDeps(dep: string, commit = "") =
  444. # the hashes/urls are version controlled here, so can be changed seamlessly
  445. # and tied to a nim release (mimicking git submodules)
  446. var commit = commit
  447. case dep
  448. of "tinyc":
  449. if commit.len == 0: commit = "916cc2f94818a8a382dd8d4b8420978816c1dfb3"
  450. cloneDependency(distDir, "https://github.com/timotheecour/nim-tinyc-archive", commit)
  451. else: doAssert false, "unsupported: " & dep
  452. # xxx: also add linenoise, niminst etc, refs https://github.com/nim-lang/RFCs/issues/206
  453. proc runCI(cmd: string) =
  454. doAssert cmd.len == 0, cmd # avoid silently ignoring
  455. echo "runCI: ", cmd
  456. echo hostInfo()
  457. # boot without -d:nimHasLibFFI to make sure this still works
  458. kochExecFold("Boot in release mode", "boot -d:release")
  459. ## build nimble early on to enable remainder to depend on it if needed
  460. kochExecFold("Build Nimble", "nimble")
  461. if getEnv("NIM_TEST_PACKAGES", "0") == "1":
  462. execFold("Test selected Nimble packages (1)", "nim c -r testament/testament cat nimble-packages-1")
  463. elif getEnv("NIM_TEST_PACKAGES", "0") == "2":
  464. execFold("Test selected Nimble packages (2)", "nim c -r testament/testament cat nimble-packages-2")
  465. else:
  466. buildTools()
  467. ## run tests
  468. execFold("Test nimscript", "nim e tests/test_nimscript.nims")
  469. when defined(windows):
  470. # note: will be over-written below
  471. execFold("Compile tester", "nim c -d:nimCoroutines --os:genode -d:posix --compileOnly testament/testament")
  472. # main bottleneck here
  473. # xxx: even though this is the main bottlneck, we could use same code to batch the other tests
  474. #[
  475. BUG: with initOptParser, `--batch:'' all` interprets `all` as the argument of --batch
  476. ]#
  477. execFold("Run tester", "nim c -r -d:nimCoroutines testament/testament --pedantic --batch:$1 all -d:nimCoroutines" % ["NIM_TESTAMENT_BATCH".getEnv("_")])
  478. block CT_FFI:
  479. when defined(posix): # windows can be handled in future PR's
  480. execFold("nimble install -y libffi", "nimble install -y libffi")
  481. const nimFFI = "./bin/nim.ctffi"
  482. # no need to bootstrap with koch boot (would be slower)
  483. let backend = if doUseCpp(): "cpp" else: "c"
  484. execFold("build with -d:nimHasLibFFI", "nim $1 -d:release -d:nimHasLibFFI -o:$2 compiler/nim.nim" % [backend, nimFFI])
  485. execFold("test with -d:nimHasLibFFI", "$1 $2 -r testament/testament --nim:$1 r tests/misc/trunner.nim -d:nimTrunnerFfi" % [nimFFI, backend])
  486. execFold("Run nimdoc tests", "nim c -r nimdoc/tester")
  487. execFold("Run rst2html tests", "nim c -r nimdoc/rsttester")
  488. execFold("Run nimpretty tests", "nim c -r nimpretty/tester.nim")
  489. when defined(posix):
  490. execFold("Run nimsuggest tests", "nim c -r nimsuggest/tester")
  491. proc pushCsources() =
  492. if not dirExists("../csources/.git"):
  493. quit "[Error] no csources git repository found"
  494. csource("-d:danger")
  495. let cwd = getCurrentDir()
  496. try:
  497. copyDir("build/c_code", "../csources/c_code")
  498. copyFile("build/build.sh", "../csources/build.sh")
  499. copyFile("build/build.bat", "../csources/build.bat")
  500. copyFile("build/build64.bat", "../csources/build64.bat")
  501. copyFile("build/makefile", "../csources/makefile")
  502. setCurrentDir("../csources")
  503. for kind, path in walkDir("c_code"):
  504. if kind == pcDir:
  505. exec("git add " & path / "*.c")
  506. exec("git commit -am \"updated csources to version " & NimVersion & "\"")
  507. exec("git push origin master")
  508. exec("git tag -am \"Version $1\" v$1" % NimVersion)
  509. exec("git push origin v$1" % NimVersion)
  510. finally:
  511. setCurrentDir(cwd)
  512. proc testUnixInstall(cmdLineRest: string) =
  513. csource("-d:danger" & cmdLineRest)
  514. xz(false, cmdLineRest)
  515. let oldCurrentDir = getCurrentDir()
  516. try:
  517. let destDir = getTempDir()
  518. copyFile("build/nim-$1.tar.xz" % VersionAsString,
  519. destDir / "nim-$1.tar.xz" % VersionAsString)
  520. setCurrentDir(destDir)
  521. execCleanPath("tar -xJf nim-$1.tar.xz" % VersionAsString)
  522. setCurrentDir("nim-$1" % VersionAsString)
  523. execCleanPath("sh build.sh")
  524. # first test: try if './bin/nim --version' outputs something sane:
  525. let output = execProcess("./bin/nim --version").splitLines
  526. if output.len > 0 and output[0].contains(VersionAsString):
  527. echo "Version check: success"
  528. execCleanPath("./bin/nim c koch.nim")
  529. execCleanPath("./koch boot -d:release", destDir / "bin")
  530. # check the docs build:
  531. execCleanPath("./koch docs", destDir / "bin")
  532. # check nimble builds:
  533. execCleanPath("./koch tools")
  534. # check the tests work:
  535. putEnv("NIM_EXE_NOT_IN_PATH", "NOT_IN_PATH")
  536. execCleanPath("./koch tests --nim:./bin/nim cat megatest", destDir / "bin")
  537. else:
  538. echo "Version check: failure"
  539. finally:
  540. setCurrentDir oldCurrentDir
  541. proc valgrind(cmd: string) =
  542. # somewhat hacky: '=' sign means "pass to valgrind" else "pass to Nim"
  543. let args = parseCmdLine(cmd)
  544. var nimcmd = ""
  545. var valcmd = ""
  546. for i, a in args:
  547. if i == args.len-1:
  548. # last element is the filename:
  549. valcmd.add ' '
  550. valcmd.add changeFileExt(a, ExeExt)
  551. nimcmd.add ' '
  552. nimcmd.add a
  553. elif '=' in a:
  554. valcmd.add ' '
  555. valcmd.add a
  556. else:
  557. nimcmd.add ' '
  558. nimcmd.add a
  559. exec("nim c" & nimcmd)
  560. let supp = getAppDir() / "tools" / "nimgrind.supp"
  561. exec("valgrind --suppressions=" & supp & valcmd)
  562. proc showHelp() =
  563. quit(HelpText % [VersionAsString & spaces(44-len(VersionAsString)),
  564. CompileDate, CompileTime], QuitSuccess)
  565. when isMainModule:
  566. var op = initOptParser()
  567. var
  568. latest = false
  569. localDocsOnly = false
  570. localDocsOut = ""
  571. while true:
  572. op.next()
  573. case op.kind
  574. of cmdLongOption, cmdShortOption:
  575. case normalize(op.key)
  576. of "latest": latest = true
  577. of "stable": latest = false
  578. of "nim": nimExe = op.val.absolutePath # absolute so still works with changeDir
  579. of "localdocs":
  580. localDocsOnly = true
  581. if op.val.len > 0:
  582. localDocsOut = op.val.absolutePath
  583. else: showHelp()
  584. of cmdArgument:
  585. case normalize(op.key)
  586. of "boot": boot(op.cmdLineRest)
  587. of "clean": clean(op.cmdLineRest)
  588. of "doc", "docs": buildDocs(op.cmdLineRest, localDocsOnly, localDocsOut)
  589. of "doc0", "docs0":
  590. # undocumented command for Araq-the-merciful:
  591. buildDocs(op.cmdLineRest & gaCode)
  592. of "pdf": buildPdfDoc(op.cmdLineRest, "doc/pdf")
  593. of "csource", "csources": csource(op.cmdLineRest)
  594. of "zip": zip(latest, op.cmdLineRest)
  595. of "xz": xz(latest, op.cmdLineRest)
  596. of "nsis": nsis(latest, op.cmdLineRest)
  597. of "geninstall": geninstall(op.cmdLineRest)
  598. of "distrohelper": geninstall()
  599. of "install": install(op.cmdLineRest)
  600. of "testinstall": testUnixInstall(op.cmdLineRest)
  601. of "installdeps": installDeps(op.cmdLineRest)
  602. of "runci": runCI(op.cmdLineRest)
  603. of "test", "tests": tests(op.cmdLineRest)
  604. of "temp": temp(op.cmdLineRest)
  605. of "xtemp": xtemp(op.cmdLineRest)
  606. of "wintools": bundleWinTools(op.cmdLineRest)
  607. of "nimble": bundleNimbleExe(latest, op.cmdLineRest)
  608. of "nimsuggest": bundleNimsuggest(op.cmdLineRest)
  609. # toolsNoNimble is kept for backward compatibility with build scripts
  610. of "toolsnonimble", "toolsnoexternal":
  611. buildTools(op.cmdLineRest)
  612. of "tools":
  613. buildTools(op.cmdLineRest)
  614. bundleNimbleExe(latest, op.cmdLineRest)
  615. bundleFusion(latest)
  616. of "pushcsource", "pushcsources": pushCsources()
  617. of "valgrind": valgrind(op.cmdLineRest)
  618. of "c2nim": bundleC2nim(op.cmdLineRest)
  619. of "drnim": buildDrNim(op.cmdLineRest)
  620. of "fusion": bundleFusion(latest)
  621. else: showHelp()
  622. break
  623. of cmdEnd: break