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 "cmSetPropertyCommand.h"
13 #include "cmSetTargetPropertiesCommand.h"
14 #include "cmSetTestsPropertiesCommand.h"
15 #include "cmSetSourceFilesPropertiesCommand.h"
17 #include "cmCacheManager.h"
19 //----------------------------------------------------------------------------
20 cmSetPropertyCommand::cmSetPropertyCommand()
22 this->AppendMode = false;
23 this->AppendAsString = false;
27 //----------------------------------------------------------------------------
28 bool cmSetPropertyCommand
29 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
33 this->SetError("called with incorrect number of arguments");
37 // Get the scope on which to set the property.
38 std::vector<std::string>::const_iterator arg = args.begin();
39 cmProperty::ScopeType scope;
42 scope = cmProperty::GLOBAL;
44 else if(*arg == "DIRECTORY")
46 scope = cmProperty::DIRECTORY;
48 else if(*arg == "TARGET")
50 scope = cmProperty::TARGET;
52 else if(*arg == "SOURCE")
54 scope = cmProperty::SOURCE_FILE;
56 else if(*arg == "TEST")
58 scope = cmProperty::TEST;
60 else if(*arg == "CACHE")
62 scope = cmProperty::CACHE;
67 e << "given invalid scope " << *arg << ". "
68 << "Valid scopes are GLOBAL, DIRECTORY, TARGET, SOURCE, TEST, CACHE.";
69 this->SetError(e.str().c_str());
73 // Parse the rest of the arguments up to the values.
74 enum Doing { DoingNone, DoingNames, DoingProperty, DoingValues };
75 Doing doing = DoingNames;
77 for(++arg; arg != args.end(); ++arg)
79 if(*arg == "PROPERTY")
81 doing = DoingProperty;
83 else if(*arg == "APPEND")
86 this->AppendMode = true;
88 this->AppendAsString = false;
90 else if(*arg == "APPEND_STRING")
93 this->AppendMode = true;
95 this->AppendAsString = true;
97 else if(doing == DoingNames)
99 this->Names.insert(*arg);
101 else if(doing == DoingProperty)
103 this->PropertyName = *arg;
106 else if(doing == DoingValues)
108 this->PropertyValue += sep;
110 this->PropertyValue += *arg;
111 this->Remove = false;
116 e << "given invalid argument \"" << *arg << "\".";
117 this->SetError(e.str().c_str());
122 // Make sure a property name was found.
123 if(this->PropertyName.empty())
125 this->SetError("not given a PROPERTY <name> argument.");
129 // Dispatch property setting.
132 case cmProperty::GLOBAL: return this->HandleGlobalMode();
133 case cmProperty::DIRECTORY: return this->HandleDirectoryMode();
134 case cmProperty::TARGET: return this->HandleTargetMode();
135 case cmProperty::SOURCE_FILE: return this->HandleSourceMode();
136 case cmProperty::TEST: return this->HandleTestMode();
137 case cmProperty::CACHE: return this->HandleCacheMode();
139 case cmProperty::VARIABLE:
140 case cmProperty::CACHED_VARIABLE:
141 break; // should never happen
146 //----------------------------------------------------------------------------
147 bool cmSetPropertyCommand::HandleGlobalMode()
149 if(!this->Names.empty())
151 this->SetError("given names for GLOBAL scope.");
155 // Set or append the property.
156 cmake* cm = this->Makefile->GetCMakeInstance();
157 const char* name = this->PropertyName.c_str();
158 const char *value = this->PropertyValue.c_str();
165 cm->AppendProperty(name, value ? value : "", this->AppendAsString);
169 cm->SetProperty(name, value);
175 //----------------------------------------------------------------------------
176 bool cmSetPropertyCommand::HandleDirectoryMode()
178 if(this->Names.size() > 1)
180 this->SetError("allows at most one name for DIRECTORY scope.");
184 // Default to the current directory.
185 cmMakefile* mf = this->Makefile;
187 // Lookup the directory if given.
188 if(!this->Names.empty())
190 // Construct the directory name. Interpret relative paths with
191 // respect to the current directory.
192 std::string dir = *this->Names.begin();
193 if(!cmSystemTools::FileIsFullPath(dir.c_str()))
195 dir = this->Makefile->GetCurrentDirectory();
197 dir += *this->Names.begin();
200 // The local generators are associated with collapsed paths.
201 dir = cmSystemTools::CollapseFullPath(dir.c_str());
203 // Lookup the generator.
204 if(cmLocalGenerator* lg =
205 (this->Makefile->GetLocalGenerator()
206 ->GetGlobalGenerator()->FindLocalGenerator(dir.c_str())))
208 // Use the makefile for the directory found.
209 mf = lg->GetMakefile();
213 // Could not find the directory.
215 ("DIRECTORY scope provided but requested directory was not found. "
216 "This could be because the directory argument was invalid or, "
217 "it is valid but has not been processed yet.");
222 // Set or append the property.
223 const char* name = this->PropertyName.c_str();
224 const char *value = this->PropertyValue.c_str();
231 mf->AppendProperty(name, value ? value : "", this->AppendAsString);
235 mf->SetProperty(name, value);
241 //----------------------------------------------------------------------------
242 bool cmSetPropertyCommand::HandleTargetMode()
244 for(std::set<cmStdString>::const_iterator ni = this->Names.begin();
245 ni != this->Names.end(); ++ni)
247 if (this->Makefile->IsAlias(ni->c_str()))
249 this->SetError("can not be used on an ALIAS target.");
252 if(cmTarget* target = this->Makefile->FindTargetToUse(ni->c_str()))
254 // Handle the current target.
255 if(!this->HandleTarget(target))
263 e << "could not find TARGET " << *ni
264 << ". Perhaps it has not yet been created.";
265 this->SetError(e.str().c_str());
272 //----------------------------------------------------------------------------
273 bool cmSetPropertyCommand::HandleTarget(cmTarget* target)
275 // Set or append the property.
276 const char* name = this->PropertyName.c_str();
277 const char *value = this->PropertyValue.c_str();
284 target->AppendProperty(name, value, this->AppendAsString);
288 target->SetProperty(name, value);
291 // Check the resulting value.
292 target->CheckProperty(name, this->Makefile);
297 //----------------------------------------------------------------------------
298 bool cmSetPropertyCommand::HandleSourceMode()
300 for(std::set<cmStdString>::const_iterator ni = this->Names.begin();
301 ni != this->Names.end(); ++ni)
303 // Get the source file.
304 if(cmSourceFile* sf = this->Makefile->GetOrCreateSource(ni->c_str()))
306 if(!this->HandleSource(sf))
314 e << "given SOURCE name that could not be found or created: " << *ni;
315 this->SetError(e.str().c_str());
322 //----------------------------------------------------------------------------
323 bool cmSetPropertyCommand::HandleSource(cmSourceFile* sf)
325 // Set or append the property.
326 const char* name = this->PropertyName.c_str();
327 const char *value = this->PropertyValue.c_str();
335 sf->AppendProperty(name, value, this->AppendAsString);
339 sf->SetProperty(name, value);
344 //----------------------------------------------------------------------------
345 bool cmSetPropertyCommand::HandleTestMode()
347 // Look for tests with all names given.
348 std::set<cmStdString>::iterator next;
349 for(std::set<cmStdString>::iterator ni = this->Names.begin();
350 ni != this->Names.end(); ni = next)
354 if(cmTest* test = this->Makefile->GetTest(ni->c_str()))
356 if(this->HandleTest(test))
358 this->Names.erase(ni);
367 // Names that are still left were not found.
368 if(!this->Names.empty())
371 e << "given TEST names that do not exist:\n";
372 for(std::set<cmStdString>::const_iterator ni = this->Names.begin();
373 ni != this->Names.end(); ++ni)
375 e << " " << *ni << "\n";
377 this->SetError(e.str().c_str());
383 //----------------------------------------------------------------------------
384 bool cmSetPropertyCommand::HandleTest(cmTest* test)
386 // Set or append the property.
387 const char* name = this->PropertyName.c_str();
388 const char *value = this->PropertyValue.c_str();
395 test->AppendProperty(name, value, this->AppendAsString);
399 test->SetProperty(name, value);
405 //----------------------------------------------------------------------------
406 bool cmSetPropertyCommand::HandleCacheMode()
408 if(this->PropertyName == "ADVANCED")
411 !cmSystemTools::IsOn(this->PropertyValue.c_str()) &&
412 !cmSystemTools::IsOff(this->PropertyValue.c_str()))
415 e << "given non-boolean value \"" << this->PropertyValue
416 << "\" for CACHE property \"ADVANCED\". ";
417 this->SetError(e.str().c_str());
421 else if(this->PropertyName == "TYPE")
423 if(!cmCacheManager::IsType(this->PropertyValue.c_str()))
426 e << "given invalid CACHE entry TYPE \"" << this->PropertyValue << "\"";
427 this->SetError(e.str().c_str());
431 else if(this->PropertyName != "HELPSTRING" &&
432 this->PropertyName != "STRINGS" &&
433 this->PropertyName != "VALUE")
436 e << "given invalid CACHE property " << this->PropertyName << ". "
437 << "Settable CACHE properties are: "
438 << "ADVANCED, HELPSTRING, STRINGS, TYPE, and VALUE.";
439 this->SetError(e.str().c_str());
443 for(std::set<cmStdString>::const_iterator ni = this->Names.begin();
444 ni != this->Names.end(); ++ni)
446 // Get the source file.
447 cmMakefile* mf = this->GetMakefile();
448 cmake* cm = mf->GetCMakeInstance();
449 cmCacheManager::CacheIterator it =
450 cm->GetCacheManager()->GetCacheIterator(ni->c_str());
453 if(!this->HandleCacheEntry(it))
461 e << "could not find CACHE variable " << *ni
462 << ". Perhaps it has not yet been created.";
463 this->SetError(e.str().c_str());
470 //----------------------------------------------------------------------------
471 bool cmSetPropertyCommand::HandleCacheEntry(cmCacheManager::CacheIterator& it)
473 // Set or append the property.
474 const char* name = this->PropertyName.c_str();
475 const char* value = this->PropertyValue.c_str();
482 it.AppendProperty(name, value, this->AppendAsString);
486 it.SetProperty(name, value);