Skip to content

Commit 9807362

Browse files
committed
update koch so the nightlies will be able to bundle Nimble
- Previously, `winrelease` called `bundleNimbleExe`, which tries to clone nimble to `dist/nimble` unconditionally. - Since nightlies source tarball bundles nimble, git couldn't clone because `dist/nimble` was already there. This leads to 1.0.x couldn't be built with nimble bundled. - The fix backports the logic from Nim >= 1.2.8 which can check if `dist/nimble` was bundled by the source archive. With this fix we can re-enable bundling of nimble to the source archive for 1.0.x.
1 parent c32e002 commit 9807362

File tree

2 files changed

+58
-41
lines changed

2 files changed

+58
-41
lines changed

koch.nim

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import
2727
os, strutils, parseopt, osproc, streams
2828

2929
import tools / kochdocs
30+
import tools / deps
3031

3132
const VersionAsString = system.NimVersion
3233

@@ -115,52 +116,25 @@ proc copyExe(source, dest: string) =
115116

116117
const
117118
compileNimInst = "tools/niminst/niminst"
119+
distDir = "dist"
118120

119121
proc csource(args: string) =
120122
nimexec(("cc $1 -r $3 --var:version=$2 --var:mingw=none csource " &
121123
"--main:compiler/nim.nim compiler/installer.ini $1") %
122124
[args, VersionAsString, compileNimInst])
123125

124126
proc bundleC2nim(args: string) =
125-
if not dirExists("dist/c2nim/.git"):
126-
exec("git clone https://github.com/nim-lang/c2nim.git dist/c2nim")
127+
cloneDependency(distDir, "https://github.com/nim-lang/c2nim.git")
127128
nimCompile("dist/c2nim/c2nim",
128129
options = "--noNimblePath --path:. " & args)
129130

130131
proc bundleNimbleExe(latest: bool, args: string) =
131-
if not dirExists("dist/nimble/.git"):
132-
exec("git clone https://github.com/nim-lang/nimble.git dist/nimble")
133-
if not latest:
134-
withDir("dist/nimble"):
135-
exec("git fetch")
136-
exec("git checkout " & NimbleStableCommit)
132+
let commit = if latest: "HEAD" else: NimbleStableCommit
133+
cloneDependency(distDir, "https://github.com/nim-lang/nimble.git",
134+
commit = commit, allowBundled = true)
137135
# installer.ini expects it under $nim/bin
138136
nimCompile("dist/nimble/src/nimble.nim",
139-
options = "-d:release --nilseqs:on " & args)
140-
141-
proc buildNimble(latest: bool, args: string) =
142-
# if koch is used for a tar.xz, build the dist/nimble we shipped
143-
# with the tarball:
144-
var installDir = "dist/nimble"
145-
if not latest and dirExists(installDir) and not dirExists("dist/nimble/.git"):
146-
discard "don't do the git dance"
147-
else:
148-
if not dirExists("dist/nimble/.git"):
149-
if dirExists(installDir):
150-
var id = 0
151-
while dirExists("dist/nimble" & $id):
152-
inc id
153-
installDir = "dist/nimble" & $id
154-
exec("git clone https://github.com/nim-lang/nimble.git " & installDir)
155-
withDir(installDir):
156-
if latest:
157-
exec("git checkout -f master")
158-
exec("git pull")
159-
else:
160-
exec("git fetch")
161-
exec("git checkout " & NimbleStableCommit)
162-
nimCompile(installDir / "src/nimble.nim",
163-
options = "--noNimblePath --nilseqs:on -d:release " & args)
137+
options = "-d:release --noNimblePath --nilseqs:on " & args)
164138

165139
proc bundleNimsuggest(args: string) =
166140
nimCompileFold("Compile nimsuggest", "nimsuggest/nimsuggest.nim",
@@ -318,10 +292,10 @@ proc boot(args: string) =
318292
# in order to use less memory, we split the build into two steps:
319293
# --compileOnly produces a $project.json file and does not run GCC/Clang.
320294
# jsonbuild then uses the $project.json file to build the Nim binary.
321-
exec "$# $# $# $# --nimcache:$# --compileOnly compiler" / "nim.nim" %
322-
[nimi, bootOptions, extraOption, args, smartNimcache]
323-
exec "$# jsonscript $# --nimcache:$# compiler" / "nim.nim" %
324-
[nimi, args, smartNimcache]
295+
exec "$# $# $# --nimcache:$# $# --compileOnly compiler" / "nim.nim" %
296+
[nimi, bootOptions, extraOption, smartNimcache, args]
297+
exec "$# jsonscript --nimcache:$# $# compiler" / "nim.nim" %
298+
[nimi, smartNimcache, args]
325299

326300
if sameFileContent(output, i.thVersion):
327301
copyExe(output, finalDest)
@@ -479,7 +453,7 @@ proc runCI(cmd: string) =
479453
when defined(posix): # appveyor (on windows) didn't run this
480454
kochExecFold("Boot", "boot")
481455
# boot without -d:nimHasLibFFI to make sure this still works
482-
kochExecFold("Boot in release mode", "boot -d:release -d:danger")
456+
kochExecFold("Boot in release mode", "boot -d:release")
483457

484458
## build nimble early on to enable remainder to depend on it if needed
485459
kochExecFold("Build Nimble", "nimble")
@@ -491,7 +465,7 @@ proc runCI(cmd: string) =
491465
if getEnv("NIM_TEST_PACKAGES", "false") == "true":
492466
execFold("Test selected Nimble packages", "nim c -r testament/testament cat nimble-packages")
493467
else:
494-
buildTools() # altenatively, kochExec "tools --toolsNoNimble"
468+
buildTools()
495469

496470
## run tests
497471
execFold("Test nimscript", "nim e tests/test_nimscript.nims")
@@ -628,13 +602,13 @@ when isMainModule:
628602
of "temp": temp(op.cmdLineRest)
629603
of "xtemp": xtemp(op.cmdLineRest)
630604
of "wintools": bundleWinTools(op.cmdLineRest)
631-
of "nimble": buildNimble(latest, op.cmdLineRest)
605+
of "nimble": bundleNimbleExe(latest, op.cmdLineRest)
632606
of "nimsuggest": bundleNimsuggest(op.cmdLineRest)
633607
of "toolsnonimble":
634608
buildTools(op.cmdLineRest)
635609
of "tools":
636610
buildTools(op.cmdLineRest)
637-
buildNimble(latest, op.cmdLineRest)
611+
bundleNimbleExe(latest, op.cmdLineRest)
638612
of "pushcsource", "pushcsources": pushCsources()
639613
of "valgrind": valgrind(op.cmdLineRest)
640614
of "c2nim": bundleC2nim(op.cmdLineRest)

tools/deps.nim

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os, uri, strformat, osproc, strutils
2+
3+
proc exec(cmd: string) =
4+
echo "deps.cmd: " & cmd
5+
let status = execShellCmd(cmd)
6+
doAssert status == 0, cmd
7+
8+
proc execEx(cmd: string): tuple[output: TaintedString, exitCode: int] =
9+
echo "deps.cmd: " & cmd
10+
execCmdEx(cmd, {poStdErrToStdOut, poUsePath, poEvalCommand})
11+
12+
proc isGitRepo(dir: string): bool =
13+
# This command is used to get the relative path to the root of the repository.
14+
# Using this, we can verify whether a folder is a git repository by checking
15+
# whether the command success and if the output is empty.
16+
let (output, status) = execEx fmt"git -C {quoteShell(dir)} rev-parse --show-cdup"
17+
# On Windows there will be a trailing newline on success, remove it.
18+
# The value of a successful call typically won't have a whitespace (it's
19+
# usually a series of ../), so we know that it's safe to unconditionally
20+
# remove trailing whitespaces from the result.
21+
result = status == 0 and output.strip() == ""
22+
23+
const commitHead* = "HEAD"
24+
25+
proc cloneDependency*(destDirBase: string, url: string, commit = commitHead,
26+
appendRepoName = true, allowBundled = false) =
27+
let destDirBase = destDirBase.absolutePath
28+
let p = url.parseUri.path
29+
let name = p.splitFile.name
30+
var destDir = destDirBase
31+
if appendRepoName: destDir = destDir / name
32+
let destDir2 = destDir.quoteShell
33+
if not dirExists(destDir):
34+
# note: old code used `destDir / .git` but that wouldn't prevent git clone
35+
# from failing
36+
exec fmt"git clone -q {url} {destDir2}"
37+
if isGitRepo(destDir):
38+
exec fmt"git -C {destDir2} fetch -q"
39+
exec fmt"git -C {destDir2} checkout -q {commit}"
40+
elif allowBundled:
41+
discard "this dependency was bundled with Nim, don't do anything"
42+
else:
43+
quit "FAILURE: " & destdir & " already exists but is not a git repo"

0 commit comments

Comments
 (0)