CMakeLists.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. cmake_minimum_required(VERSION 3.17)
  2. project(bs_roformer_cpp VERSION 1.0.0 LANGUAGES CXX)
  3. set(CMAKE_CXX_STANDARD 17)
  4. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  5. #================================================
  6. # Build Options
  7. #================================================
  8. option(GGML_CUDA "Enable CUDA backend" ON)
  9. option(MBR_BUILD_TESTS "Build tests" OFF)
  10. option(MBR_BUILD_CLI "Build CLI application" ON)
  11. #================================================
  12. # Dependencies - GGML (Flexible Resolution)
  13. #================================================
  14. # Strategy: Allow ggml to be shared across multiple projects
  15. # 1. Check if ggml target already exists (e.g., from parent project like whisper.cpp)
  16. # 2. If not, try to find ggml via CMAKE_PREFIX_PATH or GGML_DIR
  17. # 3. If not found, use local ggml (submodule or sibling directory)
  18. if(NOT TARGET ggml)
  19. # Try to find ggml package first (for system-wide or parent project installation)
  20. find_package(ggml QUIET CONFIG)
  21. if(NOT ggml_FOUND)
  22. # ggml not found as package, look for source directory
  23. # Priority 1: GGML_DIR variable (explicitly set by user or parent project)
  24. # Priority 2: Submodule in ggml/
  25. # Priority 3: Sibling directory ../ggml
  26. if(DEFINED GGML_DIR)
  27. set(GGML_PATH "${GGML_DIR}")
  28. message(STATUS "Using GGML from GGML_DIR: ${GGML_PATH}")
  29. elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/ggml/CMakeLists.txt")
  30. set(GGML_PATH "${CMAKE_CURRENT_SOURCE_DIR}/ggml")
  31. message(STATUS "Using GGML from submodule: ${GGML_PATH}")
  32. elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../ggml/CMakeLists.txt")
  33. set(GGML_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../ggml")
  34. message(STATUS "Using GGML from sibling directory: ${GGML_PATH}")
  35. else()
  36. message(FATAL_ERROR
  37. "ggml not found. Please either:\n"
  38. " 1. Add ggml as submodule: git submodule add https://github.com/ggerganov/ggml.git\n"
  39. " 2. Clone ggml to parent directory: cd .. && git clone https://github.com/ggerganov/ggml.git\n"
  40. " 3. Set GGML_DIR to point to ggml source: cmake -DGGML_DIR=/path/to/ggml\n"
  41. " 4. Let parent project provide ggml target"
  42. )
  43. endif()
  44. # Add ggml as subdirectory
  45. add_subdirectory(${GGML_PATH} ggml EXCLUDE_FROM_ALL)
  46. else()
  47. message(STATUS "Using GGML from installed package")
  48. endif()
  49. else()
  50. message(STATUS "Using GGML target from parent project")
  51. endif()
  52. #================================================
  53. # Core Library
  54. #================================================
  55. set(MBR_SOURCES
  56. src/model.cpp
  57. src/utils.cpp
  58. src/inference.cpp
  59. )
  60. add_library(bs_roformer STATIC ${MBR_SOURCES})
  61. target_include_directories(bs_roformer PUBLIC
  62. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  63. $<INSTALL_INTERFACE:include>
  64. )
  65. target_include_directories(bs_roformer PRIVATE
  66. ${CMAKE_CURRENT_SOURCE_DIR}/src
  67. )
  68. target_link_libraries(bs_roformer PUBLIC ggml)
  69. if(GGML_CUDA AND TARGET ggml-cuda)
  70. target_link_libraries(bs_roformer PUBLIC ggml-cuda)
  71. # Fix for CI: Link against CUDA stubs if the driver is not present
  72. # This prevents errors like "libcuda.so.1 needed by ... not found" during linking
  73. find_package(CUDAToolkit REQUIRED)
  74. if(TARGET CUDA::cuda_driver)
  75. target_link_libraries(bs_roformer PUBLIC CUDA::cuda_driver)
  76. message(STATUS "Linked against CUDA driver stubs")
  77. endif()
  78. endif()
  79. # Compiler options
  80. if(MSVC)
  81. target_compile_options(bs_roformer PRIVATE /W3 /utf-8)
  82. else()
  83. target_compile_options(bs_roformer PRIVATE -Wall -Wextra)
  84. endif()
  85. # OpenMP support
  86. find_package(OpenMP)
  87. if(OpenMP_CXX_FOUND)
  88. target_link_libraries(bs_roformer PUBLIC OpenMP::OpenMP_CXX)
  89. target_compile_definitions(bs_roformer PUBLIC USE_OPENMP)
  90. message(STATUS "OpenMP: ENABLED")
  91. else()
  92. message(STATUS "OpenMP: NOT FOUND")
  93. endif()
  94. #================================================
  95. # DLL Copy Helper (Windows)
  96. #================================================
  97. function(mbr_copy_ggml_dlls target_name)
  98. if(NOT WIN32)
  99. return()
  100. endif()
  101. set(GGML_DLL_TARGETS ggml ggml-base ggml-cpu ggml-cuda ggml-vulkan)
  102. foreach(dll_target ${GGML_DLL_TARGETS})
  103. if(TARGET ${dll_target})
  104. add_custom_command(TARGET ${target_name} POST_BUILD
  105. COMMAND ${CMAKE_COMMAND} -E copy_if_different
  106. $<TARGET_FILE:${dll_target}>
  107. $<TARGET_FILE_DIR:${target_name}>
  108. COMMENT "Copying ${dll_target}.dll for ${target_name}"
  109. )
  110. endif()
  111. endforeach()
  112. endfunction()
  113. #================================================
  114. # CLI Application
  115. #================================================
  116. if(MBR_BUILD_CLI)
  117. # audio.cpp implements AudioFile utilities (using dr_wav)
  118. add_executable(bs_roformer-cli
  119. cli/main.cpp
  120. src/audio.cpp
  121. )
  122. target_link_libraries(bs_roformer-cli PRIVATE bs_roformer)
  123. target_include_directories(bs_roformer-cli PRIVATE
  124. src
  125. third_party
  126. )
  127. if(MSVC)
  128. target_compile_options(bs_roformer-cli PRIVATE /W3 /utf-8)
  129. endif()
  130. mbr_copy_ggml_dlls(bs_roformer-cli)
  131. endif()
  132. #================================================
  133. # Tests (Optional)
  134. #================================================
  135. if(MBR_BUILD_TESTS)
  136. enable_testing()
  137. add_subdirectory(tests)
  138. message(STATUS "Tests: ENABLED")
  139. else()
  140. message(STATUS "Tests: DISABLED (use -DMBR_BUILD_TESTS=ON to enable)")
  141. endif()