Skip to content

Instantly share code, notes, and snippets.

@toolness
Last active May 12, 2019 14:50
Show Gist options
  • Save toolness/b0fe664b2a165a9e083bfcc6a74c3893 to your computer and use it in GitHub Desktop.
Save toolness/b0fe664b2a165a9e083bfcc6a74c3893 to your computer and use it in GitHub Desktop.
Install SDL2 libraries so Rust+MSVC can find them.
from pathlib import Path
from urllib.request import urlopen
from zipfile import ZipFile
from io import BytesIO
import os
import sys
assert 'win' in sys.platform, "We must be running on Windows."
MY_DIR = Path(__file__).parent.resolve()
HOMEDIR = Path(os.environ['HOMEDRIVE'] + os.environ['HOMEPATH'])
SDL_VERSION = "2.0.9"
SDL_URL = f"https://www.libsdl.org/release/SDL2-devel-{SDL_VERSION}-VC.zip"
ZIP_LIBDIR_PREFIX = f"SDL2-{SDL_VERSION}/lib/x64/"
DLL_NAME = "SDL2.dll"
ZIP_DLL = f"{ZIP_LIBDIR_PREFIX}{DLL_NAME}"
CHANNEL = "stable"
TARGET = "x86_64-pc-windows-msvc"
RUSTUP_LIB_DIR = (
HOMEDIR / ".multirust" / "toolchains" / f"{CHANNEL}-{TARGET}" / "lib" /
"rustlib" / TARGET / "lib"
)
def extract_to(zip: ZipFile, filename: str, dest_dir: Path):
data = zip.open(f"{ZIP_LIBDIR_PREFIX}{filename}", "r").read()
dest = dest_dir / filename
print(f"Writing {filename} to {dest_dir}.")
dest.write_bytes(data)
def main():
assert RUSTUP_LIB_DIR.exists(), f"{RUSTUP_LIB_DIR} must exist!"
print(f"Retrieving {SDL_URL}...")
zip = ZipFile(BytesIO(urlopen(SDL_URL).read()))
libnames = [
name[len(ZIP_LIBDIR_PREFIX):] for name in zip.namelist()
if (name.startswith(ZIP_LIBDIR_PREFIX) and
name.endswith('.lib'))
]
for libname in libnames:
extract_to(zip, libname, RUSTUP_LIB_DIR)
extract_to(zip, DLL_NAME, MY_DIR)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment