cmSetCommand.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmSetCommand.h"
  14. // cmSetCommand
  15. bool cmSetCommand
  16. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  17. {
  18. if(args.size() < 1 )
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. // watch for ENV signatures
  24. const char* variable = args[0].c_str(); // VAR is always first
  25. if (!strncmp(variable,"ENV{",4) && strlen(variable) > 5)
  26. {
  27. // what is the variable name
  28. char *varName = new char [strlen(variable)];
  29. strncpy(varName,variable+4,strlen(variable)-5);
  30. varName[strlen(variable)-5] = '\0';
  31. std::string putEnvArg = varName;
  32. putEnvArg += "=";
  33. // what is the current value if any
  34. const char *currValue = getenv(varName);
  35. delete [] varName;
  36. // will it be set to something, then set it
  37. if (args.size() > 1 && args[1].size())
  38. {
  39. // but only if it is different from current value
  40. if (!currValue || strcmp(currValue,args[1].c_str()))
  41. {
  42. putEnvArg += args[1];
  43. cmSystemTools::PutEnv(putEnvArg.c_str());
  44. }
  45. return true;
  46. }
  47. // if it will be cleared, then clear it if it isn;t already clear
  48. if (currValue)
  49. {
  50. cmSystemTools::PutEnv(putEnvArg.c_str());
  51. }
  52. return true;
  53. }
  54. // SET (VAR) // Removes the definition of VAR.
  55. if (args.size() == 1)
  56. {
  57. this->Makefile->RemoveDefinition(args[0].c_str());
  58. return true;
  59. }
  60. // here are the remaining options
  61. // SET (VAR value )
  62. // SET (VAR CACHE TYPE "doc String" [FORCE])
  63. // SET (VAR value CACHE TYPE "doc string" [FORCE])
  64. std::string value; // optional
  65. bool cache = false; // optional
  66. bool force = false; // optional
  67. bool parentScope = false;
  68. cmCacheManager::CacheEntryType type
  69. = cmCacheManager::STRING; // required if cache
  70. const char* docstring = 0; // required if cache
  71. std::string::size_type cacheStart = 0;
  72. unsigned int ignoreLastArgs = 0;
  73. // look for PARENT_SCOPE argument
  74. if (args.size() > 1 && args[args.size()-1] == "PARENT_SCOPE")
  75. {
  76. parentScope = true;
  77. ignoreLastArgs++;
  78. }
  79. else
  80. {
  81. // look for FORCE argument
  82. if (args.size() > 4 && args[args.size()-1] == "FORCE")
  83. {
  84. force = true;
  85. ignoreLastArgs++;
  86. }
  87. // check for cache signature
  88. if (args.size() > 3 && args[args.size() - 3 - (force ? 1 : 0)] == "CACHE")
  89. {
  90. cache = true;
  91. ignoreLastArgs+=3;
  92. }
  93. }
  94. // collect any values into a single semi-colon seperated value list
  95. if(static_cast<unsigned short>(args.size()) >
  96. static_cast<unsigned short>(1 + ignoreLastArgs))
  97. {
  98. value = args[1];
  99. size_t endPos = args.size() - ignoreLastArgs;
  100. for(size_t i = 2; i < endPos; ++i)
  101. {
  102. value += ";";
  103. value += args[i];
  104. }
  105. }
  106. if (parentScope)
  107. {
  108. if (value.empty())
  109. {
  110. this->Makefile->RaiseScope(variable, 0);
  111. }
  112. else
  113. {
  114. this->Makefile->RaiseScope(variable, value.c_str());
  115. }
  116. return true;
  117. }
  118. // we should be nice and try to catch some simple screwups if the last or
  119. // next to last args are CACHE then they screwed up. If they used FORCE
  120. // without CACHE they screwed up
  121. if (args[args.size() - 1] == "CACHE" ||
  122. args.size() > 1 && args[args.size() - 2] == "CACHE" ||
  123. force && !cache)
  124. {
  125. this->SetError("given invalid arguments for CACHE mode.");
  126. return false;
  127. }
  128. if(cache)
  129. {
  130. cacheStart = args.size() - 3 - (force ? 1 : 0);
  131. type = cmCacheManager::StringToType(args[cacheStart+1].c_str());
  132. docstring = args[cacheStart+2].c_str();
  133. }
  134. // see if this is already in the cache
  135. cmCacheManager::CacheIterator it =
  136. this->Makefile->GetCacheManager()->GetCacheIterator(variable);
  137. if(!it.IsAtEnd() && (it.GetType() != cmCacheManager::UNINITIALIZED))
  138. {
  139. // if the set is trying to CACHE the value but the value
  140. // is already in the cache and the type is not internal
  141. // then leave now without setting any definitions in the cache
  142. // or the makefile
  143. if(cache && type != cmCacheManager::INTERNAL && !force)
  144. {
  145. return true;
  146. }
  147. }
  148. // if it is meant to be in the cache then define it in the cache
  149. if(cache)
  150. {
  151. this->Makefile->AddCacheDefinition(variable,
  152. value.c_str(),
  153. docstring,
  154. type, force);
  155. }
  156. else
  157. {
  158. // add the definition
  159. this->Makefile->AddDefinition(variable, value.c_str());
  160. }
  161. return true;
  162. }