cmMakeDepend.cxx 10.0 KB

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