cmInstallCommand.cxx 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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 "cmInstallDirectoryGenerator.h"
  15. #include "cmInstallFilesGenerator.h"
  16. #include "cmInstallScriptGenerator.h"
  17. #include "cmInstallTargetGenerator.h"
  18. #include <cmsys/Glob.hxx>
  19. // cmInstallCommand
  20. bool cmInstallCommand::InitialPass(std::vector<std::string> const& args)
  21. {
  22. // Allow calling with no arguments so that arguments may be built up
  23. // using a variable that may be left empty.
  24. if(args.empty())
  25. {
  26. return true;
  27. }
  28. // Enable the install target.
  29. this->Makefile->GetLocalGenerator()
  30. ->GetGlobalGenerator()->EnableInstallTarget();
  31. // Switch among the command modes.
  32. if(args[0] == "SCRIPT")
  33. {
  34. return this->HandleScriptMode(args);
  35. }
  36. else if(args[0] == "CODE")
  37. {
  38. return this->HandleScriptMode(args);
  39. }
  40. else if(args[0] == "TARGETS")
  41. {
  42. return this->HandleTargetsMode(args);
  43. }
  44. else if(args[0] == "FILES")
  45. {
  46. return this->HandleFilesMode(args);
  47. }
  48. else if(args[0] == "PROGRAMS")
  49. {
  50. return this->HandleFilesMode(args);
  51. }
  52. else if(args[0] == "DIRECTORY")
  53. {
  54. return this->HandleDirectoryMode(args);
  55. }
  56. // Unknown mode.
  57. cmStdString e = "called with unknown mode ";
  58. e += args[0];
  59. this->SetError(e.c_str());
  60. return false;
  61. }
  62. //----------------------------------------------------------------------------
  63. bool cmInstallCommand::HandleScriptMode(std::vector<std::string> const& args)
  64. {
  65. bool doing_script = false;
  66. bool doing_code = false;
  67. for(size_t i=0; i < args.size(); ++i)
  68. {
  69. if(args[i] == "SCRIPT")
  70. {
  71. doing_script = true;
  72. doing_code = false;
  73. }
  74. else if(args[i] == "CODE")
  75. {
  76. doing_script = false;
  77. doing_code = true;
  78. }
  79. else if(doing_script)
  80. {
  81. doing_script = false;
  82. std::string script = args[i];
  83. if(!cmSystemTools::FileIsFullPath(script.c_str()))
  84. {
  85. script = this->Makefile->GetCurrentDirectory();
  86. script += "/";
  87. script += args[i];
  88. }
  89. if(cmSystemTools::FileIsDirectory(script.c_str()))
  90. {
  91. this->SetError("given a directory as value of SCRIPT argument.");
  92. return false;
  93. }
  94. this->Makefile->AddInstallGenerator(
  95. new cmInstallScriptGenerator(script.c_str()));
  96. }
  97. else if(doing_code)
  98. {
  99. doing_code = false;
  100. std::string code = args[i];
  101. this->Makefile->AddInstallGenerator(
  102. new cmInstallScriptGenerator(code.c_str(), true));
  103. }
  104. }
  105. if(doing_script)
  106. {
  107. this->SetError("given no value for SCRIPT argument.");
  108. return false;
  109. }
  110. if(doing_code)
  111. {
  112. this->SetError("given no value for CODE argument.");
  113. return false;
  114. }
  115. return true;
  116. }
  117. //----------------------------------------------------------------------------
  118. bool cmInstallCommand::HandleTargetsMode(std::vector<std::string> const& args)
  119. {
  120. // This is the TARGETS mode.
  121. bool doing_targets = true;
  122. bool doing_destination = false;
  123. bool doing_permissions = false;
  124. bool doing_component = false;
  125. bool doing_configurations = false;
  126. bool archive_settings = true;
  127. bool library_settings = true;
  128. bool runtime_settings = true;
  129. std::vector<cmTarget*> targets;
  130. const char* archive_destination = 0;
  131. const char* library_destination = 0;
  132. const char* runtime_destination = 0;
  133. std::string archive_permissions;
  134. std::string library_permissions;
  135. std::string runtime_permissions;
  136. std::string archive_component;
  137. std::string library_component;
  138. std::string runtime_component;
  139. std::vector<std::string> archive_configurations;
  140. std::vector<std::string> library_configurations;
  141. std::vector<std::string> runtime_configurations;
  142. bool archive_optional = false;
  143. bool library_optional = false;
  144. bool runtime_optional = false;
  145. for(unsigned int i=1; i < args.size(); ++i)
  146. {
  147. if(args[i] == "DESTINATION")
  148. {
  149. // Switch to setting the destination property.
  150. doing_targets = false;
  151. doing_destination = true;
  152. doing_permissions = false;
  153. doing_component = false;
  154. doing_configurations = false;
  155. }
  156. else if(args[i] == "PERMISSIONS")
  157. {
  158. // Switch to setting the permissions property.
  159. doing_targets = false;
  160. doing_destination = false;
  161. doing_permissions = true;
  162. doing_component = false;
  163. doing_configurations = false;
  164. }
  165. else if(args[i] == "COMPONENT")
  166. {
  167. // Switch to setting the component property.
  168. doing_targets = false;
  169. doing_destination = false;
  170. doing_permissions = false;
  171. doing_component = true;
  172. doing_configurations = false;
  173. }
  174. else if(args[i] == "CONFIGURATIONS")
  175. {
  176. // Switch to setting the configurations property.
  177. doing_targets = false;
  178. doing_destination = false;
  179. doing_permissions = false;
  180. doing_component = false;
  181. doing_configurations = true;
  182. }
  183. else if(args[i] == "ARCHIVE")
  184. {
  185. // Switch to setting only archive properties.
  186. doing_targets = false;
  187. doing_destination = false;
  188. doing_permissions = false;
  189. doing_component = false;
  190. doing_configurations = false;
  191. archive_settings = true;
  192. library_settings = false;
  193. runtime_settings = false;
  194. }
  195. else if(args[i] == "LIBRARY")
  196. {
  197. // Switch to setting only library properties.
  198. doing_targets = false;
  199. doing_destination = false;
  200. doing_permissions = false;
  201. doing_component = false;
  202. doing_configurations = false;
  203. archive_settings = false;
  204. library_settings = true;
  205. runtime_settings = false;
  206. }
  207. else if(args[i] == "RUNTIME")
  208. {
  209. // Switch to setting only runtime properties.
  210. doing_targets = false;
  211. doing_destination = false;
  212. doing_permissions = false;
  213. doing_component = false;
  214. doing_configurations = false;
  215. archive_settings = false;
  216. library_settings = false;
  217. runtime_settings = true;
  218. }
  219. else if(args[i] == "OPTIONAL")
  220. {
  221. // Set the optional property.
  222. doing_targets = false;
  223. doing_destination = false;
  224. doing_permissions = false;
  225. doing_component = false;
  226. doing_configurations = false;
  227. if(archive_settings)
  228. {
  229. archive_optional = true;
  230. }
  231. if(library_settings)
  232. {
  233. library_optional = true;
  234. }
  235. if(runtime_settings)
  236. {
  237. runtime_optional = true;
  238. }
  239. }
  240. else if(doing_targets)
  241. {
  242. // Lookup this target in the current directory.
  243. if(cmTarget* target = this->Makefile->FindTarget(args[i].c_str(), false))
  244. {
  245. // Found the target. Check its type.
  246. if(target->GetType() != cmTarget::EXECUTABLE &&
  247. target->GetType() != cmTarget::STATIC_LIBRARY &&
  248. target->GetType() != cmTarget::SHARED_LIBRARY &&
  249. target->GetType() != cmTarget::MODULE_LIBRARY)
  250. {
  251. cmOStringStream e;
  252. e << "TARGETS given target \"" << args[i]
  253. << "\" which is not an executable, library, or module.";
  254. this->SetError(e.str().c_str());
  255. return false;
  256. }
  257. // Store the target in the list to be installed.
  258. targets.push_back(target);
  259. }
  260. else
  261. {
  262. // Did not find the target.
  263. cmOStringStream e;
  264. e << "TARGETS given target \"" << args[i]
  265. << "\" which does not exist in this directory.";
  266. this->SetError(e.str().c_str());
  267. return false;
  268. }
  269. }
  270. else if(doing_destination)
  271. {
  272. // Set the destination in the active set(s) of properties.
  273. if(archive_settings)
  274. {
  275. archive_destination = args[i].c_str();
  276. }
  277. if(library_settings)
  278. {
  279. library_destination = args[i].c_str();
  280. }
  281. if(runtime_settings)
  282. {
  283. runtime_destination = args[i].c_str();
  284. }
  285. doing_destination = false;
  286. }
  287. else if(doing_component)
  288. {
  289. // Set the component in the active set(s) of properties.
  290. if(archive_settings)
  291. {
  292. archive_component = args[i];
  293. }
  294. if(library_settings)
  295. {
  296. library_component = args[i];
  297. }
  298. if(runtime_settings)
  299. {
  300. runtime_component = args[i];
  301. }
  302. doing_component = false;
  303. }
  304. else if(doing_permissions)
  305. {
  306. // Set the permissions in the active set(s) of properties.
  307. if(archive_settings)
  308. {
  309. // Check the requested permission.
  310. if(!this->CheckPermissions(args[i], archive_permissions))
  311. {
  312. cmOStringStream e;
  313. e << args[0] << " given invalid permission \""
  314. << args[i] << "\".";
  315. this->SetError(e.str().c_str());
  316. return false;
  317. }
  318. }
  319. if(library_settings)
  320. {
  321. // Check the requested permission.
  322. if(!this->CheckPermissions(args[i], library_permissions))
  323. {
  324. cmOStringStream e;
  325. e << args[0] << " given invalid permission \""
  326. << args[i] << "\".";
  327. this->SetError(e.str().c_str());
  328. return false;
  329. }
  330. }
  331. if(runtime_settings)
  332. {
  333. // Check the requested permission.
  334. if(!this->CheckPermissions(args[i], runtime_permissions))
  335. {
  336. cmOStringStream e;
  337. e << args[0] << " given invalid permission \""
  338. << args[i] << "\".";
  339. this->SetError(e.str().c_str());
  340. return false;
  341. }
  342. }
  343. }
  344. else if(doing_configurations)
  345. {
  346. // Add the configuration in the active set(s) of properties.
  347. if(archive_settings)
  348. {
  349. archive_configurations.push_back(args[i]);
  350. }
  351. if(library_settings)
  352. {
  353. library_configurations.push_back(args[i]);
  354. }
  355. if(runtime_settings)
  356. {
  357. runtime_configurations.push_back(args[i]);
  358. }
  359. }
  360. else
  361. {
  362. // Unknown argument.
  363. cmOStringStream e;
  364. e << "TARGETS given unknown argument \"" << args[i] << "\".";
  365. this->SetError(e.str().c_str());
  366. return false;
  367. }
  368. }
  369. // Check if there is something to do.
  370. if(targets.empty())
  371. {
  372. return true;
  373. }
  374. if(!archive_destination && !library_destination && !runtime_destination)
  375. {
  376. this->SetError("TARGETS given no DESTINATION!");
  377. return false;
  378. }
  379. // Check whether this is a DLL platform.
  380. bool dll_platform = (this->Makefile->IsOn("WIN32") ||
  381. this->Makefile->IsOn("CYGWIN") ||
  382. this->Makefile->IsOn("MINGW"));
  383. // Compute destination paths.
  384. std::string archive_dest;
  385. std::string library_dest;
  386. std::string runtime_dest;
  387. this->ComputeDestination(archive_destination, archive_dest);
  388. this->ComputeDestination(library_destination, library_dest);
  389. this->ComputeDestination(runtime_destination, runtime_dest);
  390. // Generate install script code to install the given targets.
  391. for(std::vector<cmTarget*>::iterator ti = targets.begin();
  392. ti != targets.end(); ++ti)
  393. {
  394. // Handle each target type.
  395. cmTarget& target = *(*ti);
  396. cmInstallTargetGenerator* archiveGenerator = 0;
  397. cmInstallTargetGenerator* runtimeGenerator = 0;
  398. cmInstallTargetGenerator* libraryGenerator = 0;
  399. switch(target.GetType())
  400. {
  401. case cmTarget::SHARED_LIBRARY:
  402. {
  403. // Shared libraries are handled differently on DLL and non-DLL
  404. // platforms. All windows platforms are DLL platforms
  405. // including cygwin. Currently no other platform is a DLL
  406. // platform.
  407. if(dll_platform)
  408. {
  409. // This is a DLL platform.
  410. if(archive_destination)
  411. {
  412. // The import library uses the ARCHIVE properties.
  413. archiveGenerator = new cmInstallTargetGenerator(target,
  414. archive_dest.c_str(),
  415. true,
  416. archive_permissions.c_str(),
  417. archive_configurations,
  418. archive_component.c_str(),
  419. archive_optional);
  420. }
  421. if(runtime_destination)
  422. {
  423. // The DLL uses the RUNTIME properties.
  424. runtimeGenerator = new cmInstallTargetGenerator(target,
  425. runtime_dest.c_str(),
  426. false,
  427. runtime_permissions.c_str(),
  428. runtime_configurations,
  429. runtime_component.c_str(),
  430. runtime_optional);
  431. }
  432. }
  433. else
  434. {
  435. // This is a non-DLL platform.
  436. if(library_destination)
  437. {
  438. // The shared library uses the LIBRARY properties.
  439. libraryGenerator = new cmInstallTargetGenerator(target,
  440. library_dest.c_str(),
  441. false,
  442. library_permissions.c_str(),
  443. library_configurations,
  444. library_component.c_str(),
  445. library_optional);
  446. }
  447. else
  448. {
  449. cmOStringStream e;
  450. e << "TARGETS given no LIBRARY DESTINATION for shared library "
  451. "target \"" << target.GetName() << "\".";
  452. this->SetError(e.str().c_str());
  453. return false;
  454. }
  455. }
  456. }
  457. break;
  458. case cmTarget::STATIC_LIBRARY:
  459. {
  460. // Static libraries use ARCHIVE properties.
  461. if(archive_destination)
  462. {
  463. archiveGenerator = new cmInstallTargetGenerator(target,
  464. archive_dest.c_str(),
  465. false,
  466. archive_permissions.c_str(),
  467. archive_configurations,
  468. archive_component.c_str(),
  469. archive_optional);
  470. }
  471. else
  472. {
  473. cmOStringStream e;
  474. e << "TARGETS given no ARCHIVE DESTINATION for static library "
  475. "target \"" << target.GetName() << "\".";
  476. this->SetError(e.str().c_str());
  477. return false;
  478. }
  479. }
  480. break;
  481. case cmTarget::MODULE_LIBRARY:
  482. {
  483. // Modules use LIBRARY properties.
  484. if(library_destination)
  485. {
  486. libraryGenerator = new cmInstallTargetGenerator(target,
  487. library_dest.c_str(),
  488. false,
  489. library_permissions.c_str(),
  490. library_configurations,
  491. library_component.c_str(),
  492. library_optional);
  493. }
  494. else
  495. {
  496. cmOStringStream e;
  497. e << "TARGETS given no LIBRARY DESTINATION for module target \""
  498. << target.GetName() << "\".";
  499. this->SetError(e.str().c_str());
  500. return false;
  501. }
  502. }
  503. break;
  504. case cmTarget::EXECUTABLE:
  505. {
  506. // Executables use the RUNTIME properties.
  507. if(runtime_destination)
  508. {
  509. runtimeGenerator = new cmInstallTargetGenerator(target,
  510. runtime_dest.c_str(),
  511. false,
  512. runtime_permissions.c_str(),
  513. runtime_configurations,
  514. runtime_component.c_str(),
  515. runtime_optional);
  516. }
  517. else
  518. {
  519. cmOStringStream e;
  520. e << "TARGETS given no RUNTIME DESTINATION for executable target \""
  521. << target.GetName() << "\".";
  522. this->SetError(e.str().c_str());
  523. return false;
  524. }
  525. // On DLL platforms an executable may also have an import
  526. // library. Install it to the archive destination if it
  527. // exists.
  528. if(dll_platform && archive_destination)
  529. {
  530. // The import library uses the ARCHIVE properties.
  531. archiveGenerator = new cmInstallTargetGenerator(target,
  532. archive_dest.c_str(),
  533. true,
  534. archive_permissions.c_str(),
  535. archive_configurations,
  536. archive_component.c_str(),
  537. true);
  538. }
  539. }
  540. break;
  541. default:
  542. // This should never happen due to the above type check.
  543. // Ignore the case.
  544. break;
  545. }
  546. this->Makefile->AddInstallGenerator(archiveGenerator);
  547. this->Makefile->AddInstallGenerator(runtimeGenerator);
  548. this->Makefile->AddInstallGenerator(libraryGenerator);
  549. }
  550. // Tell the global generator about any installation component names
  551. // specified.
  552. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  553. ->AddInstallComponent(archive_component.c_str());
  554. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  555. ->AddInstallComponent(library_component.c_str());
  556. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  557. ->AddInstallComponent(runtime_component.c_str());
  558. return true;
  559. }
  560. //----------------------------------------------------------------------------
  561. bool cmInstallCommand::HandleFilesMode(std::vector<std::string> const& args)
  562. {
  563. // This is the FILES mode.
  564. bool programs = (args[0] == "PROGRAMS");
  565. bool doing_files = true;
  566. bool doing_destination = false;
  567. bool doing_permissions = false;
  568. bool doing_configurations = false;
  569. bool doing_component = false;
  570. bool doing_rename = false;
  571. std::vector<std::string> files;
  572. const char* destination = 0;
  573. std::string rename;
  574. std::string permissions;
  575. std::vector<std::string> configurations;
  576. std::string component;
  577. bool optional = false;
  578. for(unsigned int i=1; i < args.size(); ++i)
  579. {
  580. if(args[i] == "DESTINATION")
  581. {
  582. // Switch to setting the destination property.
  583. doing_files = false;
  584. doing_destination = true;
  585. doing_permissions = false;
  586. doing_configurations = false;
  587. doing_component = false;
  588. doing_rename = false;
  589. }
  590. else if(args[i] == "PERMISSIONS")
  591. {
  592. // Switch to setting the permissions property.
  593. doing_files = false;
  594. doing_destination = false;
  595. doing_permissions = true;
  596. doing_configurations = false;
  597. doing_component = false;
  598. doing_rename = false;
  599. }
  600. else if(args[i] == "CONFIGURATIONS")
  601. {
  602. // Switch to setting the configurations property.
  603. doing_files = false;
  604. doing_destination = false;
  605. doing_permissions = false;
  606. doing_configurations = true;
  607. doing_component = false;
  608. doing_rename = false;
  609. }
  610. else if(args[i] == "COMPONENT")
  611. {
  612. // Switch to setting the component property.
  613. doing_files = false;
  614. doing_destination = false;
  615. doing_permissions = false;
  616. doing_configurations = false;
  617. doing_component = true;
  618. doing_rename = false;
  619. }
  620. else if(args[i] == "RENAME")
  621. {
  622. // Switch to setting the rename property.
  623. doing_files = false;
  624. doing_destination = false;
  625. doing_permissions = false;
  626. doing_configurations = false;
  627. doing_component = false;
  628. doing_rename = true;
  629. }
  630. else if(args[i] == "OPTIONAL")
  631. {
  632. // Set the optional property.
  633. doing_files = false;
  634. doing_destination = false;
  635. doing_permissions = false;
  636. doing_configurations = false;
  637. doing_component = false;
  638. doing_rename = false;
  639. optional = true;
  640. }
  641. else if(doing_files)
  642. {
  643. // Convert this file to a full path.
  644. std::string file = args[i];
  645. if(!cmSystemTools::FileIsFullPath(file.c_str()))
  646. {
  647. file = this->Makefile->GetCurrentDirectory();
  648. file += "/";
  649. file += args[i];
  650. }
  651. // Make sure the file is not a directory.
  652. if(cmSystemTools::FileIsDirectory(file.c_str()))
  653. {
  654. cmOStringStream e;
  655. e << args[0] << " given directory \"" << args[i] << "\" to install.";
  656. this->SetError(e.str().c_str());
  657. return false;
  658. }
  659. // Store the file for installation.
  660. files.push_back(file);
  661. }
  662. else if(doing_configurations)
  663. {
  664. configurations.push_back(args[i]);
  665. }
  666. else if(doing_destination)
  667. {
  668. destination = args[i].c_str();
  669. doing_destination = false;
  670. }
  671. else if(doing_component)
  672. {
  673. component = args[i];
  674. doing_component = false;
  675. }
  676. else if(doing_permissions)
  677. {
  678. // Check the requested permission.
  679. if(!this->CheckPermissions(args[i], permissions))
  680. {
  681. cmOStringStream e;
  682. e << args[0] << " given invalid permission \""
  683. << args[i] << "\".";
  684. this->SetError(e.str().c_str());
  685. return false;
  686. }
  687. }
  688. else if(doing_rename)
  689. {
  690. rename = args[i];
  691. doing_rename = false;
  692. }
  693. else
  694. {
  695. // Unknown argument.
  696. cmOStringStream e;
  697. e << args[0] << " given unknown argument \"" << args[i] << "\".";
  698. this->SetError(e.str().c_str());
  699. return false;
  700. }
  701. }
  702. // Check if there is something to do.
  703. if(files.empty())
  704. {
  705. return true;
  706. }
  707. if(!destination)
  708. {
  709. // A destination is required.
  710. cmOStringStream e;
  711. e << args[0] << " given no DESTINATION!";
  712. this->SetError(e.str().c_str());
  713. return false;
  714. }
  715. if(!rename.empty() && files.size() > 1)
  716. {
  717. // The rename option works only with one file.
  718. cmOStringStream e;
  719. e << args[0] << " given RENAME option with more than one file.";
  720. this->SetError(e.str().c_str());
  721. return false;
  722. }
  723. // Compute destination path.
  724. std::string dest;
  725. this->ComputeDestination(destination, dest);
  726. // Create the files install generator.
  727. this->Makefile->AddInstallGenerator(
  728. new cmInstallFilesGenerator(files, dest.c_str(), programs,
  729. permissions.c_str(), configurations,
  730. component.c_str(), rename.c_str(),
  731. optional));
  732. // Tell the global generator about any installation component names
  733. // specified.
  734. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  735. ->AddInstallComponent(component.c_str());
  736. return true;
  737. }
  738. //----------------------------------------------------------------------------
  739. bool
  740. cmInstallCommand::HandleDirectoryMode(std::vector<std::string> const& args)
  741. {
  742. bool doing_dirs = true;
  743. bool doing_destination = false;
  744. bool doing_pattern = false;
  745. bool doing_regex = false;
  746. bool doing_permissions_file = false;
  747. bool doing_permissions_dir = false;
  748. bool doing_permissions_match = false;
  749. bool doing_configurations = false;
  750. bool doing_component = false;
  751. bool in_match_mode = false;
  752. std::vector<std::string> dirs;
  753. const char* destination = 0;
  754. std::string permissions_file;
  755. std::string permissions_dir;
  756. std::vector<std::string> configurations;
  757. std::string component;
  758. std::string literal_args;
  759. for(unsigned int i=1; i < args.size(); ++i)
  760. {
  761. if(args[i] == "DESTINATION")
  762. {
  763. if(in_match_mode)
  764. {
  765. cmOStringStream e;
  766. e << args[0] << " does not allow \""
  767. << args[i] << "\" after PATTERN or REGEX.";
  768. this->SetError(e.str().c_str());
  769. return false;
  770. }
  771. // Switch to setting the destination property.
  772. doing_dirs = false;
  773. doing_destination = true;
  774. doing_pattern = false;
  775. doing_regex = false;
  776. doing_permissions_file = false;
  777. doing_permissions_dir = false;
  778. doing_configurations = false;
  779. doing_component = false;
  780. }
  781. else if(args[i] == "PATTERN")
  782. {
  783. // Switch to a new pattern match rule.
  784. doing_dirs = false;
  785. doing_destination = false;
  786. doing_pattern = true;
  787. doing_regex = false;
  788. doing_permissions_file = false;
  789. doing_permissions_dir = false;
  790. doing_permissions_match = false;
  791. doing_configurations = false;
  792. doing_component = false;
  793. in_match_mode = true;
  794. }
  795. else if(args[i] == "REGEX")
  796. {
  797. // Switch to a new regex match rule.
  798. doing_dirs = false;
  799. doing_destination = false;
  800. doing_pattern = false;
  801. doing_regex = true;
  802. doing_permissions_file = false;
  803. doing_permissions_dir = false;
  804. doing_permissions_match = false;
  805. doing_configurations = false;
  806. doing_component = false;
  807. in_match_mode = true;
  808. }
  809. else if(args[i] == "EXCLUDE")
  810. {
  811. // Add this property to the current match rule.
  812. if(!in_match_mode || doing_pattern || doing_regex)
  813. {
  814. cmOStringStream e;
  815. e << args[0] << " does not allow \""
  816. << args[i] << "\" before a PATTERN or REGEX is given.";
  817. this->SetError(e.str().c_str());
  818. return false;
  819. }
  820. literal_args += " EXCLUDE";
  821. doing_permissions_match = false;
  822. }
  823. else if(args[i] == "PERMISSIONS")
  824. {
  825. if(!in_match_mode)
  826. {
  827. cmOStringStream e;
  828. e << args[0] << " does not allow \""
  829. << args[i] << "\" before a PATTERN or REGEX is given.";
  830. this->SetError(e.str().c_str());
  831. return false;
  832. }
  833. // Switch to setting the current match permissions property.
  834. literal_args += " PERMISSIONS";
  835. doing_permissions_match = true;
  836. }
  837. else if(args[i] == "FILE_PERMISSIONS")
  838. {
  839. if(in_match_mode)
  840. {
  841. cmOStringStream e;
  842. e << args[0] << " does not allow \""
  843. << args[i] << "\" after PATTERN or REGEX.";
  844. this->SetError(e.str().c_str());
  845. return false;
  846. }
  847. // Switch to setting the file permissions property.
  848. doing_dirs = false;
  849. doing_destination = false;
  850. doing_pattern = false;
  851. doing_regex = false;
  852. doing_permissions_file = true;
  853. doing_permissions_dir = false;
  854. doing_configurations = false;
  855. doing_component = false;
  856. }
  857. else if(args[i] == "DIRECTORY_PERMISSIONS")
  858. {
  859. if(in_match_mode)
  860. {
  861. cmOStringStream e;
  862. e << args[0] << " does not allow \""
  863. << args[i] << "\" after PATTERN or REGEX.";
  864. this->SetError(e.str().c_str());
  865. return false;
  866. }
  867. // Switch to setting the directory permissions property.
  868. doing_dirs = false;
  869. doing_destination = false;
  870. doing_pattern = false;
  871. doing_regex = false;
  872. doing_permissions_file = false;
  873. doing_permissions_dir = true;
  874. doing_configurations = false;
  875. doing_component = false;
  876. }
  877. else if(args[i] == "USE_SOURCE_PERMISSIONS")
  878. {
  879. if(in_match_mode)
  880. {
  881. cmOStringStream e;
  882. e << args[0] << " does not allow \""
  883. << args[i] << "\" after PATTERN or REGEX.";
  884. this->SetError(e.str().c_str());
  885. return false;
  886. }
  887. // Add this option literally.
  888. doing_dirs = false;
  889. doing_destination = false;
  890. doing_pattern = false;
  891. doing_regex = false;
  892. doing_permissions_file = false;
  893. doing_permissions_dir = false;
  894. doing_configurations = false;
  895. doing_component = false;
  896. literal_args += " USE_SOURCE_PERMISSIONS";
  897. }
  898. else if(args[i] == "CONFIGURATIONS")
  899. {
  900. if(in_match_mode)
  901. {
  902. cmOStringStream e;
  903. e << args[0] << " does not allow \""
  904. << args[i] << "\" after PATTERN or REGEX.";
  905. this->SetError(e.str().c_str());
  906. return false;
  907. }
  908. // Switch to setting the configurations property.
  909. doing_dirs = false;
  910. doing_destination = false;
  911. doing_pattern = false;
  912. doing_regex = false;
  913. doing_permissions_file = false;
  914. doing_permissions_dir = false;
  915. doing_configurations = true;
  916. doing_component = false;
  917. }
  918. else if(args[i] == "COMPONENT")
  919. {
  920. if(in_match_mode)
  921. {
  922. cmOStringStream e;
  923. e << args[0] << " does not allow \""
  924. << args[i] << "\" after PATTERN or REGEX.";
  925. this->SetError(e.str().c_str());
  926. return false;
  927. }
  928. // Switch to setting the component property.
  929. doing_dirs = false;
  930. doing_destination = false;
  931. doing_pattern = false;
  932. doing_regex = false;
  933. doing_permissions_file = false;
  934. doing_permissions_dir = false;
  935. doing_configurations = false;
  936. doing_component = true;
  937. }
  938. else if(doing_dirs)
  939. {
  940. // Convert this directory to a full path.
  941. std::string dir = args[i];
  942. if(!cmSystemTools::FileIsFullPath(dir.c_str()))
  943. {
  944. dir = this->Makefile->GetCurrentDirectory();
  945. dir += "/";
  946. dir += args[i];
  947. }
  948. // Make sure the name is a directory.
  949. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  950. {
  951. cmOStringStream e;
  952. e << args[0] << " given non-directory \""
  953. << args[i] << "\" to install.";
  954. this->SetError(e.str().c_str());
  955. return false;
  956. }
  957. // Store the directory for installation.
  958. dirs.push_back(dir);
  959. }
  960. else if(doing_configurations)
  961. {
  962. configurations.push_back(args[i]);
  963. }
  964. else if(doing_destination)
  965. {
  966. destination = args[i].c_str();
  967. doing_destination = false;
  968. }
  969. else if(doing_pattern)
  970. {
  971. // Convert the pattern to a regular expression. Require a
  972. // leading slash and trailing end-of-string in the matched
  973. // string to make sure the pattern matches only whole file
  974. // names.
  975. literal_args += " REGEX \"/";
  976. std::string regex = cmsys::Glob::PatternToRegex(args[i], false);
  977. cmSystemTools::ReplaceString(regex, "\\", "\\\\");
  978. literal_args += regex;
  979. literal_args += "$\"";
  980. doing_pattern = false;
  981. }
  982. else if(doing_regex)
  983. {
  984. literal_args += " REGEX \"";
  985. // Match rules are case-insensitive on some platforms.
  986. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  987. std::string regex = cmSystemTools::LowerCase(args[i]);
  988. #else
  989. std::string regex = args[i];
  990. #endif
  991. cmSystemTools::ReplaceString(regex, "\\", "\\\\");
  992. literal_args += regex;
  993. literal_args += "\"";
  994. doing_regex = false;
  995. }
  996. else if(doing_component)
  997. {
  998. component = args[i];
  999. doing_component = false;
  1000. }
  1001. else if(doing_permissions_file)
  1002. {
  1003. // Check the requested permission.
  1004. if(!this->CheckPermissions(args[i], permissions_file))
  1005. {
  1006. cmOStringStream e;
  1007. e << args[0] << " given invalid file permission \""
  1008. << args[i] << "\".";
  1009. this->SetError(e.str().c_str());
  1010. return false;
  1011. }
  1012. }
  1013. else if(doing_permissions_dir)
  1014. {
  1015. // Check the requested permission.
  1016. if(!this->CheckPermissions(args[i], permissions_dir))
  1017. {
  1018. cmOStringStream e;
  1019. e << args[0] << " given invalid directory permission \""
  1020. << args[i] << "\".";
  1021. this->SetError(e.str().c_str());
  1022. return false;
  1023. }
  1024. }
  1025. else if(doing_permissions_match)
  1026. {
  1027. // Check the requested permission.
  1028. if(!this->CheckPermissions(args[i], literal_args))
  1029. {
  1030. cmOStringStream e;
  1031. e << args[0] << " given invalid permission \""
  1032. << args[i] << "\".";
  1033. this->SetError(e.str().c_str());
  1034. return false;
  1035. }
  1036. }
  1037. else
  1038. {
  1039. // Unknown argument.
  1040. cmOStringStream e;
  1041. e << args[0] << " given unknown argument \"" << args[i] << "\".";
  1042. this->SetError(e.str().c_str());
  1043. return false;
  1044. }
  1045. }
  1046. // Support installing an empty directory.
  1047. if(dirs.empty() && destination)
  1048. {
  1049. dirs.push_back("");
  1050. }
  1051. // Check if there is something to do.
  1052. if(dirs.empty())
  1053. {
  1054. return true;
  1055. }
  1056. if(!destination)
  1057. {
  1058. // A destination is required.
  1059. cmOStringStream e;
  1060. e << args[0] << " given no DESTINATION!";
  1061. this->SetError(e.str().c_str());
  1062. return false;
  1063. }
  1064. // Compute destination path.
  1065. std::string dest;
  1066. this->ComputeDestination(destination, dest);
  1067. // Create the directory install generator.
  1068. this->Makefile->AddInstallGenerator(
  1069. new cmInstallDirectoryGenerator(dirs, dest.c_str(),
  1070. permissions_file.c_str(),
  1071. permissions_dir.c_str(),
  1072. configurations,
  1073. component.c_str(),
  1074. literal_args.c_str()));
  1075. // Tell the global generator about any installation component names
  1076. // specified.
  1077. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  1078. ->AddInstallComponent(component.c_str());
  1079. return true;
  1080. }
  1081. //----------------------------------------------------------------------------
  1082. void cmInstallCommand::ComputeDestination(const char* destination,
  1083. std::string& dest) const
  1084. {
  1085. if(destination)
  1086. {
  1087. if(cmSystemTools::FileIsFullPath(destination))
  1088. {
  1089. // Full paths are absolute.
  1090. dest = destination;
  1091. }
  1092. else
  1093. {
  1094. // Relative paths are treated with respect to the installation prefix.
  1095. dest = "${CMAKE_INSTALL_PREFIX}/";
  1096. dest += destination;
  1097. }
  1098. // Format the path nicely. Note this also removes trailing
  1099. // slashes.
  1100. cmSystemTools::ConvertToUnixSlashes(dest);
  1101. }
  1102. else
  1103. {
  1104. dest = "";
  1105. }
  1106. }
  1107. //----------------------------------------------------------------------------
  1108. bool cmInstallCommand::CheckPermissions(std::string const& arg,
  1109. std::string& permissions) const
  1110. {
  1111. // Table of valid permissions.
  1112. const char* table[] =
  1113. {
  1114. "OWNER_READ", "OWNER_WRITE", "OWNER_EXECUTE",
  1115. "GROUP_READ", "GROUP_WRITE", "GROUP_EXECUTE",
  1116. "WORLD_READ", "WORLD_WRITE", "WORLD_EXECUTE",
  1117. "SETUID", "SETGID", 0
  1118. };
  1119. // Check the permission against the table.
  1120. for(const char** valid = table; *valid; ++valid)
  1121. {
  1122. if(arg == *valid)
  1123. {
  1124. // This is a valid permission.
  1125. permissions += " ";
  1126. permissions += arg;
  1127. return true;
  1128. }
  1129. }
  1130. // This is not a valid permission.
  1131. return false;
  1132. }