obs-localvocal/cmake/BuildWhispercpp.cmake

147 lines
7.2 KiB
CMake
Raw Normal View History

2023-08-12 20:51:51 +00:00
include(ExternalProject)
2023-08-13 20:53:25 +00:00
set(CMAKE_OSX_ARCHITECTURES_ "arm64$<SEMICOLON>x86_64")
2023-08-12 20:51:51 +00:00
if(${CMAKE_BUILD_TYPE} STREQUAL Release OR ${CMAKE_BUILD_TYPE} STREQUAL RelWithDebInfo)
set(Whispercpp_BUILD_TYPE Release)
else()
set(Whispercpp_BUILD_TYPE Debug)
endif()
# On linux add the `-fPIC` flag to the compiler
if(UNIX AND NOT APPLE)
set(WHISPER_EXTRA_CXX_FLAGS "-fPIC")
2023-09-28 05:46:21 +00:00
set(WHISPER_ADDITIONAL_CMAKE_ARGS -DWHISPER_BLAS=OFF -DWHISPER_CUBLAS=OFF -DWHISPER_OPENBLAS=OFF -DWHISPER_NO_AVX=ON
-DWHISPER_NO_AVX2=ON)
2023-08-12 20:51:51 +00:00
endif()
2023-09-11 04:48:49 +00:00
if(WIN32)
2023-09-12 05:03:05 +00:00
if(LOCALVOCAL_WITH_CUDA)
2023-09-12 05:03:32 +00:00
# Build with CUDA Check that CUDA_TOOLKIT_ROOT_DIR is set
2023-09-12 05:03:05 +00:00
if(NOT DEFINED CUDA_TOOLKIT_ROOT_DIR)
message(FATAL_ERROR "CUDA_TOOLKIT_ROOT_DIR is not set. Please set it to the root directory of your CUDA "
"installation.")
endif(NOT DEFINED CUDA_TOOLKIT_ROOT_DIR)
set(WHISPER_ADDITIONAL_ENV "CUDAToolkit_ROOT=${CUDA_TOOLKIT_ROOT_DIR}")
set(WHISPER_ADDITIONAL_CMAKE_ARGS -DWHISPER_CUBLAS=ON -DCMAKE_GENERATOR_TOOLSET=cuda=${CUDA_TOOLKIT_ROOT_DIR})
2023-09-26 03:55:32 +00:00
else()
2023-09-12 05:03:32 +00:00
# Build with OpenBLAS
2023-09-12 05:03:05 +00:00
set(OpenBLAS_URL "https://github.com/xianyi/OpenBLAS/releases/download/v0.3.24/OpenBLAS-0.3.24-x64.zip")
2023-09-16 15:40:16 +00:00
set(OpenBLAS_SHA256 "6335128ee7117ea2dd2f5f96f76dafc17256c85992637189a2d5f6da0c608163")
2023-09-12 05:03:05 +00:00
ExternalProject_Add(
2023-09-12 05:03:32 +00:00
OpenBLAS
URL ${OpenBLAS_URL}
URL_HASH SHA256=${OpenBLAS_SHA256}
DOWNLOAD_NO_PROGRESS true
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR> <INSTALL_DIR>)
2023-09-12 05:03:05 +00:00
ExternalProject_Get_Property(OpenBLAS INSTALL_DIR)
set(OpenBLAS_DIR ${INSTALL_DIR})
set(WHISPER_ADDITIONAL_ENV "OPENBLAS_PATH=${OpenBLAS_DIR}")
2023-09-26 03:58:10 +00:00
set(WHISPER_ADDITIONAL_CMAKE_ARGS -DWHISPER_BLAS=ON -DWHISPER_CUBLAS=OFF)
2023-09-26 03:55:32 +00:00
endif()
2023-09-11 04:48:49 +00:00
2023-09-11 05:14:44 +00:00
ExternalProject_Add(
Whispercpp_Build
DOWNLOAD_EXTRACT_TIMESTAMP true
GIT_REPOSITORY https://github.com/ggerganov/whisper.cpp.git
2023-11-05 02:30:23 +00:00
GIT_TAG f96e1c5b7865e01fece99f69286d922d949a260d
2023-09-11 05:14:44 +00:00
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${Whispercpp_BUILD_TYPE}
BUILD_BYPRODUCTS
<INSTALL_DIR>/lib/static/${CMAKE_STATIC_LIBRARY_PREFIX}whisper${CMAKE_STATIC_LIBRARY_SUFFIX}
<INSTALL_DIR>/bin/${CMAKE_SHARED_LIBRARY_PREFIX}whisper${CMAKE_SHARED_LIBRARY_SUFFIX}
<INSTALL_DIR>/lib/${CMAKE_IMPORT_LIBRARY_PREFIX}whisper${CMAKE_IMPORT_LIBRARY_SUFFIX}
CMAKE_GENERATOR ${CMAKE_GENERATOR}
INSTALL_COMMAND ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${Whispercpp_BUILD_TYPE} && ${CMAKE_COMMAND} -E
copy <BINARY_DIR>/${Whispercpp_BUILD_TYPE}/whisper.lib <INSTALL_DIR>/lib
CONFIGURE_COMMAND
2023-09-12 05:03:05 +00:00
${CMAKE_COMMAND} -E env ${WHISPER_ADDITIONAL_ENV} ${CMAKE_COMMAND} <SOURCE_DIR> -B <BINARY_DIR> -G
2023-09-11 05:14:44 +00:00
${CMAKE_GENERATOR} -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR> -DCMAKE_BUILD_TYPE=${Whispercpp_BUILD_TYPE}
-DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} -DCMAKE_OSX_DEPLOYMENT_TARGET=10.13
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES_} -DCMAKE_CXX_FLAGS=${WHISPER_EXTRA_CXX_FLAGS}
-DCMAKE_C_FLAGS=${WHISPER_EXTRA_CXX_FLAGS} -DBUILD_SHARED_LIBS=ON -DWHISPER_BUILD_TESTS=OFF
2023-09-12 05:03:05 +00:00
-DWHISPER_BUILD_EXAMPLES=OFF ${WHISPER_ADDITIONAL_CMAKE_ARGS})
2023-09-11 04:48:49 +00:00
2023-09-12 05:03:05 +00:00
if(NOT LOCALVOCAL_WITH_CUDA)
add_dependencies(Whispercpp_Build OpenBLAS)
endif(NOT LOCALVOCAL_WITH_CUDA)
2023-09-11 05:14:44 +00:00
else()
# On Linux and MacOS build a static Whisper library
ExternalProject_Add(
Whispercpp_Build
DOWNLOAD_EXTRACT_TIMESTAMP true
GIT_REPOSITORY https://github.com/ggerganov/whisper.cpp.git
2023-11-05 02:30:23 +00:00
GIT_TAG f96e1c5b7865e01fece99f69286d922d949a260d
2023-09-11 05:14:44 +00:00
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${Whispercpp_BUILD_TYPE}
BUILD_BYPRODUCTS <INSTALL_DIR>/lib/static/${CMAKE_STATIC_LIBRARY_PREFIX}whisper${CMAKE_STATIC_LIBRARY_SUFFIX}
CMAKE_GENERATOR ${CMAKE_GENERATOR}
INSTALL_COMMAND ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${Whispercpp_BUILD_TYPE}
CONFIGURE_COMMAND
2023-09-26 03:55:32 +00:00
${CMAKE_COMMAND} -E env ${WHISPER_ADDITIONAL_ENV} ${CMAKE_COMMAND} <SOURCE_DIR> -B <BINARY_DIR> -G
2023-09-11 05:14:44 +00:00
${CMAKE_GENERATOR} -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR> -DCMAKE_BUILD_TYPE=${Whispercpp_BUILD_TYPE}
-DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} -DCMAKE_OSX_DEPLOYMENT_TARGET=10.13
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES_} -DCMAKE_CXX_FLAGS=${WHISPER_EXTRA_CXX_FLAGS}
-DCMAKE_C_FLAGS=${WHISPER_EXTRA_CXX_FLAGS} -DBUILD_SHARED_LIBS=OFF -DWHISPER_BUILD_TESTS=OFF
2023-09-26 03:55:32 +00:00
-DWHISPER_BUILD_EXAMPLES=OFF ${WHISPER_ADDITIONAL_CMAKE_ARGS})
2023-09-11 04:48:49 +00:00
endif(WIN32)
2023-08-12 20:51:51 +00:00
ExternalProject_Get_Property(Whispercpp_Build INSTALL_DIR)
2023-09-11 05:14:44 +00:00
# add the Whisper library to the link line
if(WIN32)
add_library(Whispercpp::Whisper SHARED IMPORTED)
set_target_properties(
Whispercpp::Whisper
PROPERTIES IMPORTED_LOCATION ${INSTALL_DIR}/bin/${CMAKE_SHARED_LIBRARY_PREFIX}whisper${CMAKE_SHARED_LIBRARY_SUFFIX})
set_target_properties(
Whispercpp::Whisper
PROPERTIES IMPORTED_IMPLIB ${INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}whisper${CMAKE_STATIC_LIBRARY_SUFFIX})
2023-09-11 04:48:49 +00:00
2023-09-11 05:14:44 +00:00
install(FILES ${INSTALL_DIR}/bin/${CMAKE_SHARED_LIBRARY_PREFIX}whisper${CMAKE_SHARED_LIBRARY_SUFFIX}
DESTINATION "obs-plugins/64bit")
2023-09-11 04:48:49 +00:00
2023-09-12 05:03:05 +00:00
if(NOT LOCALVOCAL_WITH_CUDA)
# add openblas to the link line
add_library(Whispercpp::OpenBLAS STATIC IMPORTED)
set_target_properties(Whispercpp::OpenBLAS PROPERTIES IMPORTED_LOCATION ${OpenBLAS_DIR}/lib/libopenblas.dll.a)
install(FILES ${OpenBLAS_DIR}/bin/libopenblas.dll DESTINATION "obs-plugins/64bit")
2023-09-20 19:50:43 +00:00
else(NOT LOCALVOCAL_WITH_CUDA)
2023-09-20 20:18:39 +00:00
# normalize CUDA path with file(TO_CMAKE_PATH)
file(TO_CMAKE_PATH ${CUDA_TOOLKIT_ROOT_DIR} CUDA_TOOLKIT_ROOT_DIR)
# find the CUDA DLLs for cuBLAS in the bin directory of the CUDA installation e.g. cublas64_NN.dll
2023-09-20 19:50:43 +00:00
file(GLOB CUBLAS_DLLS "${CUDA_TOOLKIT_ROOT_DIR}/bin/cublas64_*.dll")
# find cublasLt DLL, e.g. cublasLt64_11.dll
file(GLOB CUBLASLT_DLLS "${CUDA_TOOLKIT_ROOT_DIR}/bin/cublasLt64_*.dll")
# find cudart DLL, e.g. cudart64_110.dll
file(GLOB CUDART_DLLS "${CUDA_TOOLKIT_ROOT_DIR}/bin/cudart64_*.dll")
# if any of the files cannot be found, abort
2023-09-20 20:18:39 +00:00
if(NOT CUBLAS_DLLS
OR NOT CUBLASLT_DLLS
OR NOT CUDART_DLLS)
2023-09-20 19:50:43 +00:00
message(FATAL_ERROR "Could not find cuBLAS, cuBLASLt or cuDART DLLs in ${CUDA_TOOLKIT_ROOT_DIR}/bin")
2023-09-20 20:18:39 +00:00
endif()
2023-09-20 19:50:43 +00:00
# copy the DLLs to the OBS plugin directory
install(FILES ${CUBLAS_DLLS} ${CUBLASLT_DLLS} ${CUDART_DLLS} DESTINATION "obs-plugins/64bit")
2023-09-12 05:03:05 +00:00
endif(NOT LOCALVOCAL_WITH_CUDA)
2023-09-11 05:14:44 +00:00
else()
# on Linux and MacOS add the static Whisper library to the link line
add_library(Whispercpp::Whisper STATIC IMPORTED)
set_target_properties(
Whispercpp::Whisper
PROPERTIES IMPORTED_LOCATION
${INSTALL_DIR}/lib/static/${CMAKE_STATIC_LIBRARY_PREFIX}whisper${CMAKE_STATIC_LIBRARY_SUFFIX})
2023-09-11 04:48:49 +00:00
endif(WIN32)
2023-08-12 20:51:51 +00:00
add_library(Whispercpp INTERFACE)
add_dependencies(Whispercpp Whispercpp_Build)
2023-09-11 05:14:44 +00:00
target_link_libraries(Whispercpp INTERFACE Whispercpp::Whisper)
2023-09-12 05:03:05 +00:00
if(WIN32 AND NOT LOCALVOCAL_WITH_CUDA)
2023-09-11 05:14:44 +00:00
target_link_libraries(Whispercpp INTERFACE Whispercpp::OpenBLAS)
2023-09-12 05:03:05 +00:00
endif()
2023-08-12 20:51:51 +00:00
set_target_properties(Whispercpp::Whisper PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)
if(APPLE)
target_link_libraries(Whispercpp INTERFACE "-framework Accelerate")
endif(APPLE)