koch.nim 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. # examples of possible values: #head, #ea82b54, 1.2.3
  14. FusionStableHash = "#372ee4313827ef9f2ea388840f7d6b46c2b1b014"
  15. HeadHash = "#head"
  16. when not defined(windows):
  17. const
  18. Z3StableCommit = "65de3f748a6812eecd7db7c478d5fc54424d368b" # the version of Z3 that DrNim uses
  19. when defined(gcc) and defined(windows):
  20. when defined(x86):
  21. {.link: "icons/koch.res".}
  22. else:
  23. {.link: "icons/koch_icon.o".}
  24. when defined(amd64) and defined(windows) and defined(vcc):
  25. {.link: "icons/koch-amd64-windows-vcc.res".}
  26. when defined(i386) and defined(windows) and defined(vcc):
  27. {.link: "icons/koch-i386-windows-vcc.res".}
  28. import std/[os, strutils, parseopt, osproc]
  29. # Using `std/os` instead of `os` to fail early if config isn't set up properly.
  30. # If this fails with: `Error: cannot open file: std/os`, see
  31. # https://github.com/nim-lang/Nim/pull/14291 for explanation + how to fix.
  32. import tools / kochdocs
  33. import tools / deps
  34. const VersionAsString = system.NimVersion
  35. const
  36. HelpText = """
  37. +-----------------------------------------------------------------+
  38. | Maintenance program for Nim |
  39. | Version $1|
  40. | (c) 2017 Andreas Rumpf |
  41. +-----------------------------------------------------------------+
  42. Build time: $2, $3
  43. Usage:
  44. koch [options] command [options for command]
  45. Options:
  46. --help, -h shows this help and quits
  47. --latest bundle the installers with bleeding edge versions of
  48. external components.
  49. --stable bundle the installers with stable versions of
  50. external components (default).
  51. --nim:path use specified path for nim binary
  52. --localdocs[:path] only build local documentations. If a path is not
  53. specified (or empty), the default is used.
  54. Possible Commands:
  55. boot [options] bootstraps with given command line options
  56. distrohelper [bindir] helper for distro packagers
  57. tools builds Nim related tools
  58. toolsNoExternal builds Nim related tools (except external tools,
  59. e.g. nimble)
  60. doesn't require network connectivity
  61. nimble builds the Nimble tool
  62. fusion installs fusion via Nimble
  63. Boot options:
  64. -d:release produce a release version of the compiler
  65. -d:nimUseLinenoise use the linenoise library for interactive mode
  66. `nim secret` (not needed on Windows)
  67. -d:leanCompiler produce a compiler without JS codegen or
  68. documentation generator in order to use less RAM
  69. for bootstrapping
  70. Commands for core developers:
  71. runCI runs continuous integration (CI), e.g. from travis
  72. docs [options] generates the full documentation
  73. csource -d:danger builds the C sources for installation
  74. pdf builds the PDF documentation
  75. zip builds the installation zip package
  76. xz builds the installation tar.xz package
  77. testinstall test tar.xz package; Unix only!
  78. installdeps [options] installs external dependency (e.g. tinyc) to dist/
  79. tests [options] run the testsuite (run a subset of tests by
  80. specifying a category, e.g. `tests cat async`)
  81. temp options creates a temporary compiler for testing
  82. Web options:
  83. --googleAnalytics:UA-... add the given google analytics code to the docs. To
  84. build the official docs, use UA-48159761-1
  85. """
  86. 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`
  87. else: getAppDir() / "koch".exe # works for winrelease
  88. proc kochExec*(cmd: string) =
  89. exec kochExe.quoteShell & " " & cmd
  90. proc kochExecFold*(desc, cmd: string) =
  91. execFold(desc, kochExe.quoteShell & " " & cmd)
  92. template withDir(dir, body) =
  93. let old = getCurrentDir()
  94. try:
  95. setCurrentDir(dir)
  96. body
  97. finally:
  98. setCurrentDir(old)
  99. let origDir = getCurrentDir()
  100. setCurrentDir(getAppDir())
  101. proc tryExec(cmd: string): bool =
  102. echo(cmd)
  103. result = execShellCmd(cmd) == 0
  104. proc safeRemove(filename: string) =
  105. if fileExists(filename): removeFile(filename)
  106. proc overwriteFile(source, dest: string) =
  107. safeRemove(dest)
  108. moveFile(source, dest)
  109. proc copyExe(source, dest: string) =
  110. safeRemove(dest)
  111. copyFile(dest=dest, source=source)
  112. inclFilePermissions(dest, {fpUserExec, fpGroupExec, fpOthersExec})
  113. const
  114. compileNimInst = "tools/niminst/niminst"
  115. distDir = "dist"
  116. proc csource(args: string) =
  117. nimexec(("cc $1 -r $3 --var:version=$2 --var:mingw=none csource " &
  118. "--main:compiler/nim.nim compiler/installer.ini $1") %
  119. [args, VersionAsString, compileNimInst])
  120. proc bundleC2nim(args: string) =
  121. cloneDependency(distDir, "https://github.com/nim-lang/c2nim.git")
  122. nimCompile("dist/c2nim/c2nim",
  123. options = "--noNimblePath --path:. " & args)
  124. proc bundleNimbleExe(latest: bool, args: string) =
  125. let commit = if latest: "HEAD" else: NimbleStableCommit
  126. cloneDependency(distDir, "https://github.com/nim-lang/nimble.git",
  127. commit = commit, allowBundled = true)
  128. # installer.ini expects it under $nim/bin
  129. nimCompile("dist/nimble/src/nimble.nim",
  130. options = "-d:release --noNimblePath " & args)
  131. proc bundleNimsuggest(args: string) =
  132. nimCompileFold("Compile nimsuggest", "nimsuggest/nimsuggest.nim",
  133. options = "-d:danger " & args)
  134. proc buildVccTool(args: string) =
  135. let input = "tools/vccexe/vccexe.nim"
  136. if contains(args, "--cc:vcc"):
  137. nimCompileFold("Compile Vcc", input, "build", options = args)
  138. let fileName = input.splitFile.name
  139. moveFile(exe("build" / fileName), exe("bin" / fileName))
  140. else:
  141. nimCompileFold("Compile Vcc", input, options = args)
  142. proc bundleNimpretty(args: string) =
  143. nimCompileFold("Compile nimpretty", "nimpretty/nimpretty.nim",
  144. options = "-d:release " & args)
  145. proc bundleWinTools(args: string) =
  146. nimCompile("tools/finish.nim", outputDir = "", options = args)
  147. buildVccTool(args)
  148. nimCompile("tools/nimgrab.nim", options = "-d:ssl " & args)
  149. nimCompile("tools/nimgrep.nim", options = args)
  150. nimCompile("testament/testament.nim", options = args)
  151. when false:
  152. # not yet a tool worth including
  153. nimCompile(r"tools\downloader.nim",
  154. options = r"--cc:vcc --app:gui -d:ssl --noNimblePath --path:..\ui " & args)
  155. proc zip(latest: bool; args: string) =
  156. bundleNimbleExe(latest, args)
  157. bundleNimsuggest(args)
  158. bundleNimpretty(args)
  159. bundleWinTools(args)
  160. nimexec("cc -r $2 --var:version=$1 --var:mingw=none --main:compiler/nim.nim scripts compiler/installer.ini" %
  161. [VersionAsString, compileNimInst])
  162. exec("$# --var:version=$# --var:mingw=none --main:compiler/nim.nim zip compiler/installer.ini" %
  163. ["tools/niminst/niminst".exe, VersionAsString])
  164. proc ensureCleanGit() =
  165. let (outp, status) = osproc.execCmdEx("git diff")
  166. if outp.len != 0:
  167. quit "Not a clean git repository; 'git diff' not empty!"
  168. if status != 0:
  169. quit "Not a clean git repository; 'git diff' returned non-zero!"
  170. proc xz(latest: bool; args: string) =
  171. ensureCleanGit()
  172. nimexec("cc -r $2 --var:version=$1 --var:mingw=none --main:compiler/nim.nim scripts compiler/installer.ini" %
  173. [VersionAsString, compileNimInst])
  174. exec("$# --var:version=$# --var:mingw=none --main:compiler/nim.nim xz compiler/installer.ini" %
  175. ["tools" / "niminst" / "niminst".exe, VersionAsString])
  176. proc buildTool(toolname, args: string) =
  177. nimexec("cc $# $#" % [args, toolname])
  178. copyFile(dest="bin" / splitFile(toolname).name.exe, source=toolname.exe)
  179. proc buildTools(args: string = "") =
  180. bundleNimsuggest(args)
  181. nimCompileFold("Compile nimgrep", "tools/nimgrep.nim",
  182. options = "-d:release " & args)
  183. when defined(windows): buildVccTool(args)
  184. bundleNimpretty(args)
  185. nimCompileFold("Compile testament", "testament/testament.nim",
  186. options = "-d:release " & args)
  187. proc nsis(latest: bool; args: string) =
  188. bundleNimbleExe(latest, args)
  189. bundleNimsuggest(args)
  190. bundleWinTools(args)
  191. # make sure we have generated the niminst executables:
  192. buildTool("tools/niminst/niminst", args)
  193. #buildTool("tools/nimgrep", args)
  194. # produce 'nim_debug.exe':
  195. #exec "nim c compiler" / "nim.nim"
  196. #copyExe("compiler/nim".exe, "bin/nim_debug".exe)
  197. exec(("tools" / "niminst" / "niminst --var:version=$# --var:mingw=mingw$#" &
  198. " nsis compiler/installer.ini") % [VersionAsString, $(sizeof(pointer)*8)])
  199. proc geninstall(args="") =
  200. nimexec("cc -r $# --var:version=$# --var:mingw=none --main:compiler/nim.nim scripts compiler/installer.ini $#" %
  201. [compileNimInst, VersionAsString, args])
  202. proc install(args: string) =
  203. geninstall()
  204. exec("sh ./install.sh $#" % args)
  205. when false:
  206. proc web(args: string) =
  207. nimexec("js tools/dochack/dochack.nim")
  208. nimexec("cc -r tools/nimweb.nim $# web/website.ini --putenv:nimversion=$#" %
  209. [args, VersionAsString])
  210. proc website(args: string) =
  211. nimexec("cc -r tools/nimweb.nim $# --website web/website.ini --putenv:nimversion=$#" %
  212. [args, VersionAsString])
  213. proc pdf(args="") =
  214. exec("$# cc -r tools/nimweb.nim $# --pdf web/website.ini --putenv:nimversion=$#" %
  215. [findNim().quoteShell(), args, VersionAsString], additionalPATH=findNim().splitFile.dir)
  216. # -------------- boot ---------------------------------------------------------
  217. proc findStartNim: string =
  218. # we try several things before giving up:
  219. # * nimExe
  220. # * bin/nim
  221. # * $PATH/nim
  222. # If these fail, we try to build nim with the "build.(sh|bat)" script.
  223. let (nim, ok) = findNimImpl()
  224. if ok: return nim
  225. when defined(posix):
  226. const buildScript = "build.sh"
  227. if fileExists(buildScript):
  228. if tryExec("./" & buildScript): return "bin" / nim
  229. else:
  230. const buildScript = "build.bat"
  231. if fileExists(buildScript):
  232. if tryExec(buildScript): return "bin" / nim
  233. echo("Found no nim compiler and every attempt to build one failed!")
  234. quit("FAILURE")
  235. proc thVersion(i: int): string =
  236. result = ("compiler" / "nim" & $i).exe
  237. template doUseCpp(): bool = getEnv("NIM_COMPILE_TO_CPP", "false") == "true"
  238. proc boot(args: string) =
  239. ## bootstrapping is a process that involves 3 steps:
  240. ## 1. use csourcesAny to produce nim1.exe. This nim1.exe is buggy but
  241. ## rock solid for building a Nim compiler. It shouldn't be used for anything else.
  242. ## 2. use nim1.exe to produce nim2.exe. nim2.exe is the one you really need.
  243. ## 3. We use nim2.exe to build nim3.exe. nim3.exe is equal to nim2.exe except for timestamps.
  244. ## This step ensures a minimum amount of quality. We know that nim2.exe can be used
  245. ## for Nim compiler development.
  246. var output = "compiler" / "nim".exe
  247. var finalDest = "bin" / "nim".exe
  248. # default to use the 'c' command:
  249. let useCpp = doUseCpp()
  250. let smartNimcache = (if "release" in args or "danger" in args: "nimcache/r_" else: "nimcache/d_") &
  251. hostOS & "_" & hostCPU
  252. let nimStart = findStartNim().quoteShell()
  253. for i in 0..2:
  254. # Nim versions < (1, 1) expect Nim's exception type to have a 'raiseId' field for
  255. # C++ interop. Later Nim versions do this differently and removed the 'raiseId' field.
  256. # Thus we always bootstrap the first iteration with "c" and not with "cpp" as
  257. # a workaround.
  258. let defaultCommand = if useCpp and i > 0: "cpp" else: "c"
  259. let bootOptions = if args.len == 0 or args.startsWith("-"): defaultCommand else: ""
  260. echo "iteration: ", i+1
  261. var extraOption = ""
  262. var nimi = i.thVersion
  263. if i == 0:
  264. nimi = nimStart
  265. extraOption.add " --skipUserCfg --skipParentCfg"
  266. # The configs are skipped for bootstrap
  267. # (1st iteration) to prevent newer flags from breaking bootstrap phase.
  268. let ret = execCmdEx(nimStart & " --version")
  269. doAssert ret.exitCode == 0
  270. let version = ret.output.splitLines[0]
  271. if version.startsWith "Nim Compiler Version 0.20.0":
  272. extraOption.add " --lib:lib" # see https://github.com/nim-lang/Nim/pull/14291
  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. let output = d / "compiler" / "nim".exe
  391. let 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 = " -d:leanCompiler" & bootArgs
  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 icTest(args: string) =
  414. temp("")
  415. let inp = os.parseCmdLine(args)[0]
  416. let content = readFile(inp)
  417. let nimExe = getAppDir() / "bin" / "nim_temp".exe
  418. var i = 0
  419. for fragment in content.split("#!EDIT!#"):
  420. let file = inp.replace(".nim", "_temp.nim")
  421. writeFile(file, fragment)
  422. var cmd = nimExe & " cpp --ic:on -d:nimIcIntegrityChecks --listcmd "
  423. if i == 0:
  424. cmd.add "-f "
  425. cmd.add quoteShell(file)
  426. exec(cmd)
  427. inc i
  428. proc buildDrNim(args: string) =
  429. if not dirExists("dist/nimz3"):
  430. exec("git clone https://github.com/zevv/nimz3.git dist/nimz3")
  431. when defined(windows):
  432. if not dirExists("dist/dlls"):
  433. exec("git clone -q https://github.com/nim-lang/dlls.git dist/dlls")
  434. copyExe("dist/dlls/libz3.dll", "bin/libz3.dll")
  435. execFold("build drnim", "nim c -o:$1 $2 drnim/drnim" % ["bin/drnim".exe, args])
  436. else:
  437. if not dirExists("dist/z3"):
  438. exec("git clone -q https://github.com/Z3Prover/z3.git dist/z3")
  439. withDir("dist/z3"):
  440. exec("git fetch")
  441. exec("git checkout " & Z3StableCommit)
  442. createDir("build")
  443. withDir("build"):
  444. exec("""cmake -DZ3_BUILD_LIBZ3_SHARED=FALSE -G "Unix Makefiles" ../""")
  445. exec("make -j4")
  446. execFold("build drnim", "nim cpp --dynlibOverride=libz3 -o:$1 $2 drnim/drnim" % ["bin/drnim".exe, args])
  447. # always run the tests for now:
  448. exec("testament/testament".exe & " --nim:" & "drnim".exe & " pat drnim/tests")
  449. proc hostInfo(): string =
  450. "hostOS: $1, hostCPU: $2, int: $3, float: $4, cpuEndian: $5, cwd: $6" %
  451. [hostOS, hostCPU, $int.sizeof, $float.sizeof, $cpuEndian, getCurrentDir()]
  452. proc installDeps(dep: string, commit = "") =
  453. # the hashes/urls are version controlled here, so can be changed seamlessly
  454. # and tied to a nim release (mimicking git submodules)
  455. var commit = commit
  456. case dep
  457. of "tinyc":
  458. if commit.len == 0: commit = "916cc2f94818a8a382dd8d4b8420978816c1dfb3"
  459. cloneDependency(distDir, "https://github.com/timotheecour/nim-tinyc-archive", commit)
  460. else: doAssert false, "unsupported: " & dep
  461. # xxx: also add linenoise, niminst etc, refs https://github.com/nim-lang/RFCs/issues/206
  462. proc runCI(cmd: string) =
  463. doAssert cmd.len == 0, cmd # avoid silently ignoring
  464. echo "runCI: ", cmd
  465. echo hostInfo()
  466. # boot without -d:nimHasLibFFI to make sure this still works
  467. # `--lib:lib` is needed for bootstrap on openbsd, for reasons described in
  468. # https://github.com/nim-lang/Nim/pull/14291 (`getAppFilename` bugsfor older nim on openbsd).
  469. kochExecFold("Boot in release mode", "boot -d:release -d:nimStrictMode --lib:lib")
  470. when false: # debugging: when you need to run only 1 test in CI, use something like this:
  471. execFold("debugging test", "nim r tests/stdlib/tosproc.nim")
  472. doAssert false, "debugging only"
  473. ## build nimble early on to enable remainder to depend on it if needed
  474. kochExecFold("Build Nimble", "nimble")
  475. let batchParam = "--batch:$1" % "NIM_TESTAMENT_BATCH".getEnv("_")
  476. if getEnv("NIM_TEST_PACKAGES", "0") == "1":
  477. execFold("Test selected Nimble packages", "nim r testament/testament $# pcat nimble-packages" % batchParam)
  478. else:
  479. buildTools()
  480. for a in "zip opengl sdl1 jester@#head".split:
  481. let buildDeps = "build"/"deps" # xxx factor pending https://github.com/timotheecour/Nim/issues/616
  482. # if this gives `Additional info: "build/deps" [OSError]`, make sure nimble is >= v0.12.0,
  483. # otherwise `absolutePath` is needed, refs https://github.com/nim-lang/nimble/issues/901
  484. execFold("", "nimble install -y --nimbleDir:$# $#" % [buildDeps.quoteShell, a])
  485. ## run tests
  486. execFold("Test nimscript", "nim e tests/test_nimscript.nims")
  487. when defined(windows):
  488. execFold("Compile tester", "nim c --usenimcache -d:nimCoroutines --os:genode -d:posix --compileOnly testament/testament")
  489. # main bottleneck here
  490. # xxx: even though this is the main bottleneck, we could speedup the rest via batching with `--batch`.
  491. # BUG: with initOptParser, `--batch:'' all` interprets `all` as the argument of --batch, pending bug #14343
  492. execFold("Run tester", "nim c -r --putenv:NIM_TESTAMENT_REMOTE_NETWORKING:1 -d:nimStrictMode testament/testament $# all -d:nimCoroutines" % batchParam)
  493. block: # nimHasLibFFI:
  494. when defined(posix): # windows can be handled in future PR's
  495. execFold("nimble install -y libffi", "nimble install -y libffi")
  496. const nimFFI = "bin/nim.ctffi"
  497. # no need to bootstrap with koch boot (would be slower)
  498. let backend = if doUseCpp(): "cpp" else: "c"
  499. execFold("build with -d:nimHasLibFFI", "nim $1 -d:release -d:nimHasLibFFI -o:$2 compiler/nim.nim" % [backend, nimFFI])
  500. execFold("test with -d:nimHasLibFFI", "$1 $2 -r testament/testament --nim:$1 r tests/misc/trunner.nim -d:nimTrunnerFfi" % [nimFFI, backend])
  501. execFold("Run nimdoc tests", "nim r nimdoc/tester")
  502. execFold("Run rst2html tests", "nim r nimdoc/rsttester")
  503. execFold("Run nimpretty tests", "nim r nimpretty/tester.nim")
  504. when defined(posix):
  505. execFold("Run nimsuggest tests", "nim r nimsuggest/tester")
  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. proc branchDone() =
  560. let thisBranch = execProcess("git symbolic-ref --short HEAD").strip()
  561. if thisBranch != "devel" and thisBranch != "":
  562. exec("git checkout devel")
  563. exec("git branch -D " & thisBranch)
  564. exec("git pull --rebase")
  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. of "pushcsource":
  616. quit "use this instead: https://github.com/nim-lang/csources_v1/blob/master/push_c_code.nim"
  617. of "valgrind": valgrind(op.cmdLineRest)
  618. of "c2nim": bundleC2nim(op.cmdLineRest)
  619. of "drnim": buildDrNim(op.cmdLineRest)
  620. of "fusion":
  621. let suffix = if latest: HeadHash else: FusionStableHash
  622. exec("nimble install -y fusion@$#" % suffix)
  623. of "ic": icTest(op.cmdLineRest)
  624. of "branchdone": branchDone()
  625. else: showHelp()
  626. break
  627. of cmdEnd: break