Is there a way to extract rendered preset information from CMake?

Viewed 390

I have a CMakePresets.json file which makes use of inheritance and macro expansion. Here is an excerpt, in reality I use multiple versions of "Foo":

{
 "configurePresets": [
        {
            "name": "default",
            "hidden": true,
            "generator": "Unix Makefiles",
            "binaryDir": "cmake-build-${presetName}",
            "environment": {
                "PATH":  "/opt/foo/$env{FOO_VERSION}/bin:$penv{PATH}",
                "LD_LIBRARY_PATH": "/opt/foo/$env{FOO_VERSION}.0/lib"
            },
            "cacheVariables": {
                "FOO_VERSION": "$env{FOO_VERSION}",
            }
        },
        {
            "name": "debug-foo1",
            "inherits": "default",
            "environment": { "FOO_VERSION": "1" }
        },
        {
            "name": "release-foo1",
            "inherits": "debug-foo1",
            "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" }
        }
    ],
    "buildPresets": [
        {
            "name": "debug-foo1",
            "configurePreset": "debug-foo1"
        },
        {
            "name": "release-foo1",
            "configurePreset": "release-foo1"
        }
    ]
}

Now assume I select the preset release-foo1. This would render the following variables, among others:

  • binaryDir = "cmake-build-release-foo1"
  • FOO_VERSION = "1"
  • LD_LIBRARY_PATH = "/opt/foo/1.0/lib"

Is there a way to query these results for a given preset? For example, given release-foo1, I want to know the resulting binaryDir.

Of course I could parse the JSON myself, but that seems tedious, especially because of the cross references and substitutions which are being made by CMake.

1 Answers

You can use the -N flag to print the computed presets info without running the configure or generate steps. After modifying the presets file in your question to work, I created an empty CMakeLists.txt next to it and ran this test (*** stands in for my PATH):

$ cmake --preset=release-foo1 -N
Preset CMake variables:

  CMAKE_BUILD_TYPE="Release"
  FOO_VERSION="1"

Preset environment variables:

  FOO_VERSION="1"
  LD_LIBRARY_PATH="/opt/foo/1.0/lib"
  PATH="/opt/foo/1/bin:***"

This is good enough for debugging your presets. If you need to go further, you could process the output using standard unix command line tools, like awk, grep, cut, etc.

I think this is the best you can do for now (CMake <=3.21) since there's nothing else in the command line documentation or the file API for IDEs that I could find.

Related