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
|
#!/usr/bin/env python3
import hashlib
import json
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
# path within the repository (constants)
REPOS = "etc/repos.json"
BOOTSTRAP_CC = ["clang++", "-std=c++20", "-DBOOTSTRAP_BUILD_TOOL"]
MAIN_MODULE = ""
MAIN_TARGET = ""
MAIN_STAGE = "bin/just"
# relevant directories (global variables)
SRCDIR = os.getcwd()
WRKDIR = None
DISTDIR = []
def git_hash(content):
header = "blob {}\0".format(len(content)).encode('utf-8')
h = hashlib.sha1()
h.update(header)
h.update(content)
return h.hexdigest()
def get_checksum(filename):
with open(filename, "rb") as f:
data = f.read()
return git_hash(data)
def get_archive(*, distfile, fetch):
# Fetch the archive, if necessary. Return path to archive
for d in DISTDIR:
candidate_path = os.path.join(d, distfile)
if os.path.isfile(candidate_path):
return candidate_path
# Fetch to bootstrap working directory
fetch_dir = os.path.join(WRKDIR, "fetch")
os.makedirs(fetch_dir, exist_ok=True)
target = os.path.join(fetch_dir, distfile)
subprocess.run(["wget", "-O", target, fetch])
return target
def run(cmd, *, cwd, **kwargs):
print("Running %r in %r" % (cmd, cwd), flush=True)
subprocess.run(cmd, cwd=cwd, check=True, **kwargs)
def setup_deps():
# unpack all dependencies and return a list of
# additional C++ flags required
with open(os.path.join(SRCDIR, REPOS)) as f:
config = json.load(f)["repositories"]
include_location = os.path.join(WRKDIR, "dep_includes")
link_flags = []
os.makedirs(include_location)
for repo, total_desc in config.items():
desc = total_desc.get("repository", {})
if not isinstance(desc, dict):
# Indirect definition; we will set up the repository at the
# resolved place, which also has to be part of the global
# repository description.
continue
hints = total_desc.get("bootstrap", {})
if desc.get("type") in ["archive", "zip"]:
fetch = desc["fetch"]
distfile = desc.get("distfile") or os.path.basename(fetch)
archive = get_archive(distfile=distfile, fetch=fetch)
actual_checksum = get_checksum(archive)
expected_checksum = desc.get("content")
if actual_checksum != expected_checksum:
print("Checksum mismatch for %r. Expected %r, found %r"
% (archive, expected_checksum, actual_checksum))
print("Unpacking %r from %r" % (repo, archive))
unpack_location = os.path.join(WRKDIR, "deps", repo)
os.makedirs(unpack_location)
if desc["type"] == "zip":
subprocess.run(["unzip", "-d", ".", archive],
cwd=unpack_location, stdout=subprocess.DEVNULL)
else:
subprocess.run(["tar", "xf", archive],
cwd=unpack_location)
subdir = os.path.join(unpack_location,
desc.get("subdir", "."))
include_dir = os.path.join(subdir,
hints.get("include_dir", "."))
include_name = hints.get("include_name", repo)
if include_name == ".":
for entry in os.listdir(include_dir):
os.symlink(os.path.normpath(os.path.join(include_dir, entry)),
os.path.join(include_location, entry))
else:
os.symlink(os.path.normpath(include_dir),
os.path.join(include_location, include_name))
if "build" in hints:
run(["sh", "-c", hints["build"]], cwd=subdir)
if "link" in hints:
link_flags.extend(["-L", subdir])
if "link" in hints:
link_flags.extend(hints["link"])
return {
"include": ["-I", include_location],
"link": link_flags
}
def bootstrap():
# TODO: add package build mode, building against preinstalled dependencies
# rather than building dependencies ourselves.
print("Bootstrapping in %r from sources %r, taking files from %r"
% (WRKDIR, SRCDIR, DISTDIR))
os.makedirs(WRKDIR, exist_ok=True)
dep_flags = setup_deps();
# handle proto
src_wrkdir = os.path.join(WRKDIR, "src")
shutil.copytree(SRCDIR, src_wrkdir)
flags = ["-I", src_wrkdir] + dep_flags["include"]
cpp_files = []
for root, dirs, files in os.walk(src_wrkdir):
if 'test' in dirs:
dirs.remove('test')
if 'execution_api' in dirs:
dirs.remove('execution_api')
for f in files:
if f.endswith(".cpp"):
cpp_files.append(os.path.join(root, f))
object_files = []
for f in cpp_files:
obj_file_name =f[:-len(".cpp")] + ".o"
object_files.append(obj_file_name)
cmd = BOOTSTRAP_CC + flags + ["-c", f, "-o", obj_file_name]
run(cmd, cwd=src_wrkdir)
bootstrap_just = os.path.join(WRKDIR, "bootstrap-just")
cmd = BOOTSTRAP_CC + ["-o", bootstrap_just] + object_files + dep_flags["link"]
run(cmd, cwd=src_wrkdir)
CONF_FILE = os.path.join(WRKDIR, "repo-conf.json")
LOCAL_ROOT = os.path.join(WRKDIR, ".just")
os.makedirs(LOCAL_ROOT, exist_ok=True)
run(["sh", "-c",
"cp `./bin/just-mr.py --always-file -C %s --local-build-root=%s setup just` %s"
% (REPOS, LOCAL_ROOT, CONF_FILE)],
cwd=src_wrkdir)
GRAPH = os.path.join(WRKDIR, "graph.json")
TO_BUILD = os.path.join(WRKDIR, "to_build.json")
run([bootstrap_just, "analyse",
"-C", CONF_FILE,
"--dump-graph", GRAPH,
"--dump-artifacts-to-build", TO_BUILD,
MAIN_MODULE, MAIN_TARGET],
cwd=src_wrkdir)
run(["./bin/bootstrap-traverser.py",
"-C", CONF_FILE,
"--default-workspace", src_wrkdir,
GRAPH, TO_BUILD],
cwd=src_wrkdir)
OUT = os.path.join(WRKDIR, "out")
run(["./out-boot/%s" % (MAIN_STAGE,),
"install", "-C", CONF_FILE,
"-o", OUT, "--local-build-root", LOCAL_ROOT,
MAIN_MODULE, MAIN_TARGET],
cwd=src_wrkdir)
def main(args):
global SRCDIR
global WRKDIR
global DISTDIR
if len(args) > 1:
SRCDIR = os.path.abspath(args[1])
if len(args) > 2:
WRKDIR = os.path.abspath(args[2])
if not WRKDIR:
WRKDIR = tempfile.mkdtemp()
if not DISTDIR:
DISTDIR = [os.path.join(Path.home(), ".distfiles")]
bootstrap()
if __name__ == "__main__":
# Parse options, set DISTDIR
main(sys.argv)
|