cmMakeDepend.cxx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. cmTargets &tgts = m_Makefile->GetTargets();
  46. for(cmTargets::iterator l = tgts.begin();
  47. l != tgts.end(); l++)
  48. {
  49. l->second.GenerateSourceFilesFromSourceLists(*m_Makefile);
  50. std::vector<cmSourceFile> &classes = l->second.GetSourceFiles();
  51. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  52. i != classes.end(); ++i)
  53. {
  54. if(!i->GetIsAHeaderFileOnly())
  55. {
  56. cmDependInformation* info = new cmDependInformation;
  57. info->m_FullPath = this->FullPath(i->GetFullPath().c_str());
  58. this->AddFileToSearchPath(info->m_FullPath.c_str());
  59. info->m_IncludeName = i->GetFullPath();
  60. info->m_ClassFileIndex = &*i;
  61. m_DependInformation.push_back(info);
  62. }
  63. }
  64. }
  65. }
  66. // Compute the depends.
  67. void cmMakeDepend::DoDepends()
  68. {
  69. // The size of the m_DependInformation will change as
  70. // Depend is called so do not use an iterater but rather
  71. // depend on the size of the array.
  72. unsigned int j = 0;
  73. while(j != m_DependInformation.size())
  74. {
  75. cmDependInformation* info = m_DependInformation[j];
  76. // compute the depend information for the info object
  77. // this may add more objects to the m_DependInformation
  78. // array
  79. this->Depend(info);
  80. ++j;
  81. }
  82. // Now update the depend information for each cmClassFile
  83. // in the cmMakefile m_Makefile
  84. for(DependArray::iterator i = m_DependInformation.begin();
  85. i != m_DependInformation.end(); ++i)
  86. {
  87. cmDependInformation* info = *i;
  88. // find the class
  89. if(info->m_ClassFileIndex != 0)
  90. {
  91. cmSourceFile& cfile = *(info->m_ClassFileIndex);
  92. for( cmDependInformation::IndexSet::const_iterator indx = info->m_IndexSet.begin();
  93. indx != info->m_IndexSet.end(); ++indx)
  94. {
  95. cfile.GetDepends().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 != 0)
  114. {
  115. cmSourceFile& cFile = *(info->m_ClassFileIndex);
  116. cFile.GetDepends().erase(cFile.GetDepends().begin(),
  117. cFile.GetDepends().end());
  118. }
  119. // Use the real file to find its dependencies.
  120. this->DependWalk(info, path);
  121. info->m_DependDone = true;
  122. return;
  123. }
  124. // The file doesn't exist. See if the cmClassFile for it has any files
  125. // specified as dependency hints.
  126. if(info->m_ClassFileIndex != 0)
  127. {
  128. // Get the cmClassFile corresponding to this.
  129. cmSourceFile& cFile = *(info->m_ClassFileIndex);
  130. // See if there are any hints for finding dependencies for the missing
  131. // file.
  132. if(!cFile.GetDepends().empty())
  133. {
  134. // Initial dependencies have been given. Use them to begin the
  135. // recursion.
  136. for(std::vector<std::string>::iterator file =
  137. cFile.GetDepends().begin(); file != cFile.GetDepends().end();
  138. ++file)
  139. {
  140. this->AddDependency(info, file->c_str());
  141. }
  142. // Erase the dependency hints from the cmClassFile. They will be
  143. // put in again as real dependencies later.
  144. cFile.GetDepends().erase(cFile.GetDepends().begin(),
  145. cFile.GetDepends().end());
  146. // Found dependency information. We are done.
  147. return;
  148. }
  149. }
  150. // Couldn't find any dependency information.
  151. cmSystemTools::Error("error cannot find dependencies for ", path);
  152. }
  153. // This function actually reads the file specified and scans it for
  154. // #include directives
  155. void cmMakeDepend::DependWalk(cmDependInformation* info, const char* file)
  156. {
  157. std::ifstream fin(file);
  158. if(!fin)
  159. {
  160. cmSystemTools::Error("error can not open ", file);
  161. return;
  162. }
  163. char line[255];
  164. while(!fin.eof() && !fin.fail())
  165. {
  166. fin.getline(line, 255);
  167. if(!strncmp(line, "#include", 8))
  168. {
  169. // if it is an include line then create a string class
  170. std::string currentline = line;
  171. size_t qstart = currentline.find('\"', 8);
  172. size_t qend;
  173. // if a quote is not found look for a <
  174. if(qstart == std::string::npos)
  175. {
  176. qstart = currentline.find('<', 8);
  177. // if a < is not found then move on
  178. if(qstart == std::string::npos)
  179. {
  180. cmSystemTools::Error("unknown include directive ",
  181. currentline.c_str() );
  182. continue;
  183. }
  184. else
  185. {
  186. qend = currentline.find('>', qstart+1);
  187. }
  188. }
  189. else
  190. {
  191. qend = currentline.find('\"', qstart+1);
  192. }
  193. // extract the file being included
  194. std::string includeFile = currentline.substr(qstart+1, qend - qstart-1);
  195. // see if the include matches the regular expression
  196. if(!m_IncludeFileRegularExpression.find(includeFile))
  197. {
  198. if(m_Verbose)
  199. {
  200. std::string message = "Skipping ";
  201. message += includeFile;
  202. message += " for file ";
  203. message += file;
  204. cmSystemTools::Error(message.c_str(), 0);
  205. }
  206. continue;
  207. }
  208. // Add this file and all its dependencies.
  209. this->AddDependency(info, includeFile.c_str());
  210. }
  211. }
  212. }
  213. void cmMakeDepend::AddDependency(cmDependInformation* info, const char* file)
  214. {
  215. // find the index of the include file in the
  216. // m_DependInformation array, if it is not
  217. // there then FindInformation will create it
  218. int index = this->FindInformation(file);
  219. // add the index to the depends of the current
  220. // depend info object
  221. info->m_IndexSet.insert(index);
  222. // Get the depend information object for the include file
  223. cmDependInformation* dependInfo = m_DependInformation[index];
  224. // if the depends are not known for an include file, then compute them
  225. // recursively
  226. if(!dependInfo->m_DependDone)
  227. {
  228. // stop the recursion here
  229. dependInfo->m_DependDone = true;
  230. this->Depend(dependInfo);
  231. }
  232. // add the depends of the included file to the includer
  233. info->MergeInfo(dependInfo);
  234. }
  235. // Find the cmDependInformation array index of the
  236. // given include file. Create a new cmDependInformation
  237. // object if one is not found
  238. int cmMakeDepend::FindInformation(const char* fname)
  239. {
  240. unsigned int i = 0;
  241. while(i < m_DependInformation.size())
  242. {
  243. if(m_DependInformation[i]->m_IncludeName == fname)
  244. {
  245. return i;
  246. }
  247. ++i;
  248. }
  249. cmDependInformation* newinfo = new cmDependInformation;
  250. newinfo->m_FullPath = this->FullPath(fname);
  251. // Add the directory where this file was found to the search path
  252. // may have been foo/bar.h, but bar may include files from the foo
  253. // directory without the foo prefix
  254. this->AddFileToSearchPath(newinfo->m_FullPath.c_str());
  255. newinfo->m_IncludeName = fname;
  256. m_DependInformation.push_back(newinfo);
  257. return m_DependInformation.size()-1;
  258. }
  259. // add the depend information from info to the m_IndexSet varible of this class.
  260. void cmDependInformation::MergeInfo(cmDependInformation* info)
  261. {
  262. if(this != info)
  263. {
  264. m_IndexSet.insert(info->m_IndexSet.begin(), info->m_IndexSet.end());
  265. }
  266. }
  267. // find the full path to fname by searching the m_IncludeDirectories array
  268. std::string cmMakeDepend::FullPath(const char* fname)
  269. {
  270. if(cmSystemTools::FileExists(fname))
  271. {
  272. return std::string(fname);
  273. }
  274. for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
  275. i != m_IncludeDirectories.end(); ++i)
  276. {
  277. std::string path = *i;
  278. path = path + "/";
  279. path = path + fname;
  280. if(cmSystemTools::FileExists(path.c_str()))
  281. {
  282. return path;
  283. }
  284. }
  285. return std::string(fname);
  286. }
  287. // Add a directory to the search path
  288. void cmMakeDepend::AddSearchPath(const char* path)
  289. {
  290. m_IncludeDirectories.push_back(path);
  291. }
  292. // Add a directory to the search path
  293. void cmMakeDepend::AddFileToSearchPath(const char* file)
  294. {
  295. std::string filepath = file;
  296. std::string::size_type pos = filepath.rfind('/');
  297. if(pos != std::string::npos)
  298. {
  299. std::string path = filepath.substr(0, pos);
  300. if(std::find(m_IncludeDirectories.begin(),
  301. m_IncludeDirectories.end(), path)
  302. == m_IncludeDirectories.end())
  303. {
  304. m_IncludeDirectories.push_back(path);
  305. return;
  306. }
  307. }
  308. }