cmMakeDepend.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. cmMakefile::ClassMap &Classes = m_Makefile->GetClasses();
  46. for(cmMakefile::ClassMap::iterator l = Classes.begin();
  47. l != Classes.end(); l++)
  48. {
  49. for(std::vector<cmClassFile>::iterator i = l->second.begin();
  50. i != l->second.end(); ++i)
  51. {
  52. if(!i->m_HeaderFileOnly)
  53. {
  54. cmDependInformation* info = new cmDependInformation;
  55. info->m_FullPath = this->FullPath(i->m_FullPath.c_str());
  56. this->AddFileToSearchPath(info->m_FullPath.c_str());
  57. info->m_IncludeName = i->m_FullPath;
  58. info->m_ClassFileIndex = i;
  59. m_DependInformation.push_back(info);
  60. }
  61. }
  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. // find the class
  87. if(info->m_ClassFileIndex != 0)
  88. {
  89. cmClassFile& cfile = *(info->m_ClassFileIndex);
  90. for( cmDependInformation::IndexSet::const_iterator indx = info->m_IndexSet.begin();
  91. indx != info->m_IndexSet.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 != 0)
  112. {
  113. cmClassFile& cFile = *(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 != 0)
  124. {
  125. // Get the cmClassFile corresponding to this.
  126. cmClassFile& cFile = *(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_IndexSet.insert(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. // add the depend information from info to the m_IndexSet varible of this class.
  255. void cmDependInformation::MergeInfo(cmDependInformation* info)
  256. {
  257. if(this != info)
  258. {
  259. m_IndexSet.insert(info->m_IndexSet.begin(), info->m_IndexSet.end());
  260. }
  261. }
  262. // find the full path to fname by searching the m_IncludeDirectories array
  263. std::string cmMakeDepend::FullPath(const char* fname)
  264. {
  265. if(cmSystemTools::FileExists(fname))
  266. {
  267. return std::string(fname);
  268. }
  269. for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
  270. i != m_IncludeDirectories.end(); ++i)
  271. {
  272. std::string path = *i;
  273. path = path + "/";
  274. path = path + fname;
  275. if(cmSystemTools::FileExists(path.c_str()))
  276. {
  277. return path;
  278. }
  279. }
  280. return std::string(fname);
  281. }
  282. // Add a directory to the search path
  283. void cmMakeDepend::AddSearchPath(const char* path)
  284. {
  285. m_IncludeDirectories.push_back(path);
  286. }
  287. // Add a directory to the search path
  288. void cmMakeDepend::AddFileToSearchPath(const char* file)
  289. {
  290. std::string filepath = file;
  291. std::string::size_type pos = filepath.rfind('/');
  292. if(pos != std::string::npos)
  293. {
  294. std::string path = filepath.substr(0, pos);
  295. if(std::find(m_IncludeDirectories.begin(),
  296. m_IncludeDirectories.end(), path)
  297. == m_IncludeDirectories.end())
  298. {
  299. m_IncludeDirectories.push_back(path);
  300. return;
  301. }
  302. }
  303. }