cmFileCommand.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. m_Makefile->AddWrittenFile(fileName.c_str());
  90. return true;
  91. }
  92. //----------------------------------------------------------------------------
  93. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  94. {
  95. if ( args.size() != 3 )
  96. {
  97. this->SetError("READ must be called with two additional arguments");
  98. return false;
  99. }
  100. std::string fileName = args[1];
  101. if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
  102. {
  103. fileName = m_Makefile->GetCurrentDirectory();
  104. fileName += "/" + args[1];
  105. }
  106. std::string variable = args[2];
  107. std::ifstream file(fileName.c_str(), std::ios::in);
  108. if ( !file )
  109. {
  110. std::string error = "Internal CMake error when trying to open file: ";
  111. error += fileName.c_str();
  112. error += " for reading.";
  113. this->SetError(error.c_str());
  114. return false;
  115. }
  116. std::string output;
  117. std::string line;
  118. bool has_newline = false;
  119. while ( cmSystemTools::GetLineFromStream(file, line, &has_newline) )
  120. {
  121. output += line;
  122. if ( has_newline )
  123. {
  124. output += "\n";
  125. }
  126. }
  127. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  128. return true;
  129. }
  130. //----------------------------------------------------------------------------
  131. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  132. bool recurse)
  133. {
  134. if ( args.size() < 2 )
  135. {
  136. this->SetError("GLOB requires at least a variable name");
  137. return false;
  138. }
  139. std::vector<std::string>::const_iterator i = args.begin();
  140. i++; // Get rid of subcommand
  141. std::string variable = *i;
  142. i++;
  143. cmGlob g;
  144. g.SetRecurse(recurse);
  145. std::string output = "";
  146. bool first = true;
  147. for ( ; i != args.end(); ++i )
  148. {
  149. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  150. {
  151. std::string expr = m_Makefile->GetCurrentDirectory();
  152. // Handle script mode
  153. if ( expr.size() > 0 )
  154. {
  155. expr += "/" + *i;
  156. g.FindFiles(expr);
  157. }
  158. else
  159. {
  160. g.FindFiles(*i);
  161. }
  162. }
  163. else
  164. {
  165. g.FindFiles(*i);
  166. }
  167. std::vector<std::string>::size_type cc;
  168. std::vector<std::string>& files = g.GetFiles();
  169. for ( cc = 0; cc < files.size(); cc ++ )
  170. {
  171. if ( !first )
  172. {
  173. output += ";";
  174. }
  175. output += files[cc];
  176. first = false;
  177. }
  178. }
  179. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  180. return true;
  181. }
  182. //----------------------------------------------------------------------------
  183. bool cmFileCommand::HandleMakeDirectoryCommand(
  184. std::vector<std::string> const& args)
  185. {
  186. if(args.size() < 2 )
  187. {
  188. this->SetError("called with incorrect number of arguments");
  189. return false;
  190. }
  191. std::vector<std::string>::const_iterator i = args.begin();
  192. i++; // Get rid of subcommand
  193. std::string expr;
  194. for ( ; i != args.end(); ++i )
  195. {
  196. const std::string* cdir = &(*i);
  197. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  198. {
  199. expr = m_Makefile->GetCurrentDirectory();
  200. expr += "/" + *i;
  201. cdir = &expr;
  202. }
  203. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  204. {
  205. std::string error = "problem creating directory: " + *cdir;
  206. this->SetError(error.c_str());
  207. return false;
  208. }
  209. }
  210. return true;
  211. }
  212. //----------------------------------------------------------------------------
  213. bool cmFileCommand::HandleInstallCommand(
  214. std::vector<std::string> const& args)
  215. {
  216. if ( args.size() < 6 )
  217. {
  218. this->SetError("called with incorrect number of arguments");
  219. return false;
  220. }
  221. std::string destination = "";
  222. std::string stype = "FILES";
  223. const char* build_type = m_Makefile->GetDefinition("BUILD_TYPE");
  224. const char* debug_postfix
  225. = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  226. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  227. std::string extra_dir = "";
  228. int debug = 0;
  229. if ( build_type )
  230. {
  231. extra_dir = build_type;
  232. std::string btype = cmSystemTools::LowerCase(build_type);
  233. if ( btype == "debug" )
  234. {
  235. debug = 1;
  236. }
  237. }
  238. std::vector<std::string> files;
  239. int itype = cmTarget::INSTALL_FILES;
  240. std::vector<std::string>::size_type i = 0;
  241. i++; // Get rid of subcommand
  242. bool in_files = false;
  243. bool optional = false;
  244. for ( ; i != args.size(); ++i )
  245. {
  246. const std::string* cstr = &args[i];
  247. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  248. {
  249. i++;
  250. destination = args[i];
  251. in_files = false;
  252. }
  253. else if ( *cstr == "TYPE" && i < args.size()-1 )
  254. {
  255. i++;
  256. stype = args[i];
  257. if ( args[i+1] == "OPTIONAL" )
  258. {
  259. i++;
  260. optional = true;
  261. }
  262. in_files = false;
  263. }
  264. else if ( *cstr == "FILES" && !in_files)
  265. {
  266. in_files = true;
  267. }
  268. else if ( in_files )
  269. {
  270. files.push_back(*cstr);
  271. }
  272. else
  273. {
  274. this->SetError("called with inappropriate arguments");
  275. return false;
  276. }
  277. }
  278. if ( destination.size() < 2 )
  279. {
  280. this->SetError("called with inapropriate arguments. "
  281. "No DESTINATION provided or .");
  282. return false;
  283. }
  284. if ( destdir && *destdir )
  285. {
  286. std::string sdestdir = destdir;
  287. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  288. char ch1 = destination[0];
  289. char ch2 = destination[1];
  290. char ch3 = 0;
  291. if ( destination.size() > 2 )
  292. {
  293. ch3 = destination[2];
  294. }
  295. int skip = 0;
  296. if ( ch1 != '/' )
  297. {
  298. int relative = 0;
  299. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'a' && ch1 <= 'z' ) &&
  300. ch2 == ':' )
  301. {
  302. // Assume windows
  303. // let's do some destdir magic:
  304. skip = 2;
  305. if ( ch3 != '/' )
  306. {
  307. relative = 1;
  308. }
  309. }
  310. else
  311. {
  312. relative = 1;
  313. }
  314. if ( relative )
  315. {
  316. // This is relative path on unix or windows. Since we are doing
  317. // destdir, this case does not make sense.
  318. this->SetError("called with relative DESTINATION. This "
  319. "does not make sense when using DESTDIR. Specify "
  320. "absolute path or remove DESTDIR environment variable.");
  321. return false;
  322. }
  323. }
  324. else
  325. {
  326. if ( ch2 == '/' )
  327. {
  328. // looks like a network path.
  329. this->SetError("called with network path DESTINATION. This "
  330. "does not make sense when using DESTDIR. Specify local "
  331. "absolute path or remove DESTDIR environment variable.");
  332. return false;
  333. }
  334. }
  335. destination = sdestdir + (destination.c_str() + skip);
  336. }
  337. if ( files.size() == 0 )
  338. {
  339. this->SetError(
  340. "called with inapropriate arguments. No FILES provided.");
  341. return false;
  342. }
  343. if ( stype == "EXECUTABLE" )
  344. {
  345. itype = cmTarget::EXECUTABLE;
  346. }
  347. else if ( stype == "PROGRAM" )
  348. {
  349. itype = cmTarget::INSTALL_PROGRAMS;
  350. }
  351. else if ( stype == "STATIC_LIBRARY" )
  352. {
  353. itype = cmTarget::STATIC_LIBRARY;
  354. }
  355. else if ( stype == "SHARED_LIBRARY" )
  356. {
  357. itype = cmTarget::SHARED_LIBRARY;
  358. }
  359. else if ( stype == "MODULE" )
  360. {
  361. itype = cmTarget::MODULE_LIBRARY;
  362. }
  363. if ( !cmSystemTools::FileExists(destination.c_str()) )
  364. {
  365. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  366. {
  367. std::string errstring = "cannot create directory: " + destination +
  368. ". Maybe need administrative privileges.";
  369. this->SetError(errstring.c_str());
  370. return false;
  371. }
  372. }
  373. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  374. {
  375. std::string errstring = "found file: " + destination +
  376. " where expecting directory with the same name.";
  377. this->SetError(errstring.c_str());
  378. return false;
  379. }
  380. const char* manifest_files =
  381. m_Makefile->GetDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  382. std::string smanifest_files;
  383. if ( manifest_files )
  384. {
  385. smanifest_files = manifest_files;
  386. }
  387. for ( i = 0; i < files.size(); i ++ )
  388. {
  389. std::string destfile
  390. = destination + "/" + cmSystemTools::GetFilenameName(files[i]);
  391. std::string ctarget = files[i].c_str();
  392. std::string fname = cmSystemTools::GetFilenameName(ctarget);
  393. std::string ext = cmSystemTools::GetFilenameExtension(ctarget);
  394. std::string fnamewe
  395. = cmSystemTools::GetFilenameWithoutExtension(ctarget);
  396. switch( itype )
  397. {
  398. case cmTarget::MODULE_LIBRARY:
  399. case cmTarget::STATIC_LIBRARY:
  400. case cmTarget::SHARED_LIBRARY:
  401. if ( debug )
  402. {
  403. fname = fnamewe + debug_postfix + ext;
  404. }
  405. case cmTarget::EXECUTABLE:
  406. if ( extra_dir.size() > 0 )
  407. {
  408. cmOStringStream str;
  409. str << cmSystemTools::GetFilenamePath(ctarget)
  410. << "/" << extra_dir << "/"
  411. << fname;
  412. ctarget = str.str();
  413. }
  414. break;
  415. }
  416. if ( cmSystemTools::FileExists(ctarget.c_str()) )
  417. {
  418. if ( !cmSystemTools::CopyFileAlways(ctarget.c_str(),
  419. destination.c_str()) )
  420. {
  421. std::string errstring = "cannot copy file: " + ctarget +
  422. " to directory : " + destination + ".";
  423. this->SetError(errstring.c_str());
  424. return false;
  425. }
  426. switch( itype )
  427. {
  428. case cmTarget::MODULE_LIBRARY:
  429. case cmTarget::SHARED_LIBRARY:
  430. case cmTarget::EXECUTABLE:
  431. case cmTarget::INSTALL_PROGRAMS:
  432. if ( !cmSystemTools::SetPermissions(destfile.c_str(),
  433. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  434. S_IREAD | S_IWRITE | S_IEXEC
  435. #elif defined( __BORLANDC__ )
  436. S_IRUSR | S_IWUSR | S_IXUSR
  437. #else
  438. S_IRUSR | S_IWUSR | S_IXUSR |
  439. S_IRGRP | S_IXGRP |
  440. S_IROTH | S_IXOTH
  441. #endif
  442. ) )
  443. {
  444. perror("problem doing chmod.");
  445. }
  446. smanifest_files += ";" + destfile;
  447. }
  448. }
  449. else
  450. {
  451. if ( !optional )
  452. {
  453. std::string errstring = "cannot find file: " +
  454. ctarget + " to install.";
  455. this->SetError(errstring.c_str());
  456. return false;
  457. }
  458. }
  459. }
  460. m_Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  461. smanifest_files.c_str());
  462. return true;
  463. }