以下 CMakePresets.json 演示了如何强制使用 configurePresets 条件设置环境变量:
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20
},
"configurePresets": [
{
"name": "fail_if_no_foo",
"displayName": "Fail if no FOO",
"description": "Demo failing if environment variable FOO is unset",
"binaryDir": "${sourceDir}/build",
"generator": "Unix Makefiles",
"condition": {
"type": "notEquals",
"lhs": "$penv{FOO}",
"rhs": ""
}
}
]
}
$ cmake --preset fail_if_no_foo
CMake Error: Could not use disabled preset "fail_if_no_foo"
$ FOO=bar cmake --preset fail_if_no_foo
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/dev/cmake_learn/preset_condition/build
$
下面的修改表明条件可以被继承:
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20
},
"configurePresets": [
{
"name": "fail_if_no_foo",
"displayName": "Fail if no FOO",
"description": "Demo failing if environment variable FOO is unset",
"binaryDir": "${sourceDir}/build",
"generator": "Unix Makefiles",
"condition": {
"type": "notEquals",
"lhs": "$penv{FOO}",
"rhs": ""
}
},
{
"name": "inherited_preset",
"inherits": "fail_if_no_foo",
"displayName": "Inherited fail if no FOO",
"description": "Demo inherited condition to fail if environment variable FOO is unset"
}
]
}
$ cmake --preset inherited_preset
CMake Error: Could not use disabled preset "inherited_preset"
$ FOO=bar cmake --preset inherited_preset
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/dev/cmake_learn/preset_condition/build
$
但是,如果继承的 conditionPresets 引入了自己的条件,则父条件的继承似乎就会消失:
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20
},
"configurePresets": [
{
"name": "fail_if_no_foo",
"displayName": "Fail if no FOO",
"description": "Demo failing if environment variable FOO is unset",
"binaryDir": "${sourceDir}/build",
"generator": "Unix Makefiles",
"condition": {
"type": "notEquals",
"lhs": "$penv{FOO}",
"rhs": ""
}
},
{
"name": "inherited_preset",
"inherits": "fail_if_no_foo",
"displayName": "Inherited fail if no FOO",
"description": "Demo inherited condition to fail if environment variable FOO is unset",
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
}
}
]
}
$ uname -s
Linux
$ cmake --preset inherited_preset
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/dev/cmake_learn/preset_condition/build
$
有没有办法在当前configurePreset中实现的条件与继承的configurePreset中的条件之间实现“与”关系?
我想强制执行“Linux”并且父环境定义的inherited_preset
条件。
CMake Presets 文档注释了一个关键字,但没有给出如何使用它的示例,而且我还没有弄清楚如何使用它来实现我的需求。${hostSystemName}
FOO
allOf
以下是我从文档中解释如何使用的两次尝试allOf
:
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20
},
"configurePresets": [
{
"name": "fail_if_no_foo",
"displayName": "Fail if no FOO",
"description": "Demo failing if environment variable FOO is unset",
"binaryDir": "${sourceDir}/build",
"generator": "Unix Makefiles",
"condition": {
"allOf": {
"conditions": [
{
"type": "notEquals",
"lhs": "$penv{FOO}",
"rhs": ""
}
]
}
}
},
{
"name": "inherited_preset",
"inherits": "fail_if_no_foo",
"displayName": "Inherited fail if no FOO",
"description": "Demo inherited condition to fail if environment variable FOO is unset",
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
}
}
]
}
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20
},
"configurePresets": [
{
"name": "fail_if_no_foo",
"displayName": "Fail if no FOO",
"description": "Demo failing if environment variable FOO is unset",
"binaryDir": "${sourceDir}/build",
"generator": "Unix Makefiles",
"condition": {
"allOf": {
"conditions": [
{
"type": "notEquals",
"lhs": "$penv{FOO}",
"rhs": ""
}
]
}
}
},
{
"name": "inherited_preset",
"inherits": "fail_if_no_foo",
"displayName": "Inherited fail if no FOO",
"description": "Demo inherited condition to fail if environment variable FOO is unset",
"condition": {
"allOf": {
"conditions": [
{
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
}
]
}
}
}
]
}
...但是两次尝试都因相同的错误而解析失败:
$ cmake --preset fail_if_no_foo
CMake Error: Could not read presets from /home/user/dev/cmake_learn/preset_condition: Invalid preset condition
更新:我认为我在之前尝试使用allOf
. 以下尝试进行解析,但仍然无法实现强制执行子级和父级条件的所需行为:
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20
},
"configurePresets": [
{
"name": "fail_if_no_foo",
"displayName": "Fail if no FOO",
"description": "Demo failing if environment variable FOO is unset",
"binaryDir": "${sourceDir}/build",
"generator": "Unix Makefiles",
"condition": {
"type": "allOf",
"conditions": [
{
"type": "notEquals",
"lhs": "$penv{FOO}",
"rhs": ""
}
]
}
},
{
"name": "inherited_preset",
"inherits": "fail_if_no_foo",
"displayName": "Inherited fail if no FOO",
"description": "Demo inherited condition to fail if environment variable FOO is unset",
"condition": {
"type": "allOf",
"conditions": [
{
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
}
]
}
}
]
}
$ cmake --preset fail_if_no_foo
CMake Error: Could not use disabled preset "fail_if_no_foo"
$ FOO=bar cmake --preset fail_if_no_foo
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/dev/cmake_learn/preset_condition/build
$ cmake --preset inherited_preset
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/dev/cmake_learn/preset_condition/build
$
在我看来,您想要的是开放功能请求预设中所要求的:条件预设的继承,它要求不是仅在所有继承完成后才评估条件的效果,而是评估每个继承的效果在继承发生之前在继承树中预设。我建议您对该问题票竖起大拇指以表示支持。您还可以订阅它以获取有关讨论和进展的通知。请避免在那里发表嘈杂的评论,比如只包含“+1”/“bump”的评论。