cmFindProgramCommand.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "cmFindProgramCommand.h"
  14. #include "cmCacheManager.h"
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. // cmFindProgramCommand
  18. bool cmFindProgramCommand::InitialPass(std::vector<std::string> const& argsIn)
  19. {
  20. if(argsIn.size() < 2 )
  21. {
  22. this->SetError("called with incorrect number of arguments");
  23. return false;
  24. }
  25. std::string doc = "Path to a program.";
  26. unsigned int size = argsIn.size();
  27. std::vector<std::string> args;
  28. for(unsigned int j = 0; j < size; ++j)
  29. {
  30. if(argsIn[j] != "DOC")
  31. {
  32. args.push_back(argsIn[j]);
  33. }
  34. else
  35. {
  36. if(j+1 < size)
  37. {
  38. doc = argsIn[j+1];
  39. }
  40. break;
  41. }
  42. }
  43. std::vector<std::string>::iterator i = args.begin();
  44. // Use the first argument as the name of something to be defined
  45. const char* define = (*i).c_str();
  46. i++; // move iterator to next arg
  47. // Now check and see if the value has been stored in the cache
  48. // already, if so use that value and don't look for the program
  49. const char* cacheValue
  50. = m_Makefile->GetDefinition(define);
  51. if(cacheValue && strcmp(cacheValue, "NOTFOUND"))
  52. {
  53. return true;
  54. }
  55. if(cacheValue)
  56. {
  57. cmCacheManager::CacheEntry* e =
  58. cmCacheManager::GetInstance()->GetCacheEntry(args[0].c_str());
  59. if(e)
  60. {
  61. doc = e->m_HelpString;
  62. }
  63. }
  64. std::vector<std::string> path;
  65. std::vector<std::string> names;
  66. bool foundName = false;
  67. bool foundPath = false;
  68. bool doingNames = true;
  69. for (unsigned int j = 1; j < args.size(); ++j)
  70. {
  71. if(args[j] == "NAMES")
  72. {
  73. doingNames = true;
  74. foundName = true;
  75. }
  76. else if (args[j] == "PATHS")
  77. {
  78. doingNames = false;
  79. foundPath = true;
  80. }
  81. else
  82. {
  83. m_Makefile->ExpandVariablesInString(args[j]);
  84. if(doingNames)
  85. {
  86. names.push_back(args[j]);
  87. }
  88. else
  89. {
  90. cmSystemTools::ExpandRegistryValues(args[j]);
  91. // Glob the entry in case of wildcards.
  92. cmSystemTools::GlobDirs(args[j].c_str(), path);
  93. }
  94. }
  95. }
  96. // if it is not in the cache, then search the system path
  97. // add any user specified paths
  98. if(!foundPath && !foundName)
  99. {
  100. path.clear();
  101. names.clear();
  102. names.push_back(args[1]);
  103. for (unsigned int j = 2; j < args.size(); j++)
  104. {
  105. // expand variables
  106. std::string exp = args[j];
  107. m_Makefile->ExpandVariablesInString(exp);
  108. cmSystemTools::ExpandRegistryValues(exp);
  109. // Glob the entry in case of wildcards.
  110. cmSystemTools::GlobDirs(exp.c_str(), path);
  111. }
  112. }
  113. for(std::vector<std::string>::iterator i = names.begin();
  114. i != names.end() ; ++i)
  115. {
  116. // Try to find the program.
  117. std::string result = cmSystemTools::FindProgram(i->c_str(), path);
  118. if(result != "")
  119. {
  120. // Save the value in the cache
  121. m_Makefile->AddCacheDefinition(define,
  122. result.c_str(),
  123. doc.c_str(),
  124. cmCacheManager::FILEPATH);
  125. return true;
  126. }
  127. }
  128. m_Makefile->AddCacheDefinition(args[0].c_str(),
  129. "NOTFOUND",
  130. doc.c_str(),
  131. cmCacheManager::FILEPATH);
  132. return true;
  133. }