Commit Graph
128 Commits
Author SHA1 Message Date
Mickaël Germain 860583ceac if: Add PATH_IS_PREFIX operator
Testing whether one path is a prefix of another is possible today with
cmake_path(IS_PREFIX), but the idiom needs a separate statement plus a
scratch variable and takes the prefix as a variable name, so projects
reach for if(path MATCHES "^${prefix}") instead.  That is wrong whenever
the prefix contains a regex metacharacter, and it accepts siblings,
because '^/a/b' matches '/a/bc'.

Add a binary operator where the left operand is the candidate prefix and
the right is the path, matching the operand order of cmake_path(IS_PREFIX)
and $<PATH:IS_PREFIX> so that the same operation reads the same way on all
three surfaces.  Neither operand is normalized, matching the default of
cmake_path(IS_PREFIX) and the existing PATH_EQUAL operator, so '.' and
'..' are compared as ordinary components.  The test is non-strict, purely
lexical, and applies no relative-to-absolute reconciliation.

Add policy CMP0222 for compatibility, modeled on CMP0139.  The keyword
is consumed as an operator only when it appears in the second argument
slot, so a variable named PATH_IS_PREFIX keeps working in unary and
left-operand position.  What the policy covers is such a variable
appearing after another token, as in if(NOT PATH_IS_PREFIX AND other),
which becomes a configure error under NEW.

Since the operator is an if() spelling of cmake_path(IS_PREFIX) and shares
its implementation, test it by asserting the two agree over a corpus of
inputs rather than by restating expected values.  Those are pinned by the
cmake_path(IS_PREFIX) test, so the operator's tests stay limited to what
has no command equivalent, and no platform branching is needed because
parity holds whatever the host path model answers.

Fixes: #28040
2026-09-10 07:09:47 -04:00
Matthew Woehlke 597c573d7d if: Add DIAGNOSTIC <category> condition
Add an existence check to the `if` command for diagnostic categories,
mirroring the check for policies. Aside from being desirable for
consistency's sake, this will make it easier for users to write logic to
manipulate diagnostics from newer versions of CMake that don't exist
today.
2026-06-16 15:14:13 -04:00
Ruslan Baratov 240481490c if: Diagnose invalid argument in parentheses
Fixes: #26424
2026-05-27 12:24:32 -04:00
Matthew Woehlke a7415d243a Diagnostics: Simplify issuing policy warnings
Add and use a couple helper functions for issuing policy warnings. This
improves consistency and allows some simplification of many call sites.
(One or two instances in particular are greatly simplified.)
2026-05-20 11:55:13 -04:00
Matthew Woehlke 028d3183ad Diagnostics: Add POLICY, EXPERIMENTAL
Add new diagnostic categories CMD_POLICY and CMD_EXPERIMENTAL, both as
children of CMD_AUTHOR. Use the former when issuing policy warnings. Use
the latter when issuing experimental feature warnings.
2026-05-15 09:57:23 -04:00
Matthew Woehlke 1309532390 Diagnostics: Use new methods
Find places that are currently relying on diagnostic-specific message
types to issue diagnostics and replace these with calls to the new
diagnostic methods.
2026-04-16 15:10:35 -04:00
Kitware Robot 1772622772 LICENSE: Replace references to Copyright.txt with LICENSE.rst
```
git grep -lz 'Copyright.txt or https://cmake.org/licensing ' |
  while IFS= read -r -d $'\0' f ; do
    sed -i '/Copyright.txt or https:\/\/cmake.org\/licensing / {
              s/Copyright.txt/LICENSE.rst/
            }' "$f" ; done
```
2025-03-03 10:43:35 -05:00
Kitware Robot 0b96ae1f6a Revise C++ coding style using clang-format with "east const"
Run the `clang-format.bash` script to update all our C and C++ code to a
new style defined by `.clang-format`, now with "east const" enforcement.
Use `clang-format` version 18.

* If you reached this commit for a line in `git blame`, re-run the blame
  operation starting at the parent of this commit to see older history
  for the content.

* See the parent commit for instructions to rebase a change across this
  style transition commit.

Issue: #26123
2025-01-23 13:09:50 -05:00
Brad King d9dd38cccf CMP0064: Remove support for OLD behavior 2025-01-22 10:40:55 -05:00
Brad King c283aafe62 CMP0057: Remove support for OLD behavior 2025-01-22 10:40:53 -05:00
Brad King 90d814f024 CMP0054: Remove support for OLD behavior 2025-01-20 09:46:55 -05:00
Brad King 98a59ba8ad CMP0012: Remove support for OLD behavior 2025-01-17 09:28:36 -05:00
Brad King 659d8bd430 cmPolicies: Remove now-unused REQUIRED_IF_USED and REQUIRED_ALWAYS states 2024-11-13 11:43:07 -05:00
Vitaly Stakhovsky 1a49b439a5 Source: Use cmValue::IsOn and IsOff
Speed up a bit by calling members directly.
2024-03-17 19:05:37 -04:00
Marc Chevrier 337bc5662c if(): add operators IS_READABLE, IS_WRITABLE and IS_EXECUTABLE.
Offers possibility to check for file or directory permissions.
2023-11-01 14:53:31 +01:00
Brad King 93ee2b369c Source: Fix -Wdangling-reference warnings exposed by gcc 13 2023-05-22 17:14:54 -04:00
Marc Chevrier e08ba229ee CMake code rely on cmList class for CMake lists management (part. 1) 2023-04-24 10:41:10 +02:00
Marc Chevrier be4b9e10af if command: Add PATH_EQUAL operator 2022-06-03 19:31:36 +02:00
Brad King 5ceed2bb28 Merge topic 'reduce-policy-eval-scope'
cb19a63499 cmConditionEvaluator: Reduce the scope of the CMP0064 evaluation

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !7287
2022-05-24 10:08:52 -04:00
Ben Boeckel 6ff03d463f clang-tidy: address google-readability-casting lints
At least those involving `static_cast`.
2022-05-24 09:09:43 -04:00
Alex Turbov cb19a63499 cmConditionEvaluator: Reduce the scope of the CMP0064 evaluation
When evaluatig conditions, avoid testing CMP0064 unless it's really needed
(Cuz most of the time it doesn't)
2022-05-23 09:08:51 -04:00
Sean McBride 37859e3244 Source: Fix clang -Wimplicit-fallthrough warnings 2021-09-28 10:53:54 -04:00
Sean McBride 907d098838 Source: Fix clang -Wextra-semi warnings 2021-09-28 09:59:24 -04:00
Marc Chevrier cc56dc7468 Rename cmProp in cmValue 2021-09-21 17:14:04 +02:00
Marc Chevrier 7e154ebd59 cmSystemTools::VersionCompare: use std::string for arguments 2021-09-17 11:58:46 +02:00
Alex Turbov 61b33c3f4e Fix: Regression in the cmConditionEvaluator::HandleLevel0
As reported in the BUG #22524, mismatched parenthesis reported differently
for `while()` and `if()`.

The problem was in the double loop (over "handlers" and the arguments),
where the outer loop didn't check the result of the running handler.
2021-08-09 21:05:09 +03:00
Marc Chevrier e5cd39ca80 cmProp: refactoring: transform alias in class
To handle safely the values used by CMake variables and properties,
introduce the class cmProp as a replacement from the simple pointer
to std::string instance.
2021-08-08 16:19:08 +02:00
Alex Turbov 54a70b3988 Refactor: Move version compare op table out of the function
It's static data and never changed.
2021-08-03 10:55:47 -04:00
Alex Turbov f37d6a3170 Refactor: Remove redundant checks for 0 and 1 literals
They are gonna be checked by `cmIsOn()` anyway.
2021-08-03 10:55:47 -04:00
Alex Turbov 00961a4782 Refactor: Copy exactly required count of args and avoid pop_back() 2021-08-03 10:55:47 -04:00
Alex Turbov 2a72cad9be Style: Better names of local vars for IN_LIST handler 2021-08-03 10:55:47 -04:00
Alex Turbov 0dd7795706 Refactor: Extract the logic of testing for special variable to func
Introduce `looksLikeSpecialVariable`. It makes `if()` blocks readable.
2021-08-03 10:55:47 -04:00
Alex Turbov 866b0595f6 Refactor: Introduce cmArgumentList container class
The `cmArgumentList` has been turned into a class (forward declared in
the header). It inherits from the `std::list` (yeah, but we don't intend
to store polymorphic classes in it). In addition to the standard methods,
now it's possible to move `HandlePredicate` (renamed to `ReduceOneArg`)
and `HandleBinaryOp` (renamed to `ReduceTwoArgs`) as its members.

Additionally, iterators managements (`IncrementArguments`) have been
refactored into two separate classes mimicking iterators. This also
allows having a uniform `for` loop and concentrates the logic of
iterators advancing in it instead of the loop's body. The arguments
processing algorithms operate with "windows" over a collection of
arguments. Hence there are two kinds of "iteration windows" -- allowing
to observe 2 or 3 elements per loop iteration. These iteration "windows"
also passed to reducers.
2021-08-03 10:55:47 -04:00
Alex Turbov 51d9194a96 Refactor: Reduce one more condition checking on handling math compare 2021-08-03 10:55:46 -04:00
Alex Turbov 46810235e3 Refactor: Avoid if → else if → … for compare operators
When trying to match one of compare operator key inside `if()` condition,
remember the index of matched operator. Later this index used to
select the operation to perform instead of strings compare again.
2021-08-03 10:55:46 -04:00
Alex Turbov 17af3baddd Refactor: Set const to cmConditionEvaluator::IsKeyword parameters 2021-08-03 10:55:46 -04:00
Alex Turbov 2b916606c5 Refactor: No need to move iterators after match
A handler's loop of all levels gonna restart after calls to
`HandlePredcate` or `HandleBinaryOp`... And the first action in the
loop is to setup iterators. So, no need to move 'em inside the
functions. Also, no need to pass iterators by reference.

Also, reorder parameters of both functions so iterators
followed after a reference to the newArgs container.
2021-08-03 10:55:46 -04:00
Alex Turbov 97d6bbcc01 Refactor: Replace std::to_string w/ more specialized bool2string 2021-08-03 10:55:46 -04:00
Alex Turbov 9721ab416f Refactor: Reorder MATCHES handler from top below to the fail-fast 2021-08-03 10:55:46 -04:00
Alex Turbov d4d44e86f6 Refactor: Reduce variables scope in HandleLevel2 2021-08-03 10:55:46 -04:00
Alex Turbov cc6cdacc18 Refactor: Simplify boolean to string result assignments 2021-08-03 10:55:46 -04:00
Alex Turbov e3c1dbe18b Refactor: Replace if block w/ boolean expression
Signed-off-by: Alex Turbov <i.zaufi@gmail.com>
2021-08-03 10:55:45 -04:00
Alex Turbov 047f8321a0 Refactor: Avoid redundant operator<< on printing messages
Signed-off-by: Alex Turbov <i.zaufi@gmail.com>
2021-08-03 10:55:45 -04:00
Alex Turbov 4de2a4a46d Refactor: Opt-out do+while loops and reduce nesting level in handlers
Signed-off-by: Alex Turbov <i.zaufi@gmail.com>
2021-08-03 10:55:45 -04:00
Alex Turbov 78fcbb20cd Refactor: Remove reducible flag from handleLevelN() functions
The indicator that smth has been done is the `newArgs` size get
differ after an iteration.

Signed-off-by: Alex Turbov <i.zaufi@gmail.com>
2021-08-03 10:55:45 -04:00
Alex Turbov b0d6596399 Refactor: Make cmConditionEvaluator::IsTrue a bit more compact
Signed-off-by: Alex Turbov <i.zaufi@gmail.com>
2021-08-03 10:55:30 -04:00
Alex Turbov 18bd6c98ea Refactor: Generalize policy checking in HandleLevel1
Also, move OLD policy checking out of the loop to evaluate it once.
2021-07-26 23:40:18 +03:00
Alex Turbov 8bc4a740d6 Refactor: When handle predicates, there is no need to check 2nd arg
Introduce an overload for `IncrementArguments()` w/ one iterator
and use it in the handler level 1.
2021-07-26 23:40:18 +03:00
Alex Turbov 9946ff6848 Refactor: Initialize args vector from iterators instead of copy
Signed-off-by: Alex Turbov <i.zaufi@gmail.com>
2021-07-26 23:40:18 +03:00
Alex Turbov bf2fe90372 Refactor: Speedup predicates and binary operation
Before predicates and binary ops reducers use series of `if ()` blocks
to match the keywords. However, if matched the currect `arg` get replaced
with evaluation result, so further `if (<match-another-keyword>)`
is just wasting time/resources.

This patch introduce a chain of `if` → `else if` → ..., so after
first match the loop restarts w/ the next argument.
2021-07-26 23:40:18 +03:00