cmFileCommand.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmFileCommand.h"
  14. #include "cmGlob.h"
  15. // cmLibraryCommand
  16. bool cmFileCommand::InitialPass(std::vector<std::string> const& args)
  17. {
  18. if(args.size() < 2 )
  19. {
  20. this->SetError("must be called with at least two arguments.");
  21. return false;
  22. }
  23. std::string subCommand = args[0];
  24. if ( subCommand == "WRITE" )
  25. {
  26. return this->HandleWriteCommand(args, false);
  27. }
  28. else if ( subCommand == "APPEND" )
  29. {
  30. return this->HandleWriteCommand(args, true);
  31. }
  32. else if ( subCommand == "READ" )
  33. {
  34. return this->HandleReadCommand(args);
  35. }
  36. else if ( subCommand == "GLOB" )
  37. {
  38. return this->HandleGlobCommand(args, false);
  39. }
  40. else if ( subCommand == "GLOB_RECURSE" )
  41. {
  42. return this->HandleGlobCommand(args, true);
  43. }
  44. else if ( subCommand == "MAKE_DIRECTORY" )
  45. {
  46. return this->HandleMakeDirectoryCommand(args);
  47. }
  48. std::string e = "does not recognize sub-command "+subCommand;
  49. this->SetError(e.c_str());
  50. return true;
  51. }
  52. //----------------------------------------------------------------------------
  53. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  54. bool append)
  55. {
  56. std::string message;
  57. std::vector<std::string>::const_iterator i = args.begin();
  58. i++; // Get rid of subcommand
  59. std::string fileName = *i;
  60. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  61. {
  62. fileName = m_Makefile->GetCurrentDirectory();
  63. fileName += "/" + *i;
  64. }
  65. i++;
  66. for(;i != args.end(); ++i)
  67. {
  68. message += *i;
  69. }
  70. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  71. cmSystemTools::MakeDirectory(dir.c_str());
  72. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  73. if ( !file )
  74. {
  75. std::string error = "Internal CMake error when trying to open file: ";
  76. error += fileName.c_str();
  77. error += " for writting.";
  78. this->SetError(error.c_str());
  79. return false;
  80. }
  81. file << message;
  82. file.close();
  83. return true;
  84. }
  85. //----------------------------------------------------------------------------
  86. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  87. {
  88. if ( args.size() != 3 )
  89. {
  90. this->SetError("READ must be called with two additional arguments");
  91. return false;
  92. }
  93. std::string fileName = args[1];
  94. if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
  95. {
  96. fileName = m_Makefile->GetCurrentDirectory();
  97. fileName += "/" + args[1];
  98. }
  99. std::string variable = args[2];
  100. std::ifstream file(fileName.c_str(), std::ios::in);
  101. if ( !file )
  102. {
  103. std::string error = "Internal CMake error when trying to open file: ";
  104. error += fileName.c_str();
  105. error += " for reading.";
  106. this->SetError(error.c_str());
  107. return false;
  108. }
  109. std::string output;
  110. std::string line;
  111. bool has_newline = false;
  112. while ( cmSystemTools::GetLineFromStream(file, line, &has_newline) )
  113. {
  114. output += line;
  115. if ( has_newline )
  116. {
  117. output += "\n";
  118. }
  119. }
  120. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  121. return true;
  122. }
  123. //----------------------------------------------------------------------------
  124. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  125. bool recurse)
  126. {
  127. if ( args.size() < 2 )
  128. {
  129. this->SetError("GLOB requires at least a variable name");
  130. return false;
  131. }
  132. std::vector<std::string>::const_iterator i = args.begin();
  133. i++; // Get rid of subcommand
  134. std::string variable = *i;
  135. i++;
  136. cmGlob g;
  137. g.SetRecurse(recurse);
  138. std::string output = "";
  139. bool first = true;
  140. for ( ; i != args.end(); ++i )
  141. {
  142. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  143. {
  144. std::string expr = m_Makefile->GetCurrentDirectory();
  145. expr += "/" + *i;
  146. g.FindFiles(expr);
  147. }
  148. else
  149. {
  150. g.FindFiles(*i);
  151. }
  152. std::vector<std::string>::size_type cc;
  153. std::vector<std::string>& files = g.GetFiles();
  154. for ( cc = 0; cc < files.size(); cc ++ )
  155. {
  156. if ( !first )
  157. {
  158. output += ";";
  159. }
  160. output += files[cc];
  161. first = false;
  162. }
  163. }
  164. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  165. return true;
  166. }
  167. //----------------------------------------------------------------------------
  168. bool cmFileCommand::HandleMakeDirectoryCommand(std::vector<std::string> const& args)
  169. {
  170. if(args.size() < 2 )
  171. {
  172. this->SetError("called with incorrect number of arguments");
  173. return false;
  174. }
  175. std::vector<std::string>::const_iterator i = args.begin();
  176. i++; // Get rid of subcommand
  177. std::string expr;
  178. for ( ; i != args.end(); ++i )
  179. {
  180. const std::string* cdir = &(*i);
  181. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  182. {
  183. expr = m_Makefile->GetCurrentDirectory();
  184. expr += "/" + *i;
  185. cdir = &expr;
  186. }
  187. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  188. {
  189. std::string error = "problem creating directory: " + *cdir;
  190. this->SetError(error.c_str());
  191. return false;
  192. }
  193. }
  194. return true;
  195. }