def _build_shared_library()

in taskcluster/scripts/server-megazord-build.py [0:0]


def _build_shared_library(megazord, target, dist_dir):
    env = os.environ.copy()
    binary = megazord.replace("-", "_")

    if "-linux" in target:
        filename = f"lib{binary}.so"
    elif "-darwin" in target:
        filename = f"lib{binary}.dylib"
    elif "-win" in target:
        filename = f"{binary}.dll"
    else:
        raise NotImplementedError("Only targets for linux, darwin or windows available")

    if "-musl" in target:
        env["RUSTFLAGS"] = env.get("RUSTFLAGS", "") + " -C target-feature=-crt-static"
        if _host_os() == "unknown-linux":
            env["RUSTFLAGS"] = env.get("RUSTFLAGS", "") + " -C link-arg=-lgcc"
        elif _host_os() == "apple-darwin":
            if "x86_64" in target:
                env["CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER"] = (
                    "x86_64-linux-musl-gcc"
                )
                env["TARGET_CC"] = "x86_64-linux-musl-gcc"
            elif "aarch64" in target:
                env["CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER"] = (
                    "aarch64-linux-musl-gcc"
                )

    if target == "x86_64-pc-windows-gnu":
        env["RUSTFLAGS"] = env.get("RUSTFLAGS", "") + " -C panic=abort"
    elif target == "aarch64-unknown-linux-gnu":
        env["CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER"] = "aarch64-linux-gnu-gcc"

    subprocess.check_call(
        [
            "cargo",
            "build",
            "--manifest-path",
            f"{SRC_ROOT}/megazords/{megazord}/Cargo.toml",
            "--release",
            "--target",
            target,
        ],
        env=env,
        cwd=SRC_ROOT,
    )

    # This is only temporary, until cirrus uses pre-built binaries.
    _patch_uniffi_tomls()

    library_path = SRC_ROOT / "target" / target / "release" / filename

    # Generate the Python FFI. We do this with `uniffi-bindgen-library-mode` so we don't have to specify the UDL or the uniffi.toml file.
    # Use the `-l` flag rather than `-m` since we want to specify a particular target.
    subprocess.check_call(
        [
            "cargo",
            "uniffi-bindgen-library-mode",
            "-l",
            library_path.as_posix(),
            "python",
            dist_dir,
        ],
        env=env,
        cwd=SRC_ROOT,
    )

    # Move the .so file to the dist_directory
    shutil.move(
        SRC_ROOT / "target" / target / "release" / filename, dist_dir / filename
    )

    return filename