cmSetCommand.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. cmCacheManager::CacheEntryType type = cmCacheManager::STRING; // required if cache
  37. const char* docstring = 0; // required if cache
  38. std::string::size_type cacheStart = 0;
  39. // check for SET(VAR v1 v2 ... vn)
  40. // and create
  41. if(args.size() > 2)
  42. {
  43. if(args[1] != "CACHE" && args[2] != "CACHE")
  44. {
  45. value = args[1];
  46. for(size_t i =2; i < args.size(); ++i)
  47. {
  48. value += ";";
  49. value += args[i];
  50. }
  51. m_Makefile->AddDefinition(variable, value.c_str());
  52. return true;
  53. }
  54. }
  55. if(args.size() == 2)
  56. {
  57. // SET (VAR value )
  58. value= args[1];
  59. }
  60. else if(args.size() == 4)
  61. {
  62. // SET (VAR CACHE TYPE "doc String")
  63. cache = true;
  64. cacheStart = 1;
  65. }
  66. else if(args.size() == 5)
  67. {
  68. // SET (VAR value CACHE TYPE "doc string")
  69. cache = true;
  70. value = args[1];
  71. cacheStart = 2;
  72. }
  73. else
  74. {
  75. std::string message;
  76. message += "Syntax error in SET:\n";
  77. message += "CACHE requires TYPE and document string SET command:\n";
  78. message += "SET (";
  79. for(std::vector<std::string>::const_iterator i = args.begin();
  80. i != args.end(); ++i)
  81. {
  82. message += *i;
  83. }
  84. message += ")\n";
  85. this->SetError(message.c_str());
  86. return false;
  87. }
  88. if(cache)
  89. {
  90. if(args[cacheStart] != "CACHE")
  91. {
  92. std::string error = "Error in arguments to cache, expected CACHE found:";
  93. error += args[cacheStart];
  94. error += "\n";
  95. this->SetError(error.c_str());
  96. return false;
  97. }
  98. type = cmCacheManager::StringToType(args[cacheStart+1].c_str());
  99. docstring = args[cacheStart+2].c_str();
  100. }
  101. // see if this is already in the cache
  102. cmCacheManager::CacheIterator it =
  103. m_Makefile->GetCacheManager()->GetCacheIterator(variable);
  104. if(!it.IsAtEnd())
  105. {
  106. // if the set is trying to CACHE the value but the value
  107. // is already in the cache and the type is not internal
  108. // then leave now without setting any definitions in the cache
  109. // or the makefile
  110. if(cache && type != cmCacheManager::INTERNAL)
  111. {
  112. return true;
  113. }
  114. }
  115. // if it is meant to be in the cache then define it in the cache
  116. if(cache)
  117. {
  118. m_Makefile->AddCacheDefinition(variable,
  119. value.c_str(),
  120. docstring,
  121. type);
  122. }
  123. else
  124. {
  125. // add the definition
  126. m_Makefile->AddDefinition(variable, value.c_str());
  127. }
  128. return true;
  129. }