Skip to content

Commit

Permalink
Refactor build.yaml, CMakeLists.txt, and setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
royshil committed Oct 25, 2024
1 parent e771682 commit 96f019d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ jobs:
env:
SIMPLER_WHISPER_ACCELERATION: ${{ matrix.acceleration }}
SIMPLER_WHISPER_PLATFORM: ${{ matrix.platform }}
SIMPLER_WHISPER_PYTHON_VERSION: ${{ matrix.python-version }}
run: |
python setup.py build_ext --inplace
python -m build --wheel
- name: Install built wheel Non-Windows
if: startsWith(matrix.os, 'windows') == false
run: |
pip install dist/*.whl
- name: Install built wheel Windows
if: startsWith(matrix.os, 'windows') == true
if: startsWith(matrix.os, 'windows') == true
shell: pwsh
run: |
$wheelFile = Get-ChildItem dist/*.whl | Select-Object -First 1
Expand All @@ -104,15 +105,15 @@ jobs:
run: |
import os
import glob
wheel_file = glob.glob('dist/*.whl')[0]
base_name = os.path.basename(wheel_file)
name_parts = base_name.split('-')
# Insert acceleration and platform before the last part (which is like 'any.whl')
new_name_parts = name_parts[:-1] + ['${{ matrix.acceleration }}', '${{ matrix.platform }}'] + [name_parts[-1]]
new_name = '-'.join(new_name_parts)
new_path = os.path.join('dist', new_name)
os.rename(wheel_file, new_path)
print(f"Renamed {base_name} to {new_name}")
Expand All @@ -125,10 +126,9 @@ jobs:
$wheelName += "-${{ matrix.acceleration }}"
}
echo "WHEEL_NAME=$wheelName" >> $env:GITHUB_ENV
- name: Upload wheel
uses: actions/upload-artifact@v4
with:
name: ${{ env.WHEEL_NAME }}
path: dist/*.whl

4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find Python
find_package(Python COMPONENTS Interpreter Development NumPy REQUIRED)
find_package(Python ${PYTHON_VERSION} EXACT COMPONENTS Interpreter Development NumPy REQUIRED)

# Fetch pybind11
include(FetchContent)
Expand Down Expand Up @@ -40,4 +40,4 @@ if(WIN32 OR APPLE)
$<TARGET_FILE_DIR:_whisper_cpp>
)
endforeach()
endif()
endif()
85 changes: 53 additions & 32 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@
import platform
import sysconfig


class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
def __init__(self, name, sourcedir=""):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)


class CMakeBuild(build_ext):
def run(self):
try:
out = subprocess.check_output(['cmake', '--version'])
out = subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError("CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
raise RuntimeError(
"CMake must be installed to build the following extensions: "
+ ", ".join(e.name for e in self.extensions)
)

for ext in self.extensions:
self.build_extension(ext)

def build_extension(self, ext):
# This is the critical change - we need to get the proper extension suffix
ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
ext_suffix = sysconfig.get_config_var("EXT_SUFFIX")

# Get the full path where the extension should be placed
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
Expand All @@ -33,60 +37,77 @@ def build_extension(self, ext):
os.makedirs(extdir, exist_ok=True)

# Get acceleration and platform from environment variables
acceleration = os.environ.get('SIMPLER_WHISPER_ACCELERATION', 'cpu')
target_platform = os.environ.get('SIMPLER_WHISPER_PLATFORM', platform.machine())
acceleration = os.environ.get("SIMPLER_WHISPER_ACCELERATION", "cpu")
target_platform = os.environ.get("SIMPLER_WHISPER_PLATFORM", platform.machine())
python_version = os.environ.get(
"SIMPLER_WHISPER_PYTHON_VERSION",
f"{sys.version_info.major}.{sys.version_info.minor}",
)

cmake_args = [
f'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}',
f'-DPYTHON_EXTENSION_SUFFIX={ext_suffix}', # Pass the extension suffix to CMake
f'-DACCELERATION={acceleration}',
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}",
f"-DPYTHON_EXTENSION_SUFFIX={ext_suffix}", # Pass the extension suffix to CMake
f"-DACCELERATION={acceleration}",
f"-DPYTHON_VERSION={python_version}",
]

env = os.environ.copy()

# Add platform-specific arguments
if platform.system() == "Darwin": # macOS
cmake_args += [
f'-DCMAKE_OSX_ARCHITECTURES={target_platform}',
'-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON',
'-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON',
f'-DCMAKE_INSTALL_NAME_DIR=@rpath'
f"-DCMAKE_OSX_ARCHITECTURES={target_platform}",
"-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON",
"-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON",
f"-DCMAKE_INSTALL_NAME_DIR=@rpath",
]
env["MACOS_ARCH"] = target_platform

cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
cfg = "Debug" if self.debug else "Release"
build_args = ["--config", cfg]

if platform.system() == "Windows":
cmake_args += [f'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}']
cmake_args += [f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}"]
if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']
build_args += ['--', '/m']
cmake_args += ["-A", "x64"]
build_args += ["--", "/m"]
else:
cmake_args += [f'-DCMAKE_BUILD_TYPE={cfg}']
build_args += ['--', '-j2']
cmake_args += [f"-DCMAKE_BUILD_TYPE={cfg}"]
build_args += ["--", "-j2"]

env['CXXFLAGS'] = f'{env.get("CXXFLAGS", "")} -DVERSION_INFO=\\"{self.distribution.get_version()}\\"'
env["CXXFLAGS"] = (
f'{env.get("CXXFLAGS", "")} -DVERSION_INFO=\\"{self.distribution.get_version()}\\"'
)

if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)

print("CMake args:", cmake_args)
print("Build args:", build_args)
print(f"Extension will be built in: {extdir}")
print(
f"Building for Python {python_version} on {target_platform} with acceleration: {acceleration}"
)

subprocess.check_call(
["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env
)
subprocess.check_call(
["cmake", "--build", "."] + build_args, cwd=self.build_temp
)

subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)

setup(
name='simpler-whisper',
version='0.2.0',
author='Roy Shilkrot',
author_email='[email protected]',
description='A simple Python wrapper for whisper.cpp',
long_description='',
ext_modules=[CMakeExtension('simpler_whisper._whisper_cpp')],
name="simpler-whisper",
version="0.2.0",
author="Roy Shilkrot",
author_email="[email protected]",
description="A simple Python wrapper for whisper.cpp",
long_description="",
ext_modules=[CMakeExtension("simpler_whisper._whisper_cpp")],
cmdclass=dict(build_ext=CMakeBuild),
zip_safe=False,
packages=['simpler_whisper'], # Add this line to ensure the package directory is created
packages=[
"simpler_whisper"
], # Add this line to ensure the package directory is created
)

0 comments on commit 96f019d

Please sign in to comment.