cmFileCommand.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. mode_t mode =
  79. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  80. S_IREAD | S_IWRITE
  81. #elif defined( __BORLANDC__ )
  82. S_IRUSR | S_IWUSR
  83. #else
  84. S_IRUSR | S_IWUSR |
  85. S_IRGRP |
  86. S_IROTH
  87. #endif
  88. ;
  89. // Set permissions to writable
  90. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  91. {
  92. cmSystemTools::SetPermissions(fileName.c_str(),
  93. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  94. S_IREAD | S_IWRITE
  95. #else
  96. S_IRUSR | S_IWUSR
  97. #endif
  98. );
  99. }
  100. // If GetPermissions fails, pretend like it is ok. File open will fail if
  101. // the file is not writable
  102. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  103. if ( !file )
  104. {
  105. std::string error = "Internal CMake error when trying to open file: ";
  106. error += fileName.c_str();
  107. error += " for writing.";
  108. this->SetError(error.c_str());
  109. return false;
  110. }
  111. file << message;
  112. file.close();
  113. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  114. m_Makefile->AddWrittenFile(fileName.c_str());
  115. return true;
  116. }
  117. //----------------------------------------------------------------------------
  118. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  119. {
  120. if ( args.size() != 3 )
  121. {
  122. this->SetError("READ must be called with two additional arguments");
  123. return false;
  124. }
  125. std::string fileName = args[1];
  126. if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
  127. {
  128. fileName = m_Makefile->GetCurrentDirectory();
  129. fileName += "/" + args[1];
  130. }
  131. std::string variable = args[2];
  132. std::ifstream file(fileName.c_str(), std::ios::in);
  133. if ( !file )
  134. {
  135. std::string error = "Internal CMake error when trying to open file: ";
  136. error += fileName.c_str();
  137. error += " for reading.";
  138. this->SetError(error.c_str());
  139. return false;
  140. }
  141. std::string output;
  142. std::string line;
  143. bool has_newline = false;
  144. while ( cmSystemTools::GetLineFromStream(file, line, &has_newline) )
  145. {
  146. output += line;
  147. if ( has_newline )
  148. {
  149. output += "\n";
  150. }
  151. }
  152. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  153. return true;
  154. }
  155. //----------------------------------------------------------------------------
  156. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  157. bool recurse)
  158. {
  159. if ( args.size() < 2 )
  160. {
  161. this->SetError("GLOB requires at least a variable name");
  162. return false;
  163. }
  164. std::vector<std::string>::const_iterator i = args.begin();
  165. i++; // Get rid of subcommand
  166. std::string variable = *i;
  167. i++;
  168. cmGlob g;
  169. g.SetRecurse(recurse);
  170. std::string output = "";
  171. bool first = true;
  172. for ( ; i != args.end(); ++i )
  173. {
  174. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  175. {
  176. std::string expr = m_Makefile->GetCurrentDirectory();
  177. // Handle script mode
  178. if ( expr.size() > 0 )
  179. {
  180. expr += "/" + *i;
  181. g.FindFiles(expr);
  182. }
  183. else
  184. {
  185. g.FindFiles(*i);
  186. }
  187. }
  188. else
  189. {
  190. g.FindFiles(*i);
  191. }
  192. std::vector<std::string>::size_type cc;
  193. std::vector<std::string>& files = g.GetFiles();
  194. for ( cc = 0; cc < files.size(); cc ++ )
  195. {
  196. if ( !first )
  197. {
  198. output += ";";
  199. }
  200. output += files[cc];
  201. first = false;
  202. }
  203. }
  204. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  205. return true;
  206. }
  207. //----------------------------------------------------------------------------
  208. bool cmFileCommand::HandleMakeDirectoryCommand(
  209. std::vector<std::string> const& args)
  210. {
  211. if(args.size() < 2 )
  212. {
  213. this->SetError("called with incorrect number of arguments");
  214. return false;
  215. }
  216. std::vector<std::string>::const_iterator i = args.begin();
  217. i++; // Get rid of subcommand
  218. std::string expr;
  219. for ( ; i != args.end(); ++i )
  220. {
  221. const std::string* cdir = &(*i);
  222. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  223. {
  224. expr = m_Makefile->GetCurrentDirectory();
  225. expr += "/" + *i;
  226. cdir = &expr;
  227. }
  228. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  229. {
  230. std::string error = "problem creating directory: " + *cdir;
  231. this->SetError(error.c_str());
  232. return false;
  233. }
  234. }
  235. return true;
  236. }
  237. //----------------------------------------------------------------------------
  238. bool cmFileCommand::HandleInstallCommand(
  239. std::vector<std::string> const& args)
  240. {
  241. if ( args.size() < 6 )
  242. {
  243. this->SetError("called with incorrect number of arguments");
  244. return false;
  245. }
  246. std::string destination = "";
  247. std::string stype = "FILES";
  248. const char* build_type = m_Makefile->GetDefinition("BUILD_TYPE");
  249. if ( build_type && strcmp(build_type, ".") == 0 )
  250. {
  251. build_type = 0;
  252. }
  253. if ( build_type && strncmp(build_type, ".\\", 2) == 0 )
  254. {
  255. build_type += 2;
  256. }
  257. const char* debug_postfix
  258. = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  259. if(!debug_postfix)
  260. {
  261. debug_postfix = "";
  262. }
  263. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  264. std::string extra_dir = "";
  265. int debug = 0;
  266. if ( build_type )
  267. {
  268. extra_dir = build_type;
  269. std::string btype = cmSystemTools::LowerCase(build_type);
  270. if ( m_Makefile->GetDefinition("WIN32")
  271. && strncmp(btype.c_str(), "debug", strlen("debug")) == 0 )
  272. {
  273. debug = 1;
  274. }
  275. }
  276. std::vector<std::string> files;
  277. int itype = cmTarget::INSTALL_FILES;
  278. std::vector<std::string>::size_type i = 0;
  279. i++; // Get rid of subcommand
  280. std::map<cmStdString, const char*> properties;
  281. bool in_files = false;
  282. bool in_properties = false;
  283. bool optional = false;
  284. for ( ; i != args.size(); ++i )
  285. {
  286. const std::string* cstr = &args[i];
  287. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  288. {
  289. i++;
  290. destination = args[i];
  291. in_files = false;
  292. in_properties = false;
  293. }
  294. else if ( *cstr == "TYPE" && i < args.size()-1 )
  295. {
  296. i++;
  297. stype = args[i];
  298. if ( args[i+1] == "OPTIONAL" )
  299. {
  300. i++;
  301. optional = true;
  302. }
  303. in_properties = false;
  304. in_files = false;
  305. }
  306. else if ( *cstr == "PROPERTIES" )
  307. {
  308. in_properties = true;
  309. in_files = false;
  310. }
  311. else if ( *cstr == "FILES" && !in_files)
  312. {
  313. in_files = true;
  314. in_properties = false;
  315. }
  316. else if ( in_properties && i < args.size()-1 )
  317. {
  318. properties[args[i]] = args[i+1].c_str();
  319. i++;
  320. }
  321. else if ( in_files )
  322. {
  323. files.push_back(*cstr);
  324. }
  325. else
  326. {
  327. this->SetError("called with inappropriate arguments");
  328. return false;
  329. }
  330. }
  331. if ( destination.size() < 2 )
  332. {
  333. this->SetError("called with inapropriate arguments. "
  334. "No DESTINATION provided or .");
  335. return false;
  336. }
  337. int destDirLength = 0;
  338. if ( destdir && *destdir )
  339. {
  340. std::string sdestdir = destdir;
  341. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  342. char ch1 = destination[0];
  343. char ch2 = destination[1];
  344. char ch3 = 0;
  345. if ( destination.size() > 2 )
  346. {
  347. ch3 = destination[2];
  348. }
  349. int skip = 0;
  350. if ( ch1 != '/' )
  351. {
  352. int relative = 0;
  353. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'a' && ch1 <= 'z' ) &&
  354. ch2 == ':' )
  355. {
  356. // Assume windows
  357. // let's do some destdir magic:
  358. skip = 2;
  359. if ( ch3 != '/' )
  360. {
  361. relative = 1;
  362. }
  363. }
  364. else
  365. {
  366. relative = 1;
  367. }
  368. if ( relative )
  369. {
  370. // This is relative path on unix or windows. Since we are doing
  371. // destdir, this case does not make sense.
  372. this->SetError("called with relative DESTINATION. This "
  373. "does not make sense when using DESTDIR. Specify "
  374. "absolute path or remove DESTDIR environment variable.");
  375. return false;
  376. }
  377. }
  378. else
  379. {
  380. if ( ch2 == '/' )
  381. {
  382. // looks like a network path.
  383. this->SetError("called with network path DESTINATION. This "
  384. "does not make sense when using DESTDIR. Specify local "
  385. "absolute path or remove DESTDIR environment variable.");
  386. return false;
  387. }
  388. }
  389. destination = sdestdir + (destination.c_str() + skip);
  390. destDirLength = int(sdestdir.size());
  391. }
  392. if ( files.size() == 0 )
  393. {
  394. this->SetError(
  395. "called with inapropriate arguments. No FILES provided.");
  396. return false;
  397. }
  398. if ( stype == "EXECUTABLE" )
  399. {
  400. itype = cmTarget::EXECUTABLE;
  401. }
  402. else if ( stype == "PROGRAM" )
  403. {
  404. itype = cmTarget::INSTALL_PROGRAMS;
  405. }
  406. else if ( stype == "STATIC_LIBRARY" )
  407. {
  408. itype = cmTarget::STATIC_LIBRARY;
  409. }
  410. else if ( stype == "SHARED_LIBRARY" )
  411. {
  412. itype = cmTarget::SHARED_LIBRARY;
  413. }
  414. else if ( stype == "MODULE" )
  415. {
  416. itype = cmTarget::MODULE_LIBRARY;
  417. }
  418. if ( !cmSystemTools::FileExists(destination.c_str()) )
  419. {
  420. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  421. {
  422. std::string errstring = "cannot create directory: " + destination +
  423. ". Maybe need administrative privileges.";
  424. this->SetError(errstring.c_str());
  425. return false;
  426. }
  427. }
  428. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  429. {
  430. std::string errstring = "found file: " + destination +
  431. " where expecting directory with the same name.";
  432. this->SetError(errstring.c_str());
  433. return false;
  434. }
  435. const char* manifest_files =
  436. m_Makefile->GetDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  437. std::string smanifest_files;
  438. if ( manifest_files )
  439. {
  440. smanifest_files = manifest_files;
  441. }
  442. for ( i = 0; i < files.size(); i ++ )
  443. {
  444. std::string destfilewe
  445. = destination + "/"
  446. + cmSystemTools::GetFilenameWithoutExtension(files[i]);
  447. std::string ctarget = files[i].c_str();
  448. std::string fname = cmSystemTools::GetFilenameName(ctarget);
  449. std::string ext = cmSystemTools::GetFilenameExtension(ctarget);
  450. std::string fnamewe
  451. = cmSystemTools::GetFilenameWithoutExtension(ctarget);
  452. std::string destfile = destfilewe;
  453. if ( ext.size() )
  454. {
  455. destfile += ext;
  456. }
  457. switch( itype )
  458. {
  459. case cmTarget::MODULE_LIBRARY:
  460. case cmTarget::STATIC_LIBRARY:
  461. case cmTarget::SHARED_LIBRARY:
  462. if ( debug )
  463. {
  464. fname = fnamewe + debug_postfix + ext;
  465. destfile = destfilewe + debug_postfix + ext;
  466. }
  467. {
  468. // Handle shared library versioning
  469. const char* lib_version = 0;
  470. const char* lib_soversion = 0;
  471. if ( properties.find("VERSION") != properties.end() )
  472. {
  473. lib_version = properties["VERSION"];
  474. }
  475. if ( properties.find("SOVERSION") != properties.end() )
  476. {
  477. lib_soversion = properties["SOVERSION"];
  478. }
  479. if ( !lib_version && lib_soversion )
  480. {
  481. lib_version = lib_soversion;
  482. }
  483. if ( !lib_soversion && lib_version )
  484. {
  485. lib_soversion = lib_version;
  486. }
  487. if ( lib_version && lib_soversion )
  488. {
  489. std::string libname = destfile;
  490. std::string soname = destfile;
  491. std::string soname_nopath = fname;
  492. soname += ".";
  493. soname += lib_soversion;
  494. soname_nopath += ".";
  495. soname_nopath += lib_soversion;
  496. fname += ".";
  497. fname += lib_version;
  498. destfile += ".";
  499. destfile += lib_version;
  500. cmSystemTools::RemoveFile(soname.c_str());
  501. cmSystemTools::RemoveFile(libname.c_str());
  502. if (!cmSystemTools::CreateSymlink(soname_nopath.c_str(), libname.c_str()) )
  503. {
  504. std::string errstring = "error when creating symlink from: " + libname + " to " + soname_nopath;
  505. this->SetError(errstring.c_str());
  506. return false;
  507. }
  508. smanifest_files += ";";
  509. smanifest_files += libname.substr(destDirLength);;
  510. if ( destfile != soname )
  511. {
  512. if ( !cmSystemTools::CreateSymlink(fname.c_str(), soname.c_str()) )
  513. {
  514. std::string errstring = "error when creating symlink from: " + soname + " to " + fname;
  515. this->SetError(errstring.c_str());
  516. return false;
  517. }
  518. smanifest_files += ";";
  519. smanifest_files += soname.substr(destDirLength);
  520. }
  521. }
  522. // Reconstruct the source file path taking into account the
  523. // extra directory and possible new file name.
  524. cmOStringStream str;
  525. str << cmSystemTools::GetFilenamePath(ctarget) << "/";
  526. if ( extra_dir.size() > 0 )
  527. {
  528. str << extra_dir << "/";
  529. }
  530. str << fname;
  531. ctarget = str.str();
  532. }
  533. break;
  534. case cmTarget::EXECUTABLE:
  535. {
  536. // Handle executable versioning
  537. const char* exe_version = 0;
  538. if ( properties.find("VERSION") != properties.end() )
  539. {
  540. exe_version = properties["VERSION"];
  541. }
  542. if ( exe_version )
  543. {
  544. std::string exename = destfile;
  545. std::string exename_nopath = fname;
  546. exename_nopath += "-";
  547. exename_nopath += exe_version;
  548. fname += "-";
  549. fname += exe_version;
  550. destfile += "-";
  551. destfile += exe_version;
  552. cmSystemTools::RemoveFile(exename.c_str());
  553. if (!cmSystemTools::CreateSymlink(exename_nopath.c_str(), exename.c_str()) )
  554. {
  555. std::string errstring = "error when creating symlink from: " + exename + " to " + exename_nopath;
  556. this->SetError(errstring.c_str());
  557. return false;
  558. }
  559. smanifest_files += ";";
  560. smanifest_files += exename.substr(destDirLength);
  561. }
  562. // Reconstruct the source file path taking into account the
  563. // extra directory and possible new file name.
  564. cmOStringStream str;
  565. str << cmSystemTools::GetFilenamePath(ctarget) << "/";
  566. if ( extra_dir.size() > 0 )
  567. {
  568. str << extra_dir << "/";
  569. }
  570. str << fname;
  571. ctarget = str.str();
  572. }
  573. break;
  574. }
  575. if ( !cmSystemTools::SameFile(ctarget.c_str(), destfile.c_str()) )
  576. {
  577. if ( cmSystemTools::FileExists(ctarget.c_str()) )
  578. {
  579. cmSystemTools::RemoveFile(destfile.c_str());
  580. if ( !cmSystemTools::CopyFileAlways(ctarget.c_str(),
  581. destination.c_str()) )
  582. {
  583. std::string errstring = "cannot copy file: " + ctarget +
  584. " to directory : " + destination + ".";
  585. this->SetError(errstring.c_str());
  586. return false;
  587. }
  588. switch( itype )
  589. {
  590. case cmTarget::STATIC_LIBRARY:
  591. #if defined(__APPLE_CC__)
  592. {
  593. std::string ranlib = "ranlib ";
  594. ranlib += cmSystemTools::ConvertToOutputPath(destfile.c_str());
  595. if(!cmSystemTools::RunSingleCommand(ranlib.c_str()))
  596. {
  597. std::string err = "ranlib failed: ";
  598. err += ranlib;
  599. this->SetError(err.c_str());
  600. }
  601. }
  602. #endif
  603. break;
  604. case cmTarget::MODULE_LIBRARY:
  605. case cmTarget::SHARED_LIBRARY:
  606. case cmTarget::EXECUTABLE:
  607. case cmTarget::INSTALL_PROGRAMS:
  608. if ( !cmSystemTools::SetPermissions(destfile.c_str(),
  609. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  610. S_IREAD | S_IWRITE | S_IEXEC
  611. #elif defined( __BORLANDC__ )
  612. S_IRUSR | S_IWUSR | S_IXUSR
  613. #else
  614. S_IRUSR | S_IWUSR | S_IXUSR |
  615. S_IRGRP | S_IXGRP |
  616. S_IROTH | S_IXOTH
  617. #endif
  618. ) )
  619. {
  620. cmOStringStream err;
  621. err << "Problem setting permissions on file: "
  622. << destfile.c_str();
  623. perror(err.str().c_str());
  624. }
  625. }
  626. smanifest_files += ";";
  627. smanifest_files += destfile.substr(destDirLength);
  628. }
  629. else
  630. {
  631. if ( !optional )
  632. {
  633. std::string errstring = "cannot find file: " +
  634. ctarget + " to install.";
  635. this->SetError(errstring.c_str());
  636. return false;
  637. }
  638. }
  639. }
  640. }
  641. m_Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  642. smanifest_files.c_str());
  643. return true;
  644. }