[cmake-guidelines] Minor update, for if()#18980
[cmake-guidelines] Minor update, for if()#18980strega-nil wants to merge 5 commits intomicrosoft:masterfrom
if()#18980Conversation
fe25710 to
0cf1ec8
Compare
docs/maintainers/cmake-guidelines.md
Outdated
| - Exception: arguments to `if()` that are not operators must always be quoted. | ||
| - operators should be unquoted. | ||
| - Example: | ||
| ```cmake | ||
| if("${FOO}" STREQUAL "BAR") | ||
| endif() | ||
|
|
||
| if("${BAZ}" EQUAL "0") | ||
| endif() | ||
| ``` |
There was a problem hiding this comment.
is there a reason to not use if(FOO STREQUAL "BAR") ? If FOO is not set this will raise and error.
There was a problem hiding this comment.
no it won't, in either case.
Assuming FOO is not defined:
if(FOO STREQUAL "BAR") # equivalent to `if("FOO" STREQUAL "BAR")`
endif()
if("${FOO}" STREQUAL "BAR") # equivalent to `if("" STREQUAL "BAR")`
endif()There was a problem hiding this comment.
if(FOO STREQUAL "FOO")
message(STATUS "yes 1")
endif()
if("${FOO}" STREQUAL "")
message(STATUS "yes 2")
endif()prints out
-- yes 1
-- yes 2
There was a problem hiding this comment.
But FOO STREQUAL "BAR" does have exactly the desired semantics in this case. I don't think this rule is needed, since we already have rules saying all referenced variables should be previously defined:
- Variables are not assumed to be empty.
If the variable is intended to be used locally,
it must be explicitly initialized to empty withset(foo "")if it is a string variable,
andvcpkg_list(SET foo)if it is a list variable.
There was a problem hiding this comment.
Ok after testing
if(${FOO} STREQUAL "BAR")
is throwing the error I thought about.
I wonder if
if(FOO AND FOO STREQUAL "BAR")
is measurable more performant than
if("${FOO}" STREQUAL "BAR")
docs/maintainers/cmake-guidelines.md
Outdated
| - Exception: arguments to `if()` that are not operators must always be quoted. | ||
| - operators should be unquoted. | ||
| - Example: | ||
| ```cmake | ||
| if("${FOO}" STREQUAL "BAR") | ||
| endif() | ||
|
|
||
| if("${BAZ}" EQUAL "0") | ||
| endif() | ||
| ``` |
There was a problem hiding this comment.
But FOO STREQUAL "BAR" does have exactly the desired semantics in this case. I don't think this rule is needed, since we already have rules saying all referenced variables should be previously defined:
- Variables are not assumed to be empty.
If the variable is intended to be used locally,
it must be explicitly initialized to empty withset(foo "")if it is a string variable,
andvcpkg_list(SET foo)if it is a list variable.
22567d3 to
a928b6c
Compare
|
The reason I'm interested in this change is not because |
a928b6c to
127e982
Compare
|
Closed for rollup |
[cmake-guidelines] Minor update, for `if()`
* [rollup:2021-07-26 1/6] PR #18783 (@strega-nil) [scripts-audit] vcpkg_copy_tools and friends * [rollup:2021-07-26 2/6] PR #18898 (@dg0yt) [vcpkg] Fix toolchain compatibility with cmake < 3.15 * [rollup:2021-07-26 3/6] PR #18980 (@strega-nil) [cmake-guidelines] Minor update, for `if()` * [rollup:2021-07-26 4/6] PR #18981 (@strega-nil) [scripts-audit] vcpkg_check_linkage * [rollup:2021-07-26 5/6] PR #19158 (@Hoikas) [vcpkg.cmake] Fix variable case. * [rollup:2021-07-26 6/6] PR #18839 [scripts-audit] z_vcpkg_get_cmake_vars Co-authored-by: nicole mazzuca <mazzucan@outlook.com>
Basically, introduces rules for
if()as well as argument quoting.Depends on #18397