cmMakeDepend.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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(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(
  81. "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(
  119. cmSystemTools::GetFilenameWithoutExtension(path).c_str());
  120. if (srcFile)
  121. {
  122. if (srcFile->GetFullPath() == path)
  123. {
  124. found=true;
  125. }
  126. else
  127. {
  128. //try to guess which include path to use
  129. for(std::vector<std::string>::iterator t =
  130. m_IncludeDirectories.begin();
  131. t != m_IncludeDirectories.end(); ++t)
  132. {
  133. std::string incpath = *t;
  134. if (incpath.size() && incpath[incpath.size() - 1] != '/')
  135. {
  136. incpath = incpath + "/";
  137. }
  138. incpath = incpath + path;
  139. if (srcFile->GetFullPath() == incpath)
  140. {
  141. // set the path to the guessed path
  142. info->m_FullPath = incpath;
  143. found=true;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. if(!found)
  150. {
  151. // Couldn't find any dependency information.
  152. if(m_ComplainFileRegularExpression.find(info->m_IncludeName.c_str()))
  153. {
  154. cmSystemTools::Error("error cannot find dependencies for ", path);
  155. }
  156. else
  157. {
  158. // Destroy the name of the file so that it won't be output as a
  159. // dependency.
  160. info->m_FullPath = "";
  161. }
  162. }
  163. }
  164. // This function actually reads the file specified and scans it for
  165. // #include directives
  166. void cmMakeDepend::DependWalk(cmDependInformation* info)
  167. {
  168. cmsys::RegularExpression includeLine(
  169. "^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  170. std::ifstream fin(info->m_FullPath.c_str());
  171. if(!fin)
  172. {
  173. cmSystemTools::Error("Cannot open ", info->m_FullPath.c_str());
  174. return;
  175. }
  176. // TODO: Write real read loop (see cmSystemTools::CopyFile).
  177. std::string line;
  178. while( cmSystemTools::GetLineFromStream(fin, line) )
  179. {
  180. if(includeLine.find(line.c_str()))
  181. {
  182. // extract the file being included
  183. std::string includeFile = includeLine.match(1);
  184. // see if the include matches the regular expression
  185. if(!m_IncludeFileRegularExpression.find(includeFile))
  186. {
  187. if(m_Verbose)
  188. {
  189. std::string message = "Skipping ";
  190. message += includeFile;
  191. message += " for file ";
  192. message += info->m_FullPath.c_str();
  193. cmSystemTools::Error(message.c_str(), 0);
  194. }
  195. continue;
  196. }
  197. // Add this file and all its dependencies.
  198. this->AddDependency(info, includeFile.c_str());
  199. }
  200. }
  201. }
  202. void cmMakeDepend::AddDependency(cmDependInformation* info, const char* file)
  203. {
  204. cmDependInformation* dependInfo =
  205. this->GetDependInformation(file, info->m_PathOnly.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_PathOnly = cmSystemTools::GetFilenamePath(fullPath.c_str());
  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. cmTargets &tgts = m_Makefile->GetTargets();
  237. for(cmTargets::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. DirectoryToFileToPathMap::iterator m;
  259. if(extraPath)
  260. {
  261. m = m_DirectoryToFileToPathMap.find(extraPath);
  262. }
  263. else
  264. {
  265. m = m_DirectoryToFileToPathMap.find("");
  266. }
  267. if(m != m_DirectoryToFileToPathMap.end())
  268. {
  269. FileToPathMap& map = m->second;
  270. FileToPathMap::iterator p = map.find(fname);
  271. if(p != map.end())
  272. {
  273. return p->second;
  274. }
  275. }
  276. if(cmSystemTools::FileExists(fname))
  277. {
  278. std::string fp = cmSystemTools::CollapseFullPath(fname);
  279. m_DirectoryToFileToPathMap[extraPath? extraPath: ""][fname] = fp;
  280. return fp;
  281. }
  282. for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
  283. i != m_IncludeDirectories.end(); ++i)
  284. {
  285. std::string path = *i;
  286. if (path.size() && path[path.size() - 1] != '/')
  287. {
  288. path = path + "/";
  289. }
  290. path = path + fname;
  291. if(cmSystemTools::FileExists(path.c_str())
  292. && !cmSystemTools::FileIsDirectory(path.c_str()))
  293. {
  294. std::string fp = cmSystemTools::CollapseFullPath(path.c_str());
  295. m_DirectoryToFileToPathMap[extraPath? extraPath: ""][fname] = fp;
  296. return fp;
  297. }
  298. }
  299. if (extraPath)
  300. {
  301. std::string path = extraPath;
  302. if (path.size() && path[path.size() - 1] != '/')
  303. {
  304. path = path + "/";
  305. }
  306. path = path + fname;
  307. if(cmSystemTools::FileExists(path.c_str())
  308. && !cmSystemTools::FileIsDirectory(path.c_str()))
  309. {
  310. std::string fp = cmSystemTools::CollapseFullPath(path.c_str());
  311. m_DirectoryToFileToPathMap[extraPath][fname] = fp;
  312. return fp;
  313. }
  314. }
  315. // Couldn't find the file.
  316. return std::string(fname);
  317. }
  318. // Add a directory to the search path
  319. void cmMakeDepend::AddSearchPath(const char* path)
  320. {
  321. m_IncludeDirectories.push_back(path);
  322. }
  323. // Add a directory to the search path
  324. void cmMakeDepend::AddFileToSearchPath(const char* file)
  325. {
  326. std::string filepath = file;
  327. std::string::size_type pos = filepath.rfind('/');
  328. if(pos != std::string::npos)
  329. {
  330. std::string path = filepath.substr(0, pos);
  331. if(std::find(m_IncludeDirectories.begin(),
  332. m_IncludeDirectories.end(), path)
  333. == m_IncludeDirectories.end())
  334. {
  335. m_IncludeDirectories.push_back(path);
  336. return;
  337. }
  338. }
  339. }
  340. const cmDependInformation*
  341. cmMakeDepend::GetDependInformationForSourceFile(const cmSourceFile &sf) const
  342. {
  343. for(DependInformationMap::const_iterator i = m_DependInformationMap.begin();
  344. i != m_DependInformationMap.end(); ++i)
  345. {
  346. const cmDependInformation* info = i->second;
  347. if(info->m_cmSourceFile == &sf)
  348. {
  349. return info;
  350. }
  351. }
  352. return 0;
  353. }