Skip to content

Add CMakePresets.json

Mateo de Mayo requested to merge mateosss/monado:mateosss/cmake-presets into main

What do you think about adding a CMakePresets file?

As there are many Monado cmake options, having presets allows us to share with users a handful of common flag combinations (i.e., presets) they might want to use.

In this MR I added an example with a couple of presets and a user would use them as such:

# "default" configure preset: sets build to Debug, disables BUILD_DOC, and enables ASan
cmake . --preset=default

# "default" build preset: cleans cache, builds into "build" directory and installs
cmake --build --preset=default

You can also add a custom CMakeUserPresets.json (which is gitignored) to have all of the cmake options at hand in a file and not having to remember long cmake commands. For example I could be using something like the following:

CMakeUserPresets.json
{
  "version": 2,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 20,
    "patch": 0
  },
  "configurePresets": [
    {
      "name": "mateosss",
      "inherits": "service-debug",
      "cacheVariables": {
        "CMAKE_INSTALL_PREFIX": "$env{oxrinstall}",
        "XRT_OPENXR_INSTALL_ACTIVE_RUNTIME": "on",
        "SANITIZE_ADDRESS": "off",
        "SANITIZE_UNDEFINED": "off",
        "EIGEN3_INCLUDE_DIR": "$env{bsltdeps}/basalt/thirdparty/basalt-headers/thirdparty/eigen",
        "CMAKE_C_FLAGS": "-march=native",
        "CMAKE_CXX_FLAGS": "-march=native"
      }
    }
  ],
  "buildPresets": [
    {
      "name": "mateosss",
      "configurePreset": "mateosss"
    }
  ]
}

And then just run

cmake . --preset=mateosss
cmake --build --preset=mateosss

Some IDEs like visual studio code can automatically pick presets and build with the correct cmake flags.

Lastly, there is also a case for having presets for CI.

Edited by Mateo de Mayo

Merge request reports