cmFileCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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(
  183. std::vector<std::string> const& args)
  184. {
  185. if(args.size() < 2 )
  186. {
  187. this->SetError("called with incorrect number of arguments");
  188. return false;
  189. }
  190. std::vector<std::string>::const_iterator i = args.begin();
  191. i++; // Get rid of subcommand
  192. std::string expr;
  193. for ( ; i != args.end(); ++i )
  194. {
  195. const std::string* cdir = &(*i);
  196. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  197. {
  198. expr = m_Makefile->GetCurrentDirectory();
  199. expr += "/" + *i;
  200. cdir = &expr;
  201. }
  202. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  203. {
  204. std::string error = "problem creating directory: " + *cdir;
  205. this->SetError(error.c_str());
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. //----------------------------------------------------------------------------
  212. bool cmFileCommand::HandleInstallCommand(
  213. std::vector<std::string> const& args)
  214. {
  215. if ( args.size() < 6 )
  216. {
  217. this->SetError("called with incorrect number of arguments");
  218. return false;
  219. }
  220. std::string destination = "";
  221. std::string stype = "FILES";
  222. const char* build_type = m_Makefile->GetDefinition("BUILD_TYPE");
  223. const char* debug_postfix
  224. = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  225. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  226. std::string extra_dir = "";
  227. int debug = 0;
  228. if ( build_type )
  229. {
  230. extra_dir = build_type;
  231. std::string btype = cmSystemTools::LowerCase(build_type);
  232. if ( btype == "debug" )
  233. {
  234. debug = 1;
  235. }
  236. }
  237. std::vector<std::string> files;
  238. int itype = cmTarget::INSTALL_FILES;
  239. std::vector<std::string>::size_type i = 0;
  240. i++; // Get rid of subcommand
  241. bool in_files = false;
  242. bool optional = false;
  243. for ( ; i != args.size(); ++i )
  244. {
  245. const std::string* cstr = &args[i];
  246. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  247. {
  248. i++;
  249. destination = args[i];
  250. in_files = false;
  251. }
  252. else if ( *cstr == "TYPE" && i < args.size()-1 )
  253. {
  254. i++;
  255. stype = args[i];
  256. if ( args[i+1] == "OPTIONAL" )
  257. {
  258. i++;
  259. optional = true;
  260. }
  261. in_files = false;
  262. }
  263. else if ( *cstr == "FILES" && !in_files)
  264. {
  265. in_files = true;
  266. }
  267. else if ( in_files )
  268. {
  269. files.push_back(*cstr);
  270. }
  271. else
  272. {
  273. this->SetError("called with inappropriate arguments");
  274. return false;
  275. }
  276. }
  277. if ( destination.size() < 2 )
  278. {
  279. this->SetError("called with inapropriate arguments. "
  280. "No DESTINATION provided or .");
  281. return false;
  282. }
  283. if ( destdir && *destdir )
  284. {
  285. std::string sdestdir = destdir;
  286. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  287. char ch1 = destination[0];
  288. char ch2 = destination[1];
  289. char ch3 = 0;
  290. if ( destination.size() > 2 )
  291. {
  292. ch3 = destination[2];
  293. }
  294. int skip = 0;
  295. if ( ch1 != '/' )
  296. {
  297. int relative = 0;
  298. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'a' && ch1 <= 'z' ) &&
  299. ch2 == ':' )
  300. {
  301. // Assume windows
  302. // let's do some destdir magic:
  303. skip = 2;
  304. if ( ch3 != '/' )
  305. {
  306. relative = 1;
  307. }
  308. }
  309. else
  310. {
  311. relative = 1;
  312. }
  313. if ( relative )
  314. {
  315. // This is relative path on unix or windows. Since we are doing
  316. // destdir, this case does not make sense.
  317. this->SetError("called with relative DESTINATION. This "
  318. "does not make sense when using DESTDIR. Specify "
  319. "absolute path or remove DESTDIR environment variable.");
  320. return false;
  321. }
  322. }
  323. else
  324. {
  325. if ( ch2 == '/' )
  326. {
  327. // looks like a network path.
  328. this->SetError("called with network path DESTINATION. This "
  329. "does not make sense when using DESTDIR. Specify local "
  330. "absolute path or remove DESTDIR environment variable.");
  331. return false;
  332. }
  333. }
  334. destination = sdestdir + (destination.c_str() + skip);
  335. }
  336. if ( files.size() == 0 )
  337. {
  338. this->SetError(
  339. "called with inapropriate arguments. No FILES provided.");
  340. return false;
  341. }
  342. if ( stype == "EXECUTABLE" )
  343. {
  344. itype = cmTarget::EXECUTABLE;
  345. }
  346. else if ( stype == "PROGRAM" )
  347. {
  348. itype = cmTarget::INSTALL_PROGRAMS;
  349. }
  350. else if ( stype == "STATIC_LIBRARY" )
  351. {
  352. itype = cmTarget::STATIC_LIBRARY;
  353. }
  354. else if ( stype == "SHARED_LIBRARY" )
  355. {
  356. itype = cmTarget::SHARED_LIBRARY;
  357. }
  358. else if ( stype == "MODULE" )
  359. {
  360. itype = cmTarget::MODULE_LIBRARY;
  361. }
  362. if ( !cmSystemTools::FileExists(destination.c_str()) )
  363. {
  364. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  365. {
  366. std::string errstring = "cannot create directory: " + destination +
  367. ". Maybe need administrative privileges.";
  368. this->SetError(errstring.c_str());
  369. return false;
  370. }
  371. }
  372. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  373. {
  374. std::string errstring = "found file: " + destination +
  375. " where expecting directory with the same name.";
  376. this->SetError(errstring.c_str());
  377. return false;
  378. }
  379. for ( i = 0; i < files.size(); i ++ )
  380. {
  381. std::string destfile
  382. = destination + "/" + cmSystemTools::GetFilenameName(files[i]);
  383. std::string ctarget = files[i].c_str();
  384. std::string fname = cmSystemTools::GetFilenameName(ctarget);
  385. std::string ext = cmSystemTools::GetFilenameExtension(ctarget);
  386. std::string fnamewe
  387. = cmSystemTools::GetFilenameWithoutExtension(ctarget);
  388. switch( itype )
  389. {
  390. case cmTarget::MODULE_LIBRARY:
  391. case cmTarget::STATIC_LIBRARY:
  392. case cmTarget::SHARED_LIBRARY:
  393. if ( debug )
  394. {
  395. fname = fnamewe + debug_postfix + ext;
  396. }
  397. case cmTarget::EXECUTABLE:
  398. if ( extra_dir.size() > 0 )
  399. {
  400. cmOStringStream str;
  401. str << cmSystemTools::GetFilenamePath(ctarget)
  402. << "/" << extra_dir << "/"
  403. << fname;
  404. ctarget = str.str();
  405. }
  406. break;
  407. }
  408. if ( cmSystemTools::FileExists(ctarget.c_str()) )
  409. {
  410. if ( !cmSystemTools::CopyFileAlways(ctarget.c_str(),
  411. destination.c_str()) )
  412. {
  413. std::string errstring = "cannot copy file: " + ctarget +
  414. " to directory : " + destination + ".";
  415. this->SetError(errstring.c_str());
  416. return false;
  417. }
  418. switch( itype )
  419. {
  420. case cmTarget::MODULE_LIBRARY:
  421. case cmTarget::SHARED_LIBRARY:
  422. case cmTarget::EXECUTABLE:
  423. case cmTarget::INSTALL_PROGRAMS:
  424. if ( !cmSystemTools::SetPermissions(destfile.c_str(),
  425. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  426. S_IREAD | S_IWRITE | S_IEXEC
  427. #elif defined( __BORLANDC__ )
  428. S_IRUSR | S_IWUSR | S_IXUSR
  429. #else
  430. S_IRUSR | S_IWUSR | S_IXUSR |
  431. S_IRGRP | S_IXGRP |
  432. S_IROTH | S_IXOTH
  433. #endif
  434. ) )
  435. {
  436. perror("problem doing chmod.");
  437. }
  438. }
  439. }
  440. else
  441. {
  442. if ( !optional )
  443. {
  444. std::string errstring = "cannot find file: " +
  445. ctarget + " to install.";
  446. this->SetError(errstring.c_str());
  447. return false;
  448. }
  449. }
  450. }
  451. return true;
  452. }