1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmSetCommand.cxx,v $
6 Date: $Date: 2009-09-11 12:18:12 $
7 Version: $Revision: 1.37 $
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
83 unsigned int ignoreLastArgs
= 0;
84 // look for PARENT_SCOPE argument
85 if (args
.size() > 1 && args
[args
.size()-1] == "PARENT_SCOPE")
92 // look for FORCE argument
93 if (args
.size() > 4 && args
[args
.size()-1] == "FORCE")
99 // check for cache signature
100 if (args
.size() > 3 && args
[args
.size() - 3 - (force
? 1 : 0)] == "CACHE")
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
))
112 size_t endPos
= args
.size() - ignoreLastArgs
;
113 for(size_t i
= 2; i
< endPos
; ++i
)
124 this->Makefile
->RaiseScope(variable
, 0);
128 this->Makefile
->RaiseScope(variable
, value
.c_str());
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") ||
141 this->SetError("given invalid arguments for CACHE mode.");
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
161 if(cache
&& type
!= cmCacheManager::INTERNAL
&& !force
)
163 this->Makefile
->UseCacheDefinition(it
);
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());