Fangjun Kuang пре 2 година
родитељ
комит
df9ae1ff84

+ 36 - 0
.github/workflows/publish-to-pypi.yaml

@@ -0,0 +1,36 @@
+name: Publish to PyPI
+
+on:
+  push:
+    tags:
+      - '*'
+
+jobs:
+  pypi:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2
+
+      - name: Setup Python
+        uses: actions/setup-python@v2
+        with:
+          python-version: "3.8"
+
+      - name: Install Python dependencies
+        shell: bash
+        run: |
+          python3 -m pip install --upgrade pip
+          python3 -m pip install wheel twine setuptools
+
+      - name: Build
+        shell: bash
+        run: |
+          python3 setup.py sdist
+          ls -l dist/*
+
+      - name: Publish wheels to PyPI
+        env:
+          TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
+          TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
+        run: |
+          twine upload dist/sherpa-ncnn-*.tar.gz

+ 1 - 2
CMakeLists.txt

@@ -1,8 +1,6 @@
 cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
 project(sherpa-ncnn)
 
-set(SHERPA_NCNN_VERSION_MAJOR "1")
-set(SHERPA_NCNN_VERSION_MINOR "0")
 set(SHERPA_NCNN_VERSION "1.0")
 
 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
@@ -16,6 +14,7 @@ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
 if(NOT APPLE)
   set(SHERPA_NCNN_RPATH_ORIGIN "$ORIGIN")
 else()
+  set(CMAKE_MACOSX_RPATH ON)
   set(SHERPA_NCNN_RPATH_ORIGIN "@loader_path")
 endif()
 

+ 15 - 13
cmake/cmake_extension.py

@@ -58,8 +58,7 @@ class BuildExtension(build_ext):
         # build/lib.linux-x86_64-3.8
         os.makedirs(self.build_lib, exist_ok=True)
 
-        out_bin_dir = Path(self.build_lib).parent / "bin"
-        install_dir = Path(self.build_lib).resolve()
+        install_dir = Path(self.build_lib).resolve() / "sherpa_ncnn"
 
         sherpa_ncnn_dir = Path(__file__).parent.parent.resolve()
 
@@ -73,7 +72,7 @@ class BuildExtension(build_ext):
         extra_cmake_args = f" -DCMAKE_INSTALL_PREFIX={install_dir} "
         extra_cmake_args += f" -DBUILD_SHARED_LIBS=ON "
         extra_cmake_args += f" -DSHERPA_NCNN_ENABLE_PYTHON=ON "
-        extra_cmake_args += f" -DSHERPA_NCNN_ENABLE_PORTAUDIO=ON "
+        extra_cmake_args += f" -DSHERPA_NCNN_ENABLE_PORTAUDIO=OFF "
 
         if "PYTHON_EXECUTABLE" not in cmake_args:
             print(f"Setting PYTHON_EXECUTABLE to {sys.executable}")
@@ -121,13 +120,16 @@ class BuildExtension(build_ext):
                     "You can ask for help by creating an issue on GitHub.\n"
                     "\nClick:\n\thttps://github.com/k2-fsa/sherpa-ncnn/issues/new\n"  # noqa
                 )
-
-        suffix = ".exe" if is_windows() else ""
-        # Remember to also change setup.py
-        binaries = ["sherpa-ncnn"]
-        binaries += ["sherpa-ncnn-microphone"]
-
-        for f in binaries:
-            src_file = install_dir / "bin" / (f + suffix)
-            print(f"Copying {src_file} to {out_bin_dir}/")
-            shutil.copy(f"{src_file}", f"{out_bin_dir}/")
+        dirs = [
+            install_dir / "include",
+            install_dir / "lib" / "cmake",
+            install_dir / "lib" / "pkgconfig",
+            install_dir / "lib64" / "cmake",
+            install_dir / "lib64" / "pkgconfig",
+        ]
+
+        for d in dirs:
+            if not d.is_dir():
+                continue
+
+            shutil.rmtree(str(d))

+ 0 - 53
cmake/googletest.cmake

@@ -1,53 +0,0 @@
-function(download_googltest)
-  include(FetchContent)
-
-  set(googletest_URL  "https://github.com/google/googletest/archive/release-1.10.0.tar.gz")
-  set(googletest_HASH "SHA256=9dc9157a9a1551ec7a7e43daea9a694a0bb5fb8bec81235d8a1e6ef64c716dcb")
-
-  set(BUILD_GMOCK ON CACHE BOOL "" FORCE)
-  set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
-  set(gtest_disable_pthreads ON CACHE BOOL "" FORCE)
-  set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
-
-  FetchContent_Declare(googletest
-    URL               ${googletest_URL}
-    URL_HASH          ${googletest_HASH}
-  )
-
-  FetchContent_GetProperties(googletest)
-  if(NOT googletest_POPULATED)
-    message(STATUS "Downloading googletest")
-    FetchContent_Populate(googletest)
-  endif()
-  message(STATUS "googletest is downloaded to ${googletest_SOURCE_DIR}")
-  message(STATUS "googletest's binary dir is ${googletest_BINARY_DIR}")
-
-  if(APPLE)
-    set(CMAKE_MACOSX_RPATH ON) # to solve the following warning on macOS
-  endif()
-  #[==[
-  -- Generating done
-    Policy CMP0042 is not set: MACOSX_RPATH is enabled by default.  Run "cmake
-    --help-policy CMP0042" for policy details.  Use the cmake_policy command to
-    set the policy and suppress this warning.
-
-    MACOSX_RPATH is not specified for the following targets:
-
-      gmock
-      gmock_main
-      gtest
-      gtest_main
-
-  This warning is for project developers.  Use -Wno-dev to suppress it.
-  ]==]
-
-  add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)
-
-  target_include_directories(gtest
-    INTERFACE
-      ${googletest_SOURCE_DIR}/googletest/include
-      ${googletest_SOURCE_DIR}/googlemock/include
-  )
-endfunction()
-
-download_googltest()

+ 5 - 4
cmake/kaldi-native-fbank.cmake

@@ -1,12 +1,13 @@
 function(download_kaldi_native_fbank)
   include(FetchContent)
 
-  # If you don't have access to the internet, please download it to your
-  # local drive and modify the following line according to your needs.
-  # set(kaldi_native_fbank_URL  "file:///ceph-fj/fangjun/open-source/sherpa-ncnn/v1.8.tar.gz")
   set(kaldi_native_fbank_URL  "https://github.com/csukuangfj/kaldi-native-fbank/archive/refs/tags/v1.8.tar.gz")
   set(kaldi_native_fbank_HASH "SHA256=1aefda7e76456e2c5005e05fa10cdc7f91c3ffc0a7fad000a90b50e7ed18087b")
 
+  # If you don't have access to the internet, please download it to your
+  # local drive and modify the following line according to your needs.
+  # set(kaldi_native_fbank_URL  "file:///star-fj/fangjun/download/github/kaldi-native-fbank-1.8.tar.gz")
+
   set(KALDI_NATIVE_FBANK_BUILD_TESTS OFF CACHE BOOL "" FORCE)
   set(KALDI_NATIVE_FBANK_BUILD_PYTHON OFF CACHE BOOL "" FORCE)
 
@@ -24,12 +25,12 @@ function(download_kaldi_native_fbank)
   message(STATUS "kaldi-native-fbank's binary dir is ${kaldi_native_fbank_BINARY_DIR}")
 
   add_subdirectory(${kaldi_native_fbank_SOURCE_DIR} ${kaldi_native_fbank_BINARY_DIR})
+  install(TARGETS kaldi-native-fbank-core DESTINATION lib)
 
   target_include_directories(kaldi-native-fbank-core
     INTERFACE
       ${kaldi_native_fbank_SOURCE_DIR}/
   )
-  install(TARGETS kaldi-native-fbank-core DESTINATION lib)
 endfunction()
 
 download_kaldi_native_fbank()

+ 3 - 3
cmake/ncnn.cmake

@@ -5,12 +5,12 @@ function(download_ncnn)
   # The changed code is in
   # https://github.com/csukuangfj/ncnn/pull/7
 
+  set(ncnn_URL "https://github.com/csukuangfj/ncnn/archive/refs/tags/sherpa-0.8.tar.gz")
+  set(ncnn_HASH "SHA256=f605c48986406800615d00cf14b955e95f73286eadacedb6c3371542540e1df0")
+
   # If you don't have access to the internet, please download it to your
   # local drive and modify the following line according to your needs.
   # set(ncnn_URL  "file:///star-fj/fangjun/download/github/ncnn-sherpa-0.8.tar.gz")
-  set(ncnn_URL "https://github.com/csukuangfj/ncnn/archive/refs/tags/sherpa-0.8.tar.gz")
-
-  set(ncnn_HASH "SHA256=f605c48986406800615d00cf14b955e95f73286eadacedb6c3371542540e1df0")
 
   FetchContent_Declare(ncnn
     URL               ${ncnn_URL}

+ 5 - 0
cmake/portaudio.cmake

@@ -3,6 +3,11 @@ function(download_portaudio)
 
   set(portaudio_URL  "http://files.portaudio.com/archives/pa_stable_v190700_20210406.tgz")
   set(portaudio_HASH "SHA256=47efbf42c77c19a05d22e627d42873e991ec0c1357219c0d74ce6a2948cb2def")
+
+  # If you don't have access to the internet, please download it to your
+  # local drive and modify the following line according to your needs.
+  # set(portaudio_URL  "file:///star-fj/fangjun/download/github/pa_stable_v190700_20210406.tgz")
+
   if(BUILD_SHARED_LIBS)
     set(PA_BUILD_SHARED ON CACHE BOOL "" FORCE)
     set(PA_BUILD_STATIC OFF CACHE BOOL "" FORCE)

+ 4 - 0
cmake/pybind11.cmake

@@ -4,6 +4,10 @@ function(download_pybind11)
   set(pybind11_URL  "https://github.com/pybind/pybind11/archive/refs/tags/v2.10.2.tar.gz")
   set(pybind11_HASH "SHA256=93bd1e625e43e03028a3ea7389bba5d3f9f2596abc074b068e70f4ef9b1314ae")
 
+  # If you don't have access to the internet, please download it to your
+  # local drive and modify the following line according to your needs.
+  # set(pybind11_URL  "file:///star-fj/fangjun/download/github/pybind11-2.10.2.tar.gz")
+
   FetchContent_Declare(pybind11
     URL               ${pybind11_URL}
     URL_HASH          ${pybind11_HASH}

+ 6 - 15
setup.py

@@ -30,34 +30,25 @@ def get_package_version():
     return latest_version
 
 
-def get_binaries_to_install():
-    bin_dir = Path("build") / "bin"
-    bin_dir.mkdir(parents=True, exist_ok=True)
-    suffix = ".exe" if is_windows() else ""
-    # Remember to also change cmake/cmake_extension.py
-    binaries = ["sherpa-ncnn"]
-    binaries += ["sherpa-ncnn-microphone"]
-    exe = []
-    for f in binaries:
-        t = bin_dir / (f + suffix)
-        exe.append(str(t))
-    return exe
-
-
 package_name = "sherpa-ncnn"
 
 with open("sherpa-ncnn/python/sherpa_ncnn/__init__.py", "a") as f:
     f.write(f"__version__ = '{get_package_version()}'\n")
 
+install_requires = [
+    "numpy",
+]
+
 setuptools.setup(
     name=package_name,
+    python_requires=">=3.6",
+    install_requires=install_requires,
     version=get_package_version(),
     author="The sherpa-ncnn development team",
     author_email="dpovey@gmail.com",
     package_dir={
         "sherpa_ncnn": "sherpa-ncnn/python/sherpa_ncnn",
     },
-    data_files=[("bin", get_binaries_to_install())],
     packages=["sherpa_ncnn"],
     url="https://github.com/k2-fsa/sherpa-ncnn",
     long_description=read_long_description(),

+ 32 - 32
sherpa-ncnn/csrc/CMakeLists.txt

@@ -11,45 +11,45 @@ set(sherpa_ncnn_core_srcs
   wave-reader.cc
 )
 add_library(sherpa-ncnn-core ${sherpa_ncnn_core_srcs})
-target_link_libraries(sherpa-ncnn-core kaldi-native-fbank-core ncnn)
-set_target_properties(sherpa-ncnn-core PROPERTIES VERSION ${SHERPA_NCNN_VERSION} SOVERSION ${SHERPA_NCNN_VERSION_MAJOR})
+target_link_libraries(sherpa-ncnn-core PUBLIC kaldi-native-fbank-core ncnn)
 install(TARGETS sherpa-ncnn-core DESTINATION lib)
 
-add_executable(sherpa-ncnn sherpa-ncnn.cc)
-target_link_libraries(sherpa-ncnn sherpa-ncnn-core)
-install(TARGETS sherpa-ncnn DESTINATION bin)
+if(NOT SHERPA_NCNN_ENABLE_PYTHON)
+  add_executable(sherpa-ncnn sherpa-ncnn.cc)
+  target_link_libraries(sherpa-ncnn PRIVATE sherpa-ncnn-core)
+  install(TARGETS sherpa-ncnn DESTINATION bin)
 
-if(NOT DEFINED ANDROID_ABI)
+  if(NOT DEFINED ANDROID_ABI)
+    if(SHERPA_NCNN_ENABLE_PORTAUDIO)
+      add_executable(sherpa-ncnn-microphone
+        sherpa-ncnn-microphone.cc
+        microphone.cc
+      )
 
-  if(SHERPA_NCNN_ENABLE_PORTAUDIO)
-    add_executable(sherpa-ncnn-microphone
-      sherpa-ncnn-microphone.cc
-      microphone.cc
-    )
+      if(BUILD_SHARED_LIBS)
+        set(PA_LIB portaudio)
+      else()
+        set(PA_LIB portaudio_static)
+      endif()
 
-    if(BUILD_SHARED_LIBS)
-      set(PA_LIB portaudio)
-    else()
-      set(PA_LIB portaudio_static)
-    endif()
-
-    target_link_libraries(sherpa-ncnn-microphone ${PA_LIB} sherpa-ncnn-core)
+      target_link_libraries(sherpa-ncnn-microphone PRIVATE ${PA_LIB} sherpa-ncnn-core)
 
-    install(TARGETS sherpa-ncnn-microphone DESTINATION bin)
+      install(TARGETS sherpa-ncnn-microphone DESTINATION bin)
+    endif()
   endif()
-endif()
 
-set(hdrs
-  decode.h
-  features.h
-  model.h
-  symbol-table.h
-  wave-reader.h
-)
+  set(hdrs
+    decode.h
+    features.h
+    model.h
+    symbol-table.h
+    wave-reader.h
+  )
 
-install(FILES ${hdrs}
-  DESTINATION include/sherpa-ncnn/csrc
-)
+  install(FILES ${hdrs}
+    DESTINATION include/sherpa-ncnn/csrc
+  )
 
-add_executable(generate-int8-scale-table generate-int8-scale-table.cc)
-target_link_libraries(generate-int8-scale-table sherpa-ncnn-core)
+  add_executable(generate-int8-scale-table generate-int8-scale-table.cc)
+  target_link_libraries(generate-int8-scale-table sherpa-ncnn-core)
+endif()

+ 0 - 1
sherpa-ncnn/python/CMakeLists.txt

@@ -1,2 +1 @@
 add_subdirectory(csrc)
-# add_subdirectory(tests)

+ 19 - 1
sherpa-ncnn/python/csrc/CMakeLists.txt

@@ -10,4 +10,22 @@ set(srcs
 
 pybind11_add_module(_sherpa_ncnn ${srcs})
 target_link_libraries(_sherpa_ncnn PRIVATE sherpa-ncnn-core)
-target_link_libraries(_sherpa_ncnn PRIVATE ncnn)
+
+if(APPLE)
+  execute_process(
+    COMMAND "${PYTHON_EXECUTABLE}" -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
+    OUTPUT_STRIP_TRAILING_WHITESPACE
+    OUTPUT_VARIABLE PYTHON_SITE_PACKAGE_DIR
+  )
+  message(STATUS "PYTHON_SITE_PACKAGE_DIR: ${PYTHON_SITE_PACKAGE_DIR}")
+  target_link_libraries(_sherpa_ncnn PRIVATE "-Wl,-rpath,${PYTHON_SITE_PACKAGE_DIR}")
+endif()
+
+if(NOT WIN32)
+  target_link_libraries(_sherpa_ncnn PRIVATE "-Wl,-rpath,${SHERPA_NCNN_RPATH_ORIGIN}/sherpa_ncnn/lib")
+endif()
+
+
+install(TARGETS _sherpa_ncnn
+  DESTINATION ../
+)