cmFindProgramCommand.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. bool no_system_path = false;
  72. for (unsigned int j = 1; j < args.size(); ++j)
  73. {
  74. if(args[j] == "NAMES")
  75. {
  76. doingNames = true;
  77. foundName = true;
  78. }
  79. else if (args[j] == "PATHS")
  80. {
  81. doingNames = false;
  82. foundPath = true;
  83. }
  84. else if (args[j] == "NO_SYSTEM_PATH")
  85. {
  86. no_system_path = true;
  87. }
  88. else
  89. {
  90. if(doingNames)
  91. {
  92. names.push_back(args[j]);
  93. }
  94. else
  95. {
  96. cmSystemTools::ExpandRegistryValues(args[j]);
  97. // Glob the entry in case of wildcards.
  98. cmSystemTools::GlobDirs(args[j].c_str(), path);
  99. }
  100. }
  101. }
  102. // if it is not in the cache, then search the system path
  103. // add any user specified paths
  104. if(!foundPath && !foundName)
  105. {
  106. path.clear();
  107. names.clear();
  108. names.push_back(args[1]);
  109. for (unsigned int j = 2; j < args.size(); j++)
  110. {
  111. // expand variables
  112. std::string exp = args[j];
  113. cmSystemTools::ExpandRegistryValues(exp);
  114. // Glob the entry in case of wildcards.
  115. cmSystemTools::GlobDirs(exp.c_str(), path);
  116. }
  117. }
  118. for(std::vector<std::string>::iterator i = names.begin();
  119. i != names.end() ; ++i)
  120. {
  121. // Try to find the program.
  122. std::string result = cmSystemTools::FindProgram(i->c_str(),
  123. path,
  124. no_system_path);
  125. if(result != "")
  126. {
  127. // Save the value in the cache
  128. m_Makefile->AddCacheDefinition(define,
  129. result.c_str(),
  130. doc.c_str(),
  131. cmCacheManager::FILEPATH);
  132. return true;
  133. }
  134. }
  135. m_Makefile->AddCacheDefinition(args[0].c_str(),
  136. "NOTFOUND",
  137. doc.c_str(),
  138. cmCacheManager::FILEPATH);
  139. return true;
  140. }