CVS resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmSetCommand.cxx
blob44b607434d9c53ea033e10b342a06bdc60aef1f0
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmSetCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008/01/23 15:27:59 $
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 "cmSetCommand.h"
19 // cmSetCommand
20 bool cmSetCommand
21 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
23 if(args.size() < 1 )
25 this->SetError("called with incorrect number of arguments");
26 return false;
29 // watch for ENV signatures
30 const char* variable = args[0].c_str(); // VAR is always first
31 if (!strncmp(variable,"ENV{",4) && strlen(variable) > 5)
33 // what is the variable name
34 char *varName = new char [strlen(variable)];
35 strncpy(varName,variable+4,strlen(variable)-5);
36 varName[strlen(variable)-5] = '\0';
37 std::string putEnvArg = varName;
38 putEnvArg += "=";
40 // what is the current value if any
41 const char *currValue = getenv(varName);
42 delete [] varName;
44 // will it be set to something, then set it
45 if (args.size() > 1 && args[1].size())
47 // but only if it is different from current value
48 if (!currValue || strcmp(currValue,args[1].c_str()))
50 putEnvArg += args[1];
51 cmSystemTools::PutEnv(putEnvArg.c_str());
53 return true;
56 // if it will be cleared, then clear it if it isn;t already clear
57 if (currValue)
59 cmSystemTools::PutEnv(putEnvArg.c_str());
61 return true;
64 // SET (VAR) // Removes the definition of VAR.
65 if (args.size() == 1)
67 this->Makefile->RemoveDefinition(args[0].c_str());
68 return true;
71 // here are the remaining options
72 // SET (VAR value )
73 // SET (VAR CACHE TYPE "doc String" [FORCE])
74 // SET (VAR value CACHE TYPE "doc string" [FORCE])
75 std::string value; // optional
76 bool cache = false; // optional
77 bool force = false; // optional
78 bool parentScope = false;
79 cmCacheManager::CacheEntryType type
80 = cmCacheManager::STRING; // required if cache
81 const char* docstring = 0; // required if cache
82 std::string::size_type cacheStart = 0;
84 unsigned int ignoreLastArgs = 0;
85 // look for PARENT_SCOPE argument
86 if (args.size() > 1 && args[args.size()-1] == "PARENT_SCOPE")
88 parentScope = true;
89 ignoreLastArgs++;
91 else
93 // look for FORCE argument
94 if (args.size() > 4 && args[args.size()-1] == "FORCE")
96 force = true;
97 ignoreLastArgs++;
100 // check for cache signature
101 if (args.size() > 3 && args[args.size() - 3 - (force ? 1 : 0)] == "CACHE")
103 cache = true;
104 ignoreLastArgs+=3;
108 // collect any values into a single semi-colon seperated value list
109 if(static_cast<unsigned short>(args.size()) >
110 static_cast<unsigned short>(1 + ignoreLastArgs))
112 value = args[1];
113 size_t endPos = args.size() - ignoreLastArgs;
114 for(size_t i = 2; i < endPos; ++i)
116 value += ";";
117 value += args[i];
121 if (parentScope)
123 if (value.empty())
125 this->Makefile->RaiseScope(variable, 0);
127 else
129 this->Makefile->RaiseScope(variable, value.c_str());
131 return true;
135 // we should be nice and try to catch some simple screwups if the last or
136 // next to last args are CACHE then they screwed up. If they used FORCE
137 // without CACHE they screwed up
138 if (args[args.size() - 1] == "CACHE" ||
139 args.size() > 1 && args[args.size() - 2] == "CACHE" ||
140 force && !cache)
142 std::string message;
143 message += "Syntax error in SET:\n";
144 message += "See the help for the SET command:\n";
145 message += "SET (";
146 for(std::vector<std::string>::const_iterator i = args.begin();
147 i != args.end(); ++i)
149 message += *i;
151 message += ")\n";
152 this->SetError(message.c_str());
153 return false;
156 if(cache)
158 cacheStart = args.size() - 3 - (force ? 1 : 0);
159 type = cmCacheManager::StringToType(args[cacheStart+1].c_str());
160 docstring = args[cacheStart+2].c_str();
163 // see if this is already in the cache
164 cmCacheManager::CacheIterator it =
165 this->Makefile->GetCacheManager()->GetCacheIterator(variable);
166 if(!it.IsAtEnd() && (it.GetType() != cmCacheManager::UNINITIALIZED))
168 // if the set is trying to CACHE the value but the value
169 // is already in the cache and the type is not internal
170 // then leave now without setting any definitions in the cache
171 // or the makefile
172 if(cache && type != cmCacheManager::INTERNAL && !force)
174 return true;
178 // if it is meant to be in the cache then define it in the cache
179 if(cache)
181 this->Makefile->AddCacheDefinition(variable,
182 value.c_str(),
183 docstring,
184 type);
186 else
188 // add the definition
189 this->Makefile->AddDefinition(variable, value.c_str());
191 return true;