cmInstallCommand.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 "cmInstallCommand.h"
  14. #include "cmInstallFilesGenerator.h"
  15. #include "cmInstallScriptGenerator.h"
  16. #include "cmInstallTargetGenerator.h"
  17. // cmInstallCommand
  18. bool cmInstallCommand::InitialPass(std::vector<std::string> const& args)
  19. {
  20. // Allow calling with no arguments so that arguments may be built up
  21. // using a variable that may be left empty.
  22. if(args.empty())
  23. {
  24. return true;
  25. }
  26. // Switch among the command modes.
  27. if(args[0] == "SCRIPT")
  28. {
  29. return this->HandleScriptMode(args);
  30. }
  31. else if(args[0] == "TARGETS")
  32. {
  33. return this->HandleTargetsMode(args);
  34. }
  35. else if(args[0] == "FILES")
  36. {
  37. return this->HandleFilesMode(args);
  38. }
  39. else if(args[0] == "PROGRAMS")
  40. {
  41. return this->HandleFilesMode(args);
  42. }
  43. // Unknown mode.
  44. cmStdString e = "called with unknown mode ";
  45. e += args[0];
  46. this->SetError(e.c_str());
  47. return false;
  48. }
  49. //----------------------------------------------------------------------------
  50. bool cmInstallCommand::HandleScriptMode(std::vector<std::string> const& args)
  51. {
  52. bool doing_script = false;
  53. for(size_t i=0; i < args.size(); ++i)
  54. {
  55. if(args[i] == "SCRIPT")
  56. {
  57. doing_script = true;
  58. }
  59. else if(doing_script)
  60. {
  61. doing_script = false;
  62. std::string script = args[i];
  63. if(!cmSystemTools::FileIsFullPath(script.c_str()))
  64. {
  65. script = m_Makefile->GetCurrentDirectory();
  66. script += "/";
  67. script += args[i];
  68. }
  69. if(cmSystemTools::FileIsDirectory(script.c_str()))
  70. {
  71. this->SetError("given a directory as value of SCRIPT argument.");
  72. return false;
  73. }
  74. m_Makefile->AddInstallGenerator(
  75. new cmInstallScriptGenerator(script.c_str()));
  76. }
  77. }
  78. if(doing_script)
  79. {
  80. this->SetError("given no value for SCRIPT argument.");
  81. return false;
  82. }
  83. return true;
  84. }
  85. //----------------------------------------------------------------------------
  86. bool cmInstallCommand::HandleTargetsMode(std::vector<std::string> const& args)
  87. {
  88. // This is the TARGETS mode.
  89. bool doing_targets = true;
  90. bool doing_destination = false;
  91. bool doing_permissions = false;
  92. bool library_settings = true;
  93. bool runtime_settings = true;
  94. std::vector<cmTarget*> targets;
  95. const char* library_destination = 0;
  96. const char* runtime_destination = 0;
  97. std::string library_permissions;
  98. std::string runtime_permissions;
  99. for(unsigned int i=1; i < args.size(); ++i)
  100. {
  101. if(args[i] == "DESTINATION")
  102. {
  103. // Switch to setting the destination property.
  104. doing_targets = false;
  105. doing_destination = true;
  106. doing_permissions = false;
  107. }
  108. else if(args[i] == "PERMISSIONS")
  109. {
  110. // Switch to setting the permissions property.
  111. doing_targets = false;
  112. doing_destination = false;
  113. doing_permissions = true;
  114. }
  115. else if(args[i] == "LIBRARY")
  116. {
  117. // Switch to setting only library properties.
  118. doing_targets = false;
  119. doing_destination = false;
  120. doing_permissions = false;
  121. library_settings = true;
  122. runtime_settings = false;
  123. }
  124. else if(args[i] == "RUNTIME")
  125. {
  126. // Switch to setting only runtime properties.
  127. doing_targets = false;
  128. doing_destination = false;
  129. doing_permissions = false;
  130. library_settings = false;
  131. runtime_settings = true;
  132. }
  133. else if(doing_targets)
  134. {
  135. // Lookup this target in the current directory.
  136. if(cmTarget* target = m_Makefile->FindTarget(args[i].c_str()))
  137. {
  138. // Found the target. Check its type.
  139. if(target->GetType() != cmTarget::EXECUTABLE &&
  140. target->GetType() != cmTarget::STATIC_LIBRARY &&
  141. target->GetType() != cmTarget::SHARED_LIBRARY &&
  142. target->GetType() != cmTarget::MODULE_LIBRARY)
  143. {
  144. cmOStringStream e;
  145. e << "TARGETS given target \"" << args[i]
  146. << "\" which is not an executable, library, or module.";
  147. this->SetError(e.str().c_str());
  148. return false;
  149. }
  150. // Store the target in the list to be installed.
  151. targets.push_back(target);
  152. }
  153. else
  154. {
  155. // Did not find the target.
  156. cmOStringStream e;
  157. e << "TARGETS given target \"" << args[i]
  158. << "\" which does not exist in this directory.";
  159. this->SetError(e.str().c_str());
  160. return false;
  161. }
  162. }
  163. else if(doing_destination)
  164. {
  165. // Set the destination in the active set(s) of properties.
  166. if(library_settings)
  167. {
  168. library_destination = args[i].c_str();
  169. }
  170. if(runtime_settings)
  171. {
  172. runtime_destination = args[i].c_str();
  173. }
  174. doing_destination = false;
  175. }
  176. else if(doing_permissions)
  177. {
  178. // Set the permissions in the active set(s) of properties.
  179. if(library_settings)
  180. {
  181. // Check the requested permission.
  182. if(!this->CheckPermissions(args[i], library_permissions))
  183. {
  184. cmOStringStream e;
  185. e << args[0] << " given invalid permission \""
  186. << args[i] << "\".";
  187. this->SetError(e.str().c_str());
  188. return false;
  189. }
  190. }
  191. if(runtime_settings)
  192. {
  193. // Check the requested permission.
  194. if(!this->CheckPermissions(args[i], runtime_permissions))
  195. {
  196. cmOStringStream e;
  197. e << args[0] << " given invalid permission \""
  198. << args[i] << "\".";
  199. this->SetError(e.str().c_str());
  200. return false;
  201. }
  202. }
  203. }
  204. else
  205. {
  206. // Unknown argument.
  207. cmOStringStream e;
  208. e << "TARGETS given unknown argument \"" << args[i] << "\".";
  209. this->SetError(e.str().c_str());
  210. return false;
  211. }
  212. }
  213. // Check if there is something to do.
  214. if(targets.empty())
  215. {
  216. return true;
  217. }
  218. if(!library_destination && !runtime_destination)
  219. {
  220. this->SetError("TARGETS given no DESTINATION!");
  221. return false;
  222. }
  223. // Compute destination paths.
  224. std::string library_dest;
  225. std::string runtime_dest;
  226. this->ComputeDestination(library_destination, library_dest);
  227. this->ComputeDestination(runtime_destination, runtime_dest);
  228. // Generate install script code to install the given targets.
  229. for(std::vector<cmTarget*>::iterator ti = targets.begin();
  230. ti != targets.end(); ++ti)
  231. {
  232. // Handle each target type.
  233. cmTarget& target = *(*ti);
  234. switch(target.GetType())
  235. {
  236. case cmTarget::SHARED_LIBRARY:
  237. {
  238. // Shared libraries are handled differently on DLL and non-DLL
  239. // platforms. All windows platforms are DLL platforms
  240. // including cygwin. Currently no other platform is a DLL
  241. // platform.
  242. #if defined(_WIN32) || defined(__CYGWIN__)
  243. // This is a DLL platform.
  244. if(library_destination)
  245. {
  246. // The import library uses the LIBRARY properties.
  247. m_Makefile->AddInstallGenerator(
  248. new cmInstallTargetGenerator(target, library_dest.c_str(), true,
  249. library_permissions.c_str()));
  250. }
  251. if(runtime_destination)
  252. {
  253. // The DLL uses the RUNTIME properties.
  254. m_Makefile->AddInstallGenerator(
  255. new cmInstallTargetGenerator(target, runtime_dest.c_str(), false,
  256. runtime_permissions.c_str()));
  257. }
  258. #else
  259. // This is a non-DLL platform.
  260. if(library_destination)
  261. {
  262. // The shared library uses the LIBRARY properties.
  263. m_Makefile->AddInstallGenerator(
  264. new cmInstallTargetGenerator(target, library_dest.c_str(), false,
  265. library_permissions.c_str()));
  266. }
  267. #endif
  268. }
  269. break;
  270. case cmTarget::STATIC_LIBRARY:
  271. case cmTarget::MODULE_LIBRARY:
  272. {
  273. // Static libraries and modules use LIBRARY properties.
  274. if(library_destination)
  275. {
  276. m_Makefile->AddInstallGenerator(
  277. new cmInstallTargetGenerator(target, library_dest.c_str(), false,
  278. library_permissions.c_str()));
  279. }
  280. else
  281. {
  282. cmOStringStream e;
  283. e << "TARGETS given no LIBRARY DESTINATION for ";
  284. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  285. {
  286. e << "static library";
  287. }
  288. else
  289. {
  290. e << "module";
  291. }
  292. e << " target \"" << target.GetName() << "\".";
  293. this->SetError(e.str().c_str());
  294. return false;
  295. }
  296. }
  297. break;
  298. case cmTarget::EXECUTABLE:
  299. {
  300. // Executables use the RUNTIME properties.
  301. if(runtime_destination)
  302. {
  303. m_Makefile->AddInstallGenerator(
  304. new cmInstallTargetGenerator(target, runtime_dest.c_str(), false,
  305. runtime_permissions.c_str()));
  306. }
  307. else
  308. {
  309. cmOStringStream e;
  310. e << "TARGETS given no RUNTIME DESTINATION for executable target \""
  311. << target.GetName() << "\".";
  312. this->SetError(e.str().c_str());
  313. return false;
  314. }
  315. }
  316. break;
  317. default:
  318. // This should never happen due to the above type check.
  319. // Ignore the case.
  320. break;
  321. }
  322. }
  323. return true;
  324. }
  325. //----------------------------------------------------------------------------
  326. bool cmInstallCommand::HandleFilesMode(std::vector<std::string> const& args)
  327. {
  328. // This is the FILES mode.
  329. bool programs = (args[0] == "PROGRAMS");
  330. bool doing_files = true;
  331. bool doing_destination = false;
  332. bool doing_permissions = false;
  333. bool doing_rename = false;
  334. std::vector<std::string> files;
  335. const char* destination = 0;
  336. std::string rename;
  337. std::string permissions;
  338. for(unsigned int i=1; i < args.size(); ++i)
  339. {
  340. if(args[i] == "DESTINATION")
  341. {
  342. // Switch to setting the destination property.
  343. doing_files = false;
  344. doing_destination = true;
  345. doing_permissions = false;
  346. doing_rename = false;
  347. }
  348. else if(args[i] == "PERMISSIONS")
  349. {
  350. // Switch to setting the permissions property.
  351. doing_files = false;
  352. doing_destination = false;
  353. doing_permissions = true;
  354. doing_rename = false;
  355. }
  356. else if(args[i] == "RENAME")
  357. {
  358. // Switch to setting the rename property.
  359. doing_files = false;
  360. doing_destination = false;
  361. doing_permissions = false;
  362. doing_rename = true;
  363. }
  364. else if(doing_files)
  365. {
  366. // Convert this file to a full path.
  367. std::string file = args[i];
  368. if(!cmSystemTools::FileIsFullPath(file.c_str()))
  369. {
  370. file = m_Makefile->GetCurrentDirectory();
  371. file += "/";
  372. file += args[i];
  373. }
  374. // Make sure the file is not a directory.
  375. if(cmSystemTools::FileIsDirectory(file.c_str()))
  376. {
  377. cmOStringStream e;
  378. e << args[0] << " given directory \"" << args[i] << "\" to install.";
  379. this->SetError(e.str().c_str());
  380. return false;
  381. }
  382. // Store the file for installation.
  383. files.push_back(file);
  384. }
  385. else if(doing_destination)
  386. {
  387. destination = args[i].c_str();
  388. doing_destination = false;
  389. }
  390. else if(doing_permissions)
  391. {
  392. // Check the requested permission.
  393. if(!this->CheckPermissions(args[i], permissions))
  394. {
  395. cmOStringStream e;
  396. e << args[0] << " given invalid permission \""
  397. << args[i] << "\".";
  398. this->SetError(e.str().c_str());
  399. return false;
  400. }
  401. }
  402. else if(doing_rename)
  403. {
  404. rename = args[i];
  405. doing_rename = false;
  406. }
  407. else
  408. {
  409. // Unknown argument.
  410. cmOStringStream e;
  411. e << args[0] << " given unknown argument \"" << args[i] << "\".";
  412. this->SetError(e.str().c_str());
  413. return false;
  414. }
  415. }
  416. // Check if there is something to do.
  417. if(files.empty())
  418. {
  419. return true;
  420. }
  421. if(!destination)
  422. {
  423. // A destination is required.
  424. cmOStringStream e;
  425. e << args[0] << " given no DESTINATION!";
  426. this->SetError(e.str().c_str());
  427. return false;
  428. }
  429. if(!rename.empty() && files.size() > 1)
  430. {
  431. // The rename option works only with one file.
  432. cmOStringStream e;
  433. e << args[0] << " given RENAME option with more than one file.";
  434. this->SetError(e.str().c_str());
  435. return false;
  436. }
  437. // Compute destination path.
  438. std::string dest;
  439. this->ComputeDestination(destination, dest);
  440. // Create the files install generator.
  441. m_Makefile->AddInstallGenerator(
  442. new cmInstallFilesGenerator(files, dest.c_str(), programs,
  443. permissions.c_str(), rename.c_str()));
  444. return true;
  445. }
  446. //----------------------------------------------------------------------------
  447. void cmInstallCommand::ComputeDestination(const char* destination,
  448. std::string& dest)
  449. {
  450. if(destination)
  451. {
  452. if(cmSystemTools::FileIsFullPath(destination))
  453. {
  454. // Full paths are absolute.
  455. dest = destination;
  456. }
  457. else
  458. {
  459. // Relative paths are treated with respect to the installation prefix.
  460. dest = "${CMAKE_INSTALL_PREFIX}/";
  461. dest += destination;
  462. }
  463. // Format the path nicely. Note this also removes trailing
  464. // slashes.
  465. cmSystemTools::ConvertToUnixSlashes(dest);
  466. }
  467. else
  468. {
  469. dest = "";
  470. }
  471. }
  472. //----------------------------------------------------------------------------
  473. bool cmInstallCommand::CheckPermissions(std::string const& arg,
  474. std::string& permissions)
  475. {
  476. // Table of valid permissions.
  477. const char* table[] =
  478. {
  479. "OWNER_READ", "OWNER_WRITE", "OWNER_EXECUTE",
  480. "GROUP_READ", "GROUP_WRITE", "GROUP_EXECUTE",
  481. "WORLD_READ", "WORLD_WRITE", "WORLD_EXECUTE",
  482. "SETUID", "SETGID", 0
  483. };
  484. // Check the permission against the table.
  485. for(const char** valid = table; *valid; ++valid)
  486. {
  487. if(arg == *valid)
  488. {
  489. // This is a valid permission.
  490. permissions += " ";
  491. permissions += arg;
  492. return true;
  493. }
  494. }
  495. // This is not a valid permission.
  496. return false;
  497. }