cmMakeDepend.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. cmMakeDepend::cmMakeDepend()
  36. {
  37. m_Verbose = false;
  38. m_IncludeFileRegularExpression.compile("^.*$");
  39. m_ComplainFileRegularExpression.compile("^$");
  40. }
  41. cmMakeDepend::~cmMakeDepend()
  42. {
  43. for(DependArray::iterator i = m_DependInformation.begin();
  44. i != m_DependInformation.end(); ++i)
  45. {
  46. delete *i;
  47. }
  48. m_DependInformation.clear();
  49. }
  50. // Set the makefile that depends will be made from.
  51. // The pointer is kept so the cmSourceFile array can
  52. // be updated with the depend information in the cmMakefile.
  53. void cmMakeDepend::SetMakefile(const cmMakefile* makefile)
  54. {
  55. m_Makefile = makefile;
  56. // Now extract the include file regular expression from the makefile.
  57. m_IncludeFileRegularExpression.compile(
  58. m_Makefile->m_IncludeFileRegularExpression.c_str());
  59. m_ComplainFileRegularExpression.compile(
  60. m_Makefile->m_ComplainFileRegularExpression.c_str());
  61. // Now extract any include paths from the makefile flags
  62. const std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  63. std::vector<std::string>::const_iterator j;
  64. for(j = includes.begin(); j != includes.end(); ++j)
  65. {
  66. this->AddSearchPath(j->c_str());
  67. }
  68. // Now create cmDependInformation objects for files in the directory
  69. const cmTargets &tgts = m_Makefile->GetTargets();
  70. for(cmTargets::const_iterator l = tgts.begin();
  71. l != tgts.end(); l++)
  72. {
  73. const std::vector<cmSourceFile> &classes = l->second.GetSourceFiles();
  74. for(std::vector<cmSourceFile>::const_iterator i = classes.begin();
  75. i != classes.end(); ++i)
  76. {
  77. if(!i->GetIsAHeaderFileOnly())
  78. {
  79. cmDependInformation* info = new cmDependInformation;
  80. info->m_FullPath = this->FullPath(i->GetFullPath().c_str());
  81. this->AddFileToSearchPath(info->m_FullPath.c_str());
  82. info->m_IncludeName = i->GetFullPath();
  83. info->m_ClassFileIndex = &*i;
  84. m_DependInformation.push_back(info);
  85. }
  86. }
  87. }
  88. }
  89. // Compute the depends.
  90. void cmMakeDepend::GenerateDependInformation()
  91. {
  92. // The size of the m_DependInformation will change as
  93. // Depend is called so do not use an iterater but rather
  94. // depend on the size of the array.
  95. unsigned int j = 0;
  96. while(j != m_DependInformation.size())
  97. {
  98. cmDependInformation* info = m_DependInformation[j];
  99. // compute the depend information for the info object
  100. // this may add more objects to the m_DependInformation
  101. // array
  102. this->Depend(info);
  103. ++j;
  104. }
  105. }
  106. const cmDependInformation *
  107. cmMakeDepend::GetDependInformationForSourceFile(const cmSourceFile &sf) const
  108. {
  109. // Now update the depend information for each cmSourceFile
  110. // in the cmMakefile m_Makefile
  111. for(DependArray::const_iterator i = m_DependInformation.begin();
  112. i != m_DependInformation.end(); ++i)
  113. {
  114. cmDependInformation* info = *i;
  115. // find the class
  116. if(info->m_ClassFileIndex == &sf)
  117. {
  118. return info;
  119. }
  120. }
  121. return 0;
  122. }
  123. const cmDependInformation *
  124. cmMakeDepend::GetDependInformationForSourceFile(const char *fname) const
  125. {
  126. // Now update the depend information for each cmSourceFile
  127. // in the cmMakefile m_Makefile
  128. for(DependArray::const_iterator i = m_DependInformation.begin();
  129. i != m_DependInformation.end(); ++i)
  130. {
  131. cmDependInformation* info = *i;
  132. // find the class
  133. if(info->m_FullPath == fname)
  134. {
  135. return info;
  136. }
  137. }
  138. return 0;
  139. }
  140. void cmMakeDepend::Depend(cmDependInformation* info)
  141. {
  142. const char* path = info->m_FullPath.c_str();
  143. if(!path)
  144. {
  145. cmSystemTools::Error("no full path for object", 0);
  146. return;
  147. }
  148. // If the file exists, use it to find dependency information.
  149. if(cmSystemTools::FileExists(path))
  150. {
  151. // Use the real file to find its dependencies.
  152. this->DependWalk(info, path);
  153. info->m_DependDone = true;
  154. return;
  155. }
  156. // The file doesn't exist. See if the cmSourceFile for it has any files
  157. // specified as dependency hints.
  158. if(info->m_ClassFileIndex != 0)
  159. {
  160. // Get the cmSourceFile corresponding to this.
  161. const cmSourceFile& cFile = *(info->m_ClassFileIndex);
  162. // See if there are any hints for finding dependencies for the missing
  163. // file.
  164. if(!cFile.GetDepends().empty())
  165. {
  166. // Initial dependencies have been given. Use them to begin the
  167. // recursion.
  168. for(std::vector<std::string>::const_iterator file =
  169. cFile.GetDepends().begin(); file != cFile.GetDepends().end();
  170. ++file)
  171. {
  172. this->AddDependency(info, file->c_str());
  173. }
  174. // Found dependency information. We are done.
  175. return;
  176. }
  177. }
  178. // Couldn't find any dependency information.
  179. if(m_ComplainFileRegularExpression.find(info->m_IncludeName.c_str()))
  180. {
  181. cmSystemTools::Error("error cannot find dependencies for ", path);
  182. }
  183. else
  184. {
  185. // Destroy the name of the file so that it won't be output as a
  186. // dependency.
  187. info->m_FullPath = "";
  188. }
  189. }
  190. // This function actually reads the file specified and scans it for
  191. // #include directives
  192. void cmMakeDepend::DependWalk(cmDependInformation* info, const char* file)
  193. {
  194. cmRegularExpression includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  195. std::ifstream fin(file);
  196. if(!fin)
  197. {
  198. cmSystemTools::Error("error can not open ", file);
  199. return;
  200. }
  201. char line[255];
  202. for(fin.getline(line, 255); !fin.eof()&&!fin.fail(); fin.getline(line, 255))
  203. {
  204. if(includeLine.find(line))
  205. {
  206. // extract the file being included
  207. std::string includeFile = includeLine.match(1);
  208. // see if the include matches the regular expression
  209. if(!m_IncludeFileRegularExpression.find(includeFile))
  210. {
  211. if(m_Verbose)
  212. {
  213. std::string message = "Skipping ";
  214. message += includeFile;
  215. message += " for file ";
  216. message += file;
  217. cmSystemTools::Error(message.c_str(), 0);
  218. }
  219. continue;
  220. }
  221. // Add this file and all its dependencies.
  222. this->AddDependency(info, includeFile.c_str());
  223. }
  224. }
  225. }
  226. void cmMakeDepend::AddDependency(cmDependInformation* info, const char* file)
  227. {
  228. // find the index of the include file in the
  229. // m_DependInformation array, if it is not
  230. // there then FindInformation will create it
  231. int index = this->FindInformation(file);
  232. // add the index to the depends of the current
  233. // depend info object
  234. info->m_IndexSet.insert(index);
  235. // Get the depend information object for the include file
  236. cmDependInformation* dependInfo = m_DependInformation[index];
  237. // if the depends are not known for an include file, then compute them
  238. // recursively
  239. if(!dependInfo->m_DependDone)
  240. {
  241. // stop the recursion here
  242. dependInfo->m_DependDone = true;
  243. this->Depend(dependInfo);
  244. }
  245. // add the depends of the included file to the includer
  246. info->MergeInfo(dependInfo);
  247. }
  248. // Find the cmDependInformation array index of the
  249. // given include file. Create a new cmDependInformation
  250. // object if one is not found
  251. int cmMakeDepend::FindInformation(const char* fname)
  252. {
  253. unsigned int i = 0;
  254. while(i < m_DependInformation.size())
  255. {
  256. if(m_DependInformation[i]->m_IncludeName == fname)
  257. {
  258. return i;
  259. }
  260. ++i;
  261. }
  262. cmDependInformation* newinfo = new cmDependInformation;
  263. newinfo->m_FullPath = this->FullPath(fname);
  264. // Add the directory where this file was found to the search path
  265. // may have been foo/bar.h, but bar may include files from the foo
  266. // directory without the foo prefix
  267. this->AddFileToSearchPath(newinfo->m_FullPath.c_str());
  268. newinfo->m_IncludeName = fname;
  269. m_DependInformation.push_back(newinfo);
  270. return m_DependInformation.size()-1;
  271. }
  272. // add the depend information from info to the m_IndexSet varible of this class.
  273. void cmDependInformation::MergeInfo(cmDependInformation* info)
  274. {
  275. if(this != info)
  276. {
  277. for (std::set<int>::const_iterator p = info->m_IndexSet.begin();
  278. p != info->m_IndexSet.end(); ++p)
  279. {
  280. m_IndexSet.insert(*p);
  281. }
  282. }
  283. }
  284. // find the full path to fname by searching the m_IncludeDirectories array
  285. std::string cmMakeDepend::FullPath(const char* fname)
  286. {
  287. if(cmSystemTools::FileExists(fname))
  288. {
  289. return std::string(fname);
  290. }
  291. for(std::vector<std::string>::iterator i = m_IncludeDirectories.begin();
  292. i != m_IncludeDirectories.end(); ++i)
  293. {
  294. std::string path = *i;
  295. path = path + "/";
  296. path = path + fname;
  297. if(cmSystemTools::FileExists(path.c_str()))
  298. {
  299. return path;
  300. }
  301. }
  302. // Couldn't find the file.
  303. return std::string(fname);
  304. }
  305. // Add a directory to the search path
  306. void cmMakeDepend::AddSearchPath(const char* path)
  307. {
  308. m_IncludeDirectories.push_back(path);
  309. }
  310. // Add a directory to the search path
  311. void cmMakeDepend::AddFileToSearchPath(const char* file)
  312. {
  313. std::string filepath = file;
  314. std::string::size_type pos = filepath.rfind('/');
  315. if(pos != std::string::npos)
  316. {
  317. std::string path = filepath.substr(0, pos);
  318. if(std::find(m_IncludeDirectories.begin(),
  319. m_IncludeDirectories.end(), path)
  320. == m_IncludeDirectories.end())
  321. {
  322. m_IncludeDirectories.push_back(path);
  323. return;
  324. }
  325. }
  326. }