Skip to content

Commit

Permalink
Improve handling available libcurl builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Tester authored and Tester committed Jan 22, 2024
1 parent 5ac5862 commit fb2314c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 26 deletions.
52 changes: 39 additions & 13 deletions scripts/build.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
import os
import struct
import platform
import struct

from cffi import FFI


def abs_machine():
machine = platform.machine()

pointer_bits = struct.calcsize("P") * 8
if pointer_bits not in (32, 64):
raise Exception("Unsupported pointer size")

is_64 = pointer_bits == 64

# x86 based archs
if machine in ('AMD64', 'x86_64', 'i686-64', 'i386', 'i686', 'x86'):
return "x86_64" if is_64 else "i686"
# arm based archs
elif machine in ('aarch64', 'arm64', 'armv6l', 'armv7l'):
return "aarch64" if is_64 else "arm"
else:
raise Exception("Unsupported processor")


ffibuilder = FFI()
# arch = "%s-%s" % (os.uname().sysname, os.uname().machine)
uname = platform.uname()
system = platform.system()
machine = abs_machine()
parent_dir = os.path.dirname(os.path.dirname(__file__))

if uname.system == "Windows":
if struct.calcsize("P") * 8 == 64:
if system == "Windows":
if machine == "x86_64":
libdir = "./lib32"
elif machine == "i686":
libdir = "./lib64"
else:
libdir = "./lib32"
elif uname.system == "Darwin":
if uname.machine == "x86_64":
libdir = "ERROR"
elif system == "Darwin":
if machine == "x86_64":
libdir = "/Users/runner/work/_temp/install/lib"
else:
libdir = "/usr/local/lib"
libdir = "ERROR"
else:
libdir = "/usr/local/lib"
if machine in ("x86_64", "arm", "aarch64"):
libdir = "/usr/local/lib"
else:
libdir = "ERROR"

if libdir == "ERROR":
raise Exception("Unsupported platform")

ffibuilder.set_source(
"curl_cffi._wrapper",
"""
#include "shim.h"
""",
libraries=["curl-impersonate-chrome"] if uname.system != "Windows" else ["libcurl"],
libraries=["curl-impersonate-chrome"] if system != "Windows" else ["libcurl"],
library_dirs=[libdir],
source_extension=".c",
include_dirs=[
Expand All @@ -39,9 +66,8 @@
os.path.join(parent_dir, "ffi/shim.c"),
],
extra_compile_args=(
["-Wno-implicit-function-declaration"] if uname.system == "Darwin" else []
["-Wno-implicit-function-declaration"] if system == "Darwin" else []
),
# extra_link_args=["-Wl,-rpath,$ORIGIN/../libcurl/" + arch],
)

with open(os.path.join(parent_dir, "ffi/cdef.c")) as f:
Expand Down
54 changes: 41 additions & 13 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,58 @@ def get_tag(self):
return python, abi, plat


def abs_machine():
machine = platform.machine()

pointer_bits = struct.calcsize("P") * 8
if pointer_bits not in (32, 64):
raise Exception("Unsupported pointer size")

is_64 = pointer_bits == 64

# x86 based archs
if machine in ('AMD64', 'x86_64', 'i686-64', 'i386', 'i686', 'x86'):
return "x86_64" if is_64 else "i686"
# arm based archs
elif machine in ('aarch64', 'arm64', 'armv6l', 'armv7l'):
return "aarch64" if is_64 else "arm"
else:
raise Exception("Unsupported processor")


def download_so():
uname = platform.uname()
machine = uname.machine
system = platform.system()
machine = abs_machine()

if uname.system == "Windows":
if system == "Windows":
sysname = "win32"
if struct.calcsize("P") * 8 == 64:
machine = "x86_64"
so_name = "libcurl.dll"

if machine == "x86_64":
libdir = "./lib32"
elif machine == "i686":
libdir = "./lib64"
else:
machine = "i686"
libdir = "./lib32"
so_name = "libcurl.dll"
elif uname.system == "Darwin":
so_name = "SKIP"

elif system == "Darwin":
sysname = "macos"
libdir = "/Users/runner/work/_temp/install/lib"
so_name = "libcurl-impersonate-chrome.4.dylib"

if machine == "x86_64":
so_name = "libcurl-impersonate-chrome.4.dylib"
libdir = "/Users/runner/work/_temp/install/lib"
else:
so_name = "SKIP"

else:
sysname = "linux-gnu"
libdir = "/usr/local/lib"
so_name = "libcurl-impersonate-chrome.so"

if machine in ("x86_64", "arm", "aarch64"):
libdir = "/usr/local/lib"
else:
so_name = "SKIP"

if so_name == "SKIP":
print(".so file for platform is not available on github.")
return
Expand All @@ -70,7 +97,8 @@ def download_so():
print("Unpacking downloaded files...")
os.makedirs(libdir, exist_ok=True)
shutil.unpack_archive(file, libdir)
if uname.system == "Windows":

if system == "Windows":
shutil.copy2(f"{libdir}/libcurl.dll", "curl_cffi")


Expand Down

0 comments on commit fb2314c

Please sign in to comment.