cmMakeDepend.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #ifdef _MSC_VER
  2. #pragma warning ( disable : 4786 )
  3. #endif
  4. #include "cmMakeDepend.h"
  5. #include <fstream>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <functional>
  9. void cmMakeDepend::SetIncludeRegularExpression(const char* prefix)
  10. {
  11. m_IncludeFileRegularExpression.compile(prefix);
  12. }
  13. cmMakeDepend::cmMakeDepend()
  14. {
  15. m_Verbose = false;
  16. m_IncludeFileRegularExpression.compile("^itk|^vtk|^vnl|^vcl|^f2c");
  17. }
  18. cmMakeDepend::~cmMakeDepend()
  19. {
  20. for(DependArray::iterator i = m_DependInformation.begin();
  21. i != m_DependInformation.end(); ++i)
  22. {
  23. delete *i;
  24. }
  25. m_DependInformation.clear();
  26. }
  27. // Set the makefile that depends will be made from.
  28. // The pointer is kept so the cmClassFile array can
  29. // be updated with the depend information.
  30. void cmMakeDepend::SetMakefile(cmMakefile* makefile)
  31. {
  32. m_Makefile = makefile;
  33. int index = 0;
  34. std::vector<cmClassFile>::iterator i = makefile->m_Classes.begin();
  35. while(i != makefile->m_Classes.end())
  36. {
  37. if(!(*i).m_HeaderFileOnly)
  38. {
  39. cmDependInformation* info = new cmDependInformation;
  40. info->m_FullPath = this->FullPath((*i).m_FullPath.c_str());
  41. this->AddFileToSearchPath(info->m_FullPath.c_str());
  42. info->m_IncludeName = (*i).m_FullPath;
  43. m_DependInformation.push_back(info);
  44. info->m_ClassFileIndex = index;
  45. }
  46. ++i;
  47. index++;
  48. }
  49. }
  50. // Compute the depends.
  51. void cmMakeDepend::DoDepends()
  52. {
  53. // The size of the m_DependInformation will change as
  54. // Depend is called so do not use an iterater but rather
  55. // depend on the size of the array.
  56. int j = 0;
  57. while(j != m_DependInformation.size())
  58. {
  59. cmDependInformation* info = m_DependInformation[j];
  60. // compute the depend information for the info object
  61. // this may add more objects to the m_DependInformation
  62. // array
  63. this->Depend(info);
  64. ++j;
  65. }
  66. // Now update the depend information for each cmClassFile
  67. // in the cmMakefile m_Makefile
  68. for(DependArray::iterator i = m_DependInformation.begin();
  69. i != m_DependInformation.end(); ++i)
  70. {
  71. cmDependInformation* info = *i;
  72. // Remove duplicate depends
  73. info->RemoveDuplicateIndices();
  74. std::vector<cmClassFile>::iterator j = m_Makefile->m_Classes.begin();
  75. // find the class
  76. if(info->m_ClassFileIndex != -1)
  77. {
  78. cmClassFile& cfile = m_Makefile->m_Classes[info->m_ClassFileIndex];
  79. for( std::vector<int>::iterator indx = info->m_Indices.begin();
  80. indx != info->m_Indices.end(); ++indx)
  81. {
  82. cfile.m_Depends.push_back(m_DependInformation[*indx]->m_FullPath);
  83. }
  84. }
  85. }
  86. }
  87. // This function actually reads the file
  88. // and scans it for #include directives
  89. void cmMakeDepend::Depend(cmDependInformation* info)
  90. {
  91. const char* path = info->m_FullPath.c_str();
  92. if(!path)
  93. {
  94. std::cerr << "no full path for object" << std::endl;
  95. return;
  96. }
  97. std::ifstream fin(path);
  98. if(!fin)
  99. {
  100. std::cerr << "error can not open " << info->m_FullPath << std::endl;
  101. return;
  102. }
  103. char line[255];
  104. while(!fin.eof() && !fin.fail())
  105. {
  106. fin.getline(line, 255);
  107. if(!strncmp(line, "#include", 8))
  108. {
  109. // if it is an include line then create a string class
  110. std::string currentline = line;
  111. size_t qstart = currentline.find('\"', 8);
  112. size_t qend;
  113. // if a quote is not found look for a <
  114. if(qstart == std::string::npos)
  115. {
  116. qstart = currentline.find('<', 8);
  117. // if a < is not found then move on
  118. if(qstart == std::string::npos)
  119. {
  120. std::cerr << "unknown include directive " << currentline
  121. << std::endl;
  122. continue;
  123. }
  124. else
  125. {
  126. qend = currentline.find('>', qstart+1);
  127. }
  128. }
  129. else
  130. {
  131. qend = currentline.find('\"', qstart+1);
  132. }
  133. // extract the file being included
  134. std::string includeFile = currentline.substr(qstart+1, qend - qstart-1);
  135. // see if the include matches the regular expression
  136. if(!m_IncludeFileRegularExpression.find(includeFile))
  137. {
  138. if(m_Verbose)
  139. {
  140. std::cerr << "skipping " << includeFile << " for file " << path << std::endl;
  141. }
  142. continue;
  143. }
  144. // find the index of the include file in the
  145. // m_DependInformation array, if it is not
  146. // there then FindInformation will create it
  147. int index = this->FindInformation(includeFile.c_str());
  148. // add the index to the depends of the current
  149. // depend info object
  150. info->m_Indices.push_back(index);
  151. // Get the depend information object for the include file
  152. cmDependInformation* dependInfo = m_DependInformation[index];
  153. // if the depends are not known for an include file, then compute them
  154. // recursively
  155. if(!dependInfo->m_DependDone)
  156. {
  157. // stop the recursion here
  158. dependInfo->m_DependDone = true;
  159. this->Depend(dependInfo);
  160. }
  161. // add the depends of the included file to the includer
  162. info->MergeInfo(dependInfo);
  163. }
  164. }
  165. info->m_DependDone = true;
  166. }
  167. // Find the cmDependInformation array index of the
  168. // given include file. Create a new cmDependInformation
  169. // object if one is not found
  170. int cmMakeDepend::FindInformation(const char* fname)
  171. {
  172. int i = 0;
  173. while(i < m_DependInformation.size())
  174. {
  175. if(m_DependInformation[i]->m_IncludeName == fname)
  176. {
  177. return i;
  178. }
  179. ++i;
  180. }
  181. cmDependInformation* newinfo = new cmDependInformation;
  182. newinfo->m_FullPath = this->FullPath(fname);
  183. this->AddFileToSearchPath(newinfo->m_FullPath.c_str());
  184. newinfo->m_IncludeName = fname;
  185. m_DependInformation.push_back(newinfo);
  186. return m_DependInformation.size()-1;
  187. }
  188. // remove duplicate indices from the depend information
  189. void cmDependInformation::RemoveDuplicateIndices()
  190. {
  191. std::sort(m_Indices.begin(), m_Indices.end(), std::less<int>());
  192. std::vector<int>::iterator new_end =
  193. std::unique(m_Indices.begin(), m_Indices.end());
  194. m_Indices.erase(new_end, m_Indices.end());
  195. }
  196. // add the depend information from info to this
  197. void cmDependInformation::MergeInfo(cmDependInformation* info)
  198. {
  199. std::vector<int>::iterator i = info->m_Indices.begin();
  200. for(; i!= info->m_Indices.end(); ++i)
  201. {
  202. m_Indices.push_back(*i);
  203. }
  204. }
  205. // find the full path to fname by searching the m_IncludeDirectories array
  206. std::string cmMakeDepend::FullPath(const char* fname)
  207. {
  208. for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
  209. i != m_IncludeDirectories.end(); ++i)
  210. {
  211. if(cmFileExists(fname))
  212. {
  213. return std::string(fname);
  214. }
  215. std::string path = *i;
  216. path = path + "/";
  217. path = path + fname;
  218. if(cmFileExists(path.c_str()))
  219. {
  220. return path;
  221. }
  222. }
  223. std::cerr << "File not found " << fname << std::endl;
  224. return std::string(fname);
  225. }
  226. // Add a directory to the search path
  227. void cmMakeDepend::AddSearchPath(const char* path)
  228. {
  229. m_IncludeDirectories.push_back(path);
  230. }
  231. // Add a directory to the search path
  232. void cmMakeDepend::AddFileToSearchPath(const char* file)
  233. {
  234. std::string filepath = file;
  235. std::string::size_type pos = filepath.rfind('/');
  236. if(pos != std::string::npos)
  237. {
  238. std::string path = filepath.substr(0, pos);
  239. if(std::find(m_IncludeDirectories.begin(), m_IncludeDirectories.end(), path)
  240. == m_IncludeDirectories.end())
  241. {
  242. m_IncludeDirectories.push_back(path);
  243. return;
  244. }
  245. }
  246. }