cmMakeDepend.cxx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmMakeDepend.h"
  11. #include "cmSystemTools.h"
  12. #include <cmsys/RegularExpression.hxx>
  13. void cmDependInformation::AddDependencies(cmDependInformation* info)
  14. {
  15. if(this != info)
  16. {
  17. this->DependencySet.insert(info);
  18. }
  19. }
  20. cmMakeDepend::cmMakeDepend()
  21. {
  22. this->Verbose = false;
  23. this->IncludeFileRegularExpression.compile("^.*$");
  24. this->ComplainFileRegularExpression.compile("^$");
  25. }
  26. cmMakeDepend::~cmMakeDepend()
  27. {
  28. for(DependInformationMapType::iterator i =
  29. this->DependInformationMap.begin();
  30. i != this->DependInformationMap.end(); ++i)
  31. {
  32. delete i->second;
  33. }
  34. }
  35. // Set the makefile that depends will be made from.
  36. // The pointer is kept so the cmSourceFile array can
  37. // be updated with the depend information in the cmMakefile.
  38. void cmMakeDepend::SetMakefile(cmMakefile* makefile)
  39. {
  40. this->Makefile = makefile;
  41. // Now extract the include file regular expression from the makefile.
  42. this->IncludeFileRegularExpression.compile(
  43. this->Makefile->IncludeFileRegularExpression.c_str());
  44. this->ComplainFileRegularExpression.compile(
  45. this->Makefile->ComplainFileRegularExpression.c_str());
  46. // Now extract any include paths from the targets
  47. cmTargets & targets = this->Makefile->GetTargets();
  48. for (cmTargets::iterator l = targets.begin(); l != targets.end(); ++l)
  49. {
  50. const std::vector<std::string>& includes =
  51. l->second.GetIncludeDirectories();
  52. for(std::vector<std::string>::const_iterator j = includes.begin();
  53. j != includes.end(); ++j)
  54. {
  55. std::string path = *j;
  56. this->Makefile->ExpandVariablesInString(path);
  57. this->AddSearchPath(path.c_str());
  58. }
  59. }
  60. }
  61. const cmDependInformation* cmMakeDepend::FindDependencies(const char* file)
  62. {
  63. cmDependInformation* info = this->GetDependInformation(file,0);
  64. this->GenerateDependInformation(info);
  65. return info;
  66. }
  67. void cmMakeDepend::GenerateDependInformation(cmDependInformation* info)
  68. {
  69. // If dependencies are already done, stop now.
  70. if(info->DependDone)
  71. {
  72. return;
  73. }
  74. else
  75. {
  76. // Make sure we don't visit the same file more than once.
  77. info->DependDone = true;
  78. }
  79. const char* path = info->FullPath.c_str();
  80. if(!path)
  81. {
  82. cmSystemTools::Error(
  83. "Attempt to find dependencies for file without path!");
  84. return;
  85. }
  86. bool found = false;
  87. // If the file exists, use it to find dependency information.
  88. if(cmSystemTools::FileExists(path, true))
  89. {
  90. // Use the real file to find its dependencies.
  91. this->DependWalk(info);
  92. found = true;
  93. }
  94. // See if the cmSourceFile for it has any files specified as
  95. // dependency hints.
  96. if(info->SourceFile != 0)
  97. {
  98. // Get the cmSourceFile corresponding to this.
  99. const cmSourceFile& cFile = *(info->SourceFile);
  100. // See if there are any hints for finding dependencies for the missing
  101. // file.
  102. if(!cFile.GetDepends().empty())
  103. {
  104. // Dependency hints have been given. Use them to begin the
  105. // recursion.
  106. for(std::vector<std::string>::const_iterator file =
  107. cFile.GetDepends().begin(); file != cFile.GetDepends().end();
  108. ++file)
  109. {
  110. this->AddDependency(info, file->c_str());
  111. }
  112. // Found dependency information. We are done.
  113. found = true;
  114. }
  115. }
  116. if(!found)
  117. {
  118. // Try to find the file amongst the sources
  119. cmSourceFile *srcFile = this->Makefile->GetSource
  120. (cmSystemTools::GetFilenameWithoutExtension(path).c_str());
  121. if (srcFile)
  122. {
  123. if (srcFile->GetFullPath() == path)
  124. {
  125. found=true;
  126. }
  127. else
  128. {
  129. //try to guess which include path to use
  130. for(std::vector<std::string>::iterator t =
  131. this->IncludeDirectories.begin();
  132. t != this->IncludeDirectories.end(); ++t)
  133. {
  134. std::string incpath = *t;
  135. if (incpath.size() && incpath[incpath.size() - 1] != '/')
  136. {
  137. incpath = incpath + "/";
  138. }
  139. incpath = incpath + path;
  140. if (srcFile->GetFullPath() == incpath)
  141. {
  142. // set the path to the guessed path
  143. info->FullPath = incpath;
  144. found=true;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. if(!found)
  151. {
  152. // Couldn't find any dependency information.
  153. if(this->ComplainFileRegularExpression.find(info->IncludeName.c_str()))
  154. {
  155. cmSystemTools::Error("error cannot find dependencies for ", path);
  156. }
  157. else
  158. {
  159. // Destroy the name of the file so that it won't be output as a
  160. // dependency.
  161. info->FullPath = "";
  162. }
  163. }
  164. }
  165. // This function actually reads the file specified and scans it for
  166. // #include directives
  167. void cmMakeDepend::DependWalk(cmDependInformation* info)
  168. {
  169. cmsys::RegularExpression includeLine
  170. ("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  171. std::ifstream fin(info->FullPath.c_str());
  172. if(!fin)
  173. {
  174. cmSystemTools::Error("Cannot open ", info->FullPath.c_str());
  175. return;
  176. }
  177. // TODO: Write real read loop (see cmSystemTools::CopyFile).
  178. std::string line;
  179. while( cmSystemTools::GetLineFromStream(fin, line) )
  180. {
  181. if(includeLine.find(line.c_str()))
  182. {
  183. // extract the file being included
  184. std::string includeFile = includeLine.match(1);
  185. // see if the include matches the regular expression
  186. if(!this->IncludeFileRegularExpression.find(includeFile))
  187. {
  188. if(this->Verbose)
  189. {
  190. std::string message = "Skipping ";
  191. message += includeFile;
  192. message += " for file ";
  193. message += info->FullPath.c_str();
  194. cmSystemTools::Error(message.c_str(), 0);
  195. }
  196. continue;
  197. }
  198. // Add this file and all its dependencies.
  199. this->AddDependency(info, includeFile.c_str());
  200. }
  201. }
  202. }
  203. void cmMakeDepend::AddDependency(cmDependInformation* info, const char* file)
  204. {
  205. cmDependInformation* dependInfo =
  206. this->GetDependInformation(file, info->PathOnly.c_str());
  207. this->GenerateDependInformation(dependInfo);
  208. info->AddDependencies(dependInfo);
  209. }
  210. cmDependInformation* cmMakeDepend::GetDependInformation(const char* file,
  211. const char *extraPath)
  212. {
  213. // Get the full path for the file so that lookup is unambiguous.
  214. std::string fullPath = this->FullPath(file, extraPath);
  215. // Try to find the file's instance of cmDependInformation.
  216. DependInformationMapType::const_iterator result =
  217. this->DependInformationMap.find(fullPath);
  218. if(result != this->DependInformationMap.end())
  219. {
  220. // Found an instance, return it.
  221. return result->second;
  222. }
  223. else
  224. {
  225. // Didn't find an instance. Create a new one and save it.
  226. cmDependInformation* info = new cmDependInformation;
  227. info->FullPath = fullPath;
  228. info->PathOnly = cmSystemTools::GetFilenamePath(fullPath.c_str());
  229. info->IncludeName = file;
  230. this->DependInformationMap[fullPath] = info;
  231. return info;
  232. }
  233. }
  234. // find the full path to fname by searching the this->IncludeDirectories array
  235. std::string cmMakeDepend::FullPath(const char* fname, const char *extraPath)
  236. {
  237. DirectoryToFileToPathMapType::iterator m;
  238. if(extraPath)
  239. {
  240. m = this->DirectoryToFileToPathMap.find(extraPath);
  241. }
  242. else
  243. {
  244. m = this->DirectoryToFileToPathMap.find("");
  245. }
  246. if(m != this->DirectoryToFileToPathMap.end())
  247. {
  248. FileToPathMapType& map = m->second;
  249. FileToPathMapType::iterator p = map.find(fname);
  250. if(p != map.end())
  251. {
  252. return p->second;
  253. }
  254. }
  255. if(cmSystemTools::FileExists(fname, true))
  256. {
  257. std::string fp = cmSystemTools::CollapseFullPath(fname);
  258. this->DirectoryToFileToPathMap[extraPath? extraPath: ""][fname] = fp;
  259. return fp;
  260. }
  261. for(std::vector<std::string>::iterator i = this->IncludeDirectories.begin();
  262. i != this->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(), true)
  271. && !cmSystemTools::FileIsDirectory(path.c_str()))
  272. {
  273. std::string fp = cmSystemTools::CollapseFullPath(path.c_str());
  274. this->DirectoryToFileToPathMap[extraPath? extraPath: ""][fname] = fp;
  275. return fp;
  276. }
  277. }
  278. if (extraPath)
  279. {
  280. std::string path = extraPath;
  281. if (path.size() && path[path.size() - 1] != '/')
  282. {
  283. path = path + "/";
  284. }
  285. path = path + fname;
  286. if(cmSystemTools::FileExists(path.c_str(), true)
  287. && !cmSystemTools::FileIsDirectory(path.c_str()))
  288. {
  289. std::string fp = cmSystemTools::CollapseFullPath(path.c_str());
  290. this->DirectoryToFileToPathMap[extraPath][fname] = fp;
  291. return fp;
  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. this->IncludeDirectories.push_back(path);
  301. }