cmFileCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. // Handle script mode
  152. if ( expr.size() > 0 )
  153. {
  154. expr += "/" + *i;
  155. g.FindFiles(expr);
  156. }
  157. else
  158. {
  159. g.FindFiles(*i);
  160. }
  161. }
  162. else
  163. {
  164. g.FindFiles(*i);
  165. }
  166. std::vector<std::string>::size_type cc;
  167. std::vector<std::string>& files = g.GetFiles();
  168. for ( cc = 0; cc < files.size(); cc ++ )
  169. {
  170. if ( !first )
  171. {
  172. output += ";";
  173. }
  174. output += files[cc];
  175. first = false;
  176. }
  177. }
  178. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  179. return true;
  180. }
  181. //----------------------------------------------------------------------------
  182. bool cmFileCommand::HandleMakeDirectoryCommand(std::vector<std::string> const& args)
  183. {
  184. if(args.size() < 2 )
  185. {
  186. this->SetError("called with incorrect number of arguments");
  187. return false;
  188. }
  189. std::vector<std::string>::const_iterator i = args.begin();
  190. i++; // Get rid of subcommand
  191. std::string expr;
  192. for ( ; i != args.end(); ++i )
  193. {
  194. const std::string* cdir = &(*i);
  195. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  196. {
  197. expr = m_Makefile->GetCurrentDirectory();
  198. expr += "/" + *i;
  199. cdir = &expr;
  200. }
  201. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  202. {
  203. std::string error = "problem creating directory: " + *cdir;
  204. this->SetError(error.c_str());
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. //----------------------------------------------------------------------------
  211. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  212. {
  213. if ( args.size() < 6 )
  214. {
  215. this->SetError("called with incorrect number of arguments");
  216. return false;
  217. }
  218. std::string destination = "";
  219. std::string stype = "FILES";
  220. const char* build_type = m_Makefile->GetDefinition("BUILD_TYPE");
  221. const char* debug_postfix = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  222. std::string extra_dir = "";
  223. int debug = 0;
  224. if ( build_type )
  225. {
  226. extra_dir = build_type;
  227. std::string btype = cmSystemTools::LowerCase(build_type);
  228. if ( btype == "debug" )
  229. {
  230. debug = 1;
  231. }
  232. }
  233. std::vector<std::string> files;
  234. int itype = cmTarget::INSTALL_FILES;
  235. std::vector<std::string>::size_type i = 0;
  236. i++; // Get rid of subcommand
  237. bool in_files = false;
  238. bool optional = false;
  239. for ( ; i != args.size(); ++i )
  240. {
  241. const std::string* cstr = &args[i];
  242. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  243. {
  244. i++;
  245. destination = args[i];
  246. in_files = false;
  247. }
  248. else if ( *cstr == "TYPE" && i < args.size()-1 )
  249. {
  250. i++;
  251. stype = args[i];
  252. if ( args[i+1] == "OPTIONAL" )
  253. {
  254. i++;
  255. optional = true;
  256. }
  257. in_files = false;
  258. }
  259. else if ( *cstr == "FILES" && !in_files)
  260. {
  261. in_files = true;
  262. }
  263. else if ( in_files )
  264. {
  265. files.push_back(*cstr);
  266. }
  267. else
  268. {
  269. this->SetError("called with inappropriate arguments");
  270. return false;
  271. }
  272. }
  273. if ( destination.size() == 0 )
  274. {
  275. this->SetError("called with inapropriate arguments. No DESTINATION provided.");
  276. return false;
  277. }
  278. if ( files.size() == 0 )
  279. {
  280. this->SetError("called with inapropriate arguments. No FILES provided.");
  281. return false;
  282. }
  283. if ( stype == "EXECUTABLE" )
  284. {
  285. itype = cmTarget::EXECUTABLE;
  286. }
  287. else if ( stype == "PROGRAM" )
  288. {
  289. itype = cmTarget::INSTALL_PROGRAMS;
  290. }
  291. else if ( stype == "STATIC_LIBRARY" )
  292. {
  293. itype = cmTarget::STATIC_LIBRARY;
  294. }
  295. else if ( stype == "SHARED_LIBRARY" )
  296. {
  297. itype = cmTarget::SHARED_LIBRARY;
  298. }
  299. else if ( stype == "MODULE" )
  300. {
  301. itype = cmTarget::MODULE_LIBRARY;
  302. }
  303. if ( !cmSystemTools::FileExists(destination.c_str()) )
  304. {
  305. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  306. {
  307. std::string errstring = "cannot create directory: " + destination +
  308. ". Maybe need administrative privileges.";
  309. this->SetError(errstring.c_str());
  310. return false;
  311. }
  312. }
  313. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  314. {
  315. std::string errstring = "found file: " + destination +
  316. " where expecting directory with the same name.";
  317. this->SetError(errstring.c_str());
  318. return false;
  319. }
  320. for ( i = 0; i < files.size(); i ++ )
  321. {
  322. std::string destfile = destination + "/" + cmSystemTools::GetFilenameName(files[i]);
  323. std::string ctarget = files[i].c_str();
  324. std::string fname = cmSystemTools::GetFilenameName(ctarget);
  325. std::string ext = cmSystemTools::GetFilenameExtension(ctarget);
  326. std::string fnamewe = cmSystemTools::GetFilenameWithoutExtension(ctarget);
  327. switch( itype )
  328. {
  329. case cmTarget::MODULE_LIBRARY:
  330. case cmTarget::STATIC_LIBRARY:
  331. case cmTarget::SHARED_LIBRARY:
  332. if ( debug )
  333. {
  334. fname = fnamewe + debug_postfix + ext;
  335. }
  336. case cmTarget::EXECUTABLE:
  337. if ( extra_dir.size() > 0 )
  338. {
  339. cmOStringStream str;
  340. str << cmSystemTools::GetFilenamePath(ctarget) << "/" << extra_dir << "/"
  341. << fname;
  342. ctarget = str.str();
  343. }
  344. break;
  345. }
  346. if ( cmSystemTools::FileExists(ctarget.c_str()) )
  347. {
  348. if ( !cmSystemTools::CopyFileAlways(ctarget.c_str(), destination.c_str()) )
  349. {
  350. std::string errstring = "cannot copy file: " + ctarget +
  351. " to directory : " + destination + ".";
  352. this->SetError(errstring.c_str());
  353. return false;
  354. }
  355. switch( itype )
  356. {
  357. case cmTarget::MODULE_LIBRARY:
  358. case cmTarget::SHARED_LIBRARY:
  359. case cmTarget::EXECUTABLE:
  360. case cmTarget::INSTALL_PROGRAMS:
  361. if ( !cmSystemTools::SetPermissions(destfile.c_str(),
  362. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  363. S_IREAD | S_IWRITE | S_IEXEC
  364. #elif defined( __BORLANDC__ )
  365. S_IRUSR | S_IWUSR | S_IXUSR
  366. #else
  367. S_IRUSR | S_IWUSR | S_IXUSR |
  368. S_IRGRP | S_IXGRP |
  369. S_IROTH | S_IXOTH
  370. #endif
  371. ) )
  372. {
  373. perror("problem doing chmod.");
  374. }
  375. }
  376. }
  377. else
  378. {
  379. if ( !optional )
  380. {
  381. std::string errstring = "cannot find file: " + ctarget + " to install.";
  382. this->SetError(errstring.c_str());
  383. return false;
  384. }
  385. }
  386. }
  387. return true;
  388. }