cmFileCommand.cxx 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  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 <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <cmsys/Directory.hxx>
  17. #include <cmsys/Glob.hxx>
  18. #include <cmsys/RegularExpression.hxx>
  19. // Table of permissions flags.
  20. #if defined(_WIN32) && !defined(__CYGWIN__)
  21. static mode_t mode_owner_read = S_IREAD;
  22. static mode_t mode_owner_write = S_IWRITE;
  23. static mode_t mode_owner_execute = S_IEXEC;
  24. static mode_t mode_group_read = 0;
  25. static mode_t mode_group_write = 0;
  26. static mode_t mode_group_execute = 0;
  27. static mode_t mode_world_read = 0;
  28. static mode_t mode_world_write = 0;
  29. static mode_t mode_world_execute = 0;
  30. static mode_t mode_setuid = 0;
  31. static mode_t mode_setgid = 0;
  32. #else
  33. static mode_t mode_owner_read = S_IRUSR;
  34. static mode_t mode_owner_write = S_IWUSR;
  35. static mode_t mode_owner_execute = S_IXUSR;
  36. static mode_t mode_group_read = S_IRGRP;
  37. static mode_t mode_group_write = S_IWGRP;
  38. static mode_t mode_group_execute = S_IXGRP;
  39. static mode_t mode_world_read = S_IROTH;
  40. static mode_t mode_world_write = S_IWOTH;
  41. static mode_t mode_world_execute = S_IXOTH;
  42. static mode_t mode_setuid = S_ISUID;
  43. static mode_t mode_setgid = S_ISGID;
  44. #endif
  45. // cmLibraryCommand
  46. bool cmFileCommand::InitialPass(std::vector<std::string> const& args)
  47. {
  48. if(args.size() < 2 )
  49. {
  50. this->SetError("must be called with at least two arguments.");
  51. return false;
  52. }
  53. std::string subCommand = args[0];
  54. if ( subCommand == "WRITE" )
  55. {
  56. return this->HandleWriteCommand(args, false);
  57. }
  58. else if ( subCommand == "APPEND" )
  59. {
  60. return this->HandleWriteCommand(args, true);
  61. }
  62. else if ( subCommand == "READ" )
  63. {
  64. return this->HandleReadCommand(args);
  65. }
  66. else if ( subCommand == "STRINGS" )
  67. {
  68. return this->HandleStringsCommand(args);
  69. }
  70. else if ( subCommand == "GLOB" )
  71. {
  72. return this->HandleGlobCommand(args, false);
  73. }
  74. else if ( subCommand == "GLOB_RECURSE" )
  75. {
  76. return this->HandleGlobCommand(args, true);
  77. }
  78. else if ( subCommand == "MAKE_DIRECTORY" )
  79. {
  80. return this->HandleMakeDirectoryCommand(args);
  81. }
  82. else if ( subCommand == "REMOVE" )
  83. {
  84. return this->HandleRemove(args, false);
  85. }
  86. else if ( subCommand == "REMOVE_RECURSE" )
  87. {
  88. return this->HandleRemove(args, true);
  89. }
  90. else if ( subCommand == "INSTALL" )
  91. {
  92. return this->HandleInstallCommand(args);
  93. }
  94. else if ( subCommand == "RELATIVE_PATH" )
  95. {
  96. return this->HandleRelativePathCommand(args);
  97. }
  98. else if ( subCommand == "TO_CMAKE_PATH" )
  99. {
  100. return this->HandleCMakePathCommand(args, false);
  101. }
  102. else if ( subCommand == "TO_NATIVE_PATH" )
  103. {
  104. return this->HandleCMakePathCommand(args, true);
  105. }
  106. std::string e = "does not recognize sub-command "+subCommand;
  107. this->SetError(e.c_str());
  108. return false;
  109. }
  110. //----------------------------------------------------------------------------
  111. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  112. bool append)
  113. {
  114. std::string message;
  115. std::vector<std::string>::const_iterator i = args.begin();
  116. i++; // Get rid of subcommand
  117. std::string fileName = *i;
  118. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  119. {
  120. fileName = this->Makefile->GetCurrentDirectory();
  121. fileName += "/" + *i;
  122. }
  123. i++;
  124. for(;i != args.end(); ++i)
  125. {
  126. message += *i;
  127. }
  128. if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
  129. {
  130. std::string e
  131. = "attempted to write a file: " + fileName +
  132. " into a source directory.";
  133. this->SetError(e.c_str());
  134. cmSystemTools::SetFatalErrorOccured();
  135. return false;
  136. }
  137. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  138. cmSystemTools::MakeDirectory(dir.c_str());
  139. mode_t mode =
  140. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  141. S_IREAD | S_IWRITE
  142. #elif defined( __BORLANDC__ )
  143. S_IRUSR | S_IWUSR
  144. #else
  145. S_IRUSR | S_IWUSR |
  146. S_IRGRP |
  147. S_IROTH
  148. #endif
  149. ;
  150. // Set permissions to writable
  151. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  152. {
  153. cmSystemTools::SetPermissions(fileName.c_str(),
  154. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  155. S_IREAD | S_IWRITE
  156. #else
  157. S_IRUSR | S_IWUSR
  158. #endif
  159. );
  160. }
  161. // If GetPermissions fails, pretend like it is ok. File open will fail if
  162. // the file is not writable
  163. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  164. if ( !file )
  165. {
  166. std::string error = "Internal CMake error when trying to open file: ";
  167. error += fileName.c_str();
  168. error += " for writing.";
  169. this->SetError(error.c_str());
  170. return false;
  171. }
  172. file << message;
  173. file.close();
  174. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  175. this->Makefile->AddWrittenFile(fileName.c_str());
  176. return true;
  177. }
  178. //----------------------------------------------------------------------------
  179. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  180. {
  181. if ( args.size() < 3 )
  182. {
  183. this->SetError("READ must be called with at least two additional "
  184. "arguments");
  185. return false;
  186. }
  187. std::string fileName = args[1];
  188. if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
  189. {
  190. fileName = this->Makefile->GetCurrentDirectory();
  191. fileName += "/" + args[1];
  192. }
  193. std::string variable = args[2];
  194. std::ifstream file(fileName.c_str(), std::ios::in);
  195. if ( !file )
  196. {
  197. std::string error = "Internal CMake error when trying to open file: ";
  198. error += fileName.c_str();
  199. error += " for reading.";
  200. this->SetError(error.c_str());
  201. return false;
  202. }
  203. // if there a limit?
  204. long sizeLimit = -1;
  205. if (args.size() >= 5 && args[3] == "LIMIT")
  206. {
  207. sizeLimit = atoi(args[4].c_str());
  208. }
  209. std::string output;
  210. std::string line;
  211. bool has_newline = false;
  212. while (sizeLimit != 0 &&
  213. cmSystemTools::GetLineFromStream(file, line, &has_newline,
  214. sizeLimit) )
  215. {
  216. if (sizeLimit > 0)
  217. {
  218. sizeLimit = sizeLimit - static_cast<long>(line.size());
  219. if (has_newline)
  220. {
  221. sizeLimit--;
  222. }
  223. if (sizeLimit < 0)
  224. {
  225. sizeLimit = 0;
  226. }
  227. }
  228. output += line;
  229. if ( has_newline )
  230. {
  231. output += "\n";
  232. }
  233. }
  234. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  235. return true;
  236. }
  237. //----------------------------------------------------------------------------
  238. bool cmFileCommand::HandleStringsCommand(std::vector<std::string> const& args)
  239. {
  240. if(args.size() < 3)
  241. {
  242. this->SetError("STRINGS requires a file name and output variable");
  243. return false;
  244. }
  245. // Get the file to read.
  246. std::string fileName = args[1];
  247. if(!cmsys::SystemTools::FileIsFullPath(fileName.c_str()))
  248. {
  249. fileName = this->Makefile->GetCurrentDirectory();
  250. fileName += "/" + args[1];
  251. }
  252. // Get the variable in which to store the results.
  253. std::string outVar = args[2];
  254. // Parse the options.
  255. enum { arg_none,
  256. arg_limit_input,
  257. arg_limit_output,
  258. arg_limit_count,
  259. arg_length_minimum,
  260. arg_length_maximum,
  261. arg__maximum,
  262. arg_regex };
  263. unsigned int minlen = 0;
  264. unsigned int maxlen = 0;
  265. int limit_input = -1;
  266. int limit_output = -1;
  267. unsigned int limit_count = 0;
  268. cmsys::RegularExpression regex;
  269. bool have_regex = false;
  270. bool newline_consume = false;
  271. int arg_mode = arg_none;
  272. for(unsigned int i=3; i < args.size(); ++i)
  273. {
  274. if(args[i] == "LIMIT_INPUT")
  275. {
  276. arg_mode = arg_limit_input;
  277. }
  278. else if(args[i] == "LIMIT_OUTPUT")
  279. {
  280. arg_mode = arg_limit_output;
  281. }
  282. else if(args[i] == "LIMIT_COUNT")
  283. {
  284. arg_mode = arg_limit_count;
  285. }
  286. else if(args[i] == "LENGTH_MINIMUM")
  287. {
  288. arg_mode = arg_length_minimum;
  289. }
  290. else if(args[i] == "LENGTH_MAXIMUM")
  291. {
  292. arg_mode = arg_length_maximum;
  293. }
  294. else if(args[i] == "REGEX")
  295. {
  296. arg_mode = arg_regex;
  297. }
  298. else if(args[i] == "NEWLINE_CONSUME")
  299. {
  300. newline_consume = true;
  301. arg_mode = arg_none;
  302. }
  303. else if(arg_mode == arg_limit_input)
  304. {
  305. if(sscanf(args[i].c_str(), "%d", &limit_input) != 1 ||
  306. limit_input < 0)
  307. {
  308. cmOStringStream e;
  309. e << "STRINGS option LIMIT_INPUT value \""
  310. << args[i] << "\" is not an unsigned integer.";
  311. this->SetError(e.str().c_str());
  312. return false;
  313. }
  314. arg_mode = arg_none;
  315. }
  316. else if(arg_mode == arg_limit_output)
  317. {
  318. if(sscanf(args[i].c_str(), "%d", &limit_output) != 1 ||
  319. limit_output < 0)
  320. {
  321. cmOStringStream e;
  322. e << "STRINGS option LIMIT_OUTPUT value \""
  323. << args[i] << "\" is not an unsigned integer.";
  324. this->SetError(e.str().c_str());
  325. return false;
  326. }
  327. arg_mode = arg_none;
  328. }
  329. else if(arg_mode == arg_limit_count)
  330. {
  331. int count;
  332. if(sscanf(args[i].c_str(), "%d", &count) != 1 || count < 0)
  333. {
  334. cmOStringStream e;
  335. e << "STRINGS option LIMIT_COUNT value \""
  336. << args[i] << "\" is not an unsigned integer.";
  337. this->SetError(e.str().c_str());
  338. return false;
  339. }
  340. limit_count = count;
  341. arg_mode = arg_none;
  342. }
  343. else if(arg_mode == arg_length_minimum)
  344. {
  345. int len;
  346. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  347. {
  348. cmOStringStream e;
  349. e << "STRINGS option LENGTH_MINIMUM value \""
  350. << args[i] << "\" is not an unsigned integer.";
  351. this->SetError(e.str().c_str());
  352. return false;
  353. }
  354. minlen = len;
  355. arg_mode = arg_none;
  356. }
  357. else if(arg_mode == arg_length_maximum)
  358. {
  359. int len;
  360. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  361. {
  362. cmOStringStream e;
  363. e << "STRINGS option LENGTH_MAXIMUM value \""
  364. << args[i] << "\" is not an unsigned integer.";
  365. this->SetError(e.str().c_str());
  366. return false;
  367. }
  368. maxlen = len;
  369. arg_mode = arg_none;
  370. }
  371. else if(arg_mode == arg_regex)
  372. {
  373. if(!regex.compile(args[i].c_str()))
  374. {
  375. cmOStringStream e;
  376. e << "STRINGS option REGEX value \""
  377. << args[i] << "\" could not be compiled.";
  378. this->SetError(e.str().c_str());
  379. return false;
  380. }
  381. have_regex = true;
  382. arg_mode = arg_none;
  383. }
  384. else
  385. {
  386. cmOStringStream e;
  387. e << "STRINGS given unknown argument \""
  388. << args[i] << "\"";
  389. this->SetError(e.str().c_str());
  390. return false;
  391. }
  392. }
  393. // Open the specified file.
  394. #if defined(_WIN32) || defined(__CYGWIN__)
  395. std::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
  396. #else
  397. std::ifstream fin(fileName.c_str(), std::ios::in);
  398. #endif
  399. if(!fin)
  400. {
  401. cmOStringStream e;
  402. e << "STRINGS file \"" << fileName << "\" cannot be read.";
  403. this->SetError(e.str().c_str());
  404. return false;
  405. }
  406. // Parse strings out of the file.
  407. int output_size = 0;
  408. std::vector<std::string> strings;
  409. std::string s;
  410. int c;
  411. while((!limit_count || strings.size() < limit_count) &&
  412. (limit_input < 0 || static_cast<int>(fin.tellg()) < limit_input) &&
  413. (c = fin.get(), fin))
  414. {
  415. if(c == '\0')
  416. {
  417. // A terminating null character has been found. Check if the
  418. // current string matches the requirements. Since it was
  419. // terminated by a null character, we require that the length be
  420. // at least one no matter what the user specified.
  421. if(s.length() >= minlen && s.length() >= 1 &&
  422. (!have_regex || regex.find(s.c_str())))
  423. {
  424. output_size += static_cast<int>(s.size()) + 1;
  425. if(limit_output >= 0 && output_size >= limit_output)
  426. {
  427. s = "";
  428. break;
  429. }
  430. strings.push_back(s);
  431. }
  432. // Reset the string to empty.
  433. s = "";
  434. }
  435. else if(c == '\n' && !newline_consume)
  436. {
  437. // The current line has been terminated. Check if the current
  438. // string matches the requirements. The length may now be as
  439. // low as zero since blank lines are allowed.
  440. if(s.length() >= minlen &&
  441. (!have_regex || regex.find(s.c_str())))
  442. {
  443. output_size += static_cast<int>(s.size()) + 1;
  444. if(limit_output >= 0 && output_size >= limit_output)
  445. {
  446. s = "";
  447. break;
  448. }
  449. strings.push_back(s);
  450. }
  451. // Reset the string to empty.
  452. s = "";
  453. }
  454. else if(c == '\r')
  455. {
  456. // Ignore CR character to make output always have UNIX newlines.
  457. }
  458. else if(c >= 0x20 && c < 0x7F || c == '\t' ||
  459. (c == '\n' && newline_consume))
  460. {
  461. // This is an ASCII character that may be part of a string.
  462. s += c;
  463. }
  464. else
  465. {
  466. // This is a non-string character. Reset the string to emtpy.
  467. s = "";
  468. }
  469. // Terminate a string if the maximum length is reached.
  470. if(maxlen > 0 && s.size() == maxlen)
  471. {
  472. if(s.length() >= minlen &&
  473. (!have_regex || regex.find(s.c_str())))
  474. {
  475. output_size += static_cast<int>(s.size()) + 1;
  476. if(limit_output >= 0 && output_size >= limit_output)
  477. {
  478. s = "";
  479. break;
  480. }
  481. strings.push_back(s);
  482. }
  483. s = "";
  484. }
  485. }
  486. // If there is a non-empty current string we have hit the end of the
  487. // input file or the input size limit. Check if the current string
  488. // matches the requirements.
  489. if((!limit_count || strings.size() < limit_count) &&
  490. !s.empty() && s.length() >= minlen &&
  491. (!have_regex || regex.find(s.c_str())))
  492. {
  493. output_size += static_cast<int>(s.size()) + 1;
  494. if(limit_output < 0 || output_size < limit_output)
  495. {
  496. strings.push_back(s);
  497. }
  498. }
  499. // Encode the result in a CMake list.
  500. const char* sep = "";
  501. std::string output;
  502. for(std::vector<std::string>::const_iterator si = strings.begin();
  503. si != strings.end(); ++si)
  504. {
  505. // Separate the strings in the output to make it a list.
  506. output += sep;
  507. sep = ";";
  508. // Store the string in the output, but escape semicolons to
  509. // make sure it is a list.
  510. std::string const& sr = *si;
  511. for(unsigned int i=0; i < sr.size(); ++i)
  512. {
  513. if(sr[i] == ';')
  514. {
  515. output += '\\';
  516. }
  517. output += sr[i];
  518. }
  519. }
  520. // Save the output in a makefile variable.
  521. this->Makefile->AddDefinition(outVar.c_str(), output.c_str());
  522. return true;
  523. }
  524. //----------------------------------------------------------------------------
  525. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  526. bool recurse)
  527. {
  528. if ( args.size() < 2 )
  529. {
  530. this->SetError("GLOB requires at least a variable name");
  531. return false;
  532. }
  533. std::vector<std::string>::const_iterator i = args.begin();
  534. i++; // Get rid of subcommand
  535. std::string variable = *i;
  536. i++;
  537. cmsys::Glob g;
  538. g.SetRecurse(recurse);
  539. std::string output = "";
  540. bool first = true;
  541. for ( ; i != args.end(); ++i )
  542. {
  543. if ( *i == "RELATIVE" )
  544. {
  545. ++i; // skip RELATIVE
  546. if ( i == args.end() )
  547. {
  548. this->SetError("GLOB requires a directory after the RELATIVE tag");
  549. return false;
  550. }
  551. g.SetRelative(i->c_str());
  552. ++i;
  553. if(i == args.end())
  554. {
  555. this->SetError("GLOB requires a glob expression after the directory");
  556. return false;
  557. }
  558. }
  559. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  560. {
  561. std::string expr = this->Makefile->GetCurrentDirectory();
  562. // Handle script mode
  563. if ( expr.size() > 0 )
  564. {
  565. expr += "/" + *i;
  566. g.FindFiles(expr);
  567. }
  568. else
  569. {
  570. g.FindFiles(*i);
  571. }
  572. }
  573. else
  574. {
  575. g.FindFiles(*i);
  576. }
  577. std::vector<std::string>::size_type cc;
  578. std::vector<std::string>& files = g.GetFiles();
  579. for ( cc = 0; cc < files.size(); cc ++ )
  580. {
  581. if ( !first )
  582. {
  583. output += ";";
  584. }
  585. output += files[cc];
  586. first = false;
  587. }
  588. }
  589. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  590. return true;
  591. }
  592. //----------------------------------------------------------------------------
  593. bool cmFileCommand::HandleMakeDirectoryCommand(
  594. std::vector<std::string> const& args)
  595. {
  596. if(args.size() < 2 )
  597. {
  598. this->SetError("called with incorrect number of arguments");
  599. return false;
  600. }
  601. std::vector<std::string>::const_iterator i = args.begin();
  602. i++; // Get rid of subcommand
  603. std::string expr;
  604. for ( ; i != args.end(); ++i )
  605. {
  606. const std::string* cdir = &(*i);
  607. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  608. {
  609. expr = this->Makefile->GetCurrentDirectory();
  610. expr += "/" + *i;
  611. cdir = &expr;
  612. }
  613. if ( !this->Makefile->CanIWriteThisFile(cdir->c_str()) )
  614. {
  615. std::string e = "attempted to create a directory: " + *cdir
  616. + " into a source directory.";
  617. this->SetError(e.c_str());
  618. cmSystemTools::SetFatalErrorOccured();
  619. return false;
  620. }
  621. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  622. {
  623. std::string error = "problem creating directory: " + *cdir;
  624. this->SetError(error.c_str());
  625. return false;
  626. }
  627. }
  628. return true;
  629. }
  630. //----------------------------------------------------------------------------
  631. // File installation helper class.
  632. struct cmFileInstaller
  633. {
  634. // Methods to actually install files.
  635. bool InstallFile(const char* fromFile, const char* toFile, bool always);
  636. bool InstallDirectory(const char* source, const char* destination,
  637. bool always);
  638. // All instances need the file command and makefile using them.
  639. cmFileInstaller(cmFileCommand* fc, cmMakefile* mf):
  640. FileCommand(fc), Makefile(mf), DestDirLength(0)
  641. {
  642. // Get the current manifest.
  643. this->Manifest =
  644. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  645. }
  646. ~cmFileInstaller()
  647. {
  648. // Save the updated install manifest.
  649. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  650. this->Manifest.c_str());
  651. }
  652. private:
  653. cmFileCommand* FileCommand;
  654. cmMakefile* Makefile;
  655. public:
  656. // The length of the destdir setting.
  657. int DestDirLength;
  658. // The current file manifest (semicolon separated list).
  659. std::string Manifest;
  660. // Permissions for files and directories installed by this object.
  661. mode_t FilePermissions;
  662. mode_t DirPermissions;
  663. // Properties set by pattern and regex match rules.
  664. struct MatchProperties
  665. {
  666. bool Exclude;
  667. mode_t Permissions;
  668. MatchProperties(): Exclude(false), Permissions(0) {}
  669. };
  670. struct MatchRule
  671. {
  672. cmsys::RegularExpression Regex;
  673. MatchProperties Properties;
  674. std::string RegexString;
  675. MatchRule(std::string const& regex):
  676. Regex(regex.c_str()), RegexString(regex) {}
  677. };
  678. std::vector<MatchRule> MatchRules;
  679. // Get the properties from rules matching this input file.
  680. MatchProperties CollectMatchProperties(const char* file)
  681. {
  682. // Match rules are case-insensitive on some platforms.
  683. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  684. std::string lower = cmSystemTools::LowerCase(file);
  685. file = lower.c_str();
  686. #endif
  687. // Collect properties from all matching rules.
  688. MatchProperties result;
  689. for(std::vector<MatchRule>::iterator mr = this->MatchRules.begin();
  690. mr != this->MatchRules.end(); ++mr)
  691. {
  692. if(mr->Regex.find(file))
  693. {
  694. result.Exclude |= mr->Properties.Exclude;
  695. result.Permissions |= mr->Properties.Permissions;
  696. }
  697. }
  698. return result;
  699. }
  700. // Append a file to the installation manifest.
  701. void ManifestAppend(std::string const& file)
  702. {
  703. this->Manifest += ";";
  704. this->Manifest += file.substr(this->DestDirLength);
  705. }
  706. // Translate an argument to a permissions bit.
  707. bool CheckPermissions(std::string const& arg, mode_t& permissions)
  708. {
  709. if(arg == "OWNER_READ") { permissions |= mode_owner_read; }
  710. else if(arg == "OWNER_WRITE") { permissions |= mode_owner_write; }
  711. else if(arg == "OWNER_EXECUTE") { permissions |= mode_owner_execute; }
  712. else if(arg == "GROUP_READ") { permissions |= mode_group_read; }
  713. else if(arg == "GROUP_WRITE") { permissions |= mode_group_write; }
  714. else if(arg == "GROUP_EXECUTE") { permissions |= mode_group_execute; }
  715. else if(arg == "WORLD_READ") { permissions |= mode_world_read; }
  716. else if(arg == "WORLD_WRITE") { permissions |= mode_world_write; }
  717. else if(arg == "WORLD_EXECUTE") { permissions |= mode_world_execute; }
  718. else if(arg == "SETUID") { permissions |= mode_setuid; }
  719. else if(arg == "SETGID") { permissions |= mode_setgid; }
  720. else
  721. {
  722. cmOStringStream e;
  723. e << "INSTALL given invalid permission \"" << arg << "\".";
  724. this->FileCommand->SetError(e.str().c_str());
  725. return false;
  726. }
  727. return true;
  728. }
  729. private:
  730. bool InstallSymlink(const char* fromFile, const char* toFile, bool always);
  731. };
  732. //----------------------------------------------------------------------------
  733. bool cmFileInstaller::InstallSymlink(const char* fromFile, const char* toFile,
  734. bool always)
  735. {
  736. // Inform the user about this file installation.
  737. std::string message = "Installing ";
  738. message += toFile;
  739. this->Makefile->DisplayStatus(message.c_str(), -1);
  740. // Read the original symlink.
  741. std::string symlinkTarget;
  742. if(!cmSystemTools::ReadSymlink(fromFile, symlinkTarget))
  743. {
  744. cmOStringStream e;
  745. e << "INSTALL cannot read symlink \"" << fromFile
  746. << "\" to duplicate at \"" << toFile << "\".";
  747. this->FileCommand->SetError(e.str().c_str());
  748. return false;
  749. }
  750. // Compare the symlink value to that at the destination if not
  751. // always installing.
  752. if(!always)
  753. {
  754. std::string oldSymlinkTarget;
  755. if(cmSystemTools::ReadSymlink(toFile, oldSymlinkTarget))
  756. {
  757. if(symlinkTarget == oldSymlinkTarget)
  758. {
  759. return true;
  760. }
  761. }
  762. }
  763. // Remove the destination file so we can always create the symlink.
  764. cmSystemTools::RemoveFile(toFile);
  765. // Create the symlink.
  766. if(!cmSystemTools::CreateSymlink(symlinkTarget.c_str(), toFile))
  767. {
  768. cmOStringStream e;
  769. e << "INSTALL cannot duplicate symlink \"" << fromFile
  770. << "\" at \"" << toFile << "\".";
  771. this->FileCommand->SetError(e.str().c_str());
  772. return false;
  773. }
  774. // Add the file to the manifest.
  775. this->ManifestAppend(toFile);
  776. return true;
  777. }
  778. //----------------------------------------------------------------------------
  779. bool cmFileInstaller::InstallFile(const char* fromFile, const char* toFile,
  780. bool always)
  781. {
  782. // Collect any properties matching this file name.
  783. MatchProperties match_properties = this->CollectMatchProperties(fromFile);
  784. // Skip the file if it is excluded.
  785. if(match_properties.Exclude)
  786. {
  787. return true;
  788. }
  789. // Short-circuit for symbolic links.
  790. if(cmSystemTools::FileIsSymlink(fromFile))
  791. {
  792. return this->InstallSymlink(fromFile, toFile, always);
  793. }
  794. // Inform the user about this file installation.
  795. std::string message = "Installing ";
  796. message += toFile;
  797. this->Makefile->DisplayStatus(message.c_str(), -1);
  798. // Copy the file.
  799. if(!cmSystemTools::CopyAFile(fromFile, toFile, always))
  800. {
  801. cmOStringStream e;
  802. e << "INSTALL cannot copy file \"" << fromFile
  803. << "\" to \"" << toFile << "\".";
  804. this->FileCommand->SetError(e.str().c_str());
  805. return false;
  806. }
  807. // Add the file to the manifest.
  808. this->ManifestAppend(toFile);
  809. // Set permissions of the destination file.
  810. mode_t permissions = (match_properties.Permissions?
  811. match_properties.Permissions : this->FilePermissions);
  812. if(!permissions)
  813. {
  814. // No permissions were explicitly provided but the user requested
  815. // that the source file permissions be used.
  816. cmSystemTools::GetPermissions(fromFile, permissions);
  817. }
  818. if(permissions && !cmSystemTools::SetPermissions(toFile, permissions))
  819. {
  820. cmOStringStream e;
  821. e << "Problem setting permissions on file \"" << toFile << "\"";
  822. this->FileCommand->SetError(e.str().c_str());
  823. return false;
  824. }
  825. return true;
  826. }
  827. //----------------------------------------------------------------------------
  828. bool cmFileInstaller::InstallDirectory(const char* source,
  829. const char* destination,
  830. bool always)
  831. {
  832. // Collect any properties matching this directory name.
  833. MatchProperties match_properties = this->CollectMatchProperties(source);
  834. // Skip the directory if it is excluded.
  835. if(match_properties.Exclude)
  836. {
  837. return true;
  838. }
  839. // Short-circuit for symbolic links.
  840. if(cmSystemTools::FileIsSymlink(source))
  841. {
  842. return this->InstallSymlink(source, destination, always);
  843. }
  844. // Inform the user about this directory installation.
  845. std::string message = "Installing ";
  846. message += destination;
  847. this->Makefile->DisplayStatus(message.c_str(), -1);
  848. // Make sure the destination directory exists.
  849. if(!cmSystemTools::MakeDirectory(destination))
  850. {
  851. return false;
  852. }
  853. // Compute the requested permissions for the destination directory.
  854. mode_t permissions = (match_properties.Permissions?
  855. match_properties.Permissions : this->DirPermissions);
  856. if(!permissions)
  857. {
  858. // No permissions were explicitly provided but the user requested
  859. // that the source directory permissions be used.
  860. cmSystemTools::GetPermissions(source, permissions);
  861. }
  862. // Compute the set of permissions required on this directory to
  863. // recursively install files and subdirectories safely.
  864. mode_t required_permissions =
  865. mode_owner_read | mode_owner_write | mode_owner_execute;
  866. // If the required permissions are specified it is safe to set the
  867. // final permissions now. Otherwise we must add the required
  868. // permissions temporarily during file installation.
  869. mode_t permissions_before = 0;
  870. mode_t permissions_after = 0;
  871. if(permissions & required_permissions)
  872. {
  873. permissions_before = permissions;
  874. }
  875. else
  876. {
  877. permissions_before = permissions | required_permissions;
  878. permissions_after = permissions;
  879. }
  880. // Set the required permissions of the destination directory.
  881. if(permissions_before &&
  882. !cmSystemTools::SetPermissions(destination, permissions_before))
  883. {
  884. cmOStringStream e;
  885. e << "Problem setting permissions on directory \""
  886. << destination << "\"";
  887. this->FileCommand->SetError(e.str().c_str());
  888. return false;
  889. }
  890. // Load the directory contents to traverse it recursively.
  891. cmsys::Directory dir;
  892. if(source && *source)
  893. {
  894. dir.Load(source);
  895. }
  896. unsigned long numFiles = static_cast<unsigned long>(dir.GetNumberOfFiles());
  897. for(unsigned long fileNum = 0; fileNum < numFiles; ++fileNum)
  898. {
  899. if(!(strcmp(dir.GetFile(fileNum), ".") == 0 ||
  900. strcmp(dir.GetFile(fileNum), "..") == 0))
  901. {
  902. cmsys_stl::string fromPath = source;
  903. fromPath += "/";
  904. fromPath += dir.GetFile(fileNum);
  905. if(cmSystemTools::FileIsDirectory(fromPath.c_str()))
  906. {
  907. cmsys_stl::string toDir = destination;
  908. toDir += "/";
  909. toDir += dir.GetFile(fileNum);
  910. if(!this->InstallDirectory(fromPath.c_str(), toDir.c_str(), always))
  911. {
  912. return false;
  913. }
  914. }
  915. else
  916. {
  917. // Install this file.
  918. std::string toFile = destination;
  919. toFile += "/";
  920. toFile += dir.GetFile(fileNum);
  921. if(!this->InstallFile(fromPath.c_str(), toFile.c_str(), always))
  922. {
  923. return false;
  924. }
  925. }
  926. }
  927. }
  928. // Set the requested permissions of the destination directory.
  929. if(permissions_after &&
  930. !cmSystemTools::SetPermissions(destination, permissions_after))
  931. {
  932. cmOStringStream e;
  933. e << "Problem setting permissions on directory \"" << destination << "\"";
  934. this->FileCommand->SetError(e.str().c_str());
  935. return false;
  936. }
  937. return true;
  938. }
  939. //----------------------------------------------------------------------------
  940. bool cmFileCommand::HandleInstallCommand(
  941. std::vector<std::string> const& args)
  942. {
  943. if ( args.size() < 6 )
  944. {
  945. this->SetError("called with incorrect number of arguments");
  946. return false;
  947. }
  948. // Construct a file installer object.
  949. cmFileInstaller installer(this, this->Makefile);
  950. std::string rename = "";
  951. std::string destination = "";
  952. std::string stype = "FILES";
  953. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  954. std::set<cmStdString> components;
  955. std::set<cmStdString> configurations;
  956. std::vector<std::string> files;
  957. int itype = cmTarget::INSTALL_FILES;
  958. std::vector<std::string>::size_type i = 0;
  959. i++; // Get rid of subcommand
  960. std::map<cmStdString, const char*> properties;
  961. bool doing_files = false;
  962. bool doing_properties = false;
  963. bool doing_permissions_file = false;
  964. bool doing_permissions_dir = false;
  965. bool doing_permissions_match = false;
  966. bool doing_components = false;
  967. bool doing_configurations = false;
  968. bool use_given_permissions_file = false;
  969. bool use_given_permissions_dir = false;
  970. bool use_source_permissions = false;
  971. mode_t permissions_file = 0;
  972. mode_t permissions_dir = 0;
  973. bool optional = false;
  974. cmFileInstaller::MatchRule* current_match_rule = 0;
  975. for ( ; i != args.size(); ++i )
  976. {
  977. const std::string* cstr = &args[i];
  978. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  979. {
  980. if(current_match_rule)
  981. {
  982. cmOStringStream e;
  983. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  984. this->SetError(e.str().c_str());
  985. return false;
  986. }
  987. i++;
  988. destination = args[i];
  989. doing_files = false;
  990. doing_properties = false;
  991. doing_permissions_file = false;
  992. doing_permissions_dir = false;
  993. doing_components = false;
  994. doing_configurations = false;
  995. }
  996. else if ( *cstr == "TYPE" && i < args.size()-1 )
  997. {
  998. if(current_match_rule)
  999. {
  1000. cmOStringStream e;
  1001. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1002. this->SetError(e.str().c_str());
  1003. return false;
  1004. }
  1005. i++;
  1006. stype = args[i];
  1007. if ( args[i+1] == "OPTIONAL" )
  1008. {
  1009. i++;
  1010. optional = true;
  1011. }
  1012. doing_properties = false;
  1013. doing_files = false;
  1014. doing_permissions_file = false;
  1015. doing_permissions_dir = false;
  1016. doing_components = false;
  1017. doing_configurations = false;
  1018. }
  1019. else if ( *cstr == "RENAME" && i < args.size()-1 )
  1020. {
  1021. if(current_match_rule)
  1022. {
  1023. cmOStringStream e;
  1024. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1025. this->SetError(e.str().c_str());
  1026. return false;
  1027. }
  1028. i++;
  1029. rename = args[i];
  1030. doing_properties = false;
  1031. doing_files = false;
  1032. doing_permissions_file = false;
  1033. doing_permissions_dir = false;
  1034. doing_components = false;
  1035. doing_configurations = false;
  1036. }
  1037. else if ( *cstr == "REGEX" && i < args.size()-1 )
  1038. {
  1039. i++;
  1040. installer.MatchRules.push_back(cmFileInstaller::MatchRule(args[i]));
  1041. current_match_rule = &*(installer.MatchRules.end()-1);
  1042. if(!current_match_rule->Regex.is_valid())
  1043. {
  1044. cmOStringStream e;
  1045. e << "INSTALL could not compile REGEX \"" << args[i] << "\".";
  1046. this->SetError(e.str().c_str());
  1047. return false;
  1048. }
  1049. doing_properties = false;
  1050. doing_files = false;
  1051. doing_permissions_file = false;
  1052. doing_permissions_dir = false;
  1053. doing_components = false;
  1054. doing_configurations = false;
  1055. }
  1056. else if ( *cstr == "EXCLUDE" )
  1057. {
  1058. // Add this property to the current match rule.
  1059. if(!current_match_rule)
  1060. {
  1061. cmOStringStream e;
  1062. e << "INSTALL does not allow \""
  1063. << *cstr << "\" before a REGEX is given.";
  1064. this->SetError(e.str().c_str());
  1065. return false;
  1066. }
  1067. current_match_rule->Properties.Exclude = true;
  1068. doing_permissions_match = true;
  1069. }
  1070. else if ( *cstr == "PROPERTIES" )
  1071. {
  1072. if(current_match_rule)
  1073. {
  1074. cmOStringStream e;
  1075. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1076. this->SetError(e.str().c_str());
  1077. return false;
  1078. }
  1079. doing_properties = true;
  1080. doing_files = false;
  1081. doing_permissions_file = false;
  1082. doing_permissions_dir = false;
  1083. doing_components = false;
  1084. doing_configurations = false;
  1085. }
  1086. else if ( *cstr == "PERMISSIONS" )
  1087. {
  1088. if(current_match_rule)
  1089. {
  1090. doing_permissions_match = true;
  1091. doing_permissions_file = false;
  1092. }
  1093. else
  1094. {
  1095. doing_permissions_match = false;
  1096. doing_permissions_file = true;
  1097. use_given_permissions_file = true;
  1098. }
  1099. doing_properties = false;
  1100. doing_files = false;
  1101. doing_permissions_dir = false;
  1102. doing_components = false;
  1103. doing_configurations = false;
  1104. }
  1105. else if ( *cstr == "DIR_PERMISSIONS" )
  1106. {
  1107. if(current_match_rule)
  1108. {
  1109. cmOStringStream e;
  1110. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1111. this->SetError(e.str().c_str());
  1112. return false;
  1113. }
  1114. use_given_permissions_dir = true;
  1115. doing_properties = false;
  1116. doing_files = false;
  1117. doing_permissions_file = false;
  1118. doing_permissions_dir = true;
  1119. doing_components = false;
  1120. doing_configurations = false;
  1121. }
  1122. else if ( *cstr == "USE_SOURCE_PERMISSIONS" )
  1123. {
  1124. if(current_match_rule)
  1125. {
  1126. cmOStringStream e;
  1127. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1128. this->SetError(e.str().c_str());
  1129. return false;
  1130. }
  1131. doing_properties = false;
  1132. doing_files = false;
  1133. doing_permissions_file = false;
  1134. doing_permissions_dir = false;
  1135. doing_components = false;
  1136. doing_configurations = false;
  1137. use_source_permissions = true;
  1138. }
  1139. else if ( *cstr == "COMPONENTS" )
  1140. {
  1141. if(current_match_rule)
  1142. {
  1143. cmOStringStream e;
  1144. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1145. this->SetError(e.str().c_str());
  1146. return false;
  1147. }
  1148. doing_properties = false;
  1149. doing_files = false;
  1150. doing_permissions_file = false;
  1151. doing_permissions_dir = false;
  1152. doing_components = true;
  1153. doing_configurations = false;
  1154. }
  1155. else if ( *cstr == "CONFIGURATIONS" )
  1156. {
  1157. if(current_match_rule)
  1158. {
  1159. cmOStringStream e;
  1160. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1161. this->SetError(e.str().c_str());
  1162. return false;
  1163. }
  1164. doing_properties = false;
  1165. doing_files = false;
  1166. doing_permissions_file = false;
  1167. doing_permissions_dir = false;
  1168. doing_components = false;
  1169. doing_configurations = true;
  1170. }
  1171. else if ( *cstr == "FILES" && !doing_files)
  1172. {
  1173. if(current_match_rule)
  1174. {
  1175. cmOStringStream e;
  1176. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1177. this->SetError(e.str().c_str());
  1178. return false;
  1179. }
  1180. doing_files = true;
  1181. doing_properties = false;
  1182. doing_permissions_file = false;
  1183. doing_permissions_dir = false;
  1184. doing_components = false;
  1185. doing_configurations = false;
  1186. }
  1187. else if ( doing_properties && i < args.size()-1 )
  1188. {
  1189. properties[args[i]] = args[i+1].c_str();
  1190. i++;
  1191. }
  1192. else if ( doing_files )
  1193. {
  1194. files.push_back(*cstr);
  1195. }
  1196. else if ( doing_components )
  1197. {
  1198. components.insert(*cstr);
  1199. }
  1200. else if ( doing_configurations )
  1201. {
  1202. configurations.insert(cmSystemTools::UpperCase(*cstr));
  1203. }
  1204. else if(doing_permissions_file)
  1205. {
  1206. if(!installer.CheckPermissions(args[i], permissions_file))
  1207. {
  1208. return false;
  1209. }
  1210. }
  1211. else if(doing_permissions_dir)
  1212. {
  1213. if(!installer.CheckPermissions(args[i], permissions_dir))
  1214. {
  1215. return false;
  1216. }
  1217. }
  1218. else if(doing_permissions_match)
  1219. {
  1220. if(!installer.CheckPermissions(
  1221. args[i], current_match_rule->Properties.Permissions))
  1222. {
  1223. return false;
  1224. }
  1225. }
  1226. else
  1227. {
  1228. this->SetError("called with inappropriate arguments");
  1229. return false;
  1230. }
  1231. }
  1232. if ( destination.size() < 2 )
  1233. {
  1234. this->SetError("called with inapropriate arguments. "
  1235. "No DESTINATION provided or .");
  1236. return false;
  1237. }
  1238. // Check for component-specific installation.
  1239. const char* cmake_install_component =
  1240. this->Makefile->GetDefinition("CMAKE_INSTALL_COMPONENT");
  1241. if(cmake_install_component && *cmake_install_component)
  1242. {
  1243. // This install rule applies only if it is associated with the
  1244. // current component.
  1245. if(components.find(cmake_install_component) == components.end())
  1246. {
  1247. return true;
  1248. }
  1249. }
  1250. // Check for configuration-specific installation.
  1251. if(!configurations.empty())
  1252. {
  1253. std::string cmake_install_configuration =
  1254. cmSystemTools::UpperCase(
  1255. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_CONFIG_NAME"));
  1256. if(cmake_install_configuration.empty())
  1257. {
  1258. // No configuration specified for installation but this install
  1259. // rule is configuration-specific. Skip it.
  1260. return true;
  1261. }
  1262. else if(configurations.find(cmake_install_configuration) ==
  1263. configurations.end())
  1264. {
  1265. // This rule is specific to a configuration not being installed.
  1266. return true;
  1267. }
  1268. }
  1269. if ( destdir && *destdir )
  1270. {
  1271. std::string sdestdir = destdir;
  1272. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  1273. char ch1 = destination[0];
  1274. char ch2 = destination[1];
  1275. char ch3 = 0;
  1276. if ( destination.size() > 2 )
  1277. {
  1278. ch3 = destination[2];
  1279. }
  1280. int skip = 0;
  1281. if ( ch1 != '/' )
  1282. {
  1283. int relative = 0;
  1284. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  1285. ch2 == ':' )
  1286. {
  1287. // Assume windows
  1288. // let's do some destdir magic:
  1289. skip = 2;
  1290. if ( ch3 != '/' )
  1291. {
  1292. relative = 1;
  1293. }
  1294. }
  1295. else
  1296. {
  1297. relative = 1;
  1298. }
  1299. if ( relative )
  1300. {
  1301. // This is relative path on unix or windows. Since we are doing
  1302. // destdir, this case does not make sense.
  1303. this->SetError("called with relative DESTINATION. This "
  1304. "does not make sense when using DESTDIR. Specify "
  1305. "absolute path or remove DESTDIR environment variable.");
  1306. return false;
  1307. }
  1308. }
  1309. else
  1310. {
  1311. if ( ch2 == '/' )
  1312. {
  1313. // looks like a network path.
  1314. this->SetError("called with network path DESTINATION. This "
  1315. "does not make sense when using DESTDIR. Specify local "
  1316. "absolute path or remove DESTDIR environment variable.");
  1317. return false;
  1318. }
  1319. }
  1320. destination = sdestdir + (destination.c_str() + skip);
  1321. installer.DestDirLength = int(sdestdir.size());
  1322. }
  1323. if ( files.size() == 0 )
  1324. {
  1325. this->SetError(
  1326. "called with inapropriate arguments. No FILES provided.");
  1327. return false;
  1328. }
  1329. if ( stype == "EXECUTABLE" )
  1330. {
  1331. itype = cmTarget::EXECUTABLE;
  1332. }
  1333. else if ( stype == "PROGRAM" )
  1334. {
  1335. itype = cmTarget::INSTALL_PROGRAMS;
  1336. }
  1337. else if ( stype == "STATIC_LIBRARY" )
  1338. {
  1339. itype = cmTarget::STATIC_LIBRARY;
  1340. }
  1341. else if ( stype == "SHARED_LIBRARY" )
  1342. {
  1343. itype = cmTarget::SHARED_LIBRARY;
  1344. }
  1345. else if ( stype == "MODULE" )
  1346. {
  1347. itype = cmTarget::MODULE_LIBRARY;
  1348. }
  1349. else if ( stype == "DIRECTORY" )
  1350. {
  1351. itype = cmTarget::INSTALL_DIRECTORY;
  1352. }
  1353. if ( !cmSystemTools::FileExists(destination.c_str()) )
  1354. {
  1355. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  1356. {
  1357. std::string errstring = "cannot create directory: " + destination +
  1358. ". Maybe need administrative privileges.";
  1359. this->SetError(errstring.c_str());
  1360. return false;
  1361. }
  1362. }
  1363. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  1364. {
  1365. std::string errstring = "INSTALL destination: " + destination +
  1366. " is not a directory.";
  1367. this->SetError(errstring.c_str());
  1368. return false;
  1369. }
  1370. // Check rename form.
  1371. if(!rename.empty())
  1372. {
  1373. if(itype != cmTarget::INSTALL_FILES &&
  1374. itype != cmTarget::INSTALL_PROGRAMS)
  1375. {
  1376. this->SetError("INSTALL option RENAME may be used only with "
  1377. "FILES or PROGRAMS.");
  1378. return false;
  1379. }
  1380. if(files.size() > 1)
  1381. {
  1382. this->SetError("INSTALL option RENAME may be used only with one file.");
  1383. return false;
  1384. }
  1385. }
  1386. // Choose a default for shared library permissions.
  1387. bool install_so_no_exe = this->Makefile->IsOn("CMAKE_INSTALL_SO_NO_EXE");
  1388. // If file permissions were not specified set default permissions
  1389. // for this target type.
  1390. if(!use_given_permissions_file && !use_source_permissions)
  1391. {
  1392. switch(itype)
  1393. {
  1394. case cmTarget::SHARED_LIBRARY:
  1395. case cmTarget::MODULE_LIBRARY:
  1396. if(install_so_no_exe)
  1397. {
  1398. // Use read/write permissions.
  1399. permissions_file = 0;
  1400. permissions_file |= mode_owner_read;
  1401. permissions_file |= mode_owner_write;
  1402. permissions_file |= mode_group_read;
  1403. permissions_file |= mode_world_read;
  1404. break;
  1405. }
  1406. case cmTarget::EXECUTABLE:
  1407. case cmTarget::INSTALL_PROGRAMS:
  1408. // Use read/write/executable permissions.
  1409. permissions_file = 0;
  1410. permissions_file |= mode_owner_read;
  1411. permissions_file |= mode_owner_write;
  1412. permissions_file |= mode_owner_execute;
  1413. permissions_file |= mode_group_read;
  1414. permissions_file |= mode_group_execute;
  1415. permissions_file |= mode_world_read;
  1416. permissions_file |= mode_world_execute;
  1417. break;
  1418. default:
  1419. // Use read/write permissions.
  1420. permissions_file = 0;
  1421. permissions_file |= mode_owner_read;
  1422. permissions_file |= mode_owner_write;
  1423. permissions_file |= mode_group_read;
  1424. permissions_file |= mode_world_read;
  1425. break;
  1426. }
  1427. }
  1428. // If directory permissions were not specified set default permissions.
  1429. if(!use_given_permissions_dir && !use_source_permissions)
  1430. {
  1431. // Use read/write/executable permissions.
  1432. permissions_dir = 0;
  1433. permissions_dir |= mode_owner_read;
  1434. permissions_dir |= mode_owner_write;
  1435. permissions_dir |= mode_owner_execute;
  1436. permissions_dir |= mode_group_read;
  1437. permissions_dir |= mode_group_execute;
  1438. permissions_dir |= mode_world_read;
  1439. permissions_dir |= mode_world_execute;
  1440. }
  1441. // Set the installer permissions.
  1442. installer.FilePermissions = permissions_file;
  1443. installer.DirPermissions = permissions_dir;
  1444. // Check whether files should be copied always or only if they have
  1445. // changed.
  1446. bool copy_always =
  1447. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  1448. // Handle each file listed.
  1449. for ( i = 0; i < files.size(); i ++ )
  1450. {
  1451. // Split the input file into its directory and name components.
  1452. std::vector<std::string> fromPathComponents;
  1453. cmSystemTools::SplitPath(files[i].c_str(), fromPathComponents);
  1454. std::string fromName = *(fromPathComponents.end()-1);
  1455. std::string fromDir = cmSystemTools::JoinPath(fromPathComponents.begin(),
  1456. fromPathComponents.end()-1);
  1457. // Compute the full path to the destination file.
  1458. std::string toFile = destination;
  1459. std::string const& toName = rename.empty()? fromName : rename;
  1460. if(!toName.empty())
  1461. {
  1462. toFile += "/";
  1463. toFile += toName;
  1464. }
  1465. // Handle type-specific installation details.
  1466. switch(itype)
  1467. {
  1468. case cmTarget::MODULE_LIBRARY:
  1469. case cmTarget::STATIC_LIBRARY:
  1470. case cmTarget::SHARED_LIBRARY:
  1471. {
  1472. // Handle shared library versioning
  1473. const char* lib_version = 0;
  1474. const char* lib_soversion = 0;
  1475. if ( properties.find("VERSION") != properties.end() )
  1476. {
  1477. lib_version = properties["VERSION"];
  1478. }
  1479. if ( properties.find("SOVERSION") != properties.end() )
  1480. {
  1481. lib_soversion = properties["SOVERSION"];
  1482. }
  1483. if ( !lib_version && lib_soversion )
  1484. {
  1485. lib_version = lib_soversion;
  1486. }
  1487. if ( !lib_soversion && lib_version )
  1488. {
  1489. lib_soversion = lib_version;
  1490. }
  1491. if ( lib_version && lib_soversion )
  1492. {
  1493. std::string libname = toFile;
  1494. std::string soname = toFile;
  1495. std::string soname_nopath = fromName;
  1496. this->ComputeVersionedName(soname, lib_soversion);
  1497. this->ComputeVersionedName(soname_nopath, lib_soversion);
  1498. this->ComputeVersionedName(fromName, lib_version);
  1499. this->ComputeVersionedName(toFile, lib_version);
  1500. cmSystemTools::RemoveFile(soname.c_str());
  1501. cmSystemTools::RemoveFile(libname.c_str());
  1502. if (!cmSystemTools::CreateSymlink(soname_nopath.c_str(),
  1503. libname.c_str()) )
  1504. {
  1505. std::string errstring = "error when creating symlink from: "
  1506. + libname + " to " + soname_nopath;
  1507. this->SetError(errstring.c_str());
  1508. return false;
  1509. }
  1510. installer.ManifestAppend(libname);
  1511. if ( toFile != soname )
  1512. {
  1513. if ( !cmSystemTools::CreateSymlink(fromName.c_str(),
  1514. soname.c_str()) )
  1515. {
  1516. std::string errstring = "error when creating symlink from: "
  1517. + soname + " to " + fromName;
  1518. this->SetError(errstring.c_str());
  1519. return false;
  1520. }
  1521. installer.ManifestAppend(soname);
  1522. }
  1523. }
  1524. }
  1525. break;
  1526. case cmTarget::EXECUTABLE:
  1527. {
  1528. // Handle executable versioning
  1529. const char* exe_version = 0;
  1530. if ( properties.find("VERSION") != properties.end() )
  1531. {
  1532. exe_version = properties["VERSION"];
  1533. }
  1534. if ( exe_version )
  1535. {
  1536. std::string exename = toFile;
  1537. std::string exename_nopath = fromName;
  1538. exename_nopath += "-";
  1539. exename_nopath += exe_version;
  1540. fromName += "-";
  1541. fromName += exe_version;
  1542. toFile += "-";
  1543. toFile += exe_version;
  1544. cmSystemTools::RemoveFile(exename.c_str());
  1545. if (!cmSystemTools::CreateSymlink(exename_nopath.c_str(),
  1546. exename.c_str()) )
  1547. {
  1548. std::string errstring = "error when creating symlink from: "
  1549. + exename + " to " + exename_nopath;
  1550. this->SetError(errstring.c_str());
  1551. return false;
  1552. }
  1553. installer.ManifestAppend(exename);
  1554. }
  1555. }
  1556. break;
  1557. }
  1558. // Construct the full path to the source file. The file name may
  1559. // have been changed above.
  1560. std::string fromFile = fromDir;
  1561. if(!fromName.empty())
  1562. {
  1563. fromFile += "/";
  1564. fromFile += fromName;
  1565. }
  1566. std::string message;
  1567. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  1568. {
  1569. if(itype == cmTarget::INSTALL_DIRECTORY &&
  1570. (fromFile.empty() ||
  1571. cmSystemTools::FileIsDirectory(fromFile.c_str())))
  1572. {
  1573. // Try installing this directory.
  1574. if(!installer.InstallDirectory(fromFile.c_str(), toFile.c_str(),
  1575. copy_always))
  1576. {
  1577. return false;
  1578. }
  1579. }
  1580. else if(cmSystemTools::FileExists(fromFile.c_str()))
  1581. {
  1582. // Install this file.
  1583. if(!installer.InstallFile(fromFile.c_str(), toFile.c_str(),
  1584. copy_always))
  1585. {
  1586. return false;
  1587. }
  1588. // Perform post-installation processing on the file depending
  1589. // on its type.
  1590. #if defined(__APPLE_CC__)
  1591. // Static libraries need ranlib on this platform.
  1592. if(itype == cmTarget::STATIC_LIBRARY)
  1593. {
  1594. std::string ranlib = "ranlib ";
  1595. ranlib += cmSystemTools::ConvertToOutputPath(toFile.c_str());
  1596. if(!cmSystemTools::RunSingleCommand(ranlib.c_str()))
  1597. {
  1598. std::string err = "ranlib failed: ";
  1599. err += ranlib;
  1600. this->SetError(err.c_str());
  1601. return false;
  1602. }
  1603. }
  1604. #endif
  1605. }
  1606. else if(!optional)
  1607. {
  1608. // The input file does not exist and installation is not optional.
  1609. cmOStringStream e;
  1610. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  1611. this->SetError(e.str().c_str());
  1612. return false;
  1613. }
  1614. }
  1615. }
  1616. return true;
  1617. }
  1618. //----------------------------------------------------------------------------
  1619. void cmFileCommand::ComputeVersionedName(std::string& name,
  1620. const char* version)
  1621. {
  1622. #if defined(__APPLE__)
  1623. std::string ext;
  1624. cmsys_stl::string::size_type dot_pos = name.rfind(".");
  1625. if(dot_pos != name.npos)
  1626. {
  1627. ext = name.substr(dot_pos, name.npos);
  1628. name = name.substr(0, dot_pos);
  1629. }
  1630. #endif
  1631. name += ".";
  1632. name += version;
  1633. #if defined(__APPLE__)
  1634. name += ext;
  1635. #endif
  1636. }
  1637. //----------------------------------------------------------------------------
  1638. bool cmFileCommand::HandleRelativePathCommand(
  1639. std::vector<std::string> const& args)
  1640. {
  1641. if(args.size() != 4 )
  1642. {
  1643. this->SetError("called with incorrect number of arguments");
  1644. return false;
  1645. }
  1646. const std::string& outVar = args[1];
  1647. const std::string& directoryName = args[2];
  1648. const std::string& fileName = args[3];
  1649. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  1650. {
  1651. std::string errstring =
  1652. "RelativePath must be passed a full path to the directory: "
  1653. + directoryName;
  1654. this->SetError(errstring.c_str());
  1655. return false;
  1656. }
  1657. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  1658. {
  1659. std::string errstring =
  1660. "RelativePath must be passed a full path to the file: "
  1661. + fileName;
  1662. this->SetError(errstring.c_str());
  1663. return false;
  1664. }
  1665. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  1666. fileName.c_str());
  1667. this->Makefile->AddDefinition(outVar.c_str(),
  1668. res.c_str());
  1669. return true;
  1670. }
  1671. //----------------------------------------------------------------------------
  1672. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  1673. bool recurse)
  1674. {
  1675. std::string message;
  1676. std::vector<std::string>::const_iterator i = args.begin();
  1677. i++; // Get rid of subcommand
  1678. for(;i != args.end(); ++i)
  1679. {
  1680. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  1681. {
  1682. cmSystemTools::RemoveADirectory(i->c_str());
  1683. }
  1684. else
  1685. {
  1686. cmSystemTools::RemoveFile(i->c_str());
  1687. }
  1688. }
  1689. return true;
  1690. }
  1691. //----------------------------------------------------------------------------
  1692. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  1693. const& args,
  1694. bool nativePath)
  1695. {
  1696. std::vector<std::string>::const_iterator i = args.begin();
  1697. if(args.size() != 3)
  1698. {
  1699. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  1700. "only three arguments.");
  1701. return false;
  1702. }
  1703. i++; // Get rid of subcommand
  1704. #if defined(_WIN32) && !defined(__CYGWIN__)
  1705. char pathSep = ';';
  1706. #else
  1707. char pathSep = ':';
  1708. #endif
  1709. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  1710. pathSep);
  1711. i++;
  1712. const char* var = i->c_str();
  1713. std::string value;
  1714. for(std::vector<cmsys::String>::iterator j = path.begin();
  1715. j != path.end(); ++j)
  1716. {
  1717. if(j != path.begin())
  1718. {
  1719. value += ";";
  1720. }
  1721. if(!nativePath)
  1722. {
  1723. cmSystemTools::ConvertToUnixSlashes(*j);
  1724. }
  1725. else
  1726. {
  1727. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  1728. // remove double quotes in the path
  1729. cmsys::String& s = *j;
  1730. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  1731. {
  1732. s = s.substr(1,s.size()-2);
  1733. }
  1734. }
  1735. value += *j;
  1736. }
  1737. this->Makefile->AddDefinition(var, value.c_str());
  1738. return true;
  1739. }