cmSetCommand.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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::InitialPass(std::vector<std::string> const& args)
  16. {
  17. if(args.size() < 1 )
  18. {
  19. this->SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. // watch for ENV signatures
  23. const char* variable = args[0].c_str(); // VAR is always first
  24. if (!strncmp(variable,"ENV{",4) && strlen(variable) > 5)
  25. {
  26. // what is the variable name
  27. char *varName = new char [strlen(variable)];
  28. strncpy(varName,variable+4,strlen(variable)-5);
  29. varName[strlen(variable)-5] = '\0';
  30. std::string putEnvArg = varName;
  31. putEnvArg += "=";
  32. // what is the current value if any
  33. const char *currValue = getenv(varName);
  34. // will it be set to something, then set it
  35. if (args.size() > 1 && args[1].size())
  36. {
  37. // but only if it is different from current value
  38. if (!currValue || strcmp(currValue,args[1].c_str()))
  39. {
  40. putEnvArg += args[1];
  41. cmSystemTools::PutEnv(putEnvArg.c_str());
  42. }
  43. return true;
  44. }
  45. // if it will be cleared, then clear it if it isn;t already clear
  46. if (currValue)
  47. {
  48. cmSystemTools::PutEnv(putEnvArg.c_str());
  49. }
  50. return true;
  51. }
  52. // SET (VAR) // Removes the definition of VAR.
  53. if (args.size() == 1)
  54. {
  55. m_Makefile->RemoveDefinition(args[0].c_str());
  56. return true;
  57. }
  58. // here are the remaining options
  59. // SET (VAR value )
  60. // SET (VAR CACHE TYPE "doc String" [FORCE])
  61. // SET (VAR value CACHE TYPE "doc string" [FORCE])
  62. std::string value; // optional
  63. bool cache = false; // optional
  64. bool force = false; // optional
  65. cmCacheManager::CacheEntryType type
  66. = cmCacheManager::STRING; // required if cache
  67. const char* docstring = 0; // required if cache
  68. std::string::size_type cacheStart = 0;
  69. // look for FORCE argument
  70. if (args.size() > 4 && args[args.size()-1] == "FORCE")
  71. {
  72. force = true;
  73. }
  74. // check for cache signature
  75. if (args.size() > 3 && args[args.size() - 3 - (force ? 1 : 0)] == "CACHE")
  76. {
  77. cache = true;
  78. }
  79. // collect any values into a single semi-colon seperated value list
  80. if(args.size() >
  81. static_cast<unsigned short>(1 + (cache ? 3 : 0) + (force ? 1 : 0)))
  82. {
  83. value = args[1];
  84. size_t endPos = args.size() - (cache ? 3 : 0) - (force ? 1 : 0);
  85. for(size_t i = 2; i < endPos; ++i)
  86. {
  87. value += ";";
  88. value += args[i];
  89. }
  90. }
  91. // we should be nice and try to catch some simple screwups if the last or
  92. // next to last args are CACHE then they screwed up. If they used FORCE
  93. // without CACHE they screwed up
  94. if (args[args.size() - 1] == "CACHE" ||
  95. args.size() > 1 && args[args.size() - 2] == "CACHE" ||
  96. force && !cache)
  97. {
  98. std::string message;
  99. message += "Syntax error in SET:\n";
  100. message += "See the help for the SET command:\n";
  101. message += "SET (";
  102. for(std::vector<std::string>::const_iterator i = args.begin();
  103. i != args.end(); ++i)
  104. {
  105. message += *i;
  106. }
  107. message += ")\n";
  108. this->SetError(message.c_str());
  109. return false;
  110. }
  111. if(cache)
  112. {
  113. cacheStart = args.size() - 3 - (force ? 1 : 0);
  114. type = cmCacheManager::StringToType(args[cacheStart+1].c_str());
  115. docstring = args[cacheStart+2].c_str();
  116. }
  117. // see if this is already in the cache
  118. cmCacheManager::CacheIterator it =
  119. m_Makefile->GetCacheManager()->GetCacheIterator(variable);
  120. if(!it.IsAtEnd() && (it.GetType() != cmCacheManager::UNINITIALIZED))
  121. {
  122. // if the set is trying to CACHE the value but the value
  123. // is already in the cache and the type is not internal
  124. // then leave now without setting any definitions in the cache
  125. // or the makefile
  126. if(cache && type != cmCacheManager::INTERNAL && !force)
  127. {
  128. return true;
  129. }
  130. }
  131. // if it is meant to be in the cache then define it in the cache
  132. if(cache)
  133. {
  134. m_Makefile->AddCacheDefinition(variable,
  135. value.c_str(),
  136. docstring,
  137. type);
  138. }
  139. else
  140. {
  141. // add the definition
  142. m_Makefile->AddDefinition(variable, value.c_str());
  143. }
  144. return true;
  145. }