From 147e03ddd8db265553034d7aaf84232daa7fcb97 Mon Sep 17 00:00:00 2001 From: Jade Date: Wed, 21 Aug 2024 22:56:28 +0800 Subject: [PATCH] [build] CMake: Add CMakePresets.json for some common WPILib build settings (#6974) --- .github/workflows/cmake.yml | 18 ++++++------- CMakePresets.json | 50 +++++++++++++++++++++++++++++++++++++ README-CMAKE.md | 8 ++++-- 3 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 CMakePresets.json diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 8eb0e0ec65..1b217a64ed 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -15,12 +15,12 @@ jobs: - os: ubuntu-22.04 name: Linux container: wpilib/roborio-cross-ubuntu:2024-22.04 - flags: "-DCMAKE_BUILD_TYPE=Release -DWITH_JAVA=ON -DWITH_EXAMPLES=ON" + flags: "--preset with-java-sccache -DCMAKE_BUILD_TYPE=Release -DWITH_EXAMPLES=ON" - os: macOS-14 name: macOS container: "" env: "PATH=\"/opt/homebrew/opt/protobuf@3/bin:$PATH\"" - flags: "-DCMAKE_BUILD_TYPE=Release -DWITH_JAVA=OFF -DWITH_EXAMPLES=ON -DCMAKE_LIBRARY_PATH=/opt/homebrew/opt/protobuf@3/lib -DProtobuf_INCLUDE_DIR=/opt/homebrew/opt/protobuf@3/include -DProtobuf_PROTOC_EXECUTABLE=/opt/homebrew/opt/protobuf@3/bin/protoc" + flags: "--preset sccache -DCMAKE_BUILD_TYPE=Release -DWITH_JAVA=OFF -DWITH_EXAMPLES=ON -DCMAKE_LIBRARY_PATH=/opt/homebrew/opt/protobuf@3/lib -DProtobuf_INCLUDE_DIR=/opt/homebrew/opt/protobuf@3/include -DProtobuf_PROTOC_EXECUTABLE=/opt/homebrew/opt/protobuf@3/bin/protoc" name: "Build - ${{ matrix.name }}" runs-on: ${{ matrix.os }} @@ -40,18 +40,17 @@ jobs: - uses: actions/checkout@v4 - name: configure - run: cmake -S . -B build -G "Ninja" -DCMAKE_C_COMPILER_LAUNCHER=sccache -DCMAKE_CXX_COMPILER_LAUNCHER=sccache ${{ matrix.flags }} + run: cmake ${{ matrix.flags }} env: SCCACHE_GHA_ENABLED: "true" - name: build - working-directory: build - run: cmake --build . --parallel $(nproc) + run: cmake --build build-cmake --parallel $(nproc) env: SCCACHE_GHA_ENABLED: "true" - name: test - working-directory: build + working-directory: build-cmake run: ctest --output-on-failure build-windows: @@ -75,17 +74,16 @@ jobs: vcpkgGitCommitId: 37c3e63a1306562f7f59c4c3c8892ddd50fdf992 # HEAD on 2024-02-24 - name: configure - run: cmake -S . -B build -G "Ninja" -DCMAKE_C_COMPILER_LAUNCHER=sccache -DCMAKE_CXX_COMPILER_LAUNCHER=sccache -DCMAKE_BUILD_TYPE=Release -DWITH_JAVA=OFF -DWITH_EXAMPLES=ON -DUSE_SYSTEM_FMTLIB=ON -DUSE_SYSTEM_LIBUV=ON -DUSE_SYSTEM_EIGEN=OFF -DCMAKE_TOOLCHAIN_FILE=${{ runner.workspace }}/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_INSTALL_OPTIONS=--clean-after-build -DVCPKG_TARGET_TRIPLET=x64-windows-release -DVCPKG_HOST_TRIPLET=x64-windows-release + run: cmake --preset sccache -DCMAKE_BUILD_TYPE=Release -DWITH_JAVA=OFF -DWITH_EXAMPLES=ON -DUSE_SYSTEM_FMTLIB=ON -DUSE_SYSTEM_LIBUV=ON -DUSE_SYSTEM_EIGEN=OFF -DCMAKE_TOOLCHAIN_FILE=${{ runner.workspace }}/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_INSTALL_OPTIONS=--clean-after-build -DVCPKG_TARGET_TRIPLET=x64-windows-release -DVCPKG_HOST_TRIPLET=x64-windows-release env: SCCACHE_GHA_ENABLED: "true" - name: build - working-directory: build - run: cmake --build . --parallel $(nproc) + run: cmake --build build-cmake --parallel $(nproc) env: SCCACHE_GHA_ENABLED: "true" - name: test - working-directory: build + working-directory: build-cmake # UnitTest_test segfaults on exit occasionally run: ctest --output-on-failure -E 'UnitTest' diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000000..ab39d90fec --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,50 @@ +{ + "version": 3, + "cmakeMinimumRequired": { + "major": 3, + "minor": 21, + "patch": 0 + }, + "configurePresets": [ + { + "name": "default", + "displayName": "Default Config", + "description": "Default build using Ninja generator", + "binaryDir": "build-cmake", + "generator": "Ninja", + "cacheVariables": {} + }, + { + "name": "sccache", + "displayName": "", + "description": "Ninja config with sccache", + "generator": "Ninja", + "binaryDir": "build-cmake", + "cacheVariables": { + "CMAKE_C_COMPILER_LAUNCHER": "sccache", + "CMAKE_CXX_COMPILER_LAUNCHER": "sccache" + } + }, + { + "name": "with-java", + "displayName": "", + "description": "Ninja config with Java", + "generator": "Ninja", + "binaryDir": "build-cmake", + "cacheVariables": { + "WITH_JAVA": "ON" + } + }, + { + "name": "with-java-sccache", + "displayName": "", + "description": "Ninja config with Java and sccache", + "generator": "Ninja", + "inherits": "sccache", + "binaryDir": "build-cmake", + "cacheVariables": { + "WITH_JAVA": "ON" + } + } + ] +} diff --git a/README-CMAKE.md b/README-CMAKE.md index 1ea782dce8..dd3770b1ab 100644 --- a/README-CMAKE.md +++ b/README-CMAKE.md @@ -79,10 +79,10 @@ The following build options are available: The WPILib CMake build does not allow in source builds. Because the `build` directory is used by Gradle, we recommend a `build-cmake` directory in the root. This folder is included in the gitignore. We support building with Ninja; other options like Makefiles may be broken. -Once you have a build folder, run CMake configuration in that build directory with the following command. +Once you have a build folder, run CMake configuration in the root directory with the following command. ``` -cmake path/to/allwpilib/root -G Ninja +cmake --preset default ``` If you want to change any of the options, add `-DOPTIONHERE=VALUE` to the `cmake` command. This will check for any dependencies. If everything works properly this will succeed. If not, please check out the troubleshooting section for help. @@ -91,6 +91,10 @@ If you want, you can also use `ccmake` in order to visually set these properties Note that if you are cross-compiling, you will need to override the protobuf options manually to point to the libraries for the target platform. Leave the protoc binary location as the path to the binary for the host platform, since protoc needs to execute on the host platform. +## Presets + +The WPILib CMake setup has a variety of presets for common configurations and options used. The default sets the generator to Ninja and build directory to `build-cmake`. The other presets are `with-java` (sets `WITH_JAVA=ON`), `sccache` (sets the C/C++ compiler launcher to sccache), and `with-java-sccache` (a comibination of `with-java` and `sccache`. + ## Building Once you have CMake setup. run `cmake --build .` from the directory you configured CMake in. This will build all libraries possible. We recommend running `cmake --build .` with multiple jobs. For allwpilib, a good rule of thumb is one worker for every 2 GB of available RAM. To run a multiple job build, run the following command with x being the number of jobs you want.