|
| 1 | +# /usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Utility script for building consistent shared libraries for vpuppr. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +from os import path |
| 9 | +import subprocess |
| 10 | +from argparse import ArgumentParser |
| 11 | +from typing import Union, List |
| 12 | +from enum import Enum |
| 13 | +import platform |
| 14 | + |
| 15 | + |
| 16 | +# NOTE: this is technically the wrong way to name shared libs on Windows but whatever |
| 17 | +LIB_NAME_FORMAT: str = "libvpuppr.{}" |
| 18 | + |
| 19 | + |
| 20 | +def get_output_dir() -> Union[str, None]: |
| 21 | + """ |
| 22 | + The output path for `cargo build`. |
| 23 | + """ |
| 24 | + script_dir: str = path.dirname(__file__) |
| 25 | + output_dir: str = path.join(script_dir, "target") |
| 26 | + |
| 27 | + return output_dir if path.isdir(output_dir) else None |
| 28 | + |
| 29 | + |
| 30 | +class BuildFlag(Enum): |
| 31 | + DEBUG = 0 |
| 32 | + RELEASE = 1 |
| 33 | + |
| 34 | + |
| 35 | +def build(flag: BuildFlag) -> None: |
| 36 | + command: List[str] = ["cargo", "build"] |
| 37 | + |
| 38 | + if flag == BuildFlag.RELEASE: |
| 39 | + command.append("--release") |
| 40 | + |
| 41 | + ret = subprocess.run(command, text=True) |
| 42 | + |
| 43 | + if ret.returncode != 0: |
| 44 | + raise Exception("Failed to build: {}".format(ret.stderr)) |
| 45 | + |
| 46 | + output_dir = get_output_dir() |
| 47 | + if not output_dir: |
| 48 | + raise Exception("Unable to get output directory") |
| 49 | + |
| 50 | + if flag == BuildFlag.DEBUG: |
| 51 | + output_dir = "{}/debug".format(output_dir) |
| 52 | + elif flag == BuildFlag.RELEASE: |
| 53 | + output_dir = "{}/release".format(output_dir) |
| 54 | + else: |
| 55 | + raise Exception("Bad build flag: {}".format(flag)) |
| 56 | + |
| 57 | + if not path.isdir(output_dir): |
| 58 | + raise Exception( |
| 59 | + "{} does not exist, the build probably failed".format(output_dir)) |
| 60 | + |
| 61 | + # Sorry MacOS, you will have to build manually since automating builds |
| 62 | + # on MacOS is a huge pain |
| 63 | + lib_ending: str = "dll" if platform.system() == "Windows" else "so" |
| 64 | + rename_count: int = 0 |
| 65 | + |
| 66 | + for file in os.listdir(os.fsencode(output_dir)): |
| 67 | + file_name: str = os.fsdecode(file) |
| 68 | + |
| 69 | + if file_name.lower().endswith(lib_ending): |
| 70 | + rename_count += 1 |
| 71 | + os.rename("{}/{}".format(output_dir, file_name), |
| 72 | + "{}/{}".format(output_dir, LIB_NAME_FORMAT.format(lib_ending))) |
| 73 | + |
| 74 | + if rename_count != 1: |
| 75 | + # TODO just a warning for now |
| 76 | + print("Renamed an unexpected amount of files: {}".format( |
| 77 | + rename_count), flush=True) |
| 78 | + |
| 79 | + |
| 80 | +def main() -> None: |
| 81 | + # Make sure we are building in the correct directory |
| 82 | + os.chdir(path.dirname(__file__)) |
| 83 | + |
| 84 | + parser = ArgumentParser("libvpuppr-build") |
| 85 | + parser.add_argument("--debug", action="store_true", |
| 86 | + help="Build the lib in debug mode") |
| 87 | + parser.add_argument("--release", action="store_true", |
| 88 | + help="Build the lib in release mode") |
| 89 | + |
| 90 | + args = parser.parse_args() |
| 91 | + |
| 92 | + changed = False |
| 93 | + |
| 94 | + # NOTE: I know it's possible to configure argparse to do this automatically, and I don't care |
| 95 | + if args.debug: |
| 96 | + changed = True |
| 97 | + build(BuildFlag.DEBUG) |
| 98 | + if args.release: |
| 99 | + changed = True |
| 100 | + build(BuildFlag.RELEASE) |
| 101 | + |
| 102 | + if not changed: |
| 103 | + raise Exception("An option must be selected") |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main() |
0 commit comments