cmSetCommand.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 ) // this is a no-op
  23. if (args.size() == 1)
  24. {
  25. return true;
  26. }
  27. // here are the options
  28. // SET (VAR)
  29. // SET (VAR value )
  30. // SET (VAR CACHE TYPE "doc String")
  31. // SET (VAR value CACHE TYPE "doc string")
  32. const char* variable = args[0].c_str(); // VAR is always first
  33. std::string value; // optional
  34. bool cache = false; // optional
  35. cmCacheManager::CacheEntryType type = cmCacheManager::STRING; // required if cache
  36. const char* docstring = 0; // required if cache
  37. std::string::size_type cacheStart = 0;
  38. // check for SET(VAR v1 v2 ... vn)
  39. // and create
  40. if(args.size() > 2)
  41. {
  42. if(args[1] != "CACHE" && args[2] != "CACHE")
  43. {
  44. value = args[1];
  45. for(int i =2; i < args.size(); ++i)
  46. {
  47. value += ";";
  48. value += args[i];
  49. }
  50. m_Makefile->AddDefinition(variable, value.c_str());
  51. return true;
  52. }
  53. }
  54. if(args.size() == 2)
  55. {
  56. // SET (VAR value )
  57. value= args[1];
  58. }
  59. else if(args.size() == 4)
  60. {
  61. // SET (VAR CACHE TYPE "doc String")
  62. cache = true;
  63. cacheStart = 1;
  64. }
  65. else if(args.size() == 5)
  66. {
  67. // SET (VAR value CACHE TYPE "doc string")
  68. cache = true;
  69. value = args[1];
  70. cacheStart = 2;
  71. }
  72. else
  73. {
  74. std::string message;
  75. message += "Syntax error in SET:\n";
  76. message += "CACHE requires TYPE and document string SET command:\n";
  77. message += "SET (";
  78. for(std::vector<std::string>::const_iterator i = args.begin();
  79. i != args.end(); ++i)
  80. {
  81. message += *i;
  82. }
  83. message += ")\n";
  84. this->SetError(message.c_str());
  85. return false;
  86. }
  87. if(cache)
  88. {
  89. if(args[cacheStart] != "CACHE")
  90. {
  91. std::string error = "Error in arguments to cache, expected CACHE found:";
  92. error += args[cacheStart];
  93. error += "\n";
  94. this->SetError(error.c_str());
  95. return false;
  96. }
  97. type = cmCacheManager::StringToType(args[cacheStart+1].c_str());
  98. docstring = args[cacheStart+2].c_str();
  99. }
  100. // get the current cache value for the variable
  101. const char* cacheValue =
  102. m_Makefile->GetDefinition(variable);
  103. if(cacheValue)
  104. {
  105. // if it is not a cached value, or it is a cached
  106. // value that is not internal keep the value found
  107. // in the cache
  108. if(cache && type != cmCacheManager::INTERNAL)
  109. {
  110. return true;
  111. }
  112. }
  113. // if it is meant to be in the cache then define it in the cache
  114. if(cache)
  115. {
  116. m_Makefile->AddCacheDefinition(variable,
  117. value.c_str(),
  118. docstring,
  119. type);
  120. }
  121. else
  122. {
  123. // add the definition
  124. m_Makefile->AddDefinition(variable, value.c_str());
  125. }
  126. return true;
  127. }