cmFileCommand.cxx 16 KB

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