cmMakeDepend.cxx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmMakeDepend.h"
  33. #include "cmStandardIncludes.h"
  34. #include "cmSystemTools.h"
  35. void cmDependInformation::AddDependencies(cmDependInformation* info)
  36. {
  37. if(this != info)
  38. {
  39. m_DependencySet.insert(info);
  40. for (cmDependInformation::DependencySet::const_iterator
  41. d = info->m_DependencySet.begin();
  42. d != info->m_DependencySet.end(); ++d)
  43. {
  44. m_DependencySet.insert(*d);
  45. }
  46. }
  47. }
  48. cmMakeDepend::cmMakeDepend()
  49. {
  50. m_Verbose = false;
  51. m_IncludeFileRegularExpression.compile("^.*$");
  52. m_ComplainFileRegularExpression.compile("^$");
  53. }
  54. cmMakeDepend::~cmMakeDepend()
  55. {
  56. for(DependInformationMap::iterator i = m_DependInformationMap.begin();
  57. i != m_DependInformationMap.end(); ++i)
  58. {
  59. delete i->second;
  60. }
  61. }
  62. // Set the makefile that depends will be made from.
  63. // The pointer is kept so the cmSourceFile array can
  64. // be updated with the depend information in the cmMakefile.
  65. void cmMakeDepend::SetMakefile(const cmMakefile* makefile)
  66. {
  67. m_Makefile = makefile;
  68. // Now extract the include file regular expression from the makefile.
  69. m_IncludeFileRegularExpression.compile(
  70. m_Makefile->m_IncludeFileRegularExpression.c_str());
  71. m_ComplainFileRegularExpression.compile(
  72. m_Makefile->m_ComplainFileRegularExpression.c_str());
  73. // Now extract any include paths from the makefile flags
  74. const std::vector<std::string>& includes =
  75. m_Makefile->GetIncludeDirectories();
  76. for(std::vector<std::string>::const_iterator j = includes.begin();
  77. j != includes.end(); ++j)
  78. {
  79. std::string path = *j;
  80. m_Makefile->ExpandVariablesInString(path);
  81. this->AddSearchPath(path.c_str());
  82. }
  83. }
  84. const cmDependInformation* cmMakeDepend::FindDependencies(const char* file)
  85. {
  86. cmDependInformation* info = this->GetDependInformation(file);
  87. this->GenerateDependInformation(info);
  88. return info;
  89. }
  90. void cmMakeDepend::GenerateDependInformation(cmDependInformation* info)
  91. {
  92. // If dependencies are already done, stop now.
  93. if(info->m_DependDone)
  94. {
  95. return;
  96. }
  97. else
  98. {
  99. // Make sure we don't visit the same file more than once.
  100. info->m_DependDone = true;
  101. }
  102. const char* path = info->m_FullPath.c_str();
  103. if(!path)
  104. {
  105. cmSystemTools::Error("Attempt to find dependencies for file without path!");
  106. return;
  107. }
  108. // If the file exists, use it to find dependency information.
  109. if(cmSystemTools::FileExists(path))
  110. {
  111. // Use the real file to find its dependencies.
  112. this->DependWalk(info);
  113. return;
  114. }
  115. // The file doesn't exist. See if the cmSourceFile for it has any files
  116. // specified as dependency hints.
  117. if(info->m_cmSourceFile != 0)
  118. {
  119. // Get the cmSourceFile corresponding to this.
  120. const cmSourceFile& cFile = *(info->m_cmSourceFile);
  121. // See if there are any hints for finding dependencies for the missing
  122. // file.
  123. if(!cFile.GetDepends().empty())
  124. {
  125. // Initial dependencies have been given. Use them to begin the
  126. // recursion.
  127. for(std::vector<std::string>::const_iterator file =
  128. cFile.GetDepends().begin(); file != cFile.GetDepends().end();
  129. ++file)
  130. {
  131. this->AddDependency(info, file->c_str());
  132. }
  133. // Found dependency information. We are done.
  134. return;
  135. }
  136. }
  137. // Couldn't find any dependency information.
  138. if(m_ComplainFileRegularExpression.find(info->m_IncludeName.c_str()))
  139. {
  140. cmSystemTools::Error("error cannot find dependencies for ", path);
  141. }
  142. else
  143. {
  144. // Destroy the name of the file so that it won't be output as a
  145. // dependency.
  146. info->m_FullPath = "";
  147. }
  148. }
  149. // This function actually reads the file specified and scans it for
  150. // #include directives
  151. void cmMakeDepend::DependWalk(cmDependInformation* info)
  152. {
  153. cmRegularExpression includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  154. std::ifstream fin(info->m_FullPath.c_str());
  155. if(!fin)
  156. {
  157. cmSystemTools::Error("Cannot open ", info->m_FullPath.c_str());
  158. return;
  159. }
  160. // TODO: Write real read loop (see cmSystemTools::CopyFile).
  161. char line[255];
  162. for(fin.getline(line, 255); fin; fin.getline(line, 255))
  163. {
  164. if(includeLine.find(line))
  165. {
  166. // extract the file being included
  167. std::string includeFile = includeLine.match(1);
  168. // see if the include matches the regular expression
  169. if(!m_IncludeFileRegularExpression.find(includeFile))
  170. {
  171. if(m_Verbose)
  172. {
  173. std::string message = "Skipping ";
  174. message += includeFile;
  175. message += " for file ";
  176. message += info->m_FullPath.c_str();
  177. cmSystemTools::Error(message.c_str(), 0);
  178. }
  179. continue;
  180. }
  181. // Add this file and all its dependencies.
  182. this->AddDependency(info, includeFile.c_str());
  183. }
  184. }
  185. }
  186. void cmMakeDepend::AddDependency(cmDependInformation* info, const char* file)
  187. {
  188. cmDependInformation* dependInfo = this->GetDependInformation(file);
  189. this->GenerateDependInformation(dependInfo);
  190. info->AddDependencies(dependInfo);
  191. }
  192. cmDependInformation* cmMakeDepend::GetDependInformation(const char* file)
  193. {
  194. // Get the full path for the file so that lookup is unambiguous.
  195. std::string fullPath = this->FullPath(file);
  196. // Try to find the file's instance of cmDependInformation.
  197. DependInformationMap::const_iterator result =
  198. m_DependInformationMap.find(fullPath);
  199. if(result != m_DependInformationMap.end())
  200. {
  201. // Found an instance, return it.
  202. return result->second;
  203. }
  204. else
  205. {
  206. // Didn't find an instance. Create a new one and save it.
  207. cmDependInformation* info = new cmDependInformation;
  208. info->m_FullPath = fullPath;
  209. info->m_IncludeName = file;
  210. m_DependInformationMap[fullPath] = info;
  211. return info;
  212. }
  213. }
  214. void cmMakeDepend::GenerateMakefileDependencies()
  215. {
  216. // Now create cmDependInformation objects for files in the directory
  217. const cmTargets &tgts = m_Makefile->GetTargets();
  218. for(cmTargets::const_iterator l = tgts.begin();
  219. l != tgts.end(); l++)
  220. {
  221. const std::vector<cmSourceFile> &classes = l->second.GetSourceFiles();
  222. for(std::vector<cmSourceFile>::const_iterator i = classes.begin();
  223. i != classes.end(); ++i)
  224. {
  225. if(!i->GetIsAHeaderFileOnly())
  226. {
  227. cmDependInformation* info =
  228. this->GetDependInformation(i->GetFullPath().c_str());
  229. this->AddFileToSearchPath(info->m_FullPath.c_str());
  230. info->m_cmSourceFile = &*i;
  231. this->GenerateDependInformation(info);
  232. }
  233. }
  234. }
  235. }
  236. // find the full path to fname by searching the m_IncludeDirectories array
  237. std::string cmMakeDepend::FullPath(const char* fname)
  238. {
  239. if(cmSystemTools::FileExists(fname))
  240. {
  241. return std::string(fname);
  242. }
  243. for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
  244. i != m_IncludeDirectories.end(); ++i)
  245. {
  246. std::string path = *i;
  247. path = path + "/";
  248. path = path + fname;
  249. if(cmSystemTools::FileExists(path.c_str()))
  250. {
  251. return path;
  252. }
  253. }
  254. // Couldn't find the file.
  255. return std::string(fname);
  256. }
  257. // Add a directory to the search path
  258. void cmMakeDepend::AddSearchPath(const char* path)
  259. {
  260. m_IncludeDirectories.push_back(path);
  261. }
  262. // Add a directory to the search path
  263. void cmMakeDepend::AddFileToSearchPath(const char* file)
  264. {
  265. std::string filepath = file;
  266. std::string::size_type pos = filepath.rfind('/');
  267. if(pos != std::string::npos)
  268. {
  269. std::string path = filepath.substr(0, pos);
  270. if(std::find(m_IncludeDirectories.begin(),
  271. m_IncludeDirectories.end(), path)
  272. == m_IncludeDirectories.end())
  273. {
  274. m_IncludeDirectories.push_back(path);
  275. return;
  276. }
  277. }
  278. }
  279. const cmDependInformation*
  280. cmMakeDepend::GetDependInformationForSourceFile(const cmSourceFile &sf) const
  281. {
  282. for(DependInformationMap::const_iterator i = m_DependInformationMap.begin();
  283. i != m_DependInformationMap.end(); ++i)
  284. {
  285. const cmDependInformation* info = i->second;
  286. if(info->m_cmSourceFile == &sf)
  287. {
  288. return info;
  289. }
  290. }
  291. return 0;
  292. }