From fb2314c4d32d022a0cf8686b2cc47e1570014646 Mon Sep 17 00:00:00 2001 From: Tester Date: Mon, 22 Jan 2024 21:14:18 +0330 Subject: [PATCH] Improve handling available libcurl builds --- scripts/build.py | 52 ++++++++++++++++++++++++++++++++++------------ setup.py | 54 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 80 insertions(+), 26 deletions(-) diff --git a/scripts/build.py b/scripts/build.py index 22df3f00..e662f99c 100644 --- a/scripts/build.py +++ b/scripts/build.py @@ -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=[ @@ -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: diff --git a/setup.py b/setup.py index 108b26fd..54d00f7f 100644 --- a/setup.py +++ b/setup.py @@ -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 @@ -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")