1*a58d3d2aSXin Li# Using CMake for the Opus Project 2*a58d3d2aSXin Li 3*a58d3d2aSXin LiThis guide provides instructions for using CMake to build the Opus project with various configuration options. CMake is a widely used build system generator that helps manage the build process across different platforms. 4*a58d3d2aSXin Li 5*a58d3d2aSXin LiNote: Please keep in mind that software documentation can sometimes go out of date as new versions are released. It is always recommended to refer to the official CMake documentation for the most up-to-date and accurate information. You can find the official CMake documentation at [cmake.org/documentation](https://cmake.org/documentation/). 6*a58d3d2aSXin Li 7*a58d3d2aSXin Li## Prerequisites 8*a58d3d2aSXin Li 9*a58d3d2aSXin LiBefore proceeding, make sure you have the following prerequisites installed: 10*a58d3d2aSXin Li 11*a58d3d2aSXin Li- CMake 12*a58d3d2aSXin Li- Git (optional, but recommended for version control integration) 13*a58d3d2aSXin Li- Working C compiler 14*a58d3d2aSXin Li 15*a58d3d2aSXin Li## Build Instructions 16*a58d3d2aSXin Li 17*a58d3d2aSXin LiFollow the steps below to build the Opus project using CMake: 18*a58d3d2aSXin Li 19*a58d3d2aSXin Li1. Clone the Opus repository using Git: 20*a58d3d2aSXin Li 21*a58d3d2aSXin Li ```shell 22*a58d3d2aSXin Li git clone https://gitlab.xiph.org/xiph/opus 23*a58d3d2aSXin Li ``` 24*a58d3d2aSXin Li 25*a58d3d2aSXin Li2. Create a build directory within the Opus repository: 26*a58d3d2aSXin Li 27*a58d3d2aSXin Li ```shell 28*a58d3d2aSXin Li cd opus 29*a58d3d2aSXin Li mkdir build 30*a58d3d2aSXin Li cd build 31*a58d3d2aSXin Li ``` 32*a58d3d2aSXin Li 33*a58d3d2aSXin Li3. Configure the build with CMake. You can set the desired configuration options using CMake's `-D` flag. Here are some available options: 34*a58d3d2aSXin Li 35*a58d3d2aSXin Li - `OPUS_BUILD_SHARED_LIBRARY`: build shared library. 36*a58d3d2aSXin Li - `OPUS_BUILD_TESTING`: build tests. 37*a58d3d2aSXin Li - `OPUS_BUILD_PROGRAMS`: build programs. 38*a58d3d2aSXin Li - `OPUS_CUSTOM_MODES`, enable non-Opus modes, e.g. 44.1 kHz & 2^n frames. 39*a58d3d2aSXin Li 40*a58d3d2aSXin Li For example, to enable the custom modes and build programs, use the following command: 41*a58d3d2aSXin Li 42*a58d3d2aSXin Li ```shell 43*a58d3d2aSXin Li cmake .. -DOPUS_BUILD_PROGRAMS=ON -DOPUS_BUILD_TESTING=ON 44*a58d3d2aSXin Li ``` 45*a58d3d2aSXin Li 46*a58d3d2aSXin Li4. Build the Opus project: 47*a58d3d2aSXin Li 48*a58d3d2aSXin Li ```shell 49*a58d3d2aSXin Li cmake --build . 50*a58d3d2aSXin Li ``` 51*a58d3d2aSXin Li 52*a58d3d2aSXin Li5. After a successful build, you can find the compiled Opus library and associated files in the build directory. 53*a58d3d2aSXin Li 54*a58d3d2aSXin Li## Testing with CTest 55*a58d3d2aSXin Li 56*a58d3d2aSXin LiOpus provides a comprehensive test suite to ensure the functionality and correctness of the project. You can execute the tests using CTest, a part of the CMake build system. CTest allows for automated testing and provides useful features for managing and evaluating the test results. 57*a58d3d2aSXin Li 58*a58d3d2aSXin LiTo run the Opus tests using CTest, follow these steps: 59*a58d3d2aSXin Li 60*a58d3d2aSXin Li1. Navigate to the build directory after configuring and building the project with CMake: 61*a58d3d2aSXin Li 62*a58d3d2aSXin Li ```shell 63*a58d3d2aSXin Li cd build 64*a58d3d2aSXin Li ``` 65*a58d3d2aSXin Li 66*a58d3d2aSXin Li2. Execute the tests using CTest: 67*a58d3d2aSXin Li 68*a58d3d2aSXin Li ```shell 69*a58d3d2aSXin Li ctest 70*a58d3d2aSXin Li ``` 71*a58d3d2aSXin Li 72*a58d3d2aSXin LiNote: For Windows you need to specify which configuration to test 73*a58d3d2aSXin Li 74*a58d3d2aSXin Li```shell 75*a58d3d2aSXin Lictest -C Debug 76*a58d3d2aSXin Li``` 77*a58d3d2aSXin Li 78*a58d3d2aSXin Li## Platform Support and Bug Reporting 79*a58d3d2aSXin Li 80*a58d3d2aSXin LiCMake aims to provide broad platform support, allowing the Opus project to be built and used on major operating systems and platforms. The supported platforms include: 81*a58d3d2aSXin Li 82*a58d3d2aSXin Li- Windows 83*a58d3d2aSXin Li- macOS 84*a58d3d2aSXin Li- Linux 85*a58d3d2aSXin Li- Android 86*a58d3d2aSXin Li- iOS 87*a58d3d2aSXin Li 88*a58d3d2aSXin LiCMake achieves platform support by generating platform-specific build files (e.g., Makefiles, Visual Studio projects) based on the target platform. This allows developers to build and configure the Opus project consistently across different operating systems and environments. 89*a58d3d2aSXin Li 90*a58d3d2aSXin LiWhile CMake strives to ensure compatibility and stability across platforms, bugs or issues may still arise in specific configurations. If you encounter any problems during the configuration process or while building the Opus project, we encourage you to file an issue in the [project's issue tracker](https://gitlab.xiph.org/xiph/opus/-/issues). 91*a58d3d2aSXin Li 92*a58d3d2aSXin LiWhen reporting an issue, please provide the following information to help us understand and reproduce the configuration problem effectively: 93*a58d3d2aSXin Li 94*a58d3d2aSXin Li1. Detailed description of the issue, including any error messages or unexpected behavior observed. 95*a58d3d2aSXin Li2. Steps to reproduce the problem, including the CMake command and any specific configuration options used. 96*a58d3d2aSXin Li3. Operating system and version (e.g., Windows 10, macOS Big Sur, Ubuntu 20.04). 97*a58d3d2aSXin Li4. CMake version (e.g., CMake 3.21.1). 98*a58d3d2aSXin Li5. Any relevant information about the platform, toolchain, or dependencies used. 99*a58d3d2aSXin Li6. Additional context or details that might assist in troubleshooting the issue. 100*a58d3d2aSXin Li 101*a58d3d2aSXin LiBy providing thorough information when reporting configuration issues, you contribute to improving the Opus project's compatibility and reliability across different platforms. 102*a58d3d2aSXin Li 103*a58d3d2aSXin LiWe appreciate your help in identifying and addressing any configuration-related problems, ensuring a better experience for all users of the Opus project. 104*a58d3d2aSXin Li 105*a58d3d2aSXin Li## Platform Specific Examples 106*a58d3d2aSXin Li 107*a58d3d2aSXin LiNote: Examples can go out of date. Always refer to documentation for latest reference. 108*a58d3d2aSXin Li 109*a58d3d2aSXin Li### Cross compiling for Android 110*a58d3d2aSXin Li 111*a58d3d2aSXin Li```shell 112*a58d3d2aSXin Licmake .. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_HOME}/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a 113*a58d3d2aSXin Li``` 114*a58d3d2aSXin Li 115*a58d3d2aSXin LiFor more information about cross compiling for android, you can refer to the [Cross compiling for Android documentation](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-android). 116*a58d3d2aSXin Li 117*a58d3d2aSXin Li### Cross compiling for iOS 118*a58d3d2aSXin Li 119*a58d3d2aSXin Li```shell 120*a58d3d2aSXin Licmake .. -G "Unix Makefiles" -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_ARCHITECTURES=arm64 121*a58d3d2aSXin Li``` 122*a58d3d2aSXin Li 123*a58d3d2aSXin LiFor more information about cross compilation for iOS, you can refer to the [Cross compiling for iOS documentation](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-ios-tvos-or-watchos). 124*a58d3d2aSXin Li 125*a58d3d2aSXin Li 126*a58d3d2aSXin Li### Windows Visual Studio 127*a58d3d2aSXin Li 128*a58d3d2aSXin Li```shell 129*a58d3d2aSXin Licmake .. -G "Visual Studio 17 2022" -A x64 130*a58d3d2aSXin Li``` 131*a58d3d2aSXin Li 132*a58d3d2aSXin LiFor more information about the Visual Studio generator options and additional customization, you can refer to the [Visual Studio Generator documentation](https://cmake.org/cmake/help/latest/generator/Visual%20Studio%2017%202022.html). 133