|
| 1 | +cmake_minimum_required(VERSION 3.14) |
| 2 | +project(labsoundpy) |
| 3 | + |
| 4 | +# Set C++ standard |
| 5 | +set(CMAKE_CXX_STANDARD 17) |
| 6 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 7 | + |
| 8 | +# Find Python |
| 9 | +find_package(Python 3.7 REQUIRED COMPONENTS Interpreter Development) |
| 10 | + |
| 11 | +# Option to use git submodules or fetch dependencies |
| 12 | +option(USE_SUBMODULES "Use git submodules for dependencies" ON) |
| 13 | + |
| 14 | +# Handle nanobind dependency |
| 15 | +if(USE_SUBMODULES) |
| 16 | + # Check if nanobind submodule exists |
| 17 | + if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/nanobind/CMakeLists.txt") |
| 18 | + message(FATAL_ERROR "Nanobind submodule not found. Please run: git submodule update --init --recursive") |
| 19 | + endif() |
| 20 | +else() |
| 21 | + # Fetch nanobind if not present |
| 22 | + if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/nanobind/CMakeLists.txt") |
| 23 | + message(STATUS "Downloading nanobind...") |
| 24 | + execute_process( |
| 25 | + COMMAND git clone --depth 1 https://github.com/wjakob/nanobind.git third_party/nanobind |
| 26 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 27 | + ) |
| 28 | + endif() |
| 29 | +endif() |
| 30 | + |
| 31 | +# Add nanobind |
| 32 | +add_subdirectory(third_party/nanobind) |
| 33 | + |
| 34 | +# Handle LabSound dependency |
| 35 | +if(USE_SUBMODULES) |
| 36 | + # Check if LabSound submodule exists |
| 37 | + if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/LabSound/CMakeLists.txt") |
| 38 | + message(FATAL_ERROR "LabSound submodule not found. Please run: git submodule update --init --recursive") |
| 39 | + endif() |
| 40 | + |
| 41 | + # Build LabSound |
| 42 | + set(LABSOUND_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/LabSound") |
| 43 | + set(LABSOUND_BUILD_EXAMPLES OFF CACHE BOOL "Build LabSound examples") |
| 44 | + set(LABSOUND_BUILD_TESTS OFF CACHE BOOL "Build LabSound tests") |
| 45 | + add_subdirectory(${LABSOUND_DIR}) |
| 46 | + |
| 47 | + # Use modern CMake target-based approach |
| 48 | + set(LABSOUND_INCLUDES |
| 49 | + ${LABSOUND_DIR}/include |
| 50 | + ${LABSOUND_DIR}/src |
| 51 | + ) |
| 52 | +else() |
| 53 | + # Try to find installed LabSound |
| 54 | + find_package(LabSound QUIET) |
| 55 | + |
| 56 | + if(LabSound_FOUND) |
| 57 | + message(STATUS "Found installed LabSound") |
| 58 | + else() |
| 59 | + # If not found, use a specific location or fetch it |
| 60 | + if(NOT DEFINED LABSOUND_DIR) |
| 61 | + message(STATUS "LabSound not found. Downloading...") |
| 62 | + execute_process( |
| 63 | + COMMAND git clone --depth 1 https://github.com/LabSound/LabSound.git third_party/LabSound |
| 64 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 65 | + ) |
| 66 | + set(LABSOUND_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/LabSound") |
| 67 | + set(LABSOUND_BUILD_EXAMPLES OFF CACHE BOOL "Build LabSound examples") |
| 68 | + set(LABSOUND_BUILD_TESTS OFF CACHE BOOL "Build LabSound tests") |
| 69 | + add_subdirectory(${LABSOUND_DIR}) |
| 70 | + else() |
| 71 | + message(STATUS "Using LabSound from: ${LABSOUND_DIR}") |
| 72 | + endif() |
| 73 | + |
| 74 | + set(LABSOUND_INCLUDES |
| 75 | + ${LABSOUND_DIR}/include |
| 76 | + ${LABSOUND_DIR}/src |
| 77 | + ) |
| 78 | + endif() |
| 79 | +endif() |
| 80 | + |
| 81 | +# Include LabSound headers |
| 82 | +include_directories(${LABSOUND_INCLUDES}) |
| 83 | + |
| 84 | +# Define the extension module |
| 85 | +nanobind_add_module( |
| 86 | + _core # Module name |
| 87 | + src/core.cpp # Main binding file |
| 88 | + src/audio_context.cpp # AudioContext bindings |
| 89 | + src/audio_node.cpp # AudioNode base class bindings |
| 90 | + src/audio_param.cpp # AudioParam bindings |
| 91 | + src/nodes/oscillator_node.cpp # OscillatorNode bindings |
| 92 | + src/nodes/gain_node.cpp # GainNode bindings |
| 93 | + src/nodes/biquad_filter_node.cpp # BiquadFilterNode bindings |
| 94 | + src/nodes/buffer_source_node.cpp # BufferSourceNode bindings |
| 95 | + src/nodes/analyzer_node.cpp # AnalyzerNode bindings |
| 96 | + src/nodes/function_node.cpp # FunctionNode bindings |
| 97 | + src/nodes/destination_node.cpp # DestinationNode bindings |
| 98 | + src/nodes/channel_merger_node.cpp # ChannelMergerNode bindings |
| 99 | + src/nodes/channel_splitter_node.cpp # ChannelSplitterNode bindings |
| 100 | + src/nodes/constant_source_node.cpp # ConstantSourceNode bindings |
| 101 | + src/nodes/convolver_node.cpp # ConvolverNode bindings |
| 102 | + src/nodes/delay_node.cpp # DelayNode bindings |
| 103 | + src/nodes/dynamics_compressor_node.cpp # DynamicsCompressorNode bindings |
| 104 | + src/nodes/panner_node.cpp # PannerNode bindings |
| 105 | + src/nodes/stereo_panner_node.cpp # StereoPannerNode bindings |
| 106 | + src/nodes/wave_shaper_node.cpp # WaveShaperNode bindings |
| 107 | +) |
| 108 | + |
| 109 | +target_include_directories(_core PRIVATE |
| 110 | + ${LABSOUND_INCLUDES} |
| 111 | + ${Python_INCLUDE_DIRS} |
| 112 | + src |
| 113 | +) |
| 114 | + |
| 115 | +# Link against LabSound |
| 116 | +if(LabSound_FOUND) |
| 117 | + # Use modern CMake target |
| 118 | + target_link_libraries(_core PRIVATE LabSound::LabSound) |
| 119 | +else() |
| 120 | + # Link against LabSound libraries directly |
| 121 | + if(APPLE) |
| 122 | + target_link_libraries(_core PRIVATE |
| 123 | + ${LABSOUND_DIR}/build/bin/LabSound.framework/Versions/A/LabSound |
| 124 | + ${LABSOUND_DIR}/build/bin/libLabSoundMiniAudio.a |
| 125 | + ${LABSOUND_DIR}/build/bin/libLabSoundRtAudio.a |
| 126 | + ${LABSOUND_DIR}/build/third_party/libnyquist/lib/liblibnyquist.a |
| 127 | + ) |
| 128 | + else() |
| 129 | + # For Linux and Windows, adjust paths as needed |
| 130 | + target_link_libraries(_core PRIVATE LabSound) |
| 131 | + endif() |
| 132 | +endif() |
| 133 | + |
| 134 | +# Install the extension module |
| 135 | +install(TARGETS _core DESTINATION labsound) |
0 commit comments