cmSetCommand.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmSetCommand.h"
  33. // cmSetCommand
  34. bool cmSetCommand::Invoke(std::vector<std::string>& args)
  35. {
  36. if(args.size() < 1 )
  37. {
  38. this->SetError("called with incorrect number of arguments");
  39. return false;
  40. }
  41. // SET (VAR ) // this is a no-op
  42. if (args.size() == 1)
  43. {
  44. return true;
  45. }
  46. // here are the options
  47. // SET (VAR)
  48. // SET (VAR value )
  49. // SET (VAR CACHE TYPE "doc String")
  50. // SET (VAR value CACHE TYPE "doc string")
  51. const char* variable = args[0].c_str(); // VAR is always first
  52. std::string value; // optional
  53. bool cache = false; // optional
  54. cmCacheManager::CacheEntryType type = cmCacheManager::STRING; // required if cache
  55. const char* docstring = 0; // required if cache
  56. std::string::size_type cacheStart = 0;
  57. if(args.size() == 4)
  58. {
  59. // SET (VAR CACHE TYPE "doc String")
  60. cache = true;
  61. cacheStart = 1;
  62. }
  63. else if(args.size() == 5)
  64. {
  65. // SET (VAR value CACHE TYPE "doc string")
  66. cache = true;
  67. value = args[1];
  68. cacheStart = 2;
  69. }
  70. if(cache)
  71. {
  72. if(args[cacheStart] != "CACHE")
  73. {
  74. std::string error = "Error in arguments to cache, expected CACHE found:";
  75. error += args[cacheStart];
  76. this->SetError(error.c_str());
  77. return false;
  78. }
  79. type = cmCacheManager::StringToType(args[cacheStart+1].c_str());
  80. docstring = args[cacheStart+2].c_str();
  81. }
  82. // always expand the first argument
  83. m_Makefile->ExpandVariablesInString(value);
  84. // get the current cache value for the variable
  85. const char* cacheValue =
  86. cmCacheManager::GetInstance()->GetCacheValue(variable);
  87. if(cacheValue)
  88. {
  89. // if it is not a cached value, or it is a cached
  90. // value that is not internal keep the value found
  91. // in the cache
  92. if(cache && type != cmCacheManager::INTERNAL)
  93. {
  94. return true;
  95. }
  96. }
  97. // add the definition
  98. m_Makefile->AddDefinition(variable, value.c_str());
  99. // if it is meant to be in the cache then define it in the cache
  100. if(cache)
  101. {
  102. cmCacheManager::GetInstance()->AddCacheEntry(variable,
  103. value.c_str(),
  104. docstring,
  105. type);
  106. }
  107. return true;
  108. }