cmMakeDepend.cxx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 makefile flags
  47. const std::vector<std::string>& includes =
  48. this->Makefile->GetIncludeDirectories();
  49. for(std::vector<std::string>::const_iterator j = includes.begin();
  50. j != includes.end(); ++j)
  51. {
  52. std::string path = *j;
  53. this->Makefile->ExpandVariablesInString(path);
  54. this->AddSearchPath(path.c_str());
  55. }
  56. }
  57. const cmDependInformation* cmMakeDepend::FindDependencies(const char* file)
  58. {
  59. cmDependInformation* info = this->GetDependInformation(file,0);
  60. this->GenerateDependInformation(info);
  61. return info;
  62. }
  63. void cmMakeDepend::GenerateDependInformation(cmDependInformation* info)
  64. {
  65. // If dependencies are already done, stop now.
  66. if(info->DependDone)
  67. {
  68. return;
  69. }
  70. else
  71. {
  72. // Make sure we don't visit the same file more than once.
  73. info->DependDone = true;
  74. }
  75. const char* path = info->FullPath.c_str();
  76. if(!path)
  77. {
  78. cmSystemTools::Error(
  79. "Attempt to find dependencies for file without path!");
  80. return;
  81. }
  82. bool found = false;
  83. // If the file exists, use it to find dependency information.
  84. if(cmSystemTools::FileExists(path, true))
  85. {
  86. // Use the real file to find its dependencies.
  87. this->DependWalk(info);
  88. found = true;
  89. }
  90. // See if the cmSourceFile for it has any files specified as
  91. // dependency hints.
  92. if(info->SourceFile != 0)
  93. {
  94. // Get the cmSourceFile corresponding to this.
  95. const cmSourceFile& cFile = *(info->SourceFile);
  96. // See if there are any hints for finding dependencies for the missing
  97. // file.
  98. if(!cFile.GetDepends().empty())
  99. {
  100. // Dependency hints have been given. Use them to begin the
  101. // recursion.
  102. for(std::vector<std::string>::const_iterator file =
  103. cFile.GetDepends().begin(); file != cFile.GetDepends().end();
  104. ++file)
  105. {
  106. this->AddDependency(info, file->c_str());
  107. }
  108. // Found dependency information. We are done.
  109. found = true;
  110. }
  111. }
  112. if(!found)
  113. {
  114. // Try to find the file amongst the sources
  115. cmSourceFile *srcFile = this->Makefile->GetSource
  116. (cmSystemTools::GetFilenameWithoutExtension(path).c_str());
  117. if (srcFile)
  118. {
  119. if (srcFile->GetFullPath() == path)
  120. {
  121. found=true;
  122. }
  123. else
  124. {
  125. //try to guess which include path to use
  126. for(std::vector<std::string>::iterator t =
  127. this->IncludeDirectories.begin();
  128. t != this->IncludeDirectories.end(); ++t)
  129. {
  130. std::string incpath = *t;
  131. if (incpath.size() && incpath[incpath.size() - 1] != '/')
  132. {
  133. incpath = incpath + "/";
  134. }
  135. incpath = incpath + path;
  136. if (srcFile->GetFullPath() == incpath)
  137. {
  138. // set the path to the guessed path
  139. info->FullPath = incpath;
  140. found=true;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. if(!found)
  147. {
  148. // Couldn't find any dependency information.
  149. if(this->ComplainFileRegularExpression.find(info->IncludeName.c_str()))
  150. {
  151. cmSystemTools::Error("error cannot find dependencies for ", path);
  152. }
  153. else
  154. {
  155. // Destroy the name of the file so that it won't be output as a
  156. // dependency.
  157. info->FullPath = "";
  158. }
  159. }
  160. }
  161. // This function actually reads the file specified and scans it for
  162. // #include directives
  163. void cmMakeDepend::DependWalk(cmDependInformation* info)
  164. {
  165. cmsys::RegularExpression includeLine
  166. ("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  167. std::ifstream fin(info->FullPath.c_str());
  168. if(!fin)
  169. {
  170. cmSystemTools::Error("Cannot open ", info->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(!this->IncludeFileRegularExpression.find(includeFile))
  183. {
  184. if(this->Verbose)
  185. {
  186. std::string message = "Skipping ";
  187. message += includeFile;
  188. message += " for file ";
  189. message += info->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->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. DependInformationMapType::const_iterator result =
  213. this->DependInformationMap.find(fullPath);
  214. if(result != this->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->FullPath = fullPath;
  224. info->PathOnly = cmSystemTools::GetFilenamePath(fullPath.c_str());
  225. info->IncludeName = file;
  226. this->DependInformationMap[fullPath] = info;
  227. return info;
  228. }
  229. }
  230. // find the full path to fname by searching the this->IncludeDirectories array
  231. std::string cmMakeDepend::FullPath(const char* fname, const char *extraPath)
  232. {
  233. DirectoryToFileToPathMapType::iterator m;
  234. if(extraPath)
  235. {
  236. m = this->DirectoryToFileToPathMap.find(extraPath);
  237. }
  238. else
  239. {
  240. m = this->DirectoryToFileToPathMap.find("");
  241. }
  242. if(m != this->DirectoryToFileToPathMap.end())
  243. {
  244. FileToPathMapType& map = m->second;
  245. FileToPathMapType::iterator p = map.find(fname);
  246. if(p != map.end())
  247. {
  248. return p->second;
  249. }
  250. }
  251. if(cmSystemTools::FileExists(fname, true))
  252. {
  253. std::string fp = cmSystemTools::CollapseFullPath(fname);
  254. this->DirectoryToFileToPathMap[extraPath? extraPath: ""][fname] = fp;
  255. return fp;
  256. }
  257. for(std::vector<std::string>::iterator i = this->IncludeDirectories.begin();
  258. i != this->IncludeDirectories.end(); ++i)
  259. {
  260. std::string path = *i;
  261. if (path.size() && path[path.size() - 1] != '/')
  262. {
  263. path = path + "/";
  264. }
  265. path = path + fname;
  266. if(cmSystemTools::FileExists(path.c_str(), true)
  267. && !cmSystemTools::FileIsDirectory(path.c_str()))
  268. {
  269. std::string fp = cmSystemTools::CollapseFullPath(path.c_str());
  270. this->DirectoryToFileToPathMap[extraPath? extraPath: ""][fname] = fp;
  271. return fp;
  272. }
  273. }
  274. if (extraPath)
  275. {
  276. std::string path = extraPath;
  277. if (path.size() && path[path.size() - 1] != '/')
  278. {
  279. path = path + "/";
  280. }
  281. path = path + fname;
  282. if(cmSystemTools::FileExists(path.c_str(), true)
  283. && !cmSystemTools::FileIsDirectory(path.c_str()))
  284. {
  285. std::string fp = cmSystemTools::CollapseFullPath(path.c_str());
  286. this->DirectoryToFileToPathMap[extraPath][fname] = fp;
  287. return fp;
  288. }
  289. }
  290. // Couldn't find the file.
  291. return std::string(fname);
  292. }
  293. // Add a directory to the search path
  294. void cmMakeDepend::AddSearchPath(const char* path)
  295. {
  296. this->IncludeDirectories.push_back(path);
  297. }