cmMakeDepend.cxx 11 KB

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