cmMakeDepend.cxx 11 KB

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