cmFileCommand.cxx 19 KB

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