简单的CMakePresets.json解析 -- configurePresets

发布时间 2023-04-05 11:39:04作者: 金凯旋

CMakeLists.txt 是通用的c++项目管理文件,

在不同的设备中,环境变量,编译器等都可能不同,

将这些设置都交给CMakeLists.txt , 并不是一个好办法。

为了降低CMakeLists.txt的臃肿程度,简化其判断,

可以针对不同设备,配置不同的CMakePresets.json.

使得项目可以在不同的设备上运行、测试。

 

一个由visual studio 2022 自动生成的CMakePresets.json,稍稍修改了一下。

{
    "version": 3,
    "configurePresets": [
      {
        "name": "windows-base",
        "hidden": true,
        "generator": "Ninja",
        "binaryDir": "${sourceDir}/out/build/${presetName}",
        "installDir": "${sourceDir}/out/install/${presetName}",
        "cacheVariables": {
          "CMAKE_C_COMPILER": "cl.exe",
          "CMAKE_CXX_COMPILER": "cl.exe"
        },
        "environment": {
          "MY_ENVIRONMENT_VARIABLE": "Test"          
        },
        "condition": {
          "type": "equals",
          "lhs": "${hostSystemName}",
          "rhs": "Windows"
        }
      },
        {
            "name": "x64-debug",
            "displayName": "x64 Debug",
            "inherits": "windows-base",
            "architecture": {
                "value": "x64",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            }
        },
        {
            "name": "x64-release",
            "displayName": "x64 Release",
            "inherits": "x64-debug",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release"
            }
        },
        {
            "name": "x86-debug",
            "displayName": "x86 Debug",
            "inherits": "windows-base",
            "architecture": {
                "value": "x86",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            }
        },
        {
            "name": "x86-release",
            "displayName": "x86 Release",
            "inherits": "x86-debug",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release"
            }
        },
        {
            "name": "linux-debug",
            "displayName": "Linux Debug",
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            "installDir": "${sourceDir}/out/install/${presetName}",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            },
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Linux"
            },
            "vendor": {
                "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
                    "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
                }
            }
        },
        {
            "name": "macos-debug",
            "displayName": "macOS Debug",
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            "installDir": "${sourceDir}/out/install/${presetName}",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            },
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Darwin"
            },
            "vendor": {
                "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
                    "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
                }
            }
        }
    ]
}

configurePresets 是一个 json 字段 []。

里面包含多个 设置, 这些设置有属性:name, hidden, generator,binaryDir, installDir .... 等组成。

后面的设置可以继承前面的设置。Visual Studio IDE根据选择的编译模式,自动从这里找到对应的配置名。

其中字段:cacheVariables 中的字段可以在 cmakelists中使用。

也可以像下面方法定义:

 

      "cacheVariables": {
        "FIRST_CACHE_VARIABLE": {
          "type": "BOOL",
          "value": "OFF"
        },
        "SECOND_CACHE_VARIABLE": "ON"
      },

environment 可以定义环境变量,上文例子中的引用方法:

$ENV{MY_ENVIRONMENT_VARIABLE}

除了这个配置,还可以配置:buildPresets, testPresets

  "buildPresets": [
    {
      "name": "default",
      "configurePreset": "default"
    }
  ],
  "testPresets": [
    {
      "name": "default",
      "configurePreset": "default",
      "output": {"outputOnFailure": true},
      "execution": {"noTestsAction": "error", "stopOnFailure": true}
    }
  ],

也可以包含其他json配置。

  "include": [
    "otherThings.json",
    "moreThings.json"
  ],