-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild-codecs.py
177 lines (145 loc) · 4.89 KB
/
build-codecs.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
import os
import platform
import shutil
import struct
import subprocess
import sys
if len(sys.argv) < 2:
sys.stderr.write("Usage: build-codecs.py <prefix>\n")
sys.exit(1)
dest_dir = sys.argv[1]
build_dir = os.path.abspath("build")
patch_dir = os.path.abspath("patches")
source_dir = os.path.abspath("source")
for d in [build_dir, dest_dir]:
if os.path.exists(d):
shutil.rmtree(d)
def fetch(url, path):
run(["curl", "-L", "-o", path, url])
def mangle_path(path):
if platform.system() == "Windows":
return path.replace(os.path.sep, "/").replace("C:", "/c").replace("D:", "/d")
else:
return path
def build(package, configure_args=[]):
package_path = os.path.join(build_dir, package)
# update config.guess and config.sub
config_files = ("config.guess", "config.sub")
for root, dirs, files in os.walk(package_path):
for name in filter(lambda x: x in config_files, files):
script_path = os.path.join(root, name)
cache_path = os.path.join(source_dir, name)
if not os.path.exists(cache_path):
fetch(
"https://git.savannah.gnu.org/cgit/config.git/plain/" + name,
cache_path,
)
shutil.copy(cache_path, script_path)
os.chmod(script_path, 0o755)
os.chdir(package_path)
run(["sh", "./configure"] + configure_args + ["--prefix=" + mangle_path(dest_dir)])
run(["make"])
run(["make", "install"])
os.chdir(build_dir)
def get_platform():
system = platform.system()
machine = platform.machine()
if system == "Linux":
return f"manylinux_{machine}"
elif system == "Darwin":
# cibuildwheel sets ARCHFLAGS:
# https://github.com/pypa/cibuildwheel/blob/5255155bc57eb6224354356df648dc42e31a0028/cibuildwheel/macos.py#L207-L220
if "ARCHFLAGS" in os.environ:
machine = os.environ["ARCHFLAGS"].split()[1]
return f"macosx_{machine}"
elif system == "Windows":
if struct.calcsize("P") * 8 == 64:
return "win_amd64"
else:
return "win32"
else:
raise Exception(f"Unsupported system {system}")
def prepend_env(name, new, separator=" "):
old = os.environ.get(name)
if old:
os.environ[name] = new + separator + old
else:
os.environ[name] = new
def extract(package, url, *, strip_components=1):
path = os.path.join(build_dir, package)
patch = os.path.join(patch_dir, package + ".patch")
tarball = os.path.join(source_dir, url.split("/")[-1])
# download tarball
if not os.path.exists(tarball):
fetch(url, tarball)
# extract tarball
os.mkdir(path)
run(["tar", "xf", tarball, "-C", path, "--strip-components", str(strip_components)])
# apply patch
if os.path.exists(patch):
run(["patch", "-d", path, "-i", patch, "-p1"])
def run(cmd):
sys.stdout.write(f"- Running: {cmd}\n")
sys.stdout.flush()
subprocess.run(cmd, check=True)
output_dir = os.path.abspath("output")
if platform.system() == "Linux" and os.environ.get("CIBUILDWHEEL") == "1":
output_dir = "/output"
output_tarball = os.path.join(output_dir, f"codecs-{get_platform()}.tar.gz")
for d in [build_dir, output_dir, source_dir]:
if not os.path.exists(d):
os.mkdir(d)
if not os.path.exists(output_tarball):
os.chdir(build_dir)
prepend_env("PATH", os.path.join(dest_dir, "bin"), separator=":")
if platform.system() == "Darwin" and os.environ.get("ARCHFLAGS") == "-arch arm64":
opus_configure_args = [
"--build",
"x86_64-apple-darwin20.6.0",
"--host",
"amd64-apple-darwin20.6.0",
]
vpx_configure_args = ["--target=arm64-darwin20-gcc"]
elif platform.system() == "Windows":
opus_configure_args = []
vpx_configure_args = ["--target=x86_64-win64-gcc"]
else:
opus_configure_args = []
vpx_configure_args = []
#### BUILD TOOLS ####
# install nasm
if platform.system() != "Windows":
extract(
"nasm",
"https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.bz2",
)
build("nasm")
#### CODECS ###
# build opus
extract(
"opus",
"https://archive.mozilla.org/pub/opus/opus-1.3.1.tar.gz",
)
build(
"opus",
[
"--disable-extra-programs",
"--disable-shared",
"--enable-static",
"--with-pic",
]
+ opus_configure_args,
)
# build vpx
extract("vpx", "https://github.com/webmproject/libvpx/archive/v1.11.0.tar.gz")
build(
"vpx",
[
"--disable-examples",
"--disable-tools",
"--disable-unit-tests",
"--enable-pic",
]
+ vpx_configure_args,
)
run(["tar", "czvf", output_tarball, "-C", dest_dir, "include", "lib"])