cmFileCommand.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 writting.";
  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 ( strncmp(btype.c_str(), "debug", strlen("debug")) == 0 )
  277. {
  278. debug = 1;
  279. }
  280. }
  281. std::vector<std::string> files;
  282. int itype = cmTarget::INSTALL_FILES;
  283. std::vector<std::string>::size_type i = 0;
  284. i++; // Get rid of subcommand
  285. std::map<cmStdString, const char*> properties;
  286. bool in_files = false;
  287. bool in_properties = false;
  288. bool optional = false;
  289. for ( ; i != args.size(); ++i )
  290. {
  291. const std::string* cstr = &args[i];
  292. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  293. {
  294. i++;
  295. destination = args[i];
  296. in_files = false;
  297. in_properties = false;
  298. }
  299. else if ( *cstr == "TYPE" && i < args.size()-1 )
  300. {
  301. i++;
  302. stype = args[i];
  303. if ( args[i+1] == "OPTIONAL" )
  304. {
  305. i++;
  306. optional = true;
  307. }
  308. in_properties = false;
  309. in_files = false;
  310. }
  311. else if ( *cstr == "PROPERTIES" )
  312. {
  313. in_properties = true;
  314. in_files = false;
  315. }
  316. else if ( *cstr == "FILES" && !in_files)
  317. {
  318. in_files = true;
  319. in_properties = false;
  320. }
  321. else if ( in_properties && i < args.size()-1 )
  322. {
  323. properties[args[i]] = args[i+1].c_str();
  324. i++;
  325. }
  326. else if ( in_files )
  327. {
  328. files.push_back(*cstr);
  329. }
  330. else
  331. {
  332. this->SetError("called with inappropriate arguments");
  333. return false;
  334. }
  335. }
  336. if ( destination.size() < 2 )
  337. {
  338. this->SetError("called with inapropriate arguments. "
  339. "No DESTINATION provided or .");
  340. return false;
  341. }
  342. int destDirLength = 0;
  343. if ( destdir && *destdir )
  344. {
  345. std::string sdestdir = destdir;
  346. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  347. char ch1 = destination[0];
  348. char ch2 = destination[1];
  349. char ch3 = 0;
  350. if ( destination.size() > 2 )
  351. {
  352. ch3 = destination[2];
  353. }
  354. int skip = 0;
  355. if ( ch1 != '/' )
  356. {
  357. int relative = 0;
  358. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'a' && ch1 <= 'z' ) &&
  359. ch2 == ':' )
  360. {
  361. // Assume windows
  362. // let's do some destdir magic:
  363. skip = 2;
  364. if ( ch3 != '/' )
  365. {
  366. relative = 1;
  367. }
  368. }
  369. else
  370. {
  371. relative = 1;
  372. }
  373. if ( relative )
  374. {
  375. // This is relative path on unix or windows. Since we are doing
  376. // destdir, this case does not make sense.
  377. this->SetError("called with relative DESTINATION. This "
  378. "does not make sense when using DESTDIR. Specify "
  379. "absolute path or remove DESTDIR environment variable.");
  380. return false;
  381. }
  382. }
  383. else
  384. {
  385. if ( ch2 == '/' )
  386. {
  387. // looks like a network path.
  388. this->SetError("called with network path DESTINATION. This "
  389. "does not make sense when using DESTDIR. Specify local "
  390. "absolute path or remove DESTDIR environment variable.");
  391. return false;
  392. }
  393. }
  394. destination = sdestdir + (destination.c_str() + skip);
  395. destDirLength = int(sdestdir.size());
  396. }
  397. if ( files.size() == 0 )
  398. {
  399. this->SetError(
  400. "called with inapropriate arguments. No FILES provided.");
  401. return false;
  402. }
  403. if ( stype == "EXECUTABLE" )
  404. {
  405. itype = cmTarget::EXECUTABLE;
  406. }
  407. else if ( stype == "PROGRAM" )
  408. {
  409. itype = cmTarget::INSTALL_PROGRAMS;
  410. }
  411. else if ( stype == "STATIC_LIBRARY" )
  412. {
  413. itype = cmTarget::STATIC_LIBRARY;
  414. }
  415. else if ( stype == "SHARED_LIBRARY" )
  416. {
  417. itype = cmTarget::SHARED_LIBRARY;
  418. }
  419. else if ( stype == "MODULE" )
  420. {
  421. itype = cmTarget::MODULE_LIBRARY;
  422. }
  423. if ( !cmSystemTools::FileExists(destination.c_str()) )
  424. {
  425. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  426. {
  427. std::string errstring = "cannot create directory: " + destination +
  428. ". Maybe need administrative privileges.";
  429. this->SetError(errstring.c_str());
  430. return false;
  431. }
  432. }
  433. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  434. {
  435. std::string errstring = "found file: " + destination +
  436. " where expecting directory with the same name.";
  437. this->SetError(errstring.c_str());
  438. return false;
  439. }
  440. const char* manifest_files =
  441. m_Makefile->GetDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  442. std::string smanifest_files;
  443. if ( manifest_files )
  444. {
  445. smanifest_files = manifest_files;
  446. }
  447. for ( i = 0; i < files.size(); i ++ )
  448. {
  449. std::string destfilewe
  450. = destination + "/"
  451. + cmSystemTools::GetFilenameWithoutExtension(files[i]);
  452. std::string ctarget = files[i].c_str();
  453. std::string fname = cmSystemTools::GetFilenameName(ctarget);
  454. std::string ext = cmSystemTools::GetFilenameExtension(ctarget);
  455. std::string fnamewe
  456. = cmSystemTools::GetFilenameWithoutExtension(ctarget);
  457. std::string destfile = destfilewe;
  458. if ( ext.size() )
  459. {
  460. destfile += ext;
  461. }
  462. switch( itype )
  463. {
  464. case cmTarget::MODULE_LIBRARY:
  465. case cmTarget::STATIC_LIBRARY:
  466. case cmTarget::SHARED_LIBRARY:
  467. if ( debug )
  468. {
  469. fname = fnamewe + debug_postfix + ext;
  470. destfile = destfilewe + debug_postfix + ext;
  471. }
  472. {
  473. // Handle shared library versioning
  474. const char* lib_version = 0;
  475. const char* lib_soversion = 0;
  476. if ( properties.find("VERSION") != properties.end() )
  477. {
  478. lib_version = properties["VERSION"];
  479. }
  480. if ( properties.find("SOVERSION") != properties.end() )
  481. {
  482. lib_soversion = properties["SOVERSION"];
  483. }
  484. if ( !lib_version && lib_soversion )
  485. {
  486. lib_version = lib_soversion;
  487. }
  488. if ( !lib_soversion && lib_version )
  489. {
  490. lib_soversion = lib_version;
  491. }
  492. if ( lib_version && lib_soversion )
  493. {
  494. std::string libname = destfile;
  495. std::string soname = destfile;
  496. std::string soname_nopath = fname;
  497. soname += ".";
  498. soname += lib_soversion;
  499. soname_nopath += ".";
  500. soname_nopath += lib_soversion;
  501. fname += ".";
  502. fname += lib_version;
  503. destfile += ".";
  504. destfile += lib_version;
  505. cmSystemTools::RemoveFile(soname.c_str());
  506. cmSystemTools::RemoveFile(libname.c_str());
  507. if (!cmSystemTools::CreateSymlink(soname_nopath.c_str(), libname.c_str()) )
  508. {
  509. std::string errstring = "error when creating symlink from: " + libname + " to " + soname_nopath;
  510. this->SetError(errstring.c_str());
  511. return false;
  512. }
  513. smanifest_files += ";";
  514. smanifest_files += libname.substr(destDirLength);;
  515. if ( destfile != soname )
  516. {
  517. if ( !cmSystemTools::CreateSymlink(fname.c_str(), soname.c_str()) )
  518. {
  519. std::string errstring = "error when creating symlink from: " + soname + " to " + fname;
  520. this->SetError(errstring.c_str());
  521. return false;
  522. }
  523. smanifest_files += ";";
  524. smanifest_files += soname.substr(destDirLength);
  525. }
  526. }
  527. cmOStringStream str;
  528. str << cmSystemTools::GetFilenamePath(ctarget) << "/";
  529. if ( extra_dir.size() > 0 )
  530. {
  531. str << extra_dir << "/";
  532. }
  533. str << fname;
  534. ctarget = str.str();
  535. }
  536. break;
  537. case cmTarget::EXECUTABLE:
  538. if ( extra_dir.size() > 0 )
  539. {
  540. cmOStringStream str;
  541. str << cmSystemTools::GetFilenamePath(ctarget)
  542. << "/" << extra_dir << "/"
  543. << fname;
  544. ctarget = str.str();
  545. }
  546. break;
  547. }
  548. if ( !cmSystemTools::SameFile(ctarget.c_str(), destfile.c_str()) )
  549. {
  550. if ( cmSystemTools::FileExists(ctarget.c_str()) )
  551. {
  552. cmSystemTools::RemoveFile(destfile.c_str());
  553. if ( !cmSystemTools::CopyFileAlways(ctarget.c_str(),
  554. destination.c_str()) )
  555. {
  556. std::string errstring = "cannot copy file: " + ctarget +
  557. " to directory : " + destination + ".";
  558. this->SetError(errstring.c_str());
  559. return false;
  560. }
  561. switch( itype )
  562. {
  563. case cmTarget::STATIC_LIBRARY:
  564. #if defined(__APPLE_CC__)
  565. {
  566. std::string ranlib = "ranlib ";
  567. ranlib += destfile;
  568. if(!cmSystemTools::RunSingleCommand(ranlib.c_str()))
  569. {
  570. std::string err = "ranlib failed: ";
  571. err += ranlib;
  572. this->SetError(err.c_str());
  573. }
  574. }
  575. #endif
  576. break;
  577. case cmTarget::MODULE_LIBRARY:
  578. case cmTarget::SHARED_LIBRARY:
  579. case cmTarget::EXECUTABLE:
  580. case cmTarget::INSTALL_PROGRAMS:
  581. if ( !cmSystemTools::SetPermissions(destfile.c_str(),
  582. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  583. S_IREAD | S_IWRITE | S_IEXEC
  584. #elif defined( __BORLANDC__ )
  585. S_IRUSR | S_IWUSR | S_IXUSR
  586. #else
  587. S_IRUSR | S_IWUSR | S_IXUSR |
  588. S_IRGRP | S_IXGRP |
  589. S_IROTH | S_IXOTH
  590. #endif
  591. ) )
  592. {
  593. cmOStringStream err;
  594. err << "Problem setting permissions on file: "
  595. << destfile.c_str();
  596. perror(err.str().c_str());
  597. }
  598. }
  599. smanifest_files += ";";
  600. smanifest_files += destfile.substr(destDirLength);
  601. }
  602. else
  603. {
  604. if ( !optional )
  605. {
  606. std::string errstring = "cannot find file: " +
  607. ctarget + " to install.";
  608. this->SetError(errstring.c_str());
  609. return false;
  610. }
  611. }
  612. }
  613. }
  614. m_Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  615. smanifest_files.c_str());
  616. return true;
  617. }