You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.9 KiB

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