Skip to content

Commit

Permalink
Add support for Alpine(musllinux) (#264)
Browse files Browse the repository at this point in the history
* Add support for Alpine(musllinux)

* Enable musllinux build

* Use dynamic link for musllinus aarch64
  • Loading branch information
perklet authored Mar 6, 2024
1 parent 41c5cc7 commit c2c39a2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SHELL := bash

# this is the upstream libcurl-impersonate version
VERSION := 0.6.1
VERSION := 0.6.2b2
CURL_VERSION := curl-8.1.1

$(CURL_VERSION):
Expand Down
38 changes: 34 additions & 4 deletions libs.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,42 @@
"machine": "x86_64",
"pointer_size": 64,
"libdir": "~/.local/lib",
"sysname": "linux-gnu",
"sysname": "linux",
"link_type": "static",
"libc": "gnu",
"so_name": "libcurl-impersonate-chrome.so",
"so_arch": "x86_64"
},
{
"system": "Linux",
"machine": "x86_64",
"pointer_size": 64,
"libdir": "~/.local/lib",
"sysname": "linux",
"link_type": "static",
"libc": "musl",
"so_name": "libcurl-impersonate-chrome.so",
"so_arch": "x86_64"
},
{
"system": "Linux",
"machine": "aarch64",
"pointer_size": 64,
"libdir": "~/.local/lib",
"sysname": "linux",
"link_type": "static",
"libc": "gnu",
"so_name": "libcurl-impersonate-chrome.so",
"so_arch": "aarch64"
},
{
"system": "Linux",
"machine": "aarch64",
"pointer_size": 64,
"libdir": "~/.local/lib",
"sysname": "linux-gnu",
"sysname": "linux",
"link_type": "dynamic",
"libc": "musl",
"so_name": "libcurl-impersonate-chrome.so",
"so_arch": "aarch64"
},
Expand All @@ -58,7 +84,9 @@
"machine": "armv6l",
"pointer_size": 32,
"libdir": "~/.local/lib",
"sysname": "linux-gnueabihf",
"sysname": "linux",
"link_type": "static",
"libc": "gnueabihf",
"so_name": "libcurl-impersonate-chrome.so",
"so_arch": "arm"
},
Expand All @@ -67,7 +95,9 @@
"machine": "armv7l",
"pointer_size": 32,
"libdir": "~/.local/lib",
"sysname": "linux-gnueabihf",
"sysname": "linux",
"link_type": "static",
"libc": "gnueabihf",
"so_name": "libcurl-impersonate-chrome.so",
"so_arch": "arm"
}
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "curl_cffi"
version = "0.6.2"
version = "0.6.3b1"
authors = [{ name = "Yifei Kong", email = "[email protected]" }]
description = "libcurl ffi bindings for Python, with impersonation support."
license = { file = "LICENSE" }
Expand Down Expand Up @@ -89,6 +89,8 @@ build = [
"cp38-win32",
"cp38-manylinux_x86_64",
"cp38-manylinux_aarch64",
"cp38-musllinux_x86_64",
"cp38-musllinux_aarch64",
]
before-all = "make preprocess"
test-requires = "pytest"
Expand Down
18 changes: 15 additions & 3 deletions scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@
from cffi import FFI

# this is the upstream libcurl-impersonate version
__version__ = "0.6.2b1"
__version__ = "0.6.2b2"


def detect_arch():
with open(Path(__file__).parent.parent / "libs.json") as f:
archs = json.loads(f.read())

libc, _ = platform.libc_ver()
# https://github.com/python/cpython/issues/87414
libc = "gnu" if libc == "glibc" else "musl"
uname = platform.uname()
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)
):
arch["libdir"] = os.path.expanduser(arch["libdir"])
return arch
Expand All @@ -36,10 +42,14 @@ def download_libcurl():
return

file = "libcurl-impersonate.tar.gz"
if arch["system"] == "Linux":
sysname = "linux-" + arch["libc"]
else:
sysname = arch["sysname"]
url = (
f"https://github.com/yifeikong/curl-impersonate/releases/download/"
f"v{__version__}/libcurl-impersonate-v{__version__}"
f".{arch['so_arch']}-{arch['sysname']}.tar.gz"
f".{arch['so_arch']}-{sysname}.tar.gz"
)

print(f"Downloading libcurl-impersonate-chrome from {url}...")
Expand All @@ -53,7 +63,7 @@ def download_libcurl():
shutil.copy2(f"{arch['libdir']}/libcurl.dll", "curl_cffi")

def get_curl_archives():
if arch["system"] == "Linux":
if arch["system"] == "Linux" and arch.get("link_type") == "static":
# note that the order of libraries matters
# https://stackoverflow.com/a/36581865
return [
Expand All @@ -74,6 +84,8 @@ def get_curl_libraries():
return ["libcurl"]
elif arch["system"] == "Darwin":
return ["curl-impersonate-chrome"]
elif arch["system"] == "Linux" and arch.get("link_type") == "dynamic":
return ["curl-impersonate-chrome"]
else:
return []

Expand Down

0 comments on commit c2c39a2

Please sign in to comment.