forked from nim-works/nimskull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
koch.py
executable file
·278 lines (223 loc) · 9.47 KB
/
koch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python3
# Script to bootstrap `koch` if necessary
#
# Copyright (c) 2021 Leorize <leorize+oss@disroot.org>
#
# Licensed under the terms of the MIT license. See "license.txt" included in
# this distribution for more information.
from pathlib import Path
from typing import Any, ClassVar, Optional
import os
import shutil
import subprocess
import sys
# Nim source directory, which is the directory this script resides in
NimSource = Path(__file__).resolve(strict=True).parent
# The bootstrap nimskullcache folder
Nimcache = NimSource / "nimskullcache"
def exe(p: Path) -> Path:
"""
Add the ".exe" extension if needed for Windows
"""
if sys.platform.startswith("win32"):
if p.suffix != ".exe":
return p.with_name(p.name + ".exe")
else:
return p
else:
return p
def run(*args: Any, cwd: Optional[Path] = None, replace=False) -> None:
"""
Print and run the command described by `args`, raising on failure.
If `replace` is set to `True`, simulate a process replacement by running
the command and quit with its exit code. This is done to support Windows,
of which os.execv have a different semantics.
"""
# Stringify everything so we don't have to do this site-by-site
run_args = [str(x) for x in args]
print("Running:", run_args, flush=True)
completed = subprocess.run(run_args, check=(not replace), cwd=cwd)
if replace:
sys.exit(completed.returncode)
def capture(*args: Any) -> subprocess.CompletedProcess:
"""Run the command described by `args` and capture its output as UTF-8"""
# Stringify everything so we don't have to do this site-by-site
run_args = [str(x) for x in args]
return subprocess.run(run_args, capture_output=True, encoding="utf-8")
class Bootstrap:
"""This class manages bootstrap compilers, including building and downloading them."""
# The bootstrap source location
Source: ClassVar = NimSource / "build" / "csources"
# The bootstrap compiler location
Compiler: ClassVar = exe(NimSource / "bin" / "nim-boot")
# The bootstrap compiler binary store location
Binaries: ClassVar = Source / "bin"
# The location of build configuration
ConfigPath: ClassVar = NimSource / "config" / "build_config.txt"
# The location of bootstrap source bundle state
SourceReleasePath: ClassVar = NimSource / "build" / "csources-bundled-version"
def __init__(self) -> None:
"""Load configuration and initialize the object"""
self.sourceUrl = ""
self.sourceCommit = ""
self.sourceRelease = ""
# The tag used for the versioned bootstrap compiler
versioned_bin_tag = ""
try:
with open(Bootstrap.SourceReleasePath) as releaseFile:
self.sourceRelease = releaseFile.read().strip()
versioned_bin_tag = self.sourceRelease
except OSError:
# There are no release csources bundled
pass
# Read build configuration if there are no release csources bundled
if self.sourceRelease == "":
with open(Bootstrap.ConfigPath) as config:
for line in config:
(key, _, value) = line.rstrip().partition("=")
if key == "nim_csourcesUrl":
self.sourceUrl = value
elif key == "nim_csourcesHash":
self.sourceCommit = value
if self.sourceUrl == "":
raise RuntimeError(
"Could not find the configuration for bootstrap source URL"
)
if self.sourceCommit == "":
raise RuntimeError(
"Could not find the configuration for bootstrap source commit"
)
versioned_bin_tag = self.sourceCommit
# The name of the compiler binary in the store
self.versioned_bin = exe(Bootstrap.Binaries / ("nim" + "-" + versioned_bin_tag))
def is_built(self) -> bool:
"""Returns whether the bootstrap exist in the store"""
return self.versioned_bin.is_file()
@classmethod
def _git_capture(self, *args: Any) -> subprocess.CompletedProcess:
"""Runs git at the source folder and capture its outputs"""
return capture("git", "-C", self.Source, *args)
@classmethod
def _git(self, *args: Any) -> None:
"""Runs git at the source folder, raise an exception on error"""
run("git", "-C", self.Source, *args)
def fetch(self) -> None:
"""Download the bootstrap compiler source if needed"""
# There is no need to fetch if the release source is used
if self.sourceRelease != "":
return
localSourceUrl = Bootstrap._git_capture(
"remote", "get-url", "origin"
).stdout.rstrip()
if localSourceUrl != self.sourceUrl:
if localSourceUrl != "":
print(
f"Bootstrap source URL ({localSourceUrl}) differs from configuration ({self.sourceUrl})"
)
print(f"Removing bootstrap source at {Bootstrap.Source} and refetch")
shutil.rmtree(Bootstrap.Source)
print("Fetching bootstrap compiler source")
run(
"git",
"clone",
"--depth=1",
"--single-branch",
self.sourceUrl,
Bootstrap.Source,
)
localCommit = Bootstrap._git_capture(
"rev-parse", "--verify", "HEAD"
).stdout.rstrip()
if localCommit != self.sourceCommit:
print(
f"Local commit ({localCommit}) differs from configuration ({self.sourceCommit})"
)
print(
f"Downloading and checking out the bootstrap commit ({self.sourceCommit})"
)
Bootstrap._git("fetch", "origin", self.sourceCommit)
Bootstrap._git("checkout", self.sourceCommit)
def build(self) -> None:
"""Build the bootstrap compiler if necessary"""
if not self.is_built():
print(f"Bootstrap compiler {self.versioned_bin} not found, building")
self.fetch()
if sys.platform.startswith("win32"):
# Make on Windows is a hit and miss, use the batch file here instead.
run(Bootstrap.Source / "build.bat", cwd=Bootstrap.Source)
else:
makeprg = "make"
if shutil.which("gmake") is not None:
makeprg = "gmake"
# Check if this is GNU make
is_gnu_make = False
try:
is_gnu_make = capture(makeprg, "--version").stdout.startswith(
"GNU Make"
)
except OSError:
# Guess we don't even have make
pass
if is_gnu_make:
cpus = os.cpu_count()
# If the number of CPUs can't be detected, default to 1.
if cpus is None:
cpus = 1
run(makeprg, "-s", "-C", Bootstrap.Source, "-j", cpus)
else:
# If GNU make is not found, use the (slower) build script
run(
"sh",
Bootstrap.Source / "build.sh",
cwd=Bootstrap.Source,
)
# Copy the built binary into a "versioned" variant
shutil.copy(exe(Bootstrap.Binaries / "nim"), self.versioned_bin)
print(f"Instantiating {self.versioned_bin} as {Bootstrap.Compiler}")
shutil.copy(self.versioned_bin, Bootstrap.Compiler)
def run(self, *args) -> None:
"""Build (if needed) and run the bootstrap compiler, raise on error"""
self.build()
run(Bootstrap.Compiler, *args)
def main() -> None:
bootstrap = Bootstrap()
# Implements the fetch-bootstrap command. See koch.nim for more
# information.
if len(sys.argv) > 1 and sys.argv[1] == "fetch-bootstrap":
bootstrap.fetch()
return
# In case --help is passed before the bootstrap compiler is built, warn the
# user about it.
if "-h" in sys.argv or "--help" in sys.argv:
if not bootstrap.is_built():
print(
"Warning: koch and its dependencies will be downloaded and built before --help is processed."
)
print("Building koch.nim")
# The path to koch's binary
kochSourceDir = NimSource / "tools" / "koch"
kochBinary = exe(kochSourceDir / "koch")
kochSource = kochSourceDir / "koch.nim"
bootstrap.run(
"c",
"--hints:off",
# (as of v1.0.11) This is required to silent "CC:" even with --hints:off.
"--hint:CC:off",
# Silence "UnknownMagic" warnings that are common place due to the
# bootstrapping compiler being older than the stdlib.
"--warning:UnknownMagic:off",
# Prevent users configuration and/or configuration placed in a parent
# directory from interfering, as they might specify flags that are not
# in the bootstrap compiler.
"--skipParentCfg",
"--skipUserCfg",
f"--nimcache:{Nimcache}/koch",
f"--out:{kochBinary}",
kochSource,
)
print("Running koch.nim")
# Tell koch.nim where the source code is
os.environ["KOCH_NIM_SOURCE"] = str(NimSource)
run(kochBinary, *sys.argv[1:], replace=True)
if __name__ == "__main__":
main()