cmFindProgramCommand.cxx 4.1 KB

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