ENH: put the 64 bit paths first
[cmake.git] / Source / cmSetCommand.cxx
blob467bb4dcbda313f8a6de374efbe5ffee7b1ece84
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmSetCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2009-05-10 20:07:34 $
7 Version: $Revision: 1.35 $
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
83 unsigned int ignoreLastArgs = 0;
84 // look for PARENT_SCOPE argument
85 if (args.size() > 1 && args[args.size()-1] == "PARENT_SCOPE")
87 parentScope = true;
88 ignoreLastArgs++;
90 else
92 // look for FORCE argument
93 if (args.size() > 4 && args[args.size()-1] == "FORCE")
95 force = true;
96 ignoreLastArgs++;
99 // check for cache signature
100 if (args.size() > 3 && args[args.size() - 3 - (force ? 1 : 0)] == "CACHE")
102 cache = true;
103 ignoreLastArgs+=3;
107 // collect any values into a single semi-colon seperated value list
108 if(static_cast<unsigned short>(args.size()) >
109 static_cast<unsigned short>(1 + ignoreLastArgs))
111 value = args[1];
112 size_t endPos = args.size() - ignoreLastArgs;
113 for(size_t i = 2; i < endPos; ++i)
115 value += ";";
116 value += args[i];
120 if (parentScope)
122 if (value.empty())
124 this->Makefile->RaiseScope(variable, 0);
126 else
128 this->Makefile->RaiseScope(variable, value.c_str());
130 return true;
134 // we should be nice and try to catch some simple screwups if the last or
135 // next to last args are CACHE then they screwed up. If they used FORCE
136 // without CACHE they screwed up
137 if (args[args.size() - 1] == "CACHE" ||
138 args.size() > 1 && args[args.size() - 2] == "CACHE" ||
139 force && !cache)
141 this->SetError("given invalid arguments for CACHE mode.");
142 return false;
145 if(cache)
147 std::string::size_type cacheStart = args.size() - 3 - (force ? 1 : 0);
148 type = cmCacheManager::StringToType(args[cacheStart+1].c_str());
149 docstring = args[cacheStart+2].c_str();
152 // see if this is already in the cache
153 cmCacheManager::CacheIterator it =
154 this->Makefile->GetCacheManager()->GetCacheIterator(variable);
155 if(!it.IsAtEnd() && (it.GetType() != cmCacheManager::UNINITIALIZED))
157 // if the set is trying to CACHE the value but the value
158 // is already in the cache and the type is not internal
159 // then leave now without setting any definitions in the cache
160 // or the makefile
161 if(cache && type != cmCacheManager::INTERNAL && !force)
163 return true;
167 // if it is meant to be in the cache then define it in the cache
168 if(cache)
170 this->Makefile->AddCacheDefinition(variable,
171 value.c_str(),
172 docstring,
173 type, force);
175 else
177 // add the definition
178 this->Makefile->AddDefinition(variable, value.c_str());
180 return true;