cmMakeDepend.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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);
  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 = this->GetDependInformation(file);
  209. this->GenerateDependInformation(dependInfo);
  210. info->AddDependencies(dependInfo);
  211. }
  212. cmDependInformation* cmMakeDepend::GetDependInformation(const char* file)
  213. {
  214. // Get the full path for the file so that lookup is unambiguous.
  215. std::string fullPath = this->FullPath(file);
  216. // Try to find the file's instance of cmDependInformation.
  217. DependInformationMap::const_iterator result =
  218. m_DependInformationMap.find(fullPath);
  219. if(result != m_DependInformationMap.end())
  220. {
  221. // Found an instance, return it.
  222. return result->second;
  223. }
  224. else
  225. {
  226. // Didn't find an instance. Create a new one and save it.
  227. cmDependInformation* info = new cmDependInformation;
  228. info->m_FullPath = fullPath;
  229. info->m_IncludeName = file;
  230. m_DependInformationMap[fullPath] = info;
  231. return info;
  232. }
  233. }
  234. void cmMakeDepend::GenerateMakefileDependencies()
  235. {
  236. // Now create cmDependInformation objects for files in the directory
  237. const cmTargets &tgts = m_Makefile->GetTargets();
  238. for(cmTargets::const_iterator l = tgts.begin();
  239. l != tgts.end(); l++)
  240. {
  241. const std::vector<cmSourceFile> &classes = l->second.GetSourceFiles();
  242. for(std::vector<cmSourceFile>::const_iterator i = classes.begin();
  243. i != classes.end(); ++i)
  244. {
  245. if(!i->GetIsAHeaderFileOnly())
  246. {
  247. cmDependInformation* info =
  248. this->GetDependInformation(i->GetFullPath().c_str());
  249. this->AddFileToSearchPath(info->m_FullPath.c_str());
  250. info->m_cmSourceFile = &*i;
  251. this->GenerateDependInformation(info);
  252. }
  253. }
  254. }
  255. }
  256. // find the full path to fname by searching the m_IncludeDirectories array
  257. std::string cmMakeDepend::FullPath(const char* fname)
  258. {
  259. if(cmSystemTools::FileExists(fname))
  260. {
  261. return std::string(fname);
  262. }
  263. for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
  264. i != m_IncludeDirectories.end(); ++i)
  265. {
  266. std::string path = *i;
  267. path = path + "/";
  268. path = path + fname;
  269. if(cmSystemTools::FileExists(path.c_str()))
  270. {
  271. return path;
  272. }
  273. }
  274. // Couldn't find the file.
  275. return std::string(fname);
  276. }
  277. // Add a directory to the search path
  278. void cmMakeDepend::AddSearchPath(const char* path)
  279. {
  280. m_IncludeDirectories.push_back(path);
  281. }
  282. // Add a directory to the search path
  283. void cmMakeDepend::AddFileToSearchPath(const char* file)
  284. {
  285. std::string filepath = file;
  286. std::string::size_type pos = filepath.rfind('/');
  287. if(pos != std::string::npos)
  288. {
  289. std::string path = filepath.substr(0, pos);
  290. if(std::find(m_IncludeDirectories.begin(),
  291. m_IncludeDirectories.end(), path)
  292. == m_IncludeDirectories.end())
  293. {
  294. m_IncludeDirectories.push_back(path);
  295. return;
  296. }
  297. }
  298. }
  299. const cmDependInformation*
  300. cmMakeDepend::GetDependInformationForSourceFile(const cmSourceFile &sf) const
  301. {
  302. for(DependInformationMap::const_iterator i = m_DependInformationMap.begin();
  303. i != m_DependInformationMap.end(); ++i)
  304. {
  305. const cmDependInformation* info = i->second;
  306. if(info->m_cmSourceFile == &sf)
  307. {
  308. return info;
  309. }
  310. }
  311. return 0;
  312. }