cmFindProgramCommand.cxx 4.2 KB

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