cmMakeDepend.cxx 10 KB

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