Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmake-guidelines] Minor update, for if() #18980

Closed
wants to merge 5 commits into from

Conversation

strega-nil
Copy link
Contributor

@strega-nil strega-nil commented Jul 16, 2021

Basically, introduces rules for if() as well as argument quoting.

Depends on #18397

Comment on lines 85 to 94
- 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()
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason to not use if(FOO STREQUAL "BAR") ? If FOO is not set this will raise and error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(FOO STREQUAL "FOO")
	message(STATUS "yes 1")
endif()
if("${FOO}" STREQUAL "")
	message(STATUS "yes 2")
endif()

prints out

-- yes 1
-- yes 2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with set(foo "") if it is a string variable,
    and vcpkg_list(SET foo) if it is a list variable.

Copy link
Contributor

@Neumann-A Neumann-A Jul 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Show resolved Hide resolved
docs/maintainers/cmake-guidelines.md Outdated Show resolved Hide resolved
docs/maintainers/cmake-guidelines.md Outdated Show resolved Hide resolved
Comment on lines 85 to 94
- 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()
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with set(foo "") if it is a string variable,
    and vcpkg_list(SET foo) if it is a list variable.

@strega-nil
Copy link
Contributor Author

The reason I'm interested in this change is not because if(FOO STREQUAL "bar") can't be used without error, it's because it's extremely confusing as to when it can be used -- for example, I've had bugs in the past where I've written if(EXISTS arg_FILENAME) instead of the required if(EXISTS "${arg_FILENAME}"), or if("${foo}" MATCHES regex_var).

@strega-nil-ms strega-nil-ms added category:scripts-audit Part of the scripts audit effort. info:internal This PR or Issue was filed by the vcpkg team. labels Jul 17, 2021
@strega-nil-ms strega-nil-ms added the requires:vcpkg-team-review This PR or issue requires someone on the vcpkg team to take a further look. label Jul 21, 2021
@strega-nil-ms strega-nil-ms added requires:discussion and removed requires:vcpkg-team-review This PR or issue requires someone on the vcpkg team to take a further look. labels Jul 22, 2021
@strega-nil-ms
Copy link
Contributor

Closed for rollup

strega-nil-ms pushed a commit to strega-nil/vcpkg that referenced this pull request Jul 28, 2021
[cmake-guidelines] Minor update, for `if()`
strega-nil-ms added a commit that referenced this pull request Jul 29, 2021
* [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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:scripts-audit Part of the scripts audit effort. info:internal This PR or Issue was filed by the vcpkg team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants