cmInstallCommand.cxx 32 KB

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