cmMakeDepend.cxx 7.9 KB

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