cmMakeDepend.cxx 10 KB

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