cmFileCommand.cxx 56 KB

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