cmMakeDepend.cxx 11 KB

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