cmMakeDepend.cxx 8.3 KB

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