Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmWhileCommand.cxx
blobd5be1627912507c66592a577185fa5bee61dc0c2
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmWhileCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008/01/23 15:27:59 $
7 Version: $Revision: 1.10 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmWhileCommand.h"
18 #include "cmIfCommand.h"
20 bool cmWhileFunctionBlocker::
21 IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
22 cmExecutionStatus &inStatus)
24 // Prevent recusion and don't let this blocker block its own
25 // commands.
26 if (this->Executing)
28 return false;
31 // at end of for each execute recorded commands
32 if (!cmSystemTools::Strucmp(lff.Name.c_str(),"while"))
34 // record the number of while commands past this one
35 this->Depth++;
37 else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endwhile"))
39 // if this is the endwhile for this while loop then execute
40 if (!this->Depth)
42 char* errorString = 0;
44 std::vector<std::string> expandedArguments;
45 mf.ExpandArguments(this->Args, expandedArguments);
46 bool isTrue =
47 cmIfCommand::IsTrue(expandedArguments,&errorString,&mf);
49 this->Executing = true;
50 while (isTrue)
52 // Invoke all the functions that were collected in the block.
53 for(unsigned int c = 0; c < this->Functions.size(); ++c)
55 cmExecutionStatus status;
56 mf.ExecuteCommand(this->Functions[c],status);
57 if (status.GetReturnInvoked())
59 inStatus.SetReturnInvoked(true);
60 mf.RemoveFunctionBlocker(lff);
61 return true;
63 if (status.GetBreakInvoked())
65 mf.RemoveFunctionBlocker(lff);
66 return true;
69 expandedArguments.clear();
70 mf.ExpandArguments(this->Args, expandedArguments);
71 isTrue =
72 cmIfCommand::IsTrue(expandedArguments,&errorString,&mf);
74 mf.RemoveFunctionBlocker(lff);
75 return true;
77 else
79 // decrement for each nested while that ends
80 this->Depth--;
84 // record the command
85 this->Functions.push_back(lff);
87 // always return true
88 return true;
91 bool cmWhileFunctionBlocker::
92 ShouldRemove(const cmListFileFunction& lff, cmMakefile& mf)
94 if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endwhile"))
96 if (lff.Arguments == this->Args
97 || cmSystemTools::IsOn
98 (mf.GetPropertyOrDefinition("CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS")))
100 return true;
103 return false;
106 void cmWhileFunctionBlocker::
107 ScopeEnded(cmMakefile &mf)
109 cmSystemTools::Error(
110 "The end of a CMakeLists file was reached with a WHILE statement that "
111 "was not closed properly. Within the directory: ",
112 mf.GetCurrentDirectory());
115 bool cmWhileCommand
116 ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
117 cmExecutionStatus &)
119 if(args.size() < 1)
121 this->SetError("called with incorrect number of arguments");
122 return false;
125 // create a function blocker
126 cmWhileFunctionBlocker *f = new cmWhileFunctionBlocker();
127 f->Args = args;
128 this->Makefile->AddFunctionBlocker(f);
130 return true;