cmSetCommand.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. if(args.size() == 2)
  39. {
  40. // SET (VAR value )
  41. value= args[1];
  42. }
  43. else if(args.size() == 4)
  44. {
  45. // SET (VAR CACHE TYPE "doc String")
  46. cache = true;
  47. cacheStart = 1;
  48. }
  49. else if(args.size() == 5)
  50. {
  51. // SET (VAR value CACHE TYPE "doc string")
  52. cache = true;
  53. value = args[1];
  54. cacheStart = 2;
  55. }
  56. else
  57. {
  58. std::string message;
  59. message += "Syntax error in SET:\n";
  60. message += "CACHE requires TYPE and document string SET command:\n";
  61. message += "SET (";
  62. for(std::vector<std::string>::const_iterator i = args.begin();
  63. i != args.end(); ++i)
  64. {
  65. message += *i;
  66. }
  67. message += ")\n";
  68. this->SetError(message.c_str());
  69. return false;
  70. }
  71. if(cache)
  72. {
  73. if(args[cacheStart] != "CACHE")
  74. {
  75. std::string error = "Error in arguments to cache, expected CACHE found:";
  76. error += args[cacheStart];
  77. error += "\n";
  78. this->SetError(error.c_str());
  79. return false;
  80. }
  81. type = cmCacheManager::StringToType(args[cacheStart+1].c_str());
  82. docstring = args[cacheStart+2].c_str();
  83. }
  84. // always expand the first argument
  85. m_Makefile->ExpandVariablesInString(value);
  86. // get the current cache value for the variable
  87. const char* cacheValue =
  88. m_Makefile->GetDefinition(variable);
  89. if(cacheValue)
  90. {
  91. // if it is not a cached value, or it is a cached
  92. // value that is not internal keep the value found
  93. // in the cache
  94. if(cache && type != cmCacheManager::INTERNAL)
  95. {
  96. return true;
  97. }
  98. }
  99. // if it is meant to be in the cache then define it in the cache
  100. if(cache)
  101. {
  102. m_Makefile->AddCacheDefinition(variable,
  103. value.c_str(),
  104. docstring,
  105. type);
  106. }
  107. else
  108. {
  109. // add the definition
  110. m_Makefile->AddDefinition(variable, value.c_str());
  111. }
  112. return true;
  113. }