cmMakeDepend.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. cmSourceFile *srcFile =
  123. m_Makefile->GetSource(cmSystemTools::GetFilenameWithoutExtension(path).c_str());
  124. if (srcFile)
  125. {
  126. if (srcFile->GetFullPath() == path)
  127. {
  128. found=true;
  129. }
  130. else
  131. {
  132. //try to guess which include path to use
  133. for(std::vector<std::string>::iterator t =
  134. m_IncludeDirectories.begin();
  135. t != m_IncludeDirectories.end(); ++t)
  136. {
  137. std::string incpath = *t;
  138. incpath = incpath + "/";
  139. incpath = incpath + path;
  140. if (srcFile->GetFullPath() == incpath)
  141. {
  142. // set the path to the guessed path
  143. info->m_FullPath = incpath;
  144. found=true;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. if(!found)
  151. {
  152. // Couldn't find any dependency information.
  153. if(m_ComplainFileRegularExpression.find(info->m_IncludeName.c_str()))
  154. {
  155. cmSystemTools::Error("error cannot find dependencies for ", path);
  156. }
  157. else
  158. {
  159. // Destroy the name of the file so that it won't be output as a
  160. // dependency.
  161. info->m_FullPath = "";
  162. }
  163. }
  164. }
  165. // This function actually reads the file specified and scans it for
  166. // #include directives
  167. void cmMakeDepend::DependWalk(cmDependInformation* info)
  168. {
  169. cmRegularExpression includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  170. std::ifstream fin(info->m_FullPath.c_str());
  171. if(!fin)
  172. {
  173. cmSystemTools::Error("Cannot open ", info->m_FullPath.c_str());
  174. return;
  175. }
  176. // TODO: Write real read loop (see cmSystemTools::CopyFile).
  177. char line[255];
  178. for(fin.getline(line, 255); fin; fin.getline(line, 255))
  179. {
  180. if(includeLine.find(line))
  181. {
  182. // extract the file being included
  183. std::string includeFile = includeLine.match(1);
  184. // see if the include matches the regular expression
  185. if(!m_IncludeFileRegularExpression.find(includeFile))
  186. {
  187. if(m_Verbose)
  188. {
  189. std::string message = "Skipping ";
  190. message += includeFile;
  191. message += " for file ";
  192. message += info->m_FullPath.c_str();
  193. cmSystemTools::Error(message.c_str(), 0);
  194. }
  195. continue;
  196. }
  197. // Add this file and all its dependencies.
  198. this->AddDependency(info, includeFile.c_str());
  199. }
  200. }
  201. }
  202. void cmMakeDepend::AddDependency(cmDependInformation* info, const char* file)
  203. {
  204. cmDependInformation* dependInfo =
  205. this->GetDependInformation(file,
  206. cmSystemTools::GetFilenamePath(
  207. cmSystemTools::CollapseFullPath(
  208. info->m_FullPath.c_str())).c_str());
  209. this->GenerateDependInformation(dependInfo);
  210. info->AddDependencies(dependInfo);
  211. }
  212. cmDependInformation* cmMakeDepend::GetDependInformation(const char* file,
  213. const char *extraPath)
  214. {
  215. // Get the full path for the file so that lookup is unambiguous.
  216. std::string fullPath = this->FullPath(file, extraPath);
  217. // Try to find the file's instance of cmDependInformation.
  218. DependInformationMap::const_iterator result =
  219. m_DependInformationMap.find(fullPath);
  220. if(result != m_DependInformationMap.end())
  221. {
  222. // Found an instance, return it.
  223. return result->second;
  224. }
  225. else
  226. {
  227. // Didn't find an instance. Create a new one and save it.
  228. cmDependInformation* info = new cmDependInformation;
  229. info->m_FullPath = fullPath;
  230. info->m_IncludeName = file;
  231. m_DependInformationMap[fullPath] = info;
  232. return info;
  233. }
  234. }
  235. void cmMakeDepend::GenerateMakefileDependencies()
  236. {
  237. // Now create cmDependInformation objects for files in the directory
  238. const cmTargets &tgts = m_Makefile->GetTargets();
  239. for(cmTargets::const_iterator l = tgts.begin();
  240. l != tgts.end(); l++)
  241. {
  242. const std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
  243. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  244. i != classes.end(); ++i)
  245. {
  246. if(!(*i)->GetIsAHeaderFileOnly())
  247. {
  248. cmDependInformation* info =
  249. this->GetDependInformation((*i)->GetFullPath().c_str(),NULL);
  250. this->AddFileToSearchPath(info->m_FullPath.c_str());
  251. info->m_cmSourceFile = *i;
  252. this->GenerateDependInformation(info);
  253. }
  254. }
  255. }
  256. }
  257. // find the full path to fname by searching the m_IncludeDirectories array
  258. std::string cmMakeDepend::FullPath(const char* fname, const char *extraPath)
  259. {
  260. if(cmSystemTools::FileExists(fname))
  261. {
  262. return std::string(cmSystemTools::CollapseFullPath(fname));
  263. }
  264. for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
  265. i != m_IncludeDirectories.end(); ++i)
  266. {
  267. std::string path = *i;
  268. path = path + "/";
  269. path = path + fname;
  270. if(cmSystemTools::FileExists(path.c_str()))
  271. {
  272. return cmSystemTools::CollapseFullPath(path.c_str());
  273. }
  274. }
  275. if (extraPath)
  276. {
  277. std::string path = extraPath;
  278. path = path + "/";
  279. path = path + fname;
  280. if(cmSystemTools::FileExists(path.c_str()))
  281. {
  282. return cmSystemTools::CollapseFullPath(path.c_str());
  283. }
  284. }
  285. // Couldn't find the file.
  286. return std::string(fname);
  287. }
  288. // Add a directory to the search path
  289. void cmMakeDepend::AddSearchPath(const char* path)
  290. {
  291. m_IncludeDirectories.push_back(path);
  292. }
  293. // Add a directory to the search path
  294. void cmMakeDepend::AddFileToSearchPath(const char* file)
  295. {
  296. std::string filepath = file;
  297. std::string::size_type pos = filepath.rfind('/');
  298. if(pos != std::string::npos)
  299. {
  300. std::string path = filepath.substr(0, pos);
  301. if(std::find(m_IncludeDirectories.begin(),
  302. m_IncludeDirectories.end(), path)
  303. == m_IncludeDirectories.end())
  304. {
  305. m_IncludeDirectories.push_back(path);
  306. return;
  307. }
  308. }
  309. }
  310. const cmDependInformation*
  311. cmMakeDepend::GetDependInformationForSourceFile(const cmSourceFile &sf) const
  312. {
  313. for(DependInformationMap::const_iterator i = m_DependInformationMap.begin();
  314. i != m_DependInformationMap.end(); ++i)
  315. {
  316. const cmDependInformation* info = i->second;
  317. if(info->m_cmSourceFile == &sf)
  318. {
  319. return info;
  320. }
  321. }
  322. return 0;
  323. }