Breealzibub
Breealzibub

Reputation: 8095

Specify shared/'common' values for configurations in CppProperties.json or CMakeSettings.json

When using the "Open Folder" functionality of Visual Studio, the IDE searches for project settings and configurations in a special json file. For CPP projects, this could be CppProperties.json. For CMake projects, this could be CMakeSettings.json.

This json file contains a collection of one or more "configurations," such as "Debug" or "Release". I will use a recent CMake project as an example:

"configurations": [
    {
      "name": "ARM-Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [
        "gcc-arm"
      ],
      "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
      "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "intelliSenseMode": "linux-gcc-arm",
      "variables": [
        {
          "name": "CMAKE_TOOLCHAIN_FILE",
          "value": "${workspaceRoot}/cmake/arm-none-eabi-toolchain.cmake"
        }
      ]
    },
    {
      "name": "ARM-Release",
      "generator": "Ninja",
      "configurationType": "Release",
      "inheritEnvironments": [
        "gcc-arm"
      ],
      "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
      "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "intelliSenseMode": "linux-gcc-arm",
      "variables": [
        {
          "name": "CMAKE_TOOLCHAIN_FILE",
          "value": "${workspaceRoot}/cmake/arm-none-eabi-toolchain.cmake"
        }
      ]
    }

As you can see, I have two configurations with nearly identical properties.

My question: is it possible to define these common/shared properties once, in such a way as to allow the configurations to inherit them and avoid repeating myself?

Upvotes: 3

Views: 528

Answers (1)

Luca Cappa
Luca Cappa

Reputation: 1999

The easier way is to define an environment at global level (outside of any configuration), such as:

{
  "environments": [
    {
      "namespace" : "env",
      "varName": "varValue"
    }
  ],

Then you can reuse that wherever you need to, e.g.:

  "cmakeCommandArgs": "${env.varName}",

You can also have multiple environments, and reuse them, like this:

{
  "environments": [
    {
      "environment": "env1",
      "namespace": "env",
      "varName": "varValueEnv1"
    },
    {
      "environment": "env2",
      "namespace": "env",
      "varName": "varValueEnv2"
    }

  ],
  "configurations": [
    {
      "name": "x64-Release",
      "inheritEnvironments": [
        "msvc_x64_x64", "env2"
      ],
      "cmakeCommandArgs": "${env.varName}",
.....
    }
  ]

the 'x64-Release' will inherit the variables's value in the environment called "env2" (namespace 'env')

Upvotes: 4

Related Questions