cmSetCommand.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // 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 remaining options
  29. // SET (VAR value )
  30. // SET (VAR CACHE TYPE "doc String" [FORCE])
  31. // SET (VAR value CACHE TYPE "doc string" [FORCE])
  32. const char* variable = args[0].c_str(); // VAR is always first
  33. std::string value; // optional
  34. bool cache = false; // optional
  35. bool force = false; // optional
  36. cmCacheManager::CacheEntryType type
  37. = cmCacheManager::STRING; // required if cache
  38. const char* docstring = 0; // required if cache
  39. std::string::size_type cacheStart = 0;
  40. // look for FORCE argument
  41. if (args.size() > 4 && args[args.size()-1] == "FORCE")
  42. {
  43. force = true;
  44. }
  45. // check for cache signature
  46. if (args.size() > 3 && args[args.size() - 3 - (force ? 1 : 0)] == "CACHE")
  47. {
  48. cache = true;
  49. }
  50. // collect any values into a single semi-colon seperated value list
  51. if(args.size() >
  52. static_cast<unsigned short>(1 + (cache ? 3 : 0) + (force ? 1 : 0)))
  53. {
  54. value = args[1];
  55. size_t endPos = args.size() - (cache ? 3 : 0) - (force ? 1 : 0);
  56. for(size_t i = 2; i < endPos; ++i)
  57. {
  58. value += ";";
  59. value += args[i];
  60. }
  61. }
  62. // we should be nice and try to catch some simple screwups if the last or
  63. // next to last args are CACHE then they screwed up. If they used FORCE
  64. // without CACHE they screwed up
  65. if (args[args.size() - 1] == "CACHE" ||
  66. args.size() > 1 && args[args.size() - 2] == "CACHE" ||
  67. force && !cache)
  68. {
  69. std::string message;
  70. message += "Syntax error in SET:\n";
  71. message += "See the help for the SET command:\n";
  72. message += "SET (";
  73. for(std::vector<std::string>::const_iterator i = args.begin();
  74. i != args.end(); ++i)
  75. {
  76. message += *i;
  77. }
  78. message += ")\n";
  79. this->SetError(message.c_str());
  80. return false;
  81. }
  82. if(cache)
  83. {
  84. cacheStart = args.size() - 3 - (force ? 1 : 0);
  85. type = cmCacheManager::StringToType(args[cacheStart+1].c_str());
  86. docstring = args[cacheStart+2].c_str();
  87. }
  88. // see if this is already in the cache
  89. cmCacheManager::CacheIterator it =
  90. m_Makefile->GetCacheManager()->GetCacheIterator(variable);
  91. if(!it.IsAtEnd() && (it.GetType() != cmCacheManager::UNINITIALIZED))
  92. {
  93. // if the set is trying to CACHE the value but the value
  94. // is already in the cache and the type is not internal
  95. // then leave now without setting any definitions in the cache
  96. // or the makefile
  97. if(cache && type != cmCacheManager::INTERNAL && !force)
  98. {
  99. return true;
  100. }
  101. }
  102. // if it is meant to be in the cache then define it in the cache
  103. if(cache)
  104. {
  105. m_Makefile->AddCacheDefinition(variable,
  106. value.c_str(),
  107. docstring,
  108. type);
  109. }
  110. else
  111. {
  112. // add the definition
  113. m_Makefile->AddDefinition(variable, value.c_str());
  114. }
  115. return true;
  116. }