llvm-project
87cbea6c - [openmp][cmake][NFCI] Avoid non-eval uses of ${var} (#182267)

Commit
1 day ago
[openmp][cmake][NFCI] Avoid non-eval uses of ${var} (#182267) When using set(var "Example") if (${var}) CMake will first resolve the if-argument to if (Example) And what then happens depends on the existance of a variable with the name "Example": 1. If instead of "Example", a truth constant is used ("TRUE", "FALSE", "ON", "OFF", "", "-NOTFOUND" ...), it is prioritized (https://cmake.org/cmake/help/latest/command/if.html#constant) 2. If a variable with the name "Example" exists: Use the truthiness of its content (https://cmake.org/cmake/help/latest/command/if.html#variable) 3. Otherwise, it is false-y That is, the the result of the conditional does not only depend on the content of `var`, but also some other variable. This is usually unintended and leads to problems such as addressed with #154537. The only case where this is intended is when passing an expression to be evaluated such as with `pythonize_bool`, `append_if` and `libomp_append`. In all other cases, using `${var}` without quotes is a pattern to be avoided. Remark: If `${var}` is not one of the values considered a [truthiness constant](https://cmake.org/cmake/help/latest/command/if.html#constant), the result of `if (var)` and `if ("${var}")` is different: * `if (var)` is true-ish (https://cmake.org/cmake/help/latest/command/if.html#variable) * `if ("Example")` and therefore `if ("${var}")` are false-y (https://cmake.org/cmake/help/latest/command/if.html#string) In this PR I am preferring `if (var)` over `if ("${var}")` because has less clutter, resembles Python's behaviour, and problably what most users are expecting, even though `if (${var})` in most cases would evaluate to false-y because a variable does not exist. This ambiguity does not exist for STREQUAL and MATCHES.
Author
Parents
Loading