cmMakeDepend.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. void cmMakeDepend::Depend(cmDependInformation* info)
  101. {
  102. const char* path = info->m_FullPath.c_str();
  103. if(!path)
  104. {
  105. cmSystemTools::Error("no full path for object", 0);
  106. return;
  107. }
  108. // If the file exists, use it to find dependency information.
  109. if(cmSystemTools::FileExists(path))
  110. {
  111. // The cmClassFile may have had hints for dependencies. Delete any that
  112. // exist since we can find the dependencies for real.
  113. if(info->m_ClassFileIndex != -1)
  114. {
  115. cmClassFile& cFile = m_Makefile->m_Classes[info->m_ClassFileIndex];
  116. cFile.m_Depends.erase(cFile.m_Depends.begin(), cFile.m_Depends.end());
  117. }
  118. // Use the real file to find its dependencies.
  119. this->DependWalk(info, path);
  120. info->m_DependDone = true;
  121. return;
  122. }
  123. // The file doesn't exist. See if the cmClassFile for it has any files
  124. // specified as dependency hints.
  125. if(info->m_ClassFileIndex != -1)
  126. {
  127. // Get the cmClassFile corresponding to this.
  128. cmClassFile& cFile = m_Makefile->m_Classes[info->m_ClassFileIndex];
  129. // See if there are any hints for finding dependencies for the missing
  130. // file.
  131. if(!cFile.m_Depends.empty())
  132. {
  133. // Initial dependencies have been given. Use them to begin the
  134. // recursion.
  135. for(std::vector<std::string>::iterator file =
  136. cFile.m_Depends.begin(); file != cFile.m_Depends.end(); ++file)
  137. {
  138. this->AddDependency(info, file->c_str());
  139. }
  140. // Erase the dependency hints from the cmClassFile. They will be
  141. // put in again as real dependencies later.
  142. cFile.m_Depends.erase(cFile.m_Depends.begin(), cFile.m_Depends.end());
  143. // Found dependency information. We are done.
  144. return;
  145. }
  146. }
  147. // Couldn't find any dependency information.
  148. cmSystemTools::Error("error cannot find dependencies for ", path);
  149. }
  150. // This function actually reads the file specified and scans it for
  151. // #include directives
  152. void cmMakeDepend::DependWalk(cmDependInformation* info, const char* file)
  153. {
  154. std::ifstream fin(file);
  155. if(!fin)
  156. {
  157. cmSystemTools::Error("error can not open ", file);
  158. return;
  159. }
  160. char line[255];
  161. while(!fin.eof() && !fin.fail())
  162. {
  163. fin.getline(line, 255);
  164. if(!strncmp(line, "#include", 8))
  165. {
  166. // if it is an include line then create a string class
  167. std::string currentline = line;
  168. size_t qstart = currentline.find('\"', 8);
  169. size_t qend;
  170. // if a quote is not found look for a <
  171. if(qstart == std::string::npos)
  172. {
  173. qstart = currentline.find('<', 8);
  174. // if a < is not found then move on
  175. if(qstart == std::string::npos)
  176. {
  177. cmSystemTools::Error("unknown include directive ",
  178. currentline.c_str() );
  179. continue;
  180. }
  181. else
  182. {
  183. qend = currentline.find('>', qstart+1);
  184. }
  185. }
  186. else
  187. {
  188. qend = currentline.find('\"', qstart+1);
  189. }
  190. // extract the file being included
  191. std::string includeFile = currentline.substr(qstart+1, qend - qstart-1);
  192. // see if the include matches the regular expression
  193. if(!m_IncludeFileRegularExpression.find(includeFile))
  194. {
  195. if(m_Verbose)
  196. {
  197. std::string message = "Skipping ";
  198. message += includeFile;
  199. message += " for file ";
  200. message += file;
  201. cmSystemTools::Error(message.c_str(), 0);
  202. }
  203. continue;
  204. }
  205. // Add this file and all its dependencies.
  206. this->AddDependency(info, includeFile.c_str());
  207. }
  208. }
  209. }
  210. void cmMakeDepend::AddDependency(cmDependInformation* info, const char* file)
  211. {
  212. // find the index of the include file in the
  213. // m_DependInformation array, if it is not
  214. // there then FindInformation will create it
  215. int index = this->FindInformation(file);
  216. // add the index to the depends of the current
  217. // depend info object
  218. info->m_Indices.push_back(index);
  219. // Get the depend information object for the include file
  220. cmDependInformation* dependInfo = m_DependInformation[index];
  221. // if the depends are not known for an include file, then compute them
  222. // recursively
  223. if(!dependInfo->m_DependDone)
  224. {
  225. // stop the recursion here
  226. dependInfo->m_DependDone = true;
  227. this->Depend(dependInfo);
  228. }
  229. // add the depends of the included file to the includer
  230. info->MergeInfo(dependInfo);
  231. }
  232. // Find the cmDependInformation array index of the
  233. // given include file. Create a new cmDependInformation
  234. // object if one is not found
  235. int cmMakeDepend::FindInformation(const char* fname)
  236. {
  237. unsigned int i = 0;
  238. while(i < m_DependInformation.size())
  239. {
  240. if(m_DependInformation[i]->m_IncludeName == fname)
  241. {
  242. return i;
  243. }
  244. ++i;
  245. }
  246. cmDependInformation* newinfo = new cmDependInformation;
  247. newinfo->m_FullPath = this->FullPath(fname);
  248. // Add the directory where this file was found to the search path
  249. // may have been foo/bar.h, but bar may include files from the foo
  250. // directory without the foo prefix
  251. this->AddFileToSearchPath(newinfo->m_FullPath.c_str());
  252. newinfo->m_IncludeName = fname;
  253. m_DependInformation.push_back(newinfo);
  254. return m_DependInformation.size()-1;
  255. }
  256. // remove duplicate indices from the depend information
  257. void cmDependInformation::RemoveDuplicateIndices()
  258. {
  259. // sort the array
  260. std::sort(m_Indices.begin(), m_Indices.end(), std::less<int>());
  261. // remove duplicates
  262. std::vector<int>::iterator new_end =
  263. std::unique(m_Indices.begin(), m_Indices.end());
  264. m_Indices.erase(new_end, m_Indices.end());
  265. }
  266. // add the depend information from info to the m_Indices varible of this class.
  267. void cmDependInformation::MergeInfo(cmDependInformation* info)
  268. {
  269. if(this == info)
  270. {
  271. return;
  272. }
  273. std::vector<int>::iterator i = info->m_Indices.begin();
  274. for(; i!= info->m_Indices.end(); ++i)
  275. {
  276. m_Indices.push_back(*i);
  277. }
  278. }
  279. // find the full path to fname by searching the m_IncludeDirectories array
  280. std::string cmMakeDepend::FullPath(const char* fname)
  281. {
  282. if(cmSystemTools::FileExists(fname))
  283. {
  284. return std::string(fname);
  285. }
  286. for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
  287. i != m_IncludeDirectories.end(); ++i)
  288. {
  289. std::string path = *i;
  290. path = path + "/";
  291. path = path + fname;
  292. if(cmSystemTools::FileExists(path.c_str()))
  293. {
  294. return path;
  295. }
  296. }
  297. return std::string(fname);
  298. }
  299. // Add a directory to the search path
  300. void cmMakeDepend::AddSearchPath(const char* path)
  301. {
  302. m_IncludeDirectories.push_back(path);
  303. }
  304. // Add a directory to the search path
  305. void cmMakeDepend::AddFileToSearchPath(const char* file)
  306. {
  307. std::string filepath = file;
  308. std::string::size_type pos = filepath.rfind('/');
  309. if(pos != std::string::npos)
  310. {
  311. std::string path = filepath.substr(0, pos);
  312. if(std::find(m_IncludeDirectories.begin(),
  313. m_IncludeDirectories.end(), path)
  314. == m_IncludeDirectories.end())
  315. {
  316. m_IncludeDirectories.push_back(path);
  317. return;
  318. }
  319. }
  320. }