cmFileCommand.cxx 16 KB

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