cmFindFileCommand.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "cmFindFileCommand.h"
  14. #include "cmCacheManager.h"
  15. #include "cmGlob.h"
  16. #include <stdlib.h>
  17. // cmFindFileCommand
  18. bool cmFindFileCommand::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 helpString = "Where can the ";
  26. helpString += argsIn[1] + " file be found";
  27. size_t size = argsIn.size();
  28. std::vector<std::string> args;
  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. std::vector<std::string>::const_iterator i = args.begin();
  45. // Use the first argument as the name of something to be defined
  46. const char* define = (*i).c_str();
  47. i++; // move iterator to next arg
  48. // Now check and see if the value has been stored in the cache
  49. // already, if so use that value and don't look for the program
  50. const char* cacheValue
  51. = m_Makefile->GetDefinition(define);
  52. if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
  53. {
  54. return true;
  55. }
  56. // if it is not in the cache, then search the system path
  57. std::vector<std::string> path;
  58. // add any user specified paths
  59. for (unsigned int j = 2; j < args.size(); j++)
  60. {
  61. // Glob the entry in case of wildcards.
  62. cmSystemTools::GlobDirs(args[j].c_str(), path);
  63. }
  64. cmSystemTools::GetPath(path, "CMAKE_LIBRARY_PATH");
  65. // add the standard path
  66. cmSystemTools::GetPath(path);
  67. for(unsigned int k=0; k < path.size(); k++)
  68. {
  69. std::string tryPath = path[k];
  70. tryPath += "/";
  71. tryPath += *i;
  72. if(cmSystemTools::FileExists(tryPath.c_str()))
  73. {
  74. // Save the value in the cache
  75. m_Makefile->AddCacheDefinition(define,
  76. tryPath.c_str(),
  77. helpString.c_str(),
  78. cmCacheManager::FILEPATH);
  79. return true;
  80. }
  81. }
  82. #if defined (__APPLE__)
  83. cmStdString fpath = this->FindHeaderInFrameworks(path, args[0].c_str(), args[1].c_str());
  84. if(fpath.size())
  85. {
  86. m_Makefile->AddCacheDefinition(args[0].c_str(),
  87. fpath.c_str(),
  88. helpString.c_str(),
  89. cmCacheManager::FILEPATH);
  90. return true;
  91. }
  92. #endif
  93. std::string s = args[0] + "-NOTFOUND";
  94. m_Makefile->AddCacheDefinition(args[0].c_str(),
  95. s.c_str(),
  96. helpString.c_str(),
  97. cmCacheManager::FILEPATH);
  98. return true;
  99. }
  100. cmStdString cmFindFileCommand::FindHeaderInFrameworks(
  101. std::vector<std::string> path,
  102. const char* defineVar,
  103. const char* file)
  104. {
  105. (void)defineVar;
  106. #ifndef __APPLE__
  107. (void)path;
  108. (void)file;
  109. return cmStdString("");
  110. #else
  111. cmStdString fileName = file;
  112. cmStdString frameWorkName;
  113. cmStdString::size_type pos = fileName.find("/");
  114. std::cerr << "ff " << fileName << " " << pos << "\n";
  115. if(pos != fileName.npos)
  116. {
  117. // remove the name from the slash;
  118. fileName = fileName.substr(pos+1);
  119. frameWorkName = file;
  120. frameWorkName = frameWorkName.substr(0, frameWorkName.size()-fileName.size()-1);
  121. // if the framework has a path in it then just use the filename
  122. std::cerr << fileName << " " << frameWorkName << "\n";
  123. if(frameWorkName.find("/") != frameWorkName.npos)
  124. {
  125. fileName = file;
  126. frameWorkName = "";
  127. }
  128. }
  129. path.push_back("~/Library/Frameworks");
  130. path.push_back("/Library/Frameworks");
  131. path.push_back("/System/Library/Frameworks");
  132. path.push_back("/Network/Library/Frameworks");
  133. for( std::vector<std::string>::iterator i = path.begin();
  134. i != path.end(); ++i)
  135. {
  136. if(frameWorkName.size())
  137. {
  138. std::string fpath = *i;
  139. fpath += "/";
  140. fpath += frameWorkName;
  141. fpath += ".framework";
  142. std::string intPath = fpath;
  143. intPath += "/Headers/";
  144. intPath += fileName;
  145. std::cerr << "try " << intPath << "\n";
  146. if(cmSystemTools::FileExists(intPath.c_str()))
  147. {
  148. return fpath;
  149. }
  150. }
  151. cmStdString glob = *i;
  152. glob += "/*/Headers/";
  153. glob += file;
  154. cmGlob globIt;
  155. globIt.FindFiles(glob);
  156. std::vector<std::string> files = globIt.GetFiles();
  157. if(files.size())
  158. {
  159. cmStdString fheader = cmSystemTools::CollapseFullPath(files[0].c_str());
  160. fheader = cmSystemTools::GetFilenamePath(fheader);
  161. return fheader;
  162. }
  163. }
  164. return cmStdString("");
  165. #endif
  166. }