cmMakeDepend.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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, info->m_PathOnly.c_str());
  204. this->GenerateDependInformation(dependInfo);
  205. info->AddDependencies(dependInfo);
  206. }
  207. cmDependInformation* cmMakeDepend::GetDependInformation(const char* file,
  208. const char *extraPath)
  209. {
  210. // Get the full path for the file so that lookup is unambiguous.
  211. std::string fullPath = this->FullPath(file, extraPath);
  212. // Try to find the file's instance of cmDependInformation.
  213. DependInformationMap::const_iterator result =
  214. m_DependInformationMap.find(fullPath);
  215. if(result != m_DependInformationMap.end())
  216. {
  217. // Found an instance, return it.
  218. return result->second;
  219. }
  220. else
  221. {
  222. // Didn't find an instance. Create a new one and save it.
  223. cmDependInformation* info = new cmDependInformation;
  224. info->m_FullPath = fullPath;
  225. info->m_PathOnly = cmSystemTools::GetFilenamePath(fullPath.c_str());
  226. info->m_IncludeName = file;
  227. m_DependInformationMap[fullPath] = info;
  228. return info;
  229. }
  230. }
  231. void cmMakeDepend::GenerateMakefileDependencies()
  232. {
  233. // Now create cmDependInformation objects for files in the directory
  234. const cmTargets &tgts = m_Makefile->GetTargets();
  235. for(cmTargets::const_iterator l = tgts.begin();
  236. l != tgts.end(); l++)
  237. {
  238. const std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
  239. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  240. i != classes.end(); ++i)
  241. {
  242. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  243. {
  244. cmDependInformation* info =
  245. this->GetDependInformation((*i)->GetFullPath().c_str(),0);
  246. this->AddFileToSearchPath(info->m_FullPath.c_str());
  247. info->m_cmSourceFile = *i;
  248. this->GenerateDependInformation(info);
  249. }
  250. }
  251. }
  252. }
  253. // find the full path to fname by searching the m_IncludeDirectories array
  254. std::string cmMakeDepend::FullPath(const char* fname, const char *extraPath)
  255. {
  256. DirectoryToFileToPathMap::iterator m;
  257. if(extraPath)
  258. {
  259. m = m_DirectoryToFileToPathMap.find(extraPath);
  260. }
  261. else
  262. {
  263. m = m_DirectoryToFileToPathMap.find("");
  264. }
  265. if(m != m_DirectoryToFileToPathMap.end())
  266. {
  267. FileToPathMap& map = m->second;
  268. FileToPathMap::iterator p = map.find(fname);
  269. if(p != map.end())
  270. {
  271. return p->second;
  272. }
  273. }
  274. if(cmSystemTools::FileExists(fname))
  275. {
  276. std::string fp = cmSystemTools::CollapseFullPath(fname);
  277. m_DirectoryToFileToPathMap[extraPath? extraPath: ""][fname] = fp;
  278. return fp;
  279. }
  280. for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
  281. i != m_IncludeDirectories.end(); ++i)
  282. {
  283. std::string path = *i;
  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. std::string fp = cmSystemTools::CollapseFullPath(path.c_str());
  292. m_DirectoryToFileToPathMap[extraPath? extraPath: ""][fname] = fp;
  293. return fp;
  294. }
  295. }
  296. if (extraPath)
  297. {
  298. std::string path = extraPath;
  299. if (path.size() && path[path.size() - 1] != '/')
  300. {
  301. path = path + "/";
  302. }
  303. path = path + fname;
  304. if(cmSystemTools::FileExists(path.c_str()))
  305. {
  306. std::string fp = cmSystemTools::CollapseFullPath(path.c_str());
  307. m_DirectoryToFileToPathMap[extraPath][fname] = fp;
  308. return fp;
  309. }
  310. }
  311. // Couldn't find the file.
  312. return std::string(fname);
  313. }
  314. // Add a directory to the search path
  315. void cmMakeDepend::AddSearchPath(const char* path)
  316. {
  317. m_IncludeDirectories.push_back(path);
  318. }
  319. // Add a directory to the search path
  320. void cmMakeDepend::AddFileToSearchPath(const char* file)
  321. {
  322. std::string filepath = file;
  323. std::string::size_type pos = filepath.rfind('/');
  324. if(pos != std::string::npos)
  325. {
  326. std::string path = filepath.substr(0, pos);
  327. if(std::find(m_IncludeDirectories.begin(),
  328. m_IncludeDirectories.end(), path)
  329. == m_IncludeDirectories.end())
  330. {
  331. m_IncludeDirectories.push_back(path);
  332. return;
  333. }
  334. }
  335. }
  336. const cmDependInformation*
  337. cmMakeDepend::GetDependInformationForSourceFile(const cmSourceFile &sf) const
  338. {
  339. for(DependInformationMap::const_iterator i = m_DependInformationMap.begin();
  340. i != m_DependInformationMap.end(); ++i)
  341. {
  342. const cmDependInformation* info = i->second;
  343. if(info->m_cmSourceFile == &sf)
  344. {
  345. return info;
  346. }
  347. }
  348. return 0;
  349. }