1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmListCommand.h"
13 #include <cmsys/RegularExpression.hxx>
14 #include <cmsys/SystemTools.hxx>
16 #include <stdlib.h> // required for atoi
18 //----------------------------------------------------------------------------
20 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
24 this->SetError("must be called with at least one argument.");
28 const std::string &subCommand = args[0];
29 if(subCommand == "LENGTH")
31 return this->HandleLengthCommand(args);
33 if(subCommand == "GET")
35 return this->HandleGetCommand(args);
37 if(subCommand == "APPEND")
39 return this->HandleAppendCommand(args);
41 if(subCommand == "FIND")
43 return this->HandleFindCommand(args);
45 if(subCommand == "INSERT")
47 return this->HandleInsertCommand(args);
49 if(subCommand == "REMOVE_AT")
51 return this->HandleRemoveAtCommand(args);
53 if(subCommand == "REMOVE_ITEM")
55 return this->HandleRemoveItemCommand(args);
57 if(subCommand == "REMOVE_DUPLICATES")
59 return this->HandleRemoveDuplicatesCommand(args);
61 if(subCommand == "SORT")
63 return this->HandleSortCommand(args);
65 if(subCommand == "REVERSE")
67 return this->HandleReverseCommand(args);
70 std::string e = "does not recognize sub-command "+subCommand;
71 this->SetError(e.c_str());
75 //----------------------------------------------------------------------------
76 bool cmListCommand::GetListString(std::string& listString, const char* var)
83 const char* cacheValue
84 = this->Makefile->GetDefinition(var);
89 listString = cacheValue;
93 //----------------------------------------------------------------------------
94 bool cmListCommand::GetList(std::vector<std::string>& list, const char* var)
96 std::string listString;
97 if ( !this->GetListString(listString, var) )
101 // if the size of the list
102 if(listString.size() == 0)
106 // expand the variable into a list
107 cmSystemTools::ExpandListArgument(listString, list, true);
108 // check the list for empty values
109 bool hasEmpty = false;
110 for(std::vector<std::string>::iterator i = list.begin();
111 i != list.end(); ++i)
119 // if no empty elements then just return
124 // if we have empty elements we need to check policy CMP0007
125 switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0007))
127 case cmPolicies::WARN:
129 // Default is to warn and use old behavior
130 // OLD behavior is to allow compatibility, so recall
131 // ExpandListArgument without the true which will remove
134 cmSystemTools::ExpandListArgument(listString, list);
135 std::string warn = this->Makefile->GetPolicies()->
136 GetPolicyWarning(cmPolicies::CMP0007);
137 warn += " List has value = [";
140 this->Makefile->IssueMessage(cmake::AUTHOR_WARNING,
144 case cmPolicies::OLD:
145 // OLD behavior is to allow compatibility, so recall
146 // ExpandListArgument without the true which will remove
149 cmSystemTools::ExpandListArgument(listString, list);
151 case cmPolicies::NEW:
153 case cmPolicies::REQUIRED_IF_USED:
154 case cmPolicies::REQUIRED_ALWAYS:
155 this->Makefile->IssueMessage(
157 this->Makefile->GetPolicies()
158 ->GetRequiredPolicyError(cmPolicies::CMP0007)
165 //----------------------------------------------------------------------------
166 bool cmListCommand::HandleLengthCommand(std::vector<std::string> const& args)
170 this->SetError("sub-command LENGTH requires two arguments.");
174 const std::string& listName = args[1];
175 const std::string& variableName = args[args.size() - 1];
176 std::vector<std::string> varArgsExpanded;
177 // do not check the return value here
178 // if the list var is not found varArgsExpanded will have size 0
179 // and we will return 0
180 this->GetList(varArgsExpanded, listName.c_str());
181 size_t length = varArgsExpanded.size();
183 sprintf(buffer, "%d", static_cast<int>(length));
185 this->Makefile->AddDefinition(variableName.c_str(), buffer);
189 //----------------------------------------------------------------------------
190 bool cmListCommand::HandleGetCommand(std::vector<std::string> const& args)
194 this->SetError("sub-command GET requires at least three arguments.");
198 const std::string& listName = args[1];
199 const std::string& variableName = args[args.size() - 1];
200 // expand the variable
201 std::vector<std::string> varArgsExpanded;
202 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
204 this->Makefile->AddDefinition(variableName.c_str(), "NOTFOUND");
207 // FIXME: Add policy to make non-existing lists an error like empty lists.
208 if(varArgsExpanded.empty())
210 this->SetError("GET given empty list");
216 const char* sep = "";
217 for ( cc = 2; cc < args.size()-1; cc ++ )
219 int item = atoi(args[cc].c_str());
222 size_t nitem = varArgsExpanded.size();
225 item = (int)nitem + item;
227 if ( item < 0 || nitem <= (size_t)item )
230 str << "index: " << item << " out of range (-"
231 << varArgsExpanded.size() << ", "
232 << varArgsExpanded.size()-1 << ")";
233 this->SetError(str.str().c_str());
236 value += varArgsExpanded[item];
239 this->Makefile->AddDefinition(variableName.c_str(), value.c_str());
243 //----------------------------------------------------------------------------
244 bool cmListCommand::HandleAppendCommand(std::vector<std::string> const& args)
248 this->SetError("sub-command APPEND requires at least one argument.");
252 // Skip if nothing to append.
258 const std::string& listName = args[1];
259 // expand the variable
260 std::string listString;
261 this->GetListString(listString, listName.c_str());
263 for ( cc = 2; cc < args.size(); ++ cc )
265 if(listString.size())
269 listString += args[cc];
272 this->Makefile->AddDefinition(listName.c_str(), listString.c_str());
276 //----------------------------------------------------------------------------
277 bool cmListCommand::HandleFindCommand(std::vector<std::string> const& args)
281 this->SetError("sub-command FIND requires three arguments.");
285 const std::string& listName = args[1];
286 const std::string& variableName = args[args.size() - 1];
287 // expand the variable
288 std::vector<std::string> varArgsExpanded;
289 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
291 this->Makefile->AddDefinition(variableName.c_str(), "-1");
295 std::vector<std::string>::iterator it;
296 unsigned int index = 0;
297 for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
299 if ( *it == args[2] )
301 char indexString[32];
302 sprintf(indexString, "%d", index);
303 this->Makefile->AddDefinition(variableName.c_str(), indexString);
309 this->Makefile->AddDefinition(variableName.c_str(), "-1");
313 //----------------------------------------------------------------------------
314 bool cmListCommand::HandleInsertCommand(std::vector<std::string> const& args)
318 this->SetError("sub-command INSERT requires at least three arguments.");
322 const std::string& listName = args[1];
324 // expand the variable
325 int item = atoi(args[2].c_str());
326 std::vector<std::string> varArgsExpanded;
327 if((!this->GetList(varArgsExpanded, listName.c_str())
328 || varArgsExpanded.empty()) && item != 0)
331 str << "index: " << item << " out of range (0, 0)";
332 this->SetError(str.str().c_str());
336 if ( varArgsExpanded.size() != 0 )
338 size_t nitem = varArgsExpanded.size();
341 item = (int)nitem + item;
343 if ( item < 0 || nitem <= (size_t)item )
346 str << "index: " << item << " out of range (-"
347 << varArgsExpanded.size() << ", "
348 << (varArgsExpanded.size() == 0?0:(varArgsExpanded.size()-1)) << ")";
349 this->SetError(str.str().c_str());
355 for ( cc = 3; cc < args.size(); ++ cc )
357 varArgsExpanded.insert(varArgsExpanded.begin()+item+cnt, args[cc]);
362 const char* sep = "";
363 for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
366 value += varArgsExpanded[cc];
370 this->Makefile->AddDefinition(listName.c_str(), value.c_str());
374 //----------------------------------------------------------------------------
376 ::HandleRemoveItemCommand(std::vector<std::string> const& args)
380 this->SetError("sub-command REMOVE_ITEM requires two or more arguments.");
384 const std::string& listName = args[1];
385 // expand the variable
386 std::vector<std::string> varArgsExpanded;
387 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
389 this->SetError("sub-command REMOVE_ITEM requires list to be present.");
394 for ( cc = 2; cc < args.size(); ++ cc )
397 while ( kk < varArgsExpanded.size() )
399 if ( varArgsExpanded[kk] == args[cc] )
401 varArgsExpanded.erase(varArgsExpanded.begin()+kk);
411 const char* sep = "";
412 for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
415 value += varArgsExpanded[cc];
419 this->Makefile->AddDefinition(listName.c_str(), value.c_str());
423 //----------------------------------------------------------------------------
425 ::HandleReverseCommand(std::vector<std::string> const& args)
429 this->SetError("sub-command REVERSE requires a list as an argument.");
433 const std::string& listName = args[1];
434 // expand the variable
435 std::vector<std::string> varArgsExpanded;
436 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
438 this->SetError("sub-command REVERSE requires list to be present.");
443 std::vector<std::string>::reverse_iterator it;
444 const char* sep = "";
445 for ( it = varArgsExpanded.rbegin(); it != varArgsExpanded.rend(); ++ it )
448 value += it->c_str();
452 this->Makefile->AddDefinition(listName.c_str(), value.c_str());
456 //----------------------------------------------------------------------------
458 ::HandleRemoveDuplicatesCommand(std::vector<std::string> const& args)
463 "sub-command REMOVE_DUPLICATES requires a list as an argument.");
467 const std::string& listName = args[1];
468 // expand the variable
469 std::vector<std::string> varArgsExpanded;
470 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
473 "sub-command REMOVE_DUPLICATES requires list to be present.");
480 std::set<std::string> unique;
481 std::vector<std::string>::iterator it;
482 const char* sep = "";
483 for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
485 if (unique.find(*it) != unique.end())
491 value += it->c_str();
496 this->Makefile->AddDefinition(listName.c_str(), value.c_str());
500 //----------------------------------------------------------------------------
502 ::HandleSortCommand(std::vector<std::string> const& args)
506 this->SetError("sub-command SORT requires a list as an argument.");
510 const std::string& listName = args[1];
511 // expand the variable
512 std::vector<std::string> varArgsExpanded;
513 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
515 this->SetError("sub-command SORT requires list to be present.");
519 std::sort(varArgsExpanded.begin(), varArgsExpanded.end());
522 std::vector<std::string>::iterator it;
523 const char* sep = "";
524 for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
527 value += it->c_str();
531 this->Makefile->AddDefinition(listName.c_str(), value.c_str());
535 //----------------------------------------------------------------------------
536 bool cmListCommand::HandleRemoveAtCommand(
537 std::vector<std::string> const& args)
541 this->SetError("sub-command REMOVE_AT requires at least "
546 const std::string& listName = args[1];
547 // expand the variable
548 std::vector<std::string> varArgsExpanded;
549 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
551 this->SetError("sub-command REMOVE_AT requires list to be present.");
554 // FIXME: Add policy to make non-existing lists an error like empty lists.
555 if(varArgsExpanded.empty())
557 this->SetError("REMOVE_AT given empty list");
562 std::vector<size_t> removed;
563 for ( cc = 2; cc < args.size(); ++ cc )
565 int item = atoi(args[cc].c_str());
566 size_t nitem = varArgsExpanded.size();
569 item = (int)nitem + item;
571 if ( item < 0 || nitem <= (size_t)item )
574 str << "index: " << item << " out of range (-"
575 << varArgsExpanded.size() << ", "
576 << varArgsExpanded.size()-1 << ")";
577 this->SetError(str.str().c_str());
580 removed.push_back(static_cast<size_t>(item));
584 const char* sep = "";
585 for ( cc = 0; cc < varArgsExpanded.size(); ++ cc )
589 for ( kk = 0; kk < removed.size(); ++ kk )
591 if ( cc == removed[kk] )
600 value += varArgsExpanded[cc];
605 this->Makefile->AddDefinition(listName.c_str(), value.c_str());