Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmSetCommand.cxx
blob2802192e4f7415072709ee913b2dd049aff251ee
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmSetCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008-11-21 21:32:39 $
7 Version: $Revision: 1.34 $
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 this->SetError("given invalid arguments for CACHE mode.");
143 return false;
146 if(cache)
148 cacheStart = args.size() - 3 - (force ? 1 : 0);
149 type = cmCacheManager::StringToType(args[cacheStart+1].c_str());
150 docstring = args[cacheStart+2].c_str();
153 // see if this is already in the cache
154 cmCacheManager::CacheIterator it =
155 this->Makefile->GetCacheManager()->GetCacheIterator(variable);
156 if(!it.IsAtEnd() && (it.GetType() != cmCacheManager::UNINITIALIZED))
158 // if the set is trying to CACHE the value but the value
159 // is already in the cache and the type is not internal
160 // then leave now without setting any definitions in the cache
161 // or the makefile
162 if(cache && type != cmCacheManager::INTERNAL && !force)
164 return true;
168 // if it is meant to be in the cache then define it in the cache
169 if(cache)
171 this->Makefile->AddCacheDefinition(variable,
172 value.c_str(),
173 docstring,
174 type, force);
176 else
178 // add the definition
179 this->Makefile->AddDefinition(variable, value.c_str());
181 return true;