cmOutputRequiredFilesCommand.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "cmOutputRequiredFilesCommand.h"
  33. #include "cmMakeDepend.h"
  34. class cmLBDepend : public cmMakeDepend
  35. {
  36. /**
  37. * Compute the depend information for this class.
  38. */
  39. virtual void DependWalk(cmDependInformation* info);
  40. };
  41. void cmLBDepend::DependWalk(cmDependInformation* info)
  42. {
  43. std::ifstream fin(info->m_FullPath.c_str());
  44. if(!fin)
  45. {
  46. cmSystemTools::Error("error can not open ", info->m_FullPath.c_str());
  47. return;
  48. }
  49. char line[255];
  50. while(!fin.eof() && !fin.fail())
  51. {
  52. fin.getline(line, 255);
  53. if(!strncmp(line, "#include", 8))
  54. {
  55. // if it is an include line then create a string class
  56. std::string currentline = line;
  57. size_t qstart = currentline.find('\"', 8);
  58. size_t qend;
  59. // if a quote is not found look for a <
  60. if(qstart == std::string::npos)
  61. {
  62. qstart = currentline.find('<', 8);
  63. // if a < is not found then move on
  64. if(qstart == std::string::npos)
  65. {
  66. cmSystemTools::Error("unknown include directive ",
  67. currentline.c_str() );
  68. continue;
  69. }
  70. else
  71. {
  72. qend = currentline.find('>', qstart+1);
  73. }
  74. }
  75. else
  76. {
  77. qend = currentline.find('\"', qstart+1);
  78. }
  79. // extract the file being included
  80. std::string includeFile = currentline.substr(qstart+1, qend - qstart-1);
  81. // see if the include matches the regular expression
  82. if(!m_IncludeFileRegularExpression.find(includeFile))
  83. {
  84. if(m_Verbose)
  85. {
  86. std::string message = "Skipping ";
  87. message += includeFile;
  88. message += " for file ";
  89. message += info->m_FullPath.c_str();
  90. cmSystemTools::Error(message.c_str(), 0);
  91. }
  92. continue;
  93. }
  94. // Add this file and all its dependencies.
  95. this->AddDependency(info, includeFile.c_str());
  96. /// add the cxx file if it exists
  97. std::string cxxFile = includeFile;
  98. std::string::size_type pos = cxxFile.rfind('.');
  99. if(pos != std::string::npos)
  100. {
  101. std::string root = cxxFile.substr(0, pos);
  102. cxxFile = root + ".cxx";
  103. bool found = false;
  104. // try jumping to .cxx .cpp and .c in order
  105. if(cmSystemTools::FileExists(cxxFile.c_str()))
  106. {
  107. found = true;
  108. }
  109. for(std::vector<std::string>::iterator i =
  110. m_IncludeDirectories.begin();
  111. i != m_IncludeDirectories.end(); ++i)
  112. {
  113. std::string path = *i;
  114. path = path + "/";
  115. path = path + cxxFile;
  116. if(cmSystemTools::FileExists(path.c_str()))
  117. {
  118. found = true;
  119. }
  120. }
  121. if (!found)
  122. {
  123. cxxFile = root + ".cpp";
  124. if(cmSystemTools::FileExists(cxxFile.c_str()))
  125. {
  126. found = true;
  127. }
  128. for(std::vector<std::string>::iterator i =
  129. m_IncludeDirectories.begin();
  130. i != m_IncludeDirectories.end(); ++i)
  131. {
  132. std::string path = *i;
  133. path = path + "/";
  134. path = path + cxxFile;
  135. if(cmSystemTools::FileExists(path.c_str()))
  136. {
  137. found = true;
  138. }
  139. }
  140. }
  141. if (!found)
  142. {
  143. cxxFile = root + ".c";
  144. if(cmSystemTools::FileExists(cxxFile.c_str()))
  145. {
  146. found = true;
  147. }
  148. for(std::vector<std::string>::iterator i =
  149. m_IncludeDirectories.begin();
  150. i != m_IncludeDirectories.end(); ++i)
  151. {
  152. std::string path = *i;
  153. path = path + "/";
  154. path = path + cxxFile;
  155. if(cmSystemTools::FileExists(path.c_str()))
  156. {
  157. found = true;
  158. }
  159. }
  160. }
  161. if (found)
  162. {
  163. this->AddDependency(info, cxxFile.c_str());
  164. }
  165. }
  166. }
  167. }
  168. }
  169. // cmOutputRequiredFilesCommand
  170. bool cmOutputRequiredFilesCommand::InitialPass(std::vector<std::string> const& args)
  171. {
  172. if(args.size() < 2 )
  173. {
  174. this->SetError("called with incorrect number of arguments");
  175. return false;
  176. }
  177. // store the arg for final pass
  178. m_File = args[0];
  179. m_OutputFile = args[1];
  180. return true;
  181. }
  182. void cmOutputRequiredFilesCommand::FinalPass()
  183. {
  184. cmTargets &tgts = m_Makefile->GetTargets();
  185. for (cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  186. {
  187. l->second.GenerateSourceFilesFromSourceLists(*m_Makefile);
  188. }
  189. // compute the list of files
  190. cmLBDepend md;
  191. md.SetMakefile(m_Makefile);
  192. // always expand the first argument
  193. m_Makefile->ExpandVariablesInString(m_File);
  194. m_Makefile->ExpandVariablesInString(m_OutputFile);
  195. // find the depends for a file
  196. const cmDependInformation *info = md.FindDependencies(m_File.c_str());
  197. if (info)
  198. {
  199. // write them out
  200. FILE *fout = fopen(m_OutputFile.c_str(),"w");
  201. for(cmDependInformation::DependencySet::const_iterator d =
  202. info->m_DependencySet.begin();
  203. d != info->m_DependencySet.end(); ++d)
  204. {
  205. std::string tmp = (*d)->m_FullPath;
  206. std::string::size_type pos = tmp.rfind('.');
  207. if(pos != std::string::npos && tmp.substr(pos) == ".cxx")
  208. {
  209. tmp = tmp.substr(0, pos);
  210. fprintf(fout,"%s\n",(*d)->m_FullPath.c_str());
  211. }
  212. }
  213. fclose(fout);
  214. }
  215. }