cmMakeDepend.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmMakeDepend.h"
  14. #include "cmStandardIncludes.h"
  15. #include "cmSystemTools.h"
  16. void cmDependInformation::AddDependencies(cmDependInformation* info)
  17. {
  18. if(this != info)
  19. {
  20. m_DependencySet.insert(info);
  21. for (cmDependInformation::DependencySet::const_iterator
  22. d = info->m_DependencySet.begin();
  23. d != info->m_DependencySet.end(); ++d)
  24. {
  25. m_DependencySet.insert(*d);
  26. }
  27. }
  28. }
  29. cmMakeDepend::cmMakeDepend()
  30. {
  31. m_Verbose = false;
  32. m_IncludeFileRegularExpression.compile("^.*$");
  33. m_ComplainFileRegularExpression.compile("^$");
  34. }
  35. cmMakeDepend::~cmMakeDepend()
  36. {
  37. for(DependInformationMap::iterator i = m_DependInformationMap.begin();
  38. i != m_DependInformationMap.end(); ++i)
  39. {
  40. delete i->second;
  41. }
  42. }
  43. // Set the makefile that depends will be made from.
  44. // The pointer is kept so the cmSourceFile array can
  45. // be updated with the depend information in the cmMakefile.
  46. void cmMakeDepend::SetMakefile(const cmMakefile* makefile)
  47. {
  48. m_Makefile = makefile;
  49. // Now extract the include file regular expression from the makefile.
  50. m_IncludeFileRegularExpression.compile(
  51. m_Makefile->m_IncludeFileRegularExpression.c_str());
  52. m_ComplainFileRegularExpression.compile(
  53. m_Makefile->m_ComplainFileRegularExpression.c_str());
  54. // Now extract any include paths from the makefile flags
  55. const std::vector<std::string>& includes =
  56. m_Makefile->GetIncludeDirectories();
  57. for(std::vector<std::string>::const_iterator j = includes.begin();
  58. j != includes.end(); ++j)
  59. {
  60. std::string path = *j;
  61. m_Makefile->ExpandVariablesInString(path);
  62. this->AddSearchPath(path.c_str());
  63. }
  64. }
  65. const cmDependInformation* cmMakeDepend::FindDependencies(const char* file)
  66. {
  67. cmDependInformation* info = this->GetDependInformation(file,NULL);
  68. this->GenerateDependInformation(info);
  69. return info;
  70. }
  71. void cmMakeDepend::GenerateDependInformation(cmDependInformation* info)
  72. {
  73. // If dependencies are already done, stop now.
  74. if(info->m_DependDone)
  75. {
  76. return;
  77. }
  78. else
  79. {
  80. // Make sure we don't visit the same file more than once.
  81. info->m_DependDone = true;
  82. }
  83. const char* path = info->m_FullPath.c_str();
  84. if(!path)
  85. {
  86. cmSystemTools::Error("Attempt to find dependencies for file without path!");
  87. return;
  88. }
  89. bool found = false;
  90. // If the file exists, use it to find dependency information.
  91. if(cmSystemTools::FileExists(path))
  92. {
  93. // Use the real file to find its dependencies.
  94. this->DependWalk(info);
  95. found = true;
  96. }
  97. // See if the cmSourceFile for it has any files specified as
  98. // dependency hints.
  99. if(info->m_cmSourceFile != 0)
  100. {
  101. // Get the cmSourceFile corresponding to this.
  102. const cmSourceFile& cFile = *(info->m_cmSourceFile);
  103. // See if there are any hints for finding dependencies for the missing
  104. // file.
  105. if(!cFile.GetDepends().empty())
  106. {
  107. // Dependency hints have been given. Use them to begin the
  108. // recursion.
  109. for(std::vector<std::string>::const_iterator file =
  110. cFile.GetDepends().begin(); file != cFile.GetDepends().end();
  111. ++file)
  112. {
  113. this->AddDependency(info, file->c_str());
  114. }
  115. // Found dependency information. We are done.
  116. found = true;
  117. }
  118. }
  119. if(!found)
  120. {
  121. // Try to find the file amongst the sources
  122. cmMakefile::SourceMap srcmap = m_Makefile->GetSources();
  123. cmMakefile::SourceMap::iterator l;
  124. for (l= srcmap.begin() ; l!=srcmap.end() ; l++)
  125. {
  126. for(std::vector<cmSourceFile*>::iterator i = l->second.begin();
  127. i != l->second.end(); i++)
  128. {
  129. if ((*i)->GetFullPath() == path)
  130. {
  131. found=true;
  132. }
  133. else
  134. {
  135. //try to guess which include path to use
  136. for(std::vector<std::string>::iterator t =
  137. m_IncludeDirectories.begin();
  138. t != m_IncludeDirectories.end(); ++t)
  139. {
  140. std::string incpath = *t;
  141. incpath = incpath + "/";
  142. incpath = incpath + path;
  143. if ((*i)->GetFullPath() == incpath)
  144. {
  145. // set the path to the guessed path
  146. info->m_FullPath = incpath;
  147. found=true;
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. if(!found)
  155. {
  156. // Couldn't find any dependency information.
  157. if(m_ComplainFileRegularExpression.find(info->m_IncludeName.c_str()))
  158. {
  159. cmSystemTools::Error("error cannot find dependencies for ", path);
  160. }
  161. else
  162. {
  163. // Destroy the name of the file so that it won't be output as a
  164. // dependency.
  165. info->m_FullPath = "";
  166. }
  167. }
  168. }
  169. // This function actually reads the file specified and scans it for
  170. // #include directives
  171. void cmMakeDepend::DependWalk(cmDependInformation* info)
  172. {
  173. cmRegularExpression includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  174. std::ifstream fin(info->m_FullPath.c_str());
  175. if(!fin)
  176. {
  177. cmSystemTools::Error("Cannot open ", info->m_FullPath.c_str());
  178. return;
  179. }
  180. // TODO: Write real read loop (see cmSystemTools::CopyFile).
  181. char line[255];
  182. for(fin.getline(line, 255); fin; fin.getline(line, 255))
  183. {
  184. if(includeLine.find(line))
  185. {
  186. // extract the file being included
  187. std::string includeFile = includeLine.match(1);
  188. // see if the include matches the regular expression
  189. if(!m_IncludeFileRegularExpression.find(includeFile))
  190. {
  191. if(m_Verbose)
  192. {
  193. std::string message = "Skipping ";
  194. message += includeFile;
  195. message += " for file ";
  196. message += info->m_FullPath.c_str();
  197. cmSystemTools::Error(message.c_str(), 0);
  198. }
  199. continue;
  200. }
  201. // Add this file and all its dependencies.
  202. this->AddDependency(info, includeFile.c_str());
  203. }
  204. }
  205. }
  206. void cmMakeDepend::AddDependency(cmDependInformation* info, const char* file)
  207. {
  208. cmDependInformation* dependInfo =
  209. this->GetDependInformation(file,
  210. cmSystemTools::GetFilenamePath(
  211. cmSystemTools::CollapseFullPath(
  212. info->m_FullPath.c_str())).c_str());
  213. this->GenerateDependInformation(dependInfo);
  214. info->AddDependencies(dependInfo);
  215. }
  216. cmDependInformation* cmMakeDepend::GetDependInformation(const char* file,
  217. const char *extraPath)
  218. {
  219. // Get the full path for the file so that lookup is unambiguous.
  220. std::string fullPath = this->FullPath(file, extraPath);
  221. // Try to find the file's instance of cmDependInformation.
  222. DependInformationMap::const_iterator result =
  223. m_DependInformationMap.find(fullPath);
  224. if(result != m_DependInformationMap.end())
  225. {
  226. // Found an instance, return it.
  227. return result->second;
  228. }
  229. else
  230. {
  231. // Didn't find an instance. Create a new one and save it.
  232. cmDependInformation* info = new cmDependInformation;
  233. info->m_FullPath = fullPath;
  234. info->m_IncludeName = file;
  235. m_DependInformationMap[fullPath] = info;
  236. return info;
  237. }
  238. }
  239. void cmMakeDepend::GenerateMakefileDependencies()
  240. {
  241. // Now create cmDependInformation objects for files in the directory
  242. const cmTargets &tgts = m_Makefile->GetTargets();
  243. for(cmTargets::const_iterator l = tgts.begin();
  244. l != tgts.end(); l++)
  245. {
  246. const std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
  247. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  248. i != classes.end(); ++i)
  249. {
  250. if(!(*i)->GetIsAHeaderFileOnly())
  251. {
  252. cmDependInformation* info =
  253. this->GetDependInformation((*i)->GetFullPath().c_str(),NULL);
  254. this->AddFileToSearchPath(info->m_FullPath.c_str());
  255. info->m_cmSourceFile = *i;
  256. this->GenerateDependInformation(info);
  257. }
  258. }
  259. }
  260. }
  261. // find the full path to fname by searching the m_IncludeDirectories array
  262. std::string cmMakeDepend::FullPath(const char* fname, const char *extraPath)
  263. {
  264. if(cmSystemTools::FileExists(fname))
  265. {
  266. return std::string(cmSystemTools::CollapseFullPath(fname));
  267. }
  268. for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
  269. i != m_IncludeDirectories.end(); ++i)
  270. {
  271. std::string path = *i;
  272. path = path + "/";
  273. path = path + fname;
  274. if(cmSystemTools::FileExists(path.c_str()))
  275. {
  276. return cmSystemTools::CollapseFullPath(path.c_str());
  277. }
  278. }
  279. if (extraPath)
  280. {
  281. std::string path = extraPath;
  282. path = path + "/";
  283. path = path + fname;
  284. if(cmSystemTools::FileExists(path.c_str()))
  285. {
  286. return cmSystemTools::CollapseFullPath(path.c_str());
  287. }
  288. }
  289. // Couldn't find the file.
  290. return std::string(fname);
  291. }
  292. // Add a directory to the search path
  293. void cmMakeDepend::AddSearchPath(const char* path)
  294. {
  295. m_IncludeDirectories.push_back(path);
  296. }
  297. // Add a directory to the search path
  298. void cmMakeDepend::AddFileToSearchPath(const char* file)
  299. {
  300. std::string filepath = file;
  301. std::string::size_type pos = filepath.rfind('/');
  302. if(pos != std::string::npos)
  303. {
  304. std::string path = filepath.substr(0, pos);
  305. if(std::find(m_IncludeDirectories.begin(),
  306. m_IncludeDirectories.end(), path)
  307. == m_IncludeDirectories.end())
  308. {
  309. m_IncludeDirectories.push_back(path);
  310. return;
  311. }
  312. }
  313. }
  314. const cmDependInformation*
  315. cmMakeDepend::GetDependInformationForSourceFile(const cmSourceFile &sf) const
  316. {
  317. for(DependInformationMap::const_iterator i = m_DependInformationMap.begin();
  318. i != m_DependInformationMap.end(); ++i)
  319. {
  320. const cmDependInformation* info = i->second;
  321. if(info->m_cmSourceFile == &sf)
  322. {
  323. return info;
  324. }
  325. }
  326. return 0;
  327. }