cmFileCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. const char* build_type = m_Makefile->GetDefinition("BUILD_TYPE");
  213. std::string extra_dir = "";
  214. if ( build_type )
  215. {
  216. extra_dir = build_type;
  217. }
  218. std::vector<std::string> files;
  219. int itype = cmTarget::INSTALL_FILES;
  220. std::vector<std::string>::size_type i = 0;
  221. i++; // Get rid of subcommand
  222. bool in_files = false;
  223. bool optional = false;
  224. for ( ; i != args.size(); ++i )
  225. {
  226. const std::string* cstr = &args[i];
  227. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  228. {
  229. i++;
  230. destination = args[i];
  231. in_files = false;
  232. }
  233. else if ( *cstr == "TYPE" && i < args.size()-1 )
  234. {
  235. i++;
  236. stype = args[i];
  237. if ( args[i+1] == "OPTIONAL" )
  238. {
  239. i++;
  240. optional = true;
  241. }
  242. in_files = false;
  243. }
  244. else if ( *cstr == "FILES" && !in_files)
  245. {
  246. in_files = true;
  247. }
  248. else if ( in_files )
  249. {
  250. files.push_back(*cstr);
  251. }
  252. else
  253. {
  254. this->SetError("called with inappropriate arguments");
  255. return false;
  256. }
  257. }
  258. if ( destination.size() == 0 )
  259. {
  260. this->SetError("called with inapropriate arguments. No DESTINATION provided.");
  261. return false;
  262. }
  263. if ( files.size() == 0 )
  264. {
  265. this->SetError("called with inapropriate arguments. No FILES provided.");
  266. return false;
  267. }
  268. if ( stype == "EXECUTABLE" )
  269. {
  270. itype = cmTarget::EXECUTABLE;
  271. }
  272. else if ( stype == "PROGRAM" )
  273. {
  274. itype = cmTarget::INSTALL_PROGRAMS;
  275. }
  276. else if ( stype == "STATIC_LIBRARY" )
  277. {
  278. itype = cmTarget::STATIC_LIBRARY;
  279. }
  280. else if ( stype == "SHARED_LIBRARY" )
  281. {
  282. itype = cmTarget::SHARED_LIBRARY;
  283. }
  284. else if ( stype == "MODULE" )
  285. {
  286. itype = cmTarget::MODULE_LIBRARY;
  287. }
  288. if ( !cmSystemTools::FileExists(destination.c_str()) )
  289. {
  290. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  291. {
  292. std::string errstring = "cannot create directory: " + destination +
  293. ". Maybe need administrative privileges.";
  294. this->SetError(errstring.c_str());
  295. return false;
  296. }
  297. }
  298. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  299. {
  300. std::string errstring = "found file: " + destination +
  301. " where expecting directory with the same name.";
  302. this->SetError(errstring.c_str());
  303. return false;
  304. }
  305. for ( i = 0; i < files.size(); i ++ )
  306. {
  307. std::string destfile = destination + "/" + cmSystemTools::GetFilenameName(files[i]);
  308. std::string ctarget = files[i].c_str();
  309. switch( itype )
  310. {
  311. case cmTarget::MODULE_LIBRARY:
  312. case cmTarget::STATIC_LIBRARY:
  313. case cmTarget::SHARED_LIBRARY:
  314. case cmTarget::EXECUTABLE:
  315. if ( extra_dir.size() > 0 )
  316. {
  317. cmOStringStream str;
  318. str << cmSystemTools::GetFilenamePath(ctarget) << "/" << extra_dir << "/"
  319. << cmSystemTools::GetFilenameName(ctarget);
  320. ctarget = str.str();
  321. }
  322. break;
  323. }
  324. if ( cmSystemTools::FileExists(ctarget.c_str()) )
  325. {
  326. if ( !cmSystemTools::CopyFileAlways(ctarget.c_str(), destination.c_str()) )
  327. {
  328. std::string errstring = "cannot copy file: " + ctarget +
  329. " to directory : " + destination + ".";
  330. this->SetError(errstring.c_str());
  331. return false;
  332. }
  333. switch( itype )
  334. {
  335. case cmTarget::MODULE_LIBRARY:
  336. case cmTarget::SHARED_LIBRARY:
  337. case cmTarget::EXECUTABLE:
  338. case cmTarget::INSTALL_PROGRAMS:
  339. if ( !cmSystemTools::SetPermissions(destfile.c_str(),
  340. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  341. S_IREAD | S_IWRITE | S_IEXEC
  342. #elif defined( __BORLANDC__ )
  343. S_IRUSR | S_IWUSR | S_IXUSR
  344. #else
  345. S_IRUSR | S_IWUSR | S_IXUSR |
  346. S_IRGRP | S_IXGRP |
  347. S_IROTH | S_IXOTH
  348. #endif
  349. ) )
  350. {
  351. perror("problem doing chmod.");
  352. }
  353. }
  354. }
  355. else
  356. {
  357. if ( !optional )
  358. {
  359. std::string errstring = "cannot find file: " + ctarget + " to install.";
  360. this->SetError(errstring.c_str());
  361. return false;
  362. }
  363. }
  364. }
  365. return true;
  366. }