1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmSetCommand.cxx,v $
6 Date: $Date: 2008-03-13 17:52:49 $
7 Version: $Revision: 1.33 $
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"
21 ::InitialPass(std::vector
<std::string
> const& args
, cmExecutionStatus
&)
25 this->SetError("called with incorrect number of arguments");
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
;
40 // what is the current value if any
41 const char *currValue
= getenv(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()))
51 cmSystemTools::PutEnv(putEnvArg
.c_str());
56 // if it will be cleared, then clear it if it isn;t already clear
59 cmSystemTools::PutEnv(putEnvArg
.c_str());
64 // SET (VAR) // Removes the definition of VAR.
67 this->Makefile
->RemoveDefinition(args
[0].c_str());
71 // here are the remaining options
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")
93 // look for FORCE argument
94 if (args
.size() > 4 && args
[args
.size()-1] == "FORCE")
100 // check for cache signature
101 if (args
.size() > 3 && args
[args
.size() - 3 - (force
? 1 : 0)] == "CACHE")
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
))
113 size_t endPos
= args
.size() - ignoreLastArgs
;
114 for(size_t i
= 2; i
< endPos
; ++i
)
125 this->Makefile
->RaiseScope(variable
, 0);
129 this->Makefile
->RaiseScope(variable
, value
.c_str());
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" ||
142 this->SetError("given invalid arguments for CACHE mode.");
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
162 if(cache
&& type
!= cmCacheManager::INTERNAL
&& !force
)
168 // if it is meant to be in the cache then define it in the cache
171 this->Makefile
->AddCacheDefinition(variable
,
178 // add the definition
179 this->Makefile
->AddDefinition(variable
, value
.c_str());