Introduce "generator expressions" to add_test()
[cmake.git] / Source / cmAddTestCommand.cxx
blob8835f317755ba7170c407d9165b6ff709e01cdf5
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmAddTestCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2009-03-16 14:51:11 $
7 Version: $Revision: 1.32 $
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 "cmAddTestCommand.h"
19 #include "cmTestGenerator.h"
21 #include "cmTest.h"
24 // cmExecutableCommand
25 bool cmAddTestCommand
26 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
28 if(!args.empty() && args[0] == "NAME")
30 return this->HandleNameMode(args);
33 // First argument is the name of the test Second argument is the name of
34 // the executable to run (a target or external program) Remaining arguments
35 // are the arguments to pass to the executable
36 if(args.size() < 2 )
38 this->SetError("called with incorrect number of arguments");
39 return false;
42 // Collect the command with arguments.
43 std::vector<std::string> command;
44 for(std::vector<std::string>::const_iterator it = args.begin() + 1;
45 it != args.end(); ++it)
47 command.push_back(*it);
50 // Create the test but add a generator only the first time it is
51 // seen. This preserves behavior from before test generators.
52 cmTest* test = this->Makefile->GetTest(args[0].c_str());
53 if(test)
55 // If the test was already added by a new-style signature do not
56 // allow it to be duplicated.
57 if(!test->GetOldStyle())
59 cmOStringStream e;
60 e << " given test name \"" << args[0]
61 << "\" which already exists in this directory.";
62 this->SetError(e.str().c_str());
63 return false;
66 else
68 test = this->Makefile->CreateTest(args[0].c_str());
69 test->SetOldStyle(true);
70 this->Makefile->AddTestGenerator(new cmTestGenerator(test));
72 test->SetCommand(command);
74 return true;
77 //----------------------------------------------------------------------------
78 bool cmAddTestCommand::HandleNameMode(std::vector<std::string> const& args)
80 std::string name;
81 std::vector<std::string> configurations;
82 std::vector<std::string> command;
84 // Read the arguments.
85 enum Doing {
86 DoingName,
87 DoingCommand,
88 DoingConfigs,
89 DoingNone
91 Doing doing = DoingName;
92 for(unsigned int i=1; i < args.size(); ++i)
94 if(args[i] == "COMMAND")
96 if(!command.empty())
98 this->SetError(" may be given at most one COMMAND.");
99 return false;
101 doing = DoingCommand;
103 else if(args[i] == "CONFIGURATIONS")
105 if(!configurations.empty())
107 this->SetError(" may be given at most one set of CONFIGURATIONS.");
108 return false;
110 doing = DoingConfigs;
112 else if(doing == DoingName)
114 name = args[i];
115 doing = DoingNone;
117 else if(doing == DoingCommand)
119 command.push_back(args[i]);
121 else if(doing == DoingConfigs)
123 configurations.push_back(args[i]);
125 else
127 cmOStringStream e;
128 e << " given unknown argument:\n " << args[i] << "\n";
129 this->SetError(e.str().c_str());
130 return false;
134 // Require a test name.
135 if(name.empty())
137 this->SetError(" must be given non-empty NAME.");
138 return false;
141 // Require a command.
142 if(command.empty())
144 this->SetError(" must be given non-empty COMMAND.");
145 return false;
148 // Require a unique test name within the directory.
149 if(this->Makefile->GetTest(name.c_str()))
151 cmOStringStream e;
152 e << " given test NAME \"" << name
153 << "\" which already exists in this directory.";
154 this->SetError(e.str().c_str());
155 return false;
158 // Add the test.
159 cmTest* test = this->Makefile->CreateTest(name.c_str());
160 test->SetOldStyle(false);
161 test->SetCommand(command);
162 this->Makefile->AddTestGenerator(new cmTestGenerator(test, configurations));
164 return true;