cmSetCommand.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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. // SET (VAR) // Removes the definition of VAR.
  23. if (args.size() == 1)
  24. {
  25. m_Makefile->RemoveDefinition(args[0].c_str());
  26. return true;
  27. }
  28. // here are the options
  29. // SET (VAR)
  30. // SET (VAR value )
  31. // SET (VAR CACHE TYPE "doc String")
  32. // SET (VAR value CACHE TYPE "doc string")
  33. const char* variable = args[0].c_str(); // VAR is always first
  34. std::string value; // optional
  35. bool cache = false; // optional
  36. bool force = false; // optional
  37. cmCacheManager::CacheEntryType type = cmCacheManager::STRING; // required if cache
  38. const char* docstring = 0; // required if cache
  39. std::string::size_type cacheStart = 0;
  40. // check for SET(VAR v1 v2 ... vn)
  41. // and create
  42. if(args.size() > 2)
  43. {
  44. if(args[1] != "CACHE" && args[2] != "CACHE")
  45. {
  46. value = args[1];
  47. for(size_t i =2; i < args.size(); ++i)
  48. {
  49. value += ";";
  50. value += args[i];
  51. }
  52. m_Makefile->AddDefinition(variable, value.c_str());
  53. return true;
  54. }
  55. }
  56. // look for FORCE argument
  57. if (args.size() > 4 && args[args.size()-1] == "FORCE")
  58. {
  59. force = true;
  60. }
  61. std::vector<std::string>::size_type arg4, arg5;
  62. arg4 = 4 + (force ? 1 : 0);
  63. arg5 = 5 + (force ? 1 : 0);
  64. if(args.size() == 2)
  65. {
  66. // SET (VAR value )
  67. value= args[1];
  68. }
  69. else if(args.size() == arg4)
  70. {
  71. // SET (VAR CACHE TYPE "doc String")
  72. cache = true;
  73. cacheStart = 1;
  74. }
  75. else if(args.size() == arg5)
  76. {
  77. // SET (VAR value CACHE TYPE "doc string")
  78. cache = true;
  79. value = args[1];
  80. cacheStart = 2;
  81. }
  82. else
  83. {
  84. std::string message;
  85. message += "Syntax error in SET:\n";
  86. message += "CACHE requires TYPE and document string SET command:\n";
  87. message += "SET (";
  88. for(std::vector<std::string>::const_iterator i = args.begin();
  89. i != args.end(); ++i)
  90. {
  91. message += *i;
  92. }
  93. message += ")\n";
  94. this->SetError(message.c_str());
  95. return false;
  96. }
  97. if(cache)
  98. {
  99. if(args[cacheStart] != "CACHE")
  100. {
  101. std::string error = "Error in arguments to cache, expected CACHE found:";
  102. error += args[cacheStart];
  103. error += "\n";
  104. this->SetError(error.c_str());
  105. return false;
  106. }
  107. type = cmCacheManager::StringToType(args[cacheStart+1].c_str());
  108. docstring = args[cacheStart+2].c_str();
  109. }
  110. // see if this is already in the cache
  111. cmCacheManager::CacheIterator it =
  112. m_Makefile->GetCacheManager()->GetCacheIterator(variable);
  113. if(!it.IsAtEnd())
  114. {
  115. // if the set is trying to CACHE the value but the value
  116. // is already in the cache and the type is not internal
  117. // then leave now without setting any definitions in the cache
  118. // or the makefile
  119. if(cache && type != cmCacheManager::INTERNAL && !force)
  120. {
  121. return true;
  122. }
  123. }
  124. // if it is meant to be in the cache then define it in the cache
  125. if(cache)
  126. {
  127. m_Makefile->AddCacheDefinition(variable,
  128. value.c_str(),
  129. docstring,
  130. type);
  131. }
  132. else
  133. {
  134. // add the definition
  135. m_Makefile->AddDefinition(variable, value.c_str());
  136. }
  137. return true;
  138. }