cmFindPathCommand.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "cmFindPathCommand.h"
  14. #include "cmCacheManager.h"
  15. // cmFindPathCommand
  16. bool cmFindPathCommand::InitialPass(std::vector<std::string> const& argsIn)
  17. {
  18. if(argsIn.size() < 2)
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. // Now check and see if the value has been stored in the cache
  24. // already, if so use that value and don't look for the program
  25. std::string helpString = "What is the path where the file ";
  26. helpString += argsIn[1] + " can be found";
  27. std::vector<std::string> args;
  28. size_t size = argsIn.size();
  29. for(unsigned int j = 0; j < size; ++j)
  30. {
  31. if(argsIn[j] != "DOC")
  32. {
  33. args.push_back(argsIn[j]);
  34. }
  35. else
  36. {
  37. if(j+1 < size)
  38. {
  39. helpString = argsIn[j+1];
  40. }
  41. break;
  42. }
  43. }
  44. const char* cacheValue
  45. = m_Makefile->GetDefinition(args[0].c_str());
  46. if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
  47. {
  48. return true;
  49. }
  50. if(cacheValue)
  51. {
  52. cmCacheManager::CacheIterator it =
  53. m_Makefile->GetCacheManager()->GetCacheIterator(args[0].c_str());
  54. if(!it.IsAtEnd())
  55. {
  56. const char* hs = it.GetProperty("HELPSTRING");
  57. helpString = hs?hs:"(none)";
  58. }
  59. }
  60. std::vector<std::string> path;
  61. // add any user specified paths
  62. for (unsigned int j = 2; j < args.size(); j++)
  63. {
  64. // expand variables
  65. std::string exp = args[j];
  66. cmSystemTools::ExpandRegistryValues(exp);
  67. // Glob the entry in case of wildcards.
  68. cmSystemTools::GlobDirs(exp.c_str(), path);
  69. }
  70. cmSystemTools::GetPath(path, "CMAKE_INCLUDE_PATH");
  71. // add the standard path
  72. cmSystemTools::GetPath(path);
  73. unsigned int k;
  74. for(k=0; k < path.size(); k++)
  75. {
  76. std::string tryPath = path[k];
  77. tryPath += "/";
  78. tryPath += args[1];
  79. if(cmSystemTools::FileExists(tryPath.c_str()))
  80. {
  81. path[k] = cmSystemTools::CollapseFullPath(path[k].c_str());
  82. if(path[k].size() && path[k][path[k].size()-1] == '/')
  83. {
  84. path[k] = path[k].substr(0, path[k].size()-1);
  85. }
  86. m_Makefile->AddCacheDefinition(args[0].c_str(),
  87. path[k].c_str(),
  88. helpString.c_str(),
  89. cmCacheManager::PATH);
  90. return true;
  91. }
  92. }
  93. #if defined (__APPLE__)
  94. cmStdString fpath = this->FindHeaderInFrameworks(path, args[0].c_str(), args[1].c_str());
  95. if(fpath.size())
  96. {
  97. m_Makefile->AddCacheDefinition(args[0].c_str(),
  98. fpath.c_str(),
  99. helpString.c_str(),
  100. cmCacheManager::FILEPATH);
  101. return true;
  102. }
  103. #endif
  104. m_Makefile->AddCacheDefinition(args[0].c_str(),
  105. (args[0] + "-NOTFOUND").c_str(),
  106. helpString.c_str(),
  107. cmCacheManager::PATH);
  108. return true;
  109. }
  110. cmStdString cmFindPathCommand::FindHeaderInFrameworks(
  111. std::vector<std::string> path,
  112. const char* defineVar,
  113. const char* file)
  114. {
  115. (void)defineVar;
  116. #ifndef __APPLE__
  117. (void)path;
  118. (void)file;
  119. return cmStdString("");
  120. #else
  121. cmStdString fileName = file;
  122. cmStdString frameWorkName;
  123. cmStdString::size_type pos = fileName.find("/");
  124. std::cerr << "ff " << fileName << " " << pos << "\n";
  125. if(pos != fileName.npos)
  126. {
  127. // remove the name from the slash;
  128. fileName = fileName.substr(pos+1);
  129. frameWorkName = file;
  130. frameWorkName = frameWorkName.substr(0, frameWorkName.size()-fileName.size()-1);
  131. // if the framework has a path in it then just use the filename
  132. std::cerr << fileName << " " << frameWorkName << "\n";
  133. if(frameWorkName.find("/") != frameWorkName.npos)
  134. {
  135. fileName = file;
  136. frameWorkName = "";
  137. }
  138. }
  139. path.push_back("~/Library/Frameworks");
  140. path.push_back("/Library/Frameworks");
  141. path.push_back("/System/Library/Frameworks");
  142. path.push_back("/Network/Library/Frameworks");
  143. for( std::vector<std::string>::iterator i = path.begin();
  144. i != path.end(); ++i)
  145. {
  146. if(frameWorkName.size())
  147. {
  148. std::string fpath = *i;
  149. fpath += "/";
  150. fpath += frameWorkName;
  151. fpath += ".framework";
  152. std::string intPath = fpath;
  153. intPath += "/Headers/";
  154. intPath += fileName;
  155. std::cerr << "try " << intPath << "\n";
  156. if(cmSystemTools::FileExists(intPath.c_str()))
  157. {
  158. return fpath;
  159. }
  160. }
  161. cmStdString glob = *i;
  162. glob += "/*/Headers/";
  163. glob += file;
  164. cmGlob globIt;
  165. globIt.FindFiles(glob);
  166. std::vector<std::string> files = globIt.GetFiles();
  167. if(files.size())
  168. {
  169. cmStdString fheader = cmSystemTools::CollapseFullPath(files[0].c_str());
  170. fheader = cmSystemTools::GetFilenamePath(fheader);
  171. return fheader;
  172. }
  173. }
  174. return cmStdString("");
  175. #endif
  176. }