cmFileCommand.cxx 15 KB

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