Skip to content

Commit

Permalink
Fix building. (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Mar 9, 2024
1 parent 87899c2 commit 2e89994
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 16 deletions.
16 changes: 11 additions & 5 deletions .github/workflows/build-wheels-aarch64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
python-version: ["cp37", "cp38", "cp39", "cp310", "cp311", "cp312"]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand All @@ -38,8 +38,15 @@ jobs:
- name: Build wheels
uses: pypa/[email protected]
env:
CIBW_BEFORE_ALL: |
git clone --depth 1 https://github.com/alsa-project/alsa-lib
cd alsa-lib
./gitcompile
cd ..
echo "PWD"
ls -lh /project/alsa-lib/src/.libs
CIBW_ENVIRONMENT: CPLUS_INCLUDE_PATH=/project/alsa-lib/include:$CPLUS_INCLUDE_PATH SHERPA_NCNN_ALSA_LIB_DIR=/project/alsa-lib/src/.libs LD_LIBRARY_PATH=/project/build/bdist.linux-x86_64/wheel/sherpa_ncnn/lib:$SHERPA_NCNN_ALSA_LIB_DIR SHERPA_NCNN_MAKE_ARGS="VERBOSE=1" SHERPA_NCNN_ENABLE_ALSA=1 SHERPA_NCNN_CMAKE_ARGS='-DCMAKE_C_FLAGS="-march=armv8-a" -DCMAKE_CXX_FLAGS="-march=armv8-a"'
CIBW_BUILD: "${{ matrix.python-version}}-* "
CIBW_ENVIRONMENT: SHERPA_NCNN_CMAKE_ARGS='-DCMAKE_C_FLAGS="-march=armv8-a" -DCMAKE_CXX_FLAGS="-march=armv8-a"'
CIBW_SKIP: "cp27-* cp35-* cp36-* *-win32 pp* *-musllinux* *-manylinux_i686"
CIBW_BUILD_VERBOSITY: 3
CIBW_ARCHS_LINUX: aarch64
Expand All @@ -49,10 +56,9 @@ jobs:
run: |
ls -lh ./wheelhouse/
ls -lh ./wheelhouse/*.whl
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: ${{ matrix.python-version }}
path: ./wheelhouse/*.whl

- name: Publish wheels to PyPI
Expand Down
37 changes: 32 additions & 5 deletions .github/workflows/build-wheels-linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,60 @@ jobs:
python-version: ["cp37", "cp38", "cp39", "cp310", "cp311", "cp312"]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

# see https://cibuildwheel.readthedocs.io/en/stable/changelog/
# for a list of versions
- name: Build wheels
uses: pypa/[email protected]
env:
CIBW_BEFORE_ALL: |
git clone --depth 1 https://github.com/alsa-project/alsa-lib
cd alsa-lib
./gitcompile
cd ..
echo "PWD"
ls -lh /project/alsa-lib/src/.libs
CIBW_ENVIRONMENT: CPLUS_INCLUDE_PATH=/project/alsa-lib/include:$CPLUS_INCLUDE_PATH SHERPA_NCNN_ALSA_LIB_DIR=/project/alsa-lib/src/.libs LD_LIBRARY_PATH=/project/build/bdist.linux-x86_64/wheel/sherpa_ncnn/lib:$SHERPA_NCNN_ALSA_LIB_DIR SHERPA_NCNN_MAKE_ARGS="VERBOSE=1" SHERPA_NCNN_ENABLE_ALSA=1
CIBW_BUILD: "${{ matrix.python-version}}-* "
CIBW_SKIP: "cp27-* cp35-* *-win32 pp* *-musllinux*"
CIBW_SKIP: "cp27-* cp35-* cp36-* *-win32 pp* *-musllinux* *-manylinux_i686"
CIBW_BUILD_VERBOSITY: 3

- name: Display wheels
shell: bash
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
- uses: actions/upload-artifact@v4
with:
name: ${{ matrix.python-version }}
path: ./wheelhouse/*.whl

- name: Publish to huggingface
if: matrix.python-version == 'cp38'
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
uses: nick-fields/retry@v2
uses: nick-fields/retry@v3
with:
max_attempts: 20
timeout_seconds: 200
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_ncnn/lib",
f"$ORIGIN/../lib/python{py_version}/dist-packages/sherpa_ncnn/lib",
#
f"$ORIGIN/../lib/python{py_version}/site-packages/sherpa_ncnn/lib64",
f"$ORIGIN/../lib/python{py_version}/dist-packages/sherpa_ncnn/lib64",
#
f"$ORIGIN/../lib/python{py_version}/site-packages/sherpa_ncnn.libs",
]
rpaths = ":".join(rpath_list)

for filename in glob.glob(
f"{tmp_dir}/sherpa_ncnn-*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()
10 changes: 4 additions & 6 deletions sherpa-ncnn/python/csrc/faked-alsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
//
// Copyright (c) 2024 Xiaomi Corporation

#include "sherpa-ncnn/csrc/macros.h"
#include <stdio.h>

#include "sherpa-ncnn/python/csrc/alsa.h"

namespace sherpa_ncnn {

class FakedAlsa {
public:
explicit FakedAlsa(const char *) {
SHERPA_NCNN_LOGE("This function is for Linux only.");
fprintf(stderr, "This function is for Linux only.");
#if (SHERPA_NCNN_ENABLE_ALSA == 0) && (defined(__unix__) || defined(__unix))
SHERPA_NCNN_LOGE(R"doc(
fprintf(stderr, R"doc(
sherpa-ncnn is compiled without alsa support. To enable that, please run
(1) sudo apt-get install alsa-utils libasound2-dev
(2) rebuild sherpa-ncnn
Expand Down Expand Up @@ -43,5 +43,3 @@ void PybindAlsa(py::module *m) {
}

} // namespace sherpa_ncnn

#endif // SHERPA_NCNN_PYTHON_CSRC_FAKED_ALSA_H_

0 comments on commit 2e89994

Please sign in to comment.