fork/rust-sdks

git clone https://git.julia.blue/fork/rust-sdks

aoife cassidychore: update webrtc and all dependents (#512)a0a961b

main
3.7 KiB124 linesraw
1# Copyright 2023 LiveKit, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16# This file is used to download prebuilt binaries of livekit-ffi from our GH releases.
17# It is mostly used by our bindings (e.g https://github.com/livekit/client-sdk-python)
18# By default, the script will try to autodetect the platform, useful to simplify the CI.
19
20
21import argparse
22import os
23import platform
24import re
25import sys
26import tempfile
27from zipfile import ZipFile
28
29import requests
30
31
32def target_os():
33    if sys.platform.startswith("win"):
34        return "windows"
35    elif sys.platform.startswith("darwin"):
36        return "macos"
37    elif sys.platform.startswith("linux"):
38        return "linux"
39
40    return None
41
42
43def target_arch():
44    arch = platform.machine().lower()
45    arch_mapping = {
46        "amd64": "x86_64",
47        "x86_64": "x86_64",
48        "arm64": "arm64",
49        "aarch64": "arm64",
50        "armv7": "armv7",
51        "armv7l": "armv7",
52    }
53
54    # make sure cibuildwheel download the right binary when cross-compiling to arm64
55    # TODO: Make this more generic
56    print("initial arch: %s" % arch)
57    if os.environ.get("CIBUILDWHEEL") and target_os() == "macos":
58        archflags = os.environ.get("ARCHFLAGS", "")
59        print("cibuildwheel is being used, archflags: %s" % archflags)
60        if "arm64" in archflags:
61            return "arm64"
62        else:
63            return "x86_64"
64
65    return arch_mapping.get(arch)
66
67
68# extract the version from livekit-ffi/Cargo.toml
69def ffi_version():
70    file = os.path.join(os.path.dirname(__file__), "livekit-ffi", "Cargo.toml")
71    with open(file, "r") as f:
72        return re.search(r'version\s*=\s*"(\d+\.\d+\.\d+)"', f.read()).group(1)
73
74
75def download_ffi(platform, arch, version, output):
76    filename = "ffi-%s-%s.zip" % (platform, arch)
77    url = "https://github.com/livekit/client-sdk-rust/releases/download/rust-sdks/livekit-ffi@%s/%s"
78    url = url % (version, filename)
79
80    tmp = os.path.join(tempfile.gettempdir(), filename)
81
82    # download the binary
83    resp = requests.get(url, stream=True)
84    if resp.status_code != 200:
85        raise Exception("failed to download, status: %d" % (resp.status_code))
86
87    with open(tmp, mode="wb") as f:
88        for chunk in resp.iter_content(chunk_size=1024 * 8):
89            f.write(chunk)
90
91    # unzip to output
92    zip = ZipFile(tmp)
93    os.makedirs(output, exist_ok=True)
94    zip.extractall(output)
95
96
97if __name__ == "__main__":
98    parser = argparse.ArgumentParser()
99    parser.add_argument(
100        "--platform",
101        help="target platform",
102        default=target_os(),
103        choices=["macos", "linux", "windows", "ios", "android"],
104    )
105    parser.add_argument(
106        "--arch",
107        help="target architecture",
108        default=target_arch(),
109        choices=["x86_64", "arm64", "armv7"],
110    )
111    parser.add_argument(
112        "--version",
113        help="version to download",
114        default=ffi_version(),
115    )
116    parser.add_argument("--output", help="output path", required=True)
117    args = parser.parse_args()
118
119    print(
120        "downloading livekit-ffi v%s for %s-%s"
121        % (args.version, args.platform, args.arch)
122    )
123    download_ffi(args.platform, args.arch, args.version, args.output)
124    print("downloaded to %s" % os.path.abspath(args.output))