-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.py
181 lines (142 loc) · 5.42 KB
/
build.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
import argparse
import os
import re
import shutil
import stat
import subprocess
import sys
def parse_args(argv):
parser = argparse.ArgumentParser()
parser.add_argument(
"-b",
"--build",
help="path to the build directory",
metavar="PATH",
default=os.path.join(os.getcwd(), "cross-build"),
)
parser.add_argument(
"-o",
"--output",
help="path to the output directory",
metavar="PATH",
default=os.path.join(os.getcwd(), "cif-archives"),
)
parser.add_argument(
"-s",
"--src",
help="path to the CIF repositor",
metavar="PATH",
default=os.getcwd(),
)
parser.add_argument(
dest="configs",
nargs=argparse.REMAINDER,
help="crosstool-ng configurations to use",
)
return parser.parse_args(argv)
def get_cif_version(cif_bin):
version = "unknown"
try:
version = subprocess.check_output(
[cif_bin, "--version"], stderr=subprocess.DEVNULL, universal_newlines=True
).strip()
finally:
return version
def copy_and_patch(cif_src, src, dst):
with open(src, "r") as src_fh:
config_lines = src_fh.readlines()
with open(dst, "w") as dst_fh:
for line in config_lines:
m = re.search(r'"(.*?)/aspectator.*?"', line)
if m:
line = line.replace(m.group(1), cif_src)
dst_fh.write(line)
def fix_permissions(path):
for root, dirs, files in os.walk(path):
for name in dirs + files:
file_path = os.path.join(root, name)
st = os.stat(file_path)
os.chmod(file_path, st.st_mode | stat.S_IWRITE)
if __name__ == "__main__":
args = parse_args(sys.argv[1:])
config_dir = os.path.join(args.src, "config")
for config_name in os.listdir(config_dir):
if args.configs and config_name not in args.configs:
print("Skipping {!r} configuration".format(config_name))
continue
config_file = os.path.join(config_dir, config_name)
# Prepare build directory
config_build_dir = os.path.join(args.build, config_name)
os.makedirs(config_build_dir, exist_ok=True)
# Copy configuration file for crosstool-ng and replace paths inside
copy_and_patch(args.src, config_file, os.path.join(config_build_dir, ".config"))
# Run crosstool-ng
r = subprocess.run(["/home/builduser/src/crosstool-ng-1.24.0/ct-ng", "build"], cwd=config_build_dir)
if r.returncode:
print("Build failed")
sys.exit(-1)
# Name of the cif executable
cif_name = config_name + "-cif"
# Path to the directory that will contain proper cif distribution
cif_dir = os.path.join(args.build, cif_name)
# Copy cross-aspectator to cif_dir. TODO: replace by move?
ct_ng_build_dir = os.path.join(os.path.expanduser("~"), "x-tools", config_name)
aspectator_dir = os.path.join(cif_dir, cif_name)
shutil.copytree(ct_ng_build_dir, aspectator_dir)
# By default nobody can write to ct-ng build directory while we need this to copy cif binary
subprocess.run(["chmod", "-R", "u+w", aspectator_dir], stdout=subprocess.DEVNULL, check=True)
# Build cif binary
subprocess.run(["make", "cif"], cwd=args.src, stdout=subprocess.DEVNULL, check=True)
# Copy cif binary
cif_bin_src = os.path.join(args.src, "build", "cif")
cif_bin_dst = os.path.join(aspectator_dir, "bin", cif_name)
shutil.copy(cif_bin_src, cif_bin_dst)
# Create bin inside cif_dir
cif_bin_dir = os.path.join(cif_dir, "bin")
os.makedirs(cif_bin_dir, exist_ok=True)
# Create cif simlink
cif_path = os.path.join(cif_bin_dir, cif_name)
subprocess.run([
"ln",
"-sf",
os.path.join("..", cif_name, "bin", cif_name),
cif_path
], cwd=cif_bin_dir, check=True)
# Create aspectator simlink
aspectator_name = config_name + "-aspectator"
gcc_name = config_name + "-gcc"
subprocess.run([
"ln",
"-sf",
os.path.join("..", cif_name, "bin", gcc_name),
os.path.join(cif_bin_dir, aspectator_name)
], cwd=cif_bin_dir, check=True)
# Disable plugins if so
r = subprocess.run([
os.path.join(cif_bin_dir, aspectator_name),
"-print-file-name=plugin"
], stdout=subprocess.PIPE, universal_newlines=True, check=True)
plugin_dir = r.stdout.rstrip()
if os.path.isdir(plugin_dir):
os.rename(plugin_dir, plugin_dir + '~')
# Fixing permissions (chmod +w to everything inside cif_dir)
fix_permissions(cif_dir)
# Run tests
test_env = os.environ.copy()
test_env["CIF"] = cif_path
r = subprocess.run(["make", "test"], cwd=args.src, env=test_env)
if r.returncode:
print("Tests failed")
sys.exit(-1)
# Final step: create .tar.xz archive
archive_name = config_name + "-" + get_cif_version(cif_path) + ".tar.xz"
os.makedirs(args.output, exist_ok=True)
print("Create {!r} archive".format(archive_name))
subprocess.run([
"tar",
"cJf",
os.path.join(args.output, archive_name),
"-C",
cif_dir,
"."
], cwd=args.build, check=True)