cmFileCommand.cxx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. // cmLibraryCommand
  18. bool cmFileCommand::InitialPass(std::vector<std::string> const& args)
  19. {
  20. if(args.size() < 2 )
  21. {
  22. this->SetError("must be called with at least two arguments.");
  23. return false;
  24. }
  25. std::string subCommand = args[0];
  26. if ( subCommand == "WRITE" )
  27. {
  28. return this->HandleWriteCommand(args, false);
  29. }
  30. else if ( subCommand == "APPEND" )
  31. {
  32. return this->HandleWriteCommand(args, true);
  33. }
  34. else if ( subCommand == "READ" )
  35. {
  36. return this->HandleReadCommand(args);
  37. }
  38. else if ( subCommand == "GLOB" )
  39. {
  40. return this->HandleGlobCommand(args, false);
  41. }
  42. else if ( subCommand == "GLOB_RECURSE" )
  43. {
  44. return this->HandleGlobCommand(args, true);
  45. }
  46. else if ( subCommand == "MAKE_DIRECTORY" )
  47. {
  48. return this->HandleMakeDirectoryCommand(args);
  49. }
  50. else if ( subCommand == "INSTALL" )
  51. {
  52. return this->HandleInstallCommand(args);
  53. }
  54. std::string e = "does not recognize sub-command "+subCommand;
  55. this->SetError(e.c_str());
  56. return false;
  57. }
  58. //----------------------------------------------------------------------------
  59. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  60. bool append)
  61. {
  62. std::string message;
  63. std::vector<std::string>::const_iterator i = args.begin();
  64. i++; // Get rid of subcommand
  65. std::string fileName = *i;
  66. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  67. {
  68. fileName = m_Makefile->GetCurrentDirectory();
  69. fileName += "/" + *i;
  70. }
  71. i++;
  72. for(;i != args.end(); ++i)
  73. {
  74. message += *i;
  75. }
  76. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  77. cmSystemTools::MakeDirectory(dir.c_str());
  78. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  79. if ( !file )
  80. {
  81. std::string error = "Internal CMake error when trying to open file: ";
  82. error += fileName.c_str();
  83. error += " for writting.";
  84. this->SetError(error.c_str());
  85. return false;
  86. }
  87. file << message;
  88. file.close();
  89. return true;
  90. }
  91. //----------------------------------------------------------------------------
  92. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  93. {
  94. if ( args.size() != 3 )
  95. {
  96. this->SetError("READ must be called with two additional arguments");
  97. return false;
  98. }
  99. std::string fileName = args[1];
  100. if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
  101. {
  102. fileName = m_Makefile->GetCurrentDirectory();
  103. fileName += "/" + args[1];
  104. }
  105. std::string variable = args[2];
  106. std::ifstream file(fileName.c_str(), std::ios::in);
  107. if ( !file )
  108. {
  109. std::string error = "Internal CMake error when trying to open file: ";
  110. error += fileName.c_str();
  111. error += " for reading.";
  112. this->SetError(error.c_str());
  113. return false;
  114. }
  115. std::string output;
  116. std::string line;
  117. bool has_newline = false;
  118. while ( cmSystemTools::GetLineFromStream(file, line, &has_newline) )
  119. {
  120. output += line;
  121. if ( has_newline )
  122. {
  123. output += "\n";
  124. }
  125. }
  126. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  127. return true;
  128. }
  129. //----------------------------------------------------------------------------
  130. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  131. bool recurse)
  132. {
  133. if ( args.size() < 2 )
  134. {
  135. this->SetError("GLOB requires at least a variable name");
  136. return false;
  137. }
  138. std::vector<std::string>::const_iterator i = args.begin();
  139. i++; // Get rid of subcommand
  140. std::string variable = *i;
  141. i++;
  142. cmGlob g;
  143. g.SetRecurse(recurse);
  144. std::string output = "";
  145. bool first = true;
  146. for ( ; i != args.end(); ++i )
  147. {
  148. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  149. {
  150. std::string expr = m_Makefile->GetCurrentDirectory();
  151. expr += "/" + *i;
  152. g.FindFiles(expr);
  153. }
  154. else
  155. {
  156. g.FindFiles(*i);
  157. }
  158. std::vector<std::string>::size_type cc;
  159. std::vector<std::string>& files = g.GetFiles();
  160. for ( cc = 0; cc < files.size(); cc ++ )
  161. {
  162. if ( !first )
  163. {
  164. output += ";";
  165. }
  166. output += files[cc];
  167. first = false;
  168. }
  169. }
  170. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  171. return true;
  172. }
  173. //----------------------------------------------------------------------------
  174. bool cmFileCommand::HandleMakeDirectoryCommand(std::vector<std::string> const& args)
  175. {
  176. if(args.size() < 2 )
  177. {
  178. this->SetError("called with incorrect number of arguments");
  179. return false;
  180. }
  181. std::vector<std::string>::const_iterator i = args.begin();
  182. i++; // Get rid of subcommand
  183. std::string expr;
  184. for ( ; i != args.end(); ++i )
  185. {
  186. const std::string* cdir = &(*i);
  187. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  188. {
  189. expr = m_Makefile->GetCurrentDirectory();
  190. expr += "/" + *i;
  191. cdir = &expr;
  192. }
  193. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  194. {
  195. std::string error = "problem creating directory: " + *cdir;
  196. this->SetError(error.c_str());
  197. return false;
  198. }
  199. }
  200. return true;
  201. }
  202. //----------------------------------------------------------------------------
  203. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  204. {
  205. if ( args.size() < 6 )
  206. {
  207. this->SetError("called with incorrect number of arguments");
  208. return false;
  209. }
  210. std::string destination = "";
  211. std::string stype = "FILES";
  212. std::vector<std::string> files;
  213. int itype = cmTarget::INSTALL_FILES;
  214. std::vector<std::string>::size_type i = 0;
  215. i++; // Get rid of subcommand
  216. std::string expr;
  217. bool in_files = false;
  218. for ( ; i != args.size(); ++i )
  219. {
  220. const std::string* cstr = &args[i];
  221. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  222. {
  223. i++;
  224. destination = args[i];
  225. in_files = false;
  226. }
  227. else if ( *cstr == "TYPE" && i < args.size()-1 )
  228. {
  229. i++;
  230. stype = args[i];
  231. in_files = false;
  232. }
  233. else if ( *cstr == "FILES" && !in_files)
  234. {
  235. in_files = true;
  236. }
  237. else if ( in_files )
  238. {
  239. files.push_back(*cstr);
  240. }
  241. else
  242. {
  243. this->SetError("called with inappropriate arguments");
  244. return false;
  245. }
  246. }
  247. if ( destination.size() == 0 )
  248. {
  249. this->SetError("called with inapropriate arguments. No DESTINATION provided.");
  250. return false;
  251. }
  252. if ( files.size() == 0 )
  253. {
  254. this->SetError("called with inapropriate arguments. No FILES provided.");
  255. return false;
  256. }
  257. if ( stype == "EXECUTABLE" )
  258. {
  259. itype = cmTarget::EXECUTABLE;
  260. }
  261. else if ( stype == "STATIC_LIBRARY" )
  262. {
  263. itype = cmTarget::STATIC_LIBRARY;
  264. }
  265. else if ( stype == "SHARED_LIBRARY" )
  266. {
  267. itype = cmTarget::SHARED_LIBRARY;
  268. }
  269. else if ( stype == "MODULE" )
  270. {
  271. itype = cmTarget::MODULE_LIBRARY;
  272. }
  273. for ( i = 0; i < files.size(); i ++ )
  274. {
  275. std::cout << " " << files[i];
  276. }
  277. std::cout << std::endl;
  278. if ( !cmSystemTools::FileExists(destination.c_str()) )
  279. {
  280. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  281. {
  282. std::string errstring = "cannot create directory: " + destination +
  283. ". Maybe need administrative privileges.";
  284. this->SetError(errstring.c_str());
  285. return false;
  286. }
  287. }
  288. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  289. {
  290. std::string errstring = "found file: " + destination +
  291. " where expecting directory with the same name.";
  292. this->SetError(errstring.c_str());
  293. return false;
  294. }
  295. for ( i = 0; i < files.size(); i ++ )
  296. {
  297. std::string destfile = destination + "/" + cmSystemTools::GetFilenameName(files[i]);
  298. if ( !cmSystemTools::CopyFileAlways(files[i].c_str(), destination.c_str()) )
  299. {
  300. std::string errstring = "cannot copy file: " + files[i] +
  301. " to directory : " + destination + ".";
  302. this->SetError(errstring.c_str());
  303. return false;
  304. }
  305. switch( itype )
  306. {
  307. case cmTarget::MODULE_LIBRARY:
  308. case cmTarget::SHARED_LIBRARY:
  309. case cmTarget::EXECUTABLE:
  310. if ( !cmSystemTools::SetPermissions(destfile.c_str(),
  311. #ifdef _MSC_VER
  312. S_IREAD | S_IWRITE | S_IEXEC
  313. #else
  314. S_IRUSR | S_IWUSR | S_IXUSR |
  315. S_IRGRP | S_IXGRP |
  316. S_IROTH | S_IXOTH
  317. #endif
  318. ) )
  319. {
  320. perror("problem doing chmod.");
  321. }
  322. }
  323. }
  324. return true;
  325. }