koch.nim 24 KB

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