cmMakeDepend.cxx 10 KB

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