cmFileCommand.cxx 19 KB

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