-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·192 lines (143 loc) · 5.79 KB
/
build.py
File metadata and controls
executable file
·192 lines (143 loc) · 5.79 KB
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
#! /bin/python3
import json
import os
import subprocess
import sys
cwd = ""
binary_setup_path = "qor-userland/build.json"
"""
binaries = [{"name": "LibC", "make-path": "libc", "bin-path": "libc/bin/libc.a", "output-path": "/lib/libc.a", "postbuild": ["cp libc/bin/libc.a qor-userland/lib/libc.a"]},
]#{"name": "Hello", "make-path": "qor-userland/examples/hello", "bin-path": "qor-userland/examples/hello/bin/hello", "output-path": "/bin/hello"}]
"""
binaries = json.load(open(binary_setup_path, "r"))
def decons(a):
return a[0], a[1:]
def usage(prog_name):
print("USAGE: %s <subcommand> [ARGS]" % prog_name)
print("")
print(" Subcommands:")
print(" clean Delete all binaries and libraries")
print(" build Build all of the userland programs")
print(" build-run Build all of the userland programs and then run")
print(" disk Move necessary files to disk")
print(" rebuild Delete all binaries and libraries, and then build")
print(" run Run Qor")
print(" update Update header files")
def expect_arg(args, to_expect, prog_name):
if len(args) == 0:
usage(prog_name)
print("ERROR: Expected", to_expect)
sys.exit(1)
return decons(args)
def run_command(command, show=True, hide_path=True, no_capture=False, hide=False, **kwargs):
if not "cwd" in kwargs:
kwargs["cwd"] = cwd
if show:
if not hide_path:
d = "." + kwargs["cwd"].split(cwd)[1]
print(" ", d)
print(" ", command)
if not no_capture:
kwargs["text"] = True
kwargs["capture_output"] = True
result = subprocess.run(command, **kwargs)
if not no_capture:
if not hide:
for line in result.stdout.split("\n"):
if line == "":
continue
print(" ", line)
for line in result.stderr.split("\n"):
if line == "":
continue
print(" ", line)
return result
def build_path(path, name):
if run_command("make -q", show=False, shell=True, cwd=cwd+path).returncode > 0:
print("Building", name)
result = run_command("make", shell=True, hide_path=True, cwd=cwd+path, text=True, no_capture=True)
result.check_returncode()
return True
return False
def clean_path(path, name):
print("Removing", name)
run_command("make clean", shell=True, cwd=cwd+path).check_returncode()
def clean():
print("Removing Binaries")
for entry in binaries:
clean_path("/" + entry["make-path"], entry["name"])
def build():
print("Ensuring Directories Exist")
run_command("mkdir qor-userland/lib", shell=True, hide=True)
print("Building Binaries")
for entry in binaries:
if build_path("/" + entry["make-path"], entry["name"]):
if "postbuild" in entry:
cmds = entry["postbuild"]
print("Running post build for", entry["name"])
for cmd in cmds:
run_command(cmd, shell=True).check_returncode()
def update_headers():
print("Updating Headers")
run_command("mkdir qor-userland/include/libc", shell=True, hide=True)
run_command("cp libc/include/* qor-userland/include/libc/ -r", shell=True, cwd=cwd).check_returncode()
def mount_disk():
run_command("sudo losetup /dev/loop16 qor-os/hdd.dsk; sudo mount /dev/loop16 /mnt", show=True, shell=True).check_returncode()
def unmount_disk():
run_command("sudo sync /mnt; sudo umount /mnt; sudo losetup -d /dev/loop16", show=True, shell=True).check_returncode()
def update_disk():
print("Copying files to Disk")
mount_disk()
try:
run_command("sudo rm -rf /mnt/*", shell=True, hide=True)
run_command("sudo mkdir /mnt/lib", shell=True, hide=True)
run_command("sudo mkdir /mnt/bin", shell=True, hide=True)
run_command("sudo cp -rp qor-userland/root/* /mnt", shell=True, hide=True).check_returncode()
run_command("sudo cp -rp qor-userland/lib/* /mnt/lib", shell=True, hide=True).check_returncode()
for entry in binaries:
run_command("sudo cp -p " + entry["bin-path"] + " /mnt" + entry["output-path"], shell=True).check_returncode()
finally:
unmount_disk()
def run():
print("Starting Qor")
run_command("cargo run --release", cwd=cwd+"/qor-os", shell=True, no_capture=True).check_returncode()
copy_output()
def copy_output():
print("Copying output")
mount_disk()
try:
run_command("rm -rf qor-userland/root-output", shell=True, hide=True)
run_command("mkdir qor-userland/root-output", shell=True, hide=True)
run_command("cp -rp /mnt/home/root qor-userland/root-output", shell=True, hide=True).check_returncode()
finally:
unmount_disk()
if __name__ == "__main__":
prog_name, args = decons(sys.argv)
subcommand, args = expect_arg(args, "subcommand", prog_name)
cwd = subprocess.run(["pwd"], shell=True, text=True, capture_output=True).stdout.strip()
os.environ['qorIncludePath'] = cwd + "/qor-userland/include"
os.environ['qorLibPath'] = cwd + "/qor-userland/lib"
if subcommand == "clean":
clean()
elif subcommand == "rebuild":
clean()
update_headers()
build()
update_disk()
elif subcommand == "build":
build()
update_disk()
elif subcommand == "build-run":
build()
update_disk()
run()
elif subcommand == "update":
update_headers()
elif subcommand == "disk":
update_disk()
elif subcommand == "run":
run()
else:
usage(prog_name)
print("ERROR: Unknown subcommand", subcommand)
sys.exit(1)