cmFindProgramCommand.cxx 4.1 KB

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