-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathbuild.py
165 lines (137 loc) · 4.69 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
import json
import os
import platform
import shutil
import struct
import tempfile
from glob import glob
from pathlib import Path
from urllib.request import urlretrieve
from cffi import FFI
# this is the upstream libcurl-impersonate version
__version__ = "0.9.1"
def detect_arch():
with open(Path(__file__).parent.parent / "libs.json") as f:
archs = json.loads(f.read())
uname = platform.uname()
glibc_flavor = "gnueabihf" if uname.machine in ["armv7l", "armv6l"] else "gnu"
libc, _ = platform.libc_ver()
# https://github.com/python/cpython/issues/87414
libc = glibc_flavor if libc == "glibc" else "musl"
pointer_size = struct.calcsize("P") * 8
for arch in archs:
if (
arch["system"] == uname.system
and arch["machine"] == uname.machine
and arch["pointer_size"] == pointer_size
and ("libc" not in arch or arch.get("libc") == libc)
):
if arch["libdir"]:
arch["libdir"] = os.path.expanduser(arch["libdir"])
else:
global tmpdir
if "CI" in os.environ:
tmpdir = "./tmplibdir"
os.makedirs(tmpdir, exist_ok=True)
arch["libdir"] = tmpdir
else:
tmpdir = tempfile.TemporaryDirectory()
arch["libdir"] = tmpdir.name
return arch
raise Exception(f"Unsupported arch: {uname}")
arch = detect_arch()
print(f"Using {arch['libdir']} to store libcurl-impersonate")
def download_libcurl():
if (Path(arch["libdir"]) / arch["so_name"]).exists():
print(".so files already downloaded.")
return
file = "libcurl-impersonate.tar.gz"
sysname = "linux-" + arch["libc"] if arch["system"] == "Linux" else arch["sysname"]
url = (
f"https://github.com/lexiforest/curl-impersonate/releases/download/"
f"v{__version__}/libcurl-impersonate-v{__version__}"
f".{arch['so_arch']}-{sysname}.tar.gz"
)
print(f"Downloading libcurl-impersonate-chrome from {url}...")
urlretrieve(url, file)
print("Unpacking downloaded files...")
os.makedirs(arch["libdir"], exist_ok=True)
shutil.unpack_archive(file, arch["libdir"])
if arch["system"] == "Windows":
for file in glob(os.path.join(arch["libdir"], "lib/*.lib")):
shutil.move(file, arch["libdir"])
for file in glob(os.path.join(arch["libdir"], "bin/*.dll")):
shutil.move(file, arch["libdir"])
print("Files after unpacking")
print(os.listdir(arch["libdir"]))
def get_curl_archives():
print("Files for linking")
print(os.listdir(arch["libdir"]))
if arch["system"] == "Linux" and arch.get("link_type") == "static":
# note that the order of libraries matters
# https://stackoverflow.com/a/36581865
return [
f"{arch['libdir']}/libcurl-impersonate-chrome.a",
f"{arch['libdir']}/libssl.a",
f"{arch['libdir']}/libcrypto.a",
f"{arch['libdir']}/libz.a",
f"{arch['libdir']}/libzstd.a",
f"{arch['libdir']}/libnghttp2.a",
f"{arch['libdir']}/libbrotlidec.a",
f"{arch['libdir']}/libbrotlienc.a",
f"{arch['libdir']}/libbrotlicommon.a",
]
else:
return []
def get_curl_libraries():
if arch["system"] == "Windows":
return [
"Crypt32",
"Secur32",
"wldap32",
"Normaliz",
"libcurl",
"zstd",
"zlib",
"ssl",
"nghttp2",
"crypto",
"brotlienc",
"brotlidec",
"brotlicommon",
]
elif arch["system"] == "Darwin" or (
arch["system"] == "Linux" and arch.get("link_type") == "dynamic"
):
return ["curl-impersonate-chrome"]
else:
return []
ffibuilder = FFI()
system = platform.system()
root_dir = Path(__file__).parent.parent
download_libcurl()
ffibuilder.set_source(
"curl_cffi._wrapper",
"""
#include "shim.h"
""",
# FIXME from `curl-impersonate`
libraries=get_curl_libraries(),
extra_objects=get_curl_archives(),
library_dirs=[arch["libdir"]],
source_extension=".c",
include_dirs=[
str(root_dir / "include"),
str(root_dir / "ffi"),
],
sources=[
str(root_dir / "ffi/shim.c"),
],
extra_compile_args=(["-Wno-implicit-function-declaration"] if system == "Darwin" else []),
extra_link_args=(["-lstdc++"] if system != "Windows" else []),
)
with open(root_dir / "ffi/cdef.c") as f:
cdef_content = f.read()
ffibuilder.cdef(cdef_content)
if __name__ == "__main__":
ffibuilder.compile(verbose=False)