Files
stt/CMakeLists.txt
2025-11-27 15:06:01 +08:00

89 lines
2.4 KiB
CMake
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
cmake_minimum_required(VERSION 3.15.2)
# 设置工程名称
project(stt VERSION 1.4.1 LANGUAGES CXX)
# 设置C++标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 编译选项
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
message(STATUS "Platform: " ${CMAKE_HOST_SYSTEM_NAME})
message(STATUS "Install prefix: " ${CMAKE_INSTALL_PREFIX})
message(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
# 获取所有源文件
aux_source_directory(src STT_SRC)
# 创建可执行文件(保留原有功能)
add_executable(stt ${STT_SRC})
set_target_properties(stt PROPERTIES CXX_STANDARD 11)
# 安装可执行文件
install(TARGETS stt RUNTIME DESTINATION sbin)
# Python绑定支持可选
option(BUILD_PYTHON_MODULE "Build Python module" OFF)
if(BUILD_PYTHON_MODULE)
# 查找pybind11
find_package(pybind11 REQUIRED)
if(NOT pybind11_FOUND)
message(STATUS "pybind11 not found, trying to find it via Python")
# 尝试通过Python找到pybind11
execute_process(
COMMAND ${Python_EXECUTABLE} -m pybind11 --includes
OUTPUT_VARIABLE PYBIND11_INCLUDES
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if(NOT PYBIND11_INCLUDES)
message(FATAL_ERROR "pybind11 is required for Python module build")
endif()
endif()
message(STATUS "Building Python module")
# 创建Python模块
pybind11_add_module(stt_python
pybind/stt_binding.cpp
${STT_SRC}
)
# 设置模块属性
set_target_properties(stt_python PROPERTIES
CXX_STANDARD 11
OUTPUT_NAME "stt"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/pybind"
)
# 添加包含目录
target_include_directories(stt_python PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}
)
# 定义宏
target_compile_definitions(stt_python PRIVATE
VERSION_INFO="${PROJECT_VERSION}"
)
# 链接库(如果需要)
# target_link_libraries(stt_python PRIVATE ...)
message(STATUS "Python module will be built as: pybind/stt${PYTHON_MODULE_EXTENSION}")
endif()
# 安装Python绑定文件如果构建了Python模块
if(BUILD_PYTHON_MODULE)
install(FILES
pybind/__init__.py
pybind/example_usage.py
DESTINATION ${Python_SITEARCH}/stt
)
endif()