Files
cmake/Source/cmConditionEvaluator.h
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

72 lines
2.2 KiB
C++

/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#pragma once
#include "cmConfigure.h" // IWYU pragma: keep
#include <string>
#include <vector>
#include <cmext/string_view>
#include "cmListFileCache.h"
#include "cmMessageType.h" // IWYU pragma: keep
#include "cmPolicies.h"
#include "cmValue.h"
class cmExpandedCommandArgument;
class cmMakefile;
class cmConditionEvaluator
{
public:
cmConditionEvaluator(cmMakefile& makefile, cmListFileBacktrace bt);
// this is a shared function for both If and Else to determine if the
// arguments were valid, and if so, was the response true. If there is
// an error, the errorString will be set.
bool IsTrue(std::vector<cmExpandedCommandArgument> const& args,
std::string& errorString, MessageType& status);
private:
class cmArgumentList;
cmValue GetDefinitionIfUnquoted(
cmExpandedCommandArgument const& argument) const;
cmValue GetVariableOrString(cmExpandedCommandArgument const& argument) const;
bool IsKeyword(cm::static_string_view keyword,
cmExpandedCommandArgument const& argument) const;
bool GetBooleanValue(cmExpandedCommandArgument& arg) const;
template <int N>
int matchKeysImpl(cmExpandedCommandArgument const&);
template <int N, typename T, typename... Keys>
int matchKeysImpl(cmExpandedCommandArgument const&, T, Keys...);
template <typename... Keys>
int matchKeys(cmExpandedCommandArgument const&, Keys...);
bool HandleLevel0(cmArgumentList& newArgs, std::string& errorString,
MessageType& status);
bool HandleLevel1(cmArgumentList& newArgs, std::string&, MessageType&);
bool HandleLevel2(cmArgumentList& newArgs, std::string& errorString,
MessageType& status);
bool HandleLevel3(cmArgumentList& newArgs, std::string& errorString,
MessageType& status);
bool HandleLevel4(cmArgumentList& newArgs, std::string& errorString,
MessageType& status);
cmMakefile& Makefile;
cmListFileBacktrace Backtrace;
cmPolicies::PolicyStatus Policy139Status;
cmPolicies::PolicyStatus Policy222Status;
};