cmFileCommand.cxx 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928
  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. void cmFileCommand::HandleInstallPermissions(cmFileInstaller& installer,
  941. mode_t& permissions_file,
  942. mode_t& permissions_dir,
  943. int itype,
  944. bool use_given_permissions_file,
  945. bool use_given_permissions_dir,
  946. bool use_source_permissions) const
  947. {
  948. // Choose a default for shared library permissions.
  949. bool install_so_no_exe = this->Makefile->IsOn("CMAKE_INSTALL_SO_NO_EXE");
  950. // If file permissions were not specified set default permissions
  951. // for this target type.
  952. if(!use_given_permissions_file && !use_source_permissions)
  953. {
  954. switch(itype)
  955. {
  956. case cmTarget::SHARED_LIBRARY:
  957. case cmTarget::MODULE_LIBRARY:
  958. if(install_so_no_exe)
  959. {
  960. // Use read/write permissions.
  961. permissions_file = 0;
  962. permissions_file |= mode_owner_read;
  963. permissions_file |= mode_owner_write;
  964. permissions_file |= mode_group_read;
  965. permissions_file |= mode_world_read;
  966. break;
  967. }
  968. case cmTarget::EXECUTABLE:
  969. case cmTarget::INSTALL_PROGRAMS:
  970. // Use read/write/executable permissions.
  971. permissions_file = 0;
  972. permissions_file |= mode_owner_read;
  973. permissions_file |= mode_owner_write;
  974. permissions_file |= mode_owner_execute;
  975. permissions_file |= mode_group_read;
  976. permissions_file |= mode_group_execute;
  977. permissions_file |= mode_world_read;
  978. permissions_file |= mode_world_execute;
  979. break;
  980. default:
  981. // Use read/write permissions.
  982. permissions_file = 0;
  983. permissions_file |= mode_owner_read;
  984. permissions_file |= mode_owner_write;
  985. permissions_file |= mode_group_read;
  986. permissions_file |= mode_world_read;
  987. break;
  988. }
  989. }
  990. // If directory permissions were not specified set default permissions.
  991. if(!use_given_permissions_dir && !use_source_permissions)
  992. {
  993. // Use read/write/executable permissions.
  994. permissions_dir = 0;
  995. permissions_dir |= mode_owner_read;
  996. permissions_dir |= mode_owner_write;
  997. permissions_dir |= mode_owner_execute;
  998. permissions_dir |= mode_group_read;
  999. permissions_dir |= mode_group_execute;
  1000. permissions_dir |= mode_world_read;
  1001. permissions_dir |= mode_world_execute;
  1002. }
  1003. // Set the installer permissions.
  1004. installer.FilePermissions = permissions_file;
  1005. installer.DirPermissions = permissions_dir;
  1006. }
  1007. //----------------------------------------------------------------------------
  1008. void cmFileCommand
  1009. ::GetTargetTypeFromString(const std::string& stype, int& itype) const
  1010. {
  1011. if ( stype == "EXECUTABLE" )
  1012. {
  1013. itype = cmTarget::EXECUTABLE;
  1014. }
  1015. else if ( stype == "PROGRAM" )
  1016. {
  1017. itype = cmTarget::INSTALL_PROGRAMS;
  1018. }
  1019. else if ( stype == "STATIC_LIBRARY" )
  1020. {
  1021. itype = cmTarget::STATIC_LIBRARY;
  1022. }
  1023. else if ( stype == "SHARED_LIBRARY" )
  1024. {
  1025. itype = cmTarget::SHARED_LIBRARY;
  1026. }
  1027. else if ( stype == "MODULE" )
  1028. {
  1029. itype = cmTarget::MODULE_LIBRARY;
  1030. }
  1031. else if ( stype == "DIRECTORY" )
  1032. {
  1033. itype = cmTarget::INSTALL_DIRECTORY;
  1034. }
  1035. }
  1036. //----------------------------------------------------------------------------
  1037. bool cmFileCommand::HandleInstallDestination(cmFileInstaller& installer,
  1038. std::string& destination)
  1039. {
  1040. if ( destination.size() < 2 )
  1041. {
  1042. this->SetError("called with inapropriate arguments. "
  1043. "No DESTINATION provided or .");
  1044. return false;
  1045. }
  1046. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  1047. if ( destdir && *destdir )
  1048. {
  1049. std::string sdestdir = destdir;
  1050. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  1051. char ch1 = destination[0];
  1052. char ch2 = destination[1];
  1053. char ch3 = 0;
  1054. if ( destination.size() > 2 )
  1055. {
  1056. ch3 = destination[2];
  1057. }
  1058. int skip = 0;
  1059. if ( ch1 != '/' )
  1060. {
  1061. int relative = 0;
  1062. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  1063. ch2 == ':' )
  1064. {
  1065. // Assume windows
  1066. // let's do some destdir magic:
  1067. skip = 2;
  1068. if ( ch3 != '/' )
  1069. {
  1070. relative = 1;
  1071. }
  1072. }
  1073. else
  1074. {
  1075. relative = 1;
  1076. }
  1077. if ( relative )
  1078. {
  1079. // This is relative path on unix or windows. Since we are doing
  1080. // destdir, this case does not make sense.
  1081. this->SetError("called with relative DESTINATION. This "
  1082. "does not make sense when using DESTDIR. Specify "
  1083. "absolute path or remove DESTDIR environment variable.");
  1084. return false;
  1085. }
  1086. }
  1087. else
  1088. {
  1089. if ( ch2 == '/' )
  1090. {
  1091. // looks like a network path.
  1092. this->SetError("called with network path DESTINATION. This "
  1093. "does not make sense when using DESTDIR. Specify local "
  1094. "absolute path or remove DESTDIR environment variable.");
  1095. return false;
  1096. }
  1097. }
  1098. destination = sdestdir + (destination.c_str() + skip);
  1099. installer.DestDirLength = int(sdestdir.size());
  1100. }
  1101. if ( !cmSystemTools::FileExists(destination.c_str()) )
  1102. {
  1103. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  1104. {
  1105. std::string errstring = "cannot create directory: " + destination +
  1106. ". Maybe need administrative privileges.";
  1107. this->SetError(errstring.c_str());
  1108. return false;
  1109. }
  1110. }
  1111. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  1112. {
  1113. std::string errstring = "INSTALL destination: " + destination +
  1114. " is not a directory.";
  1115. this->SetError(errstring.c_str());
  1116. return false;
  1117. }
  1118. return true;
  1119. }
  1120. //----------------------------------------------------------------------------
  1121. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  1122. {
  1123. if ( args.size() < 6 )
  1124. {
  1125. this->SetError("called with incorrect number of arguments");
  1126. return false;
  1127. }
  1128. // Construct a file installer object.
  1129. cmFileInstaller installer(this, this->Makefile);
  1130. std::string rename = "";
  1131. std::string destination = "";
  1132. std::set<cmStdString> components;
  1133. std::set<cmStdString> configurations;
  1134. std::vector<std::string> files;
  1135. int itype = cmTarget::INSTALL_FILES;
  1136. std::map<cmStdString, const char*> properties;
  1137. bool optional = false;
  1138. bool result = this->ParseInstallArgs(args, installer, components,
  1139. configurations, properties,
  1140. itype, rename, destination, files,
  1141. optional);
  1142. if (result == true)
  1143. {
  1144. result = this->DoInstall(installer, components, configurations, properties,
  1145. itype, rename, destination, files, optional);
  1146. }
  1147. return result;
  1148. }
  1149. //----------------------------------------------------------------------------
  1150. bool cmFileCommand::ParseInstallArgs(std::vector<std::string> const& args,
  1151. cmFileInstaller& installer,
  1152. std::set<cmStdString>& components,
  1153. std::set<cmStdString>& configurations,
  1154. std::map<cmStdString, const char*>& properties,
  1155. int& itype,
  1156. std::string& rename,
  1157. std::string& destination,
  1158. std::vector<std::string>& files,
  1159. bool& optional)
  1160. {
  1161. std::string stype = "FILES";
  1162. bool doing_files = false;
  1163. bool doing_properties = false;
  1164. bool doing_permissions_file = false;
  1165. bool doing_permissions_dir = false;
  1166. bool doing_permissions_match = false;
  1167. bool doing_components = false;
  1168. bool doing_configurations = false;
  1169. bool use_given_permissions_file = false;
  1170. bool use_given_permissions_dir = false;
  1171. bool use_source_permissions = false;
  1172. mode_t permissions_file = 0;
  1173. mode_t permissions_dir = 0;
  1174. cmFileInstaller::MatchRule* current_match_rule = 0;
  1175. std::vector<std::string>::size_type i = 0;
  1176. i++; // Get rid of subcommand
  1177. for ( ; i != args.size(); ++i )
  1178. {
  1179. const std::string* cstr = &args[i];
  1180. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  1181. {
  1182. if(current_match_rule)
  1183. {
  1184. cmOStringStream e;
  1185. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1186. this->SetError(e.str().c_str());
  1187. return false;
  1188. }
  1189. i++;
  1190. destination = args[i];
  1191. doing_files = false;
  1192. doing_properties = false;
  1193. doing_permissions_file = false;
  1194. doing_permissions_dir = false;
  1195. doing_components = false;
  1196. doing_configurations = false;
  1197. }
  1198. else if ( *cstr == "TYPE" && i < args.size()-1 )
  1199. {
  1200. if(current_match_rule)
  1201. {
  1202. cmOStringStream e;
  1203. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1204. this->SetError(e.str().c_str());
  1205. return false;
  1206. }
  1207. i++;
  1208. stype = args[i];
  1209. if ( args[i+1] == "OPTIONAL" )
  1210. {
  1211. i++;
  1212. optional = true;
  1213. }
  1214. doing_properties = false;
  1215. doing_files = false;
  1216. doing_permissions_file = false;
  1217. doing_permissions_dir = false;
  1218. doing_components = false;
  1219. doing_configurations = false;
  1220. }
  1221. else if ( *cstr == "RENAME" && i < args.size()-1 )
  1222. {
  1223. if(current_match_rule)
  1224. {
  1225. cmOStringStream e;
  1226. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1227. this->SetError(e.str().c_str());
  1228. return false;
  1229. }
  1230. i++;
  1231. rename = args[i];
  1232. doing_properties = false;
  1233. doing_files = false;
  1234. doing_permissions_file = false;
  1235. doing_permissions_dir = false;
  1236. doing_components = false;
  1237. doing_configurations = false;
  1238. }
  1239. else if ( *cstr == "REGEX" && i < args.size()-1 )
  1240. {
  1241. i++;
  1242. installer.MatchRules.push_back(cmFileInstaller::MatchRule(args[i]));
  1243. current_match_rule = &*(installer.MatchRules.end()-1);
  1244. if(!current_match_rule->Regex.is_valid())
  1245. {
  1246. cmOStringStream e;
  1247. e << "INSTALL could not compile REGEX \"" << args[i] << "\".";
  1248. this->SetError(e.str().c_str());
  1249. return false;
  1250. }
  1251. doing_properties = false;
  1252. doing_files = false;
  1253. doing_permissions_file = false;
  1254. doing_permissions_dir = false;
  1255. doing_components = false;
  1256. doing_configurations = false;
  1257. }
  1258. else if ( *cstr == "EXCLUDE" )
  1259. {
  1260. // Add this property to the current match rule.
  1261. if(!current_match_rule)
  1262. {
  1263. cmOStringStream e;
  1264. e << "INSTALL does not allow \""
  1265. << *cstr << "\" before a REGEX is given.";
  1266. this->SetError(e.str().c_str());
  1267. return false;
  1268. }
  1269. current_match_rule->Properties.Exclude = true;
  1270. doing_permissions_match = true;
  1271. }
  1272. else if ( *cstr == "PROPERTIES" )
  1273. {
  1274. if(current_match_rule)
  1275. {
  1276. cmOStringStream e;
  1277. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1278. this->SetError(e.str().c_str());
  1279. return false;
  1280. }
  1281. doing_properties = true;
  1282. doing_files = false;
  1283. doing_permissions_file = false;
  1284. doing_permissions_dir = false;
  1285. doing_components = false;
  1286. doing_configurations = false;
  1287. }
  1288. else if ( *cstr == "PERMISSIONS" )
  1289. {
  1290. if(current_match_rule)
  1291. {
  1292. doing_permissions_match = true;
  1293. doing_permissions_file = false;
  1294. }
  1295. else
  1296. {
  1297. doing_permissions_match = false;
  1298. doing_permissions_file = true;
  1299. use_given_permissions_file = true;
  1300. }
  1301. doing_properties = false;
  1302. doing_files = false;
  1303. doing_permissions_dir = false;
  1304. doing_components = false;
  1305. doing_configurations = false;
  1306. }
  1307. else if ( *cstr == "DIR_PERMISSIONS" )
  1308. {
  1309. if(current_match_rule)
  1310. {
  1311. cmOStringStream e;
  1312. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1313. this->SetError(e.str().c_str());
  1314. return false;
  1315. }
  1316. use_given_permissions_dir = true;
  1317. doing_properties = false;
  1318. doing_files = false;
  1319. doing_permissions_file = false;
  1320. doing_permissions_dir = true;
  1321. doing_components = false;
  1322. doing_configurations = false;
  1323. }
  1324. else if ( *cstr == "USE_SOURCE_PERMISSIONS" )
  1325. {
  1326. if(current_match_rule)
  1327. {
  1328. cmOStringStream e;
  1329. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1330. this->SetError(e.str().c_str());
  1331. return false;
  1332. }
  1333. doing_properties = false;
  1334. doing_files = false;
  1335. doing_permissions_file = false;
  1336. doing_permissions_dir = false;
  1337. doing_components = false;
  1338. doing_configurations = false;
  1339. use_source_permissions = true;
  1340. }
  1341. else if ( *cstr == "COMPONENTS" )
  1342. {
  1343. if(current_match_rule)
  1344. {
  1345. cmOStringStream e;
  1346. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1347. this->SetError(e.str().c_str());
  1348. return false;
  1349. }
  1350. doing_properties = false;
  1351. doing_files = false;
  1352. doing_permissions_file = false;
  1353. doing_permissions_dir = false;
  1354. doing_components = true;
  1355. doing_configurations = false;
  1356. }
  1357. else if ( *cstr == "CONFIGURATIONS" )
  1358. {
  1359. if(current_match_rule)
  1360. {
  1361. cmOStringStream e;
  1362. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1363. this->SetError(e.str().c_str());
  1364. return false;
  1365. }
  1366. doing_properties = false;
  1367. doing_files = false;
  1368. doing_permissions_file = false;
  1369. doing_permissions_dir = false;
  1370. doing_components = false;
  1371. doing_configurations = true;
  1372. }
  1373. else if ( *cstr == "FILES" && !doing_files)
  1374. {
  1375. if(current_match_rule)
  1376. {
  1377. cmOStringStream e;
  1378. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1379. this->SetError(e.str().c_str());
  1380. return false;
  1381. }
  1382. doing_files = true;
  1383. doing_properties = false;
  1384. doing_permissions_file = false;
  1385. doing_permissions_dir = false;
  1386. doing_components = false;
  1387. doing_configurations = false;
  1388. }
  1389. else if ( doing_properties && i < args.size()-1 )
  1390. {
  1391. properties[args[i]] = args[i+1].c_str();
  1392. i++;
  1393. }
  1394. else if ( doing_files )
  1395. {
  1396. files.push_back(*cstr);
  1397. }
  1398. else if ( doing_components )
  1399. {
  1400. components.insert(*cstr);
  1401. }
  1402. else if ( doing_configurations )
  1403. {
  1404. configurations.insert(cmSystemTools::UpperCase(*cstr));
  1405. }
  1406. else if(doing_permissions_file)
  1407. {
  1408. if(!installer.CheckPermissions(args[i], permissions_file))
  1409. {
  1410. return false;
  1411. }
  1412. }
  1413. else if(doing_permissions_dir)
  1414. {
  1415. if(!installer.CheckPermissions(args[i], permissions_dir))
  1416. {
  1417. return false;
  1418. }
  1419. }
  1420. else if(doing_permissions_match)
  1421. {
  1422. if(!installer.CheckPermissions(
  1423. args[i], current_match_rule->Properties.Permissions))
  1424. {
  1425. return false;
  1426. }
  1427. }
  1428. else
  1429. {
  1430. this->SetError("called with inappropriate arguments");
  1431. return false;
  1432. }
  1433. }
  1434. // now check and postprocess what has been parsed
  1435. if ( files.size() == 0 )
  1436. {
  1437. this->SetError(
  1438. "called with inapropriate arguments. No FILES provided.");
  1439. return false;
  1440. }
  1441. // Check rename form.
  1442. if(!rename.empty())
  1443. {
  1444. if(itype != cmTarget::INSTALL_FILES &&
  1445. itype != cmTarget::INSTALL_PROGRAMS)
  1446. {
  1447. this->SetError("INSTALL option RENAME may be used only with "
  1448. "FILES or PROGRAMS.");
  1449. return false;
  1450. }
  1451. if(files.size() > 1)
  1452. {
  1453. this->SetError("INSTALL option RENAME may be used only with "
  1454. "one file.");
  1455. return false;
  1456. }
  1457. }
  1458. if (this->HandleInstallDestination(installer, destination) == false)
  1459. {
  1460. return false;
  1461. }
  1462. this->GetTargetTypeFromString(stype, itype);
  1463. this->HandleInstallPermissions(installer,
  1464. permissions_file,
  1465. permissions_dir,
  1466. itype,
  1467. use_given_permissions_file,
  1468. use_given_permissions_dir,
  1469. use_source_permissions);
  1470. return true;
  1471. }
  1472. //----------------------------------------------------------------------------
  1473. bool cmFileCommand::DoInstall( cmFileInstaller& installer,
  1474. const std::set<cmStdString>& components,
  1475. const std::set<cmStdString>& configurations,
  1476. std::map<cmStdString, const char*>& properties,
  1477. const int itype,
  1478. const std::string& rename,
  1479. const std::string& destination,
  1480. const std::vector<std::string>& files,
  1481. const bool optional)
  1482. {
  1483. // Check for component-specific installation.
  1484. const char* cmake_install_component =
  1485. this->Makefile->GetDefinition("CMAKE_INSTALL_COMPONENT");
  1486. if(cmake_install_component && *cmake_install_component)
  1487. {
  1488. // This install rule applies only if it is associated with the
  1489. // current component.
  1490. if(components.find(cmake_install_component) == components.end())
  1491. {
  1492. return true;
  1493. }
  1494. }
  1495. // Check for configuration-specific installation.
  1496. if(!configurations.empty())
  1497. {
  1498. std::string cmake_install_configuration = cmSystemTools::UpperCase(
  1499. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_CONFIG_NAME"));
  1500. if(cmake_install_configuration.empty())
  1501. {
  1502. // No configuration specified for installation but this install
  1503. // rule is configuration-specific. Skip it.
  1504. return true;
  1505. }
  1506. else if(configurations.find(cmake_install_configuration) ==
  1507. configurations.end())
  1508. {
  1509. // This rule is specific to a configuration not being installed.
  1510. return true;
  1511. }
  1512. }
  1513. // Check whether files should be copied always or only if they have
  1514. // changed.
  1515. bool copy_always =
  1516. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  1517. // Handle each file listed.
  1518. for (std::vector<std::string>::size_type i = 0; i < files.size(); i ++ )
  1519. {
  1520. // Split the input file into its directory and name components.
  1521. std::vector<std::string> fromPathComponents;
  1522. cmSystemTools::SplitPath(files[i].c_str(), fromPathComponents);
  1523. std::string fromName = *(fromPathComponents.end()-1);
  1524. std::string fromDir = cmSystemTools::JoinPath(fromPathComponents.begin(),
  1525. fromPathComponents.end()-1);
  1526. // Compute the full path to the destination file.
  1527. std::string toFile = destination;
  1528. std::string const& toName = rename.empty()? fromName : rename;
  1529. if(!toName.empty())
  1530. {
  1531. toFile += "/";
  1532. toFile += toName;
  1533. }
  1534. // Handle type-specific installation details.
  1535. switch(itype)
  1536. {
  1537. case cmTarget::MODULE_LIBRARY:
  1538. case cmTarget::STATIC_LIBRARY:
  1539. case cmTarget::SHARED_LIBRARY:
  1540. {
  1541. // Handle shared library versioning
  1542. const char* lib_version = 0;
  1543. const char* lib_soversion = 0;
  1544. if ( properties.find("VERSION") != properties.end() )
  1545. {
  1546. lib_version = properties["VERSION"];
  1547. }
  1548. if ( properties.find("SOVERSION") != properties.end() )
  1549. {
  1550. lib_soversion = properties["SOVERSION"];
  1551. }
  1552. if ( !lib_version && lib_soversion )
  1553. {
  1554. lib_version = lib_soversion;
  1555. }
  1556. if ( !lib_soversion && lib_version )
  1557. {
  1558. lib_soversion = lib_version;
  1559. }
  1560. if ( lib_version && lib_soversion )
  1561. {
  1562. std::string libname = toFile;
  1563. std::string soname = toFile;
  1564. std::string soname_nopath = fromName;
  1565. this->ComputeVersionedName(soname, lib_soversion);
  1566. this->ComputeVersionedName(soname_nopath, lib_soversion);
  1567. this->ComputeVersionedName(fromName, lib_version);
  1568. this->ComputeVersionedName(toFile, lib_version);
  1569. cmSystemTools::RemoveFile(soname.c_str());
  1570. cmSystemTools::RemoveFile(libname.c_str());
  1571. if (!cmSystemTools::CreateSymlink(soname_nopath.c_str(),
  1572. libname.c_str()) )
  1573. {
  1574. std::string errstring = "error when creating symlink from: "
  1575. + libname + " to " + soname_nopath;
  1576. this->SetError(errstring.c_str());
  1577. return false;
  1578. }
  1579. installer.ManifestAppend(libname);
  1580. if ( toFile != soname )
  1581. {
  1582. if ( !cmSystemTools::CreateSymlink(fromName.c_str(),
  1583. soname.c_str()) )
  1584. {
  1585. std::string errstring = "error when creating symlink from: "
  1586. + soname + " to " + fromName;
  1587. this->SetError(errstring.c_str());
  1588. return false;
  1589. }
  1590. installer.ManifestAppend(soname);
  1591. }
  1592. }
  1593. }
  1594. break;
  1595. case cmTarget::EXECUTABLE:
  1596. {
  1597. // Handle executable versioning
  1598. const char* exe_version = 0;
  1599. if ( properties.find("VERSION") != properties.end() )
  1600. {
  1601. exe_version = properties["VERSION"];
  1602. }
  1603. if ( exe_version )
  1604. {
  1605. std::string exename = toFile;
  1606. std::string exename_nopath = fromName;
  1607. exename_nopath += "-";
  1608. exename_nopath += exe_version;
  1609. fromName += "-";
  1610. fromName += exe_version;
  1611. toFile += "-";
  1612. toFile += exe_version;
  1613. cmSystemTools::RemoveFile(exename.c_str());
  1614. if (!cmSystemTools::CreateSymlink(exename_nopath.c_str(),
  1615. exename.c_str()) )
  1616. {
  1617. std::string errstring = "error when creating symlink from: "
  1618. + exename + " to " + exename_nopath;
  1619. this->SetError(errstring.c_str());
  1620. return false;
  1621. }
  1622. installer.ManifestAppend(exename);
  1623. }
  1624. }
  1625. break;
  1626. }
  1627. // Construct the full path to the source file. The file name may
  1628. // have been changed above.
  1629. std::string fromFile = fromDir;
  1630. if(!fromName.empty())
  1631. {
  1632. fromFile += "/";
  1633. fromFile += fromName;
  1634. }
  1635. std::string message;
  1636. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  1637. {
  1638. if(itype == cmTarget::INSTALL_DIRECTORY &&
  1639. (fromFile.empty() ||
  1640. cmSystemTools::FileIsDirectory(fromFile.c_str())))
  1641. {
  1642. // Try installing this directory.
  1643. if(!installer.InstallDirectory(fromFile.c_str(), toFile.c_str(),
  1644. copy_always))
  1645. {
  1646. return false;
  1647. }
  1648. }
  1649. else if(cmSystemTools::FileExists(fromFile.c_str()))
  1650. {
  1651. // Install this file.
  1652. if(!installer.InstallFile(fromFile.c_str(), toFile.c_str(),
  1653. copy_always))
  1654. {
  1655. return false;
  1656. }
  1657. }
  1658. else if(!optional)
  1659. {
  1660. // The input file does not exist and installation is not optional.
  1661. cmOStringStream e;
  1662. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  1663. this->SetError(e.str().c_str());
  1664. return false;
  1665. }
  1666. }
  1667. }
  1668. return true;
  1669. }
  1670. //----------------------------------------------------------------------------
  1671. void cmFileCommand::ComputeVersionedName(std::string& name,
  1672. const char* version)
  1673. {
  1674. #if defined(__APPLE__)
  1675. std::string ext;
  1676. cmsys_stl::string::size_type dot_pos = name.rfind(".");
  1677. if(dot_pos != name.npos)
  1678. {
  1679. ext = name.substr(dot_pos, name.npos);
  1680. name = name.substr(0, dot_pos);
  1681. }
  1682. #endif
  1683. name += ".";
  1684. name += version;
  1685. #if defined(__APPLE__)
  1686. name += ext;
  1687. #endif
  1688. }
  1689. //----------------------------------------------------------------------------
  1690. bool cmFileCommand::HandleRelativePathCommand(
  1691. std::vector<std::string> const& args)
  1692. {
  1693. if(args.size() != 4 )
  1694. {
  1695. this->SetError("called with incorrect number of arguments");
  1696. return false;
  1697. }
  1698. const std::string& outVar = args[1];
  1699. const std::string& directoryName = args[2];
  1700. const std::string& fileName = args[3];
  1701. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  1702. {
  1703. std::string errstring =
  1704. "RelativePath must be passed a full path to the directory: "
  1705. + directoryName;
  1706. this->SetError(errstring.c_str());
  1707. return false;
  1708. }
  1709. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  1710. {
  1711. std::string errstring =
  1712. "RelativePath must be passed a full path to the file: "
  1713. + fileName;
  1714. this->SetError(errstring.c_str());
  1715. return false;
  1716. }
  1717. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  1718. fileName.c_str());
  1719. this->Makefile->AddDefinition(outVar.c_str(),
  1720. res.c_str());
  1721. return true;
  1722. }
  1723. //----------------------------------------------------------------------------
  1724. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  1725. bool recurse)
  1726. {
  1727. std::string message;
  1728. std::vector<std::string>::const_iterator i = args.begin();
  1729. i++; // Get rid of subcommand
  1730. for(;i != args.end(); ++i)
  1731. {
  1732. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  1733. {
  1734. cmSystemTools::RemoveADirectory(i->c_str());
  1735. }
  1736. else
  1737. {
  1738. cmSystemTools::RemoveFile(i->c_str());
  1739. }
  1740. }
  1741. return true;
  1742. }
  1743. //----------------------------------------------------------------------------
  1744. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  1745. const& args,
  1746. bool nativePath)
  1747. {
  1748. std::vector<std::string>::const_iterator i = args.begin();
  1749. if(args.size() != 3)
  1750. {
  1751. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  1752. "only three arguments.");
  1753. return false;
  1754. }
  1755. i++; // Get rid of subcommand
  1756. #if defined(_WIN32) && !defined(__CYGWIN__)
  1757. char pathSep = ';';
  1758. #else
  1759. char pathSep = ':';
  1760. #endif
  1761. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  1762. pathSep);
  1763. i++;
  1764. const char* var = i->c_str();
  1765. std::string value;
  1766. for(std::vector<cmsys::String>::iterator j = path.begin();
  1767. j != path.end(); ++j)
  1768. {
  1769. if(j != path.begin())
  1770. {
  1771. value += ";";
  1772. }
  1773. if(!nativePath)
  1774. {
  1775. cmSystemTools::ConvertToUnixSlashes(*j);
  1776. }
  1777. else
  1778. {
  1779. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  1780. // remove double quotes in the path
  1781. cmsys::String& s = *j;
  1782. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  1783. {
  1784. s = s.substr(1,s.size()-2);
  1785. }
  1786. }
  1787. value += *j;
  1788. }
  1789. this->Makefile->AddDefinition(var, value.c_str());
  1790. return true;
  1791. }