Skip to content

Commit

Permalink
Fix building wheels for Linux (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Aug 7, 2023
1 parent f7c05b1 commit 6235cb9
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
19 changes: 18 additions & 1 deletion .github/workflows/build-wheels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,24 @@ jobs:
run: |
ls -lh ./wheelhouse/
ls -lh ./wheelhouse/*.whl
- name: Install patchelf
if: matrix.os == 'ubuntu-latest'
shell: bash
run: |
sudo apt-get update -q
sudo apt-get install -q -y patchelf
patchelf --help
- name: Patch wheels
shell: bash
if: matrix.os == 'ubuntu-latest'
run: |
mkdir ./wheels
sudo ./scripts/wheel/patch_wheel.py --in-dir ./wheelhouse --out-dir ./wheels
ls -lh ./wheels/
rm -rf ./wheelhouse
mv ./wheels ./wheelhouse
- uses: actions/upload-artifact@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(sherpa-onnx)

set(SHERPA_ONNX_VERSION "1.6.0")
set(SHERPA_ONNX_VERSION "1.6.1")

# Disable warning about
#
Expand Down
90 changes: 90 additions & 0 deletions scripts/wheel/patch_wheel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env python3
# Copyright 2023 Xiaomi Corp. (authors: Fangjun Kuang)

import argparse
import glob
import shutil
import subprocess
import sys
from pathlib import Path


def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--in-dir",
type=Path,
required=True,
help="Input directory.",
)

parser.add_argument(
"--out-dir",
type=Path,
required=True,
help="Output directory.",
)
return parser.parse_args()


def process(out_dir: Path, whl: Path):
tmp_dir = out_dir / "tmp"
subprocess.check_call(f"unzip {whl} -d {tmp_dir}", shell=True)
if "cp37" in str(whl):
py_version = "3.7"
elif "cp38" in str(whl):
py_version = "3.8"
elif "cp39" in str(whl):
py_version = "3.9"
elif "cp310" in str(whl):
py_version = "3.10"
elif "cp311" in str(whl):
py_version = "3.11"
else:
py_version = "3.12"

rpath_list = [
f"$ORIGIN/../lib/python{py_version}/site-packages/sherpa_onnx/lib",
f"$ORIGIN/../lib/python{py_version}/dist-packages/sherpa_onnx/lib",
#
f"$ORIGIN/../lib/python{py_version}/site-packages/sherpa_onnx/lib64",
f"$ORIGIN/../lib/python{py_version}/dist-packages/sherpa_onnx/lib64",
#
f"$ORIGIN/../lib/python{py_version}/site-packages/sherpa_onnx.libs",
]
rpaths = ":".join(rpath_list)

for filename in glob.glob(
f"{tmp_dir}/sherpa_onnx-*data/data/bin/*", recursive=True
):
print(filename)
existing_rpath = (
subprocess.check_output(["patchelf", "--print-rpath", filename])
.decode()
.strip()
)
target_rpaths = rpaths + ":" + existing_rpath
subprocess.check_call(
f"patchelf --force-rpath --set-rpath '{target_rpaths}' {filename}",
shell=True,
)

outwheel = Path(shutil.make_archive(whl, "zip", tmp_dir))
Path(outwheel).rename(out_dir / whl.name)

shutil.rmtree(tmp_dir)


def main():
args = get_args()
print(args)
in_dir = args.in_dir
out_dir = args.out_dir
out_dir.mkdir(exist_ok=True, parents=True)

for whl in in_dir.glob("*.whl"):
process(out_dir, whl)


if __name__ == "__main__":
main()

0 comments on commit 6235cb9

Please sign in to comment.