cmMakeDepend.cxx 10 KB

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