load("@rules_python//python:versions.bzl", "gen_python_config_settings") load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier", "buildifier_test") load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") load("@rules_cc//cc:defs.bzl", "cc_library") load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@bazel_skylib//lib:selects.bzl", "selects") load("@rules_python//python:packaging.bzl", "py_wheel") load("@com_github_bentoml_plugins//rules/py:packaging.bzl", "py_package") load("@python_abi//:abi.bzl", "python_abi") package(default_visibility = ["//visibility:public"]) filegroup( name = "pyproject", srcs = ["pyproject.toml"], ) exports_files([ "LICENSE", "README.md", "README.rst", "yarn.lock", ]) buildifier( name = "buildfmt", ) buildifier_test( name = "buildcheck", srcs = glob([ "**/*.bzl", "**/*.bazel", ]), ) alias( name = "pyright", actual = "@npm//:node_modules/pyright/index.js", ) alias( name = "pypi_update", actual = "//requirements:pypi.update", ) alias( name = "release_update", actual = "//requirements/release:requirements.update", ) COPTS = [ "-O3", "-pthread", "-std=c++11", "-fPIC", "-Wall", ] + selects.with_or({ "//conditions:default": [], "@bazel_tools//src/conditions:linux_x86_64": [ "-mavx", "-mavx2", "-mfma", "-mf16c", "-msse3", ], }) cc_library( name = "audio_lib", srcs = ["//src/whispercpp:audio.cc"], hdrs = [ "//src/whispercpp:audio.h", "//src/whispercpp:context.h", "@com_github_ggerganov_whisper//:examples/common.h", "@com_github_ggerganov_whisper//:whisper.h", ], copts = COPTS, defines = ["BAZEL_BUILD"], linkopts = selects.with_or({ "//conditions:default": [], "@bazel_tools//src/conditions:darwin": [ "-framework", "AudioToolbox", ], }), deps = [ ":context_lib", "@com_github_ggerganov_whisper//:common", "@com_github_libsdl_sdl2//:SDL", "@com_github_libsdl_sdl2//:include", "@pybind11", ], ) cc_library( name = "context_lib", srcs = [ "//src/whispercpp:context.cc", "//src/whispercpp:params.cc", ], hdrs = [ "//src/whispercpp:context.h", "@com_github_ggerganov_whisper//:whisper.cpp", "@com_github_ggerganov_whisper//:whisper.h", ], copts = COPTS, defines = ["BAZEL_BUILD"], deps = [ "@com_github_ggerganov_whisper//:common", "@com_github_ggerganov_whisper//:whisper", "@pybind11", ], ) pybind_extension( name = "audio_cpp2py_export", srcs = [ "//src/whispercpp:audio.cc", "//src/whispercpp:audio.h", "//src/whispercpp:context.cc", "//src/whispercpp:context.h", "//src/whispercpp:params.cc", "@com_github_ggerganov_whisper//:whisper.h", ], copts = COPTS, defines = ["BAZEL_BUILD"], deps = [ ":audio_lib", ":context_lib", "@com_github_ggerganov_whisper//:common", ], ) pybind_extension( name = "api_cpp2py_export", srcs = [ "//src/whispercpp:api_cpp2py_export.cc", "//src/whispercpp:api_cpp2py_export.h", "//src/whispercpp:context.cc", "//src/whispercpp:context.h", "//src/whispercpp:params.cc", "@com_github_ggerganov_whisper//:examples/common.h", "@com_github_ggerganov_whisper//:ggml.h", "@com_github_ggerganov_whisper//:whisper.h", ], copts = COPTS, defines = ["BAZEL_BUILD"], deps = [":context_lib"], ) write_file( name = "gen_extensions", out = "extensions.sh", content = [ "#!/usr/bin/env bash", "cd $BUILD_WORKSPACE_DIRECTORY", "[[ -f src/whispercpp/api_cpp2py_export.so ]] && rm -fv src/whispercpp/api_cpp2py_export.so", "cp -fv bazel-bin/api_cpp2py_export.so src/whispercpp/api_cpp2py_export.so", "[[ -f src/whispercpp/audio_cpp2py_export.so ]] && rm -fv src/whispercpp/audio_cpp2py_export.so", "cp -fv bazel-bin/audio_cpp2py_export.so src/whispercpp/audio_cpp2py_export.so", ], ) sh_binary( name = "extensions", srcs = [":gen_extensions"], data = [ ":api_cpp2py_export.so", ":audio_cpp2py_export.so", ], ) # public exports alias( name = "whispercpp_lib", actual = "//src/whispercpp:whispercpp_lib", ) py_package( name = "whispercpp_pkg", layout = "src", packages = ["src.whispercpp"], deps = [":whispercpp_lib"], ) config_setting( name = "ci", values = {"define": "ci=true"}, ) gen_python_config_settings() py_wheel( name = "whispercpp_wheel", abi = python_abi(), author = "Aaron Pham", author_email = "aarnphm@bentoml.com", classifiers = [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Operating System :: POSIX :: Linux", "Topic :: Scientific/Engineering", "Topic :: Scientific/Engineering :: Mathematics", "Topic :: Scientific/Engineering :: Artificial Intelligence", "Topic :: Software Development", "Topic :: Software Development :: Libraries", "Topic :: Software Development :: Libraries :: Python Modules", "Programming Language :: Python", "Programming Language :: C++", "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: Implementation :: CPython", ], description_file = "README.rst", distribution = "whispercpp", homepage = "https://github.com/aarnphm/whispercpp", license = "Apache 2.0", platform = select({ "//:aarch64-apple-darwin": "macosx_10_9_arm64", "//:aarch64-unknown-linux-gnu": "manylinux2014_aarch64", "//:x86_64-apple-darwin": "macosx_10_9_x86_64", "//:x86_64-pc-windows-msvc": "win_amd64", "//:x86_64-unknown-linux-gnu": "manylinux2014_x86_64", }), python_tag = python_abi(), stamp = 1, strip_path_prefixes = ["src"], twine = "@release_twine//:pkg", # NOTE: This can be replaced by building with --stamp --embed_label=1.2.3 version = select({ "//conditions:default": "0.0.17", ":ci": "{BUILD_EMBED_LABEL}", }), visibility = ["//:__subpackages__"], deps = [":whispercpp_pkg"], )