Presets - combine env/cache variables from multiple presets

Is it possible to somehow combine the same env/cache variable from two presets into a single value? Ideally, the goal is to define CMAKE_PREFIX_PATH cache variable based on values from multiple presets. Or at least define $env{CMAKE_PREFIX_PATH} that way.

  1. From reading documentation there seems to be no way to reference one variable from another inside preset.
  2. For environment variables $env{CMAKE_PREFIX_PATH} works, but since it doesn’t allow circular references you can only save this value to a different variable, not CMAKE_PREFIX_PATH.
    Using $penv{CMAKE_PREFIX_PATH} is accessing process environment variables, so it’s not very helpful for this case.
$env:CMAKE_PREFIX_PATH="42"
cmake --preset default
# Preset CMake variables:
# 
#   DEPENDENCY_PATH="L:/path/to/second/dependency"
# 
# Preset environment variables:
# 
#   CMAKE_PREFIX_PATH="L:/path/to/first/dependency"
#   CMAKE_PREFIX_PATH_env1="L:/path/to/second/dependency;L:/path/to/first/dependency"
#   CMAKE_PREFIX_PATH_env2="L:/path/to/second/dependency;42"

CMakeUserPresets.json

{
  "version": 10,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 23,
    "patch": 0
  },
  "$comment": "An example CMakePresets.json file",
  "include": [],
  "configurePresets": [
    {
      "name": "default",
      "inherits": ["dependency"],
      "binaryDir": "${sourceDir}/build_",
      "cacheVariables": {
        "DEPENDENCY_PATH": "L:/path/to/second/dependency"
      },
      "environment": {
        "CMAKE_PREFIX_PATH_env1": "L:/path/to/second/dependency;$env{CMAKE_PREFIX_PATH}",
        "CMAKE_PREFIX_PATH_env2": "L:/path/to/second/dependency;$penv{CMAKE_PREFIX_PATH}"
      }
    },
    {
      "name": "dependency",
      "cacheVariables": {
          "DEPENDENCY_PATH": "L:/path/to/first/dependency"
        },
      "environment": {
        "CMAKE_PREFIX_PATH": "L:/path/to/first/dependency"
      }
    }
  ]
}

Not sure how much decoupling of those sections you require, but it’s possible to combine env-vars so long as they have different names. Then set the cache variable from the final result.

{
  "version": 10,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 23,
    "patch": 0
  },
  "$comment": "An example CMakePresets.json file",
  "include": [],
  "configurePresets": [
    {
      "name": "default",
      "inherits": ["dependency"],
      "binaryDir": "${sourceDir}/build_",
      "cacheVariables": {
        "CMAKE_PREFIX_PATH": "$env{PRIVATE_PREFIX_PATH}"
      },
      "environment": {
        "PRIVATE_PREFIX_PATH": "L:/path/to/second/dependency;$env{PRIVATE_PREFIX_PATH_OTHER}"
      }
    },
    {
      "name": "dependency",
      "environment": {
        "PRIVATE_PREFIX_PATH_OTHER": "L:/path/to/first/dependency"
      }
    }
  ]
}