cmMakeDepend.cxx 7.9 KB

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