1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmIfCommand.h"
9 #include <cm/string_view>
10 #include <cmext/string_view>
12 #include "cmConditionEvaluator.h"
13 #include "cmExecutionStatus.h"
14 #include "cmExpandedCommandArgument.h"
15 #include "cmFunctionBlocker.h"
16 #include "cmListFileCache.h"
17 #include "cmMakefile.h"
18 #include "cmMessageType.h"
19 #include "cmOutputConverter.h"
20 #include "cmStringAlgorithms.h"
21 #include "cmSystemTools.h"
24 static std::string cmIfCommandError(
25 std::vector<cmExpandedCommandArgument> const& args)
27 std::string err = "given arguments:\n ";
28 for (cmExpandedCommandArgument const& i : args) {
30 err += cmOutputConverter::EscapeForCMake(i.GetValue());
36 class cmIfFunctionBlocker : public cmFunctionBlocker
39 cm::string_view StartCommandName() const override { return "if"_s; }
40 cm::string_view EndCommandName() const override { return "endif"_s; }
42 bool ArgumentsMatch(cmListFileFunction const& lff,
43 cmMakefile&) const override;
45 bool Replay(std::vector<cmListFileFunction> functions,
46 cmExecutionStatus& inStatus) override;
48 std::vector<cmListFileArgument> Args;
51 bool ElseSeen = false;
54 bool cmIfFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff,
57 return lff.Arguments().empty() || lff.Arguments() == this->Args;
60 bool cmIfFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
61 cmExecutionStatus& inStatus)
63 cmMakefile& mf = inStatus.GetMakefile();
64 // execute the functions for the true parts of the if statement
66 for (cmListFileFunction const& func : functions) {
67 // keep track of scope depth
68 if (func.LowerCaseName() == "if") {
71 if (func.LowerCaseName() == "endif") {
74 // watch for our state change
75 if (scopeDepth == 0 && func.LowerCaseName() == "else") {
76 cmListFileBacktrace elseBT = mf.GetBacktrace().Push(
77 cmListFileContext{ func.OriginalName(),
78 this->GetStartingContext().FilePath, func.Line() });
81 mf.GetCMakeInstance()->IssueMessage(
82 MessageType::FATAL_ERROR,
83 "A duplicate ELSE command was found inside an IF block.", elseBT);
84 cmSystemTools::SetFatalErrorOccurred();
88 this->IsBlocking = this->HasRun;
90 this->ElseSeen = true;
92 // if trace is enabled, print a (trivially) evaluated "else"
94 if (!this->IsBlocking && mf.GetCMakeInstance()->GetTrace()) {
95 mf.PrintCommandTrace(func, elseBT,
96 cmMakefile::CommandMissingFromStack::Yes);
98 } else if (scopeDepth == 0 && func.LowerCaseName() == "elseif") {
99 cmListFileBacktrace elseifBT = mf.GetBacktrace().Push(
100 cmListFileContext{ func.OriginalName(),
101 this->GetStartingContext().FilePath, func.Line() });
102 if (this->ElseSeen) {
103 mf.GetCMakeInstance()->IssueMessage(
104 MessageType::FATAL_ERROR,
105 "An ELSEIF command was found after an ELSE command.", elseifBT);
106 cmSystemTools::SetFatalErrorOccurred();
111 this->IsBlocking = true;
113 // if trace is enabled, print the evaluated "elseif" statement
114 if (mf.GetCMakeInstance()->GetTrace()) {
115 mf.PrintCommandTrace(func, elseifBT,
116 cmMakefile::CommandMissingFromStack::Yes);
119 std::string errorString;
121 std::vector<cmExpandedCommandArgument> expandedArguments;
122 mf.ExpandArguments(func.Arguments(), expandedArguments);
124 MessageType messType;
126 cmConditionEvaluator conditionEvaluator(mf, elseifBT);
129 conditionEvaluator.IsTrue(expandedArguments, errorString, messType);
131 if (!errorString.empty()) {
133 cmStrCat(cmIfCommandError(expandedArguments), errorString);
134 mf.GetCMakeInstance()->IssueMessage(messType, err, elseifBT);
135 if (messType == MessageType::FATAL_ERROR) {
136 cmSystemTools::SetFatalErrorOccurred();
142 this->IsBlocking = false;
148 // should we execute?
149 else if (!this->IsBlocking) {
150 cmExecutionStatus status(mf);
151 mf.ExecuteCommand(func, status);
152 if (status.GetReturnInvoked()) {
153 inStatus.SetReturnInvoked(status.GetReturnVariables());
156 if (status.GetBreakInvoked()) {
157 inStatus.SetBreakInvoked();
160 if (status.GetContinueInvoked()) {
161 inStatus.SetContinueInvoked();
169 //=========================================================================
170 bool cmIfCommand(std::vector<cmListFileArgument> const& args,
171 cmExecutionStatus& inStatus)
173 cmMakefile& makefile = inStatus.GetMakefile();
174 std::string errorString;
176 std::vector<cmExpandedCommandArgument> expandedArguments;
177 makefile.ExpandArguments(args, expandedArguments);
181 cmConditionEvaluator conditionEvaluator(makefile, makefile.GetBacktrace());
184 conditionEvaluator.IsTrue(expandedArguments, errorString, status);
186 if (!errorString.empty()) {
188 cmStrCat("if ", cmIfCommandError(expandedArguments), errorString);
189 if (status == MessageType::FATAL_ERROR) {
190 makefile.IssueMessage(MessageType::FATAL_ERROR, err);
191 cmSystemTools::SetFatalErrorOccurred();
194 makefile.IssueMessage(status, err);
198 auto fb = cm::make_unique<cmIfFunctionBlocker>();
199 // if is isn't true block the commands
200 fb->IsBlocking = !isTrue;
205 makefile.AddFunctionBlocker(std::move(fb));