cmFileCommand.cxx 16 KB

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