cmFindProgramCommand.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. size_t size = argsIn.size();
  27. std::vector<std::string> argst;
  28. for(unsigned int j = 0; j < size; ++j)
  29. {
  30. if(argsIn[j] != "DOC")
  31. {
  32. argst.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> args;
  44. cmSystemTools::ExpandListArguments(argst, args);
  45. std::vector<std::string>::iterator i = args.begin();
  46. // Use the first argument as the name of something to be defined
  47. const char* define = (*i).c_str();
  48. i++; // move iterator to next arg
  49. // Now check and see if the value has been stored in the cache
  50. // already, if so use that value and don't look for the program
  51. const char* cacheValue
  52. = m_Makefile->GetDefinition(define);
  53. if(cacheValue && strcmp(cacheValue, "NOTFOUND"))
  54. {
  55. return true;
  56. }
  57. if(cacheValue)
  58. {
  59. cmCacheManager::CacheEntry* e =
  60. cmCacheManager::GetInstance()->GetCacheEntry(args[0].c_str());
  61. if(e)
  62. {
  63. doc = e->m_HelpString;
  64. }
  65. }
  66. std::vector<std::string> path;
  67. std::vector<std::string> names;
  68. bool foundName = false;
  69. bool foundPath = false;
  70. bool doingNames = true;
  71. for (unsigned int j = 1; j < args.size(); ++j)
  72. {
  73. if(args[j] == "NAMES")
  74. {
  75. doingNames = true;
  76. foundName = true;
  77. }
  78. else if (args[j] == "PATHS")
  79. {
  80. doingNames = false;
  81. foundPath = true;
  82. }
  83. else
  84. {
  85. if(doingNames)
  86. {
  87. names.push_back(args[j]);
  88. }
  89. else
  90. {
  91. cmSystemTools::ExpandRegistryValues(args[j]);
  92. // Glob the entry in case of wildcards.
  93. cmSystemTools::GlobDirs(args[j].c_str(), path);
  94. }
  95. }
  96. }
  97. // if it is not in the cache, then search the system path
  98. // add any user specified paths
  99. if(!foundPath && !foundName)
  100. {
  101. path.clear();
  102. names.clear();
  103. names.push_back(args[1]);
  104. for (unsigned int j = 2; j < args.size(); j++)
  105. {
  106. // expand variables
  107. std::string exp = args[j];
  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. }