cmFileCommand.cxx 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441
  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 "cmFileTimeComparison.h"
  17. #if defined(CMAKE_BUILD_WITH_CMAKE)
  18. #include "cm_curl.h"
  19. #endif
  20. #undef GetCurrentDirectory
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <cmsys/Directory.hxx>
  24. #include <cmsys/Glob.hxx>
  25. #include <cmsys/RegularExpression.hxx>
  26. // Table of permissions flags.
  27. #if defined(_WIN32) && !defined(__CYGWIN__)
  28. static mode_t mode_owner_read = S_IREAD;
  29. static mode_t mode_owner_write = S_IWRITE;
  30. static mode_t mode_owner_execute = S_IEXEC;
  31. static mode_t mode_group_read = 0;
  32. static mode_t mode_group_write = 0;
  33. static mode_t mode_group_execute = 0;
  34. static mode_t mode_world_read = 0;
  35. static mode_t mode_world_write = 0;
  36. static mode_t mode_world_execute = 0;
  37. static mode_t mode_setuid = 0;
  38. static mode_t mode_setgid = 0;
  39. #else
  40. static mode_t mode_owner_read = S_IRUSR;
  41. static mode_t mode_owner_write = S_IWUSR;
  42. static mode_t mode_owner_execute = S_IXUSR;
  43. static mode_t mode_group_read = S_IRGRP;
  44. static mode_t mode_group_write = S_IWGRP;
  45. static mode_t mode_group_execute = S_IXGRP;
  46. static mode_t mode_world_read = S_IROTH;
  47. static mode_t mode_world_write = S_IWOTH;
  48. static mode_t mode_world_execute = S_IXOTH;
  49. static mode_t mode_setuid = S_ISUID;
  50. static mode_t mode_setgid = S_ISGID;
  51. #endif
  52. // cmLibraryCommand
  53. bool cmFileCommand
  54. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  55. {
  56. if(args.size() < 2 )
  57. {
  58. this->SetError("must be called with at least two arguments.");
  59. return false;
  60. }
  61. std::string subCommand = args[0];
  62. if ( subCommand == "WRITE" )
  63. {
  64. return this->HandleWriteCommand(args, false);
  65. }
  66. else if ( subCommand == "APPEND" )
  67. {
  68. return this->HandleWriteCommand(args, true);
  69. }
  70. else if ( subCommand == "DOWNLOAD" )
  71. {
  72. return this->HandleDownloadCommand(args);
  73. }
  74. else if ( subCommand == "READ" )
  75. {
  76. return this->HandleReadCommand(args);
  77. }
  78. else if ( subCommand == "STRINGS" )
  79. {
  80. return this->HandleStringsCommand(args);
  81. }
  82. else if ( subCommand == "GLOB" )
  83. {
  84. return this->HandleGlobCommand(args, false);
  85. }
  86. else if ( subCommand == "GLOB_RECURSE" )
  87. {
  88. return this->HandleGlobCommand(args, true);
  89. }
  90. else if ( subCommand == "MAKE_DIRECTORY" )
  91. {
  92. return this->HandleMakeDirectoryCommand(args);
  93. }
  94. else if ( subCommand == "RENAME" )
  95. {
  96. return this->HandleRename(args);
  97. }
  98. else if ( subCommand == "REMOVE" )
  99. {
  100. return this->HandleRemove(args, false);
  101. }
  102. else if ( subCommand == "REMOVE_RECURSE" )
  103. {
  104. return this->HandleRemove(args, true);
  105. }
  106. else if ( subCommand == "INSTALL" )
  107. {
  108. return this->HandleInstallCommand(args);
  109. }
  110. else if ( subCommand == "DIFFERENT" )
  111. {
  112. return this->HandleDifferentCommand(args);
  113. }
  114. else if ( subCommand == "RPATH_CHANGE" || subCommand == "CHRPATH" )
  115. {
  116. return this->HandleRPathChangeCommand(args);
  117. }
  118. else if ( subCommand == "RPATH_CHECK" )
  119. {
  120. return this->HandleRPathCheckCommand(args);
  121. }
  122. else if ( subCommand == "RPATH_REMOVE" )
  123. {
  124. return this->HandleRPathRemoveCommand(args);
  125. }
  126. else if ( subCommand == "RELATIVE_PATH" )
  127. {
  128. return this->HandleRelativePathCommand(args);
  129. }
  130. else if ( subCommand == "TO_CMAKE_PATH" )
  131. {
  132. return this->HandleCMakePathCommand(args, false);
  133. }
  134. else if ( subCommand == "TO_NATIVE_PATH" )
  135. {
  136. return this->HandleCMakePathCommand(args, true);
  137. }
  138. std::string e = "does not recognize sub-command "+subCommand;
  139. this->SetError(e.c_str());
  140. return false;
  141. }
  142. //----------------------------------------------------------------------------
  143. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  144. bool append)
  145. {
  146. std::string message;
  147. std::vector<std::string>::const_iterator i = args.begin();
  148. i++; // Get rid of subcommand
  149. std::string fileName = *i;
  150. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  151. {
  152. fileName = this->Makefile->GetCurrentDirectory();
  153. fileName += "/" + *i;
  154. }
  155. i++;
  156. for(;i != args.end(); ++i)
  157. {
  158. message += *i;
  159. }
  160. if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
  161. {
  162. std::string e
  163. = "attempted to write a file: " + fileName +
  164. " into a source directory.";
  165. this->SetError(e.c_str());
  166. cmSystemTools::SetFatalErrorOccured();
  167. return false;
  168. }
  169. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  170. cmSystemTools::MakeDirectory(dir.c_str());
  171. mode_t mode =
  172. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  173. S_IREAD | S_IWRITE
  174. #elif defined( __BORLANDC__ )
  175. S_IRUSR | S_IWUSR
  176. #else
  177. S_IRUSR | S_IWUSR |
  178. S_IRGRP |
  179. S_IROTH
  180. #endif
  181. ;
  182. // Set permissions to writable
  183. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  184. {
  185. cmSystemTools::SetPermissions(fileName.c_str(),
  186. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  187. S_IREAD | S_IWRITE
  188. #else
  189. S_IRUSR | S_IWUSR
  190. #endif
  191. );
  192. }
  193. // If GetPermissions fails, pretend like it is ok. File open will fail if
  194. // the file is not writable
  195. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  196. if ( !file )
  197. {
  198. std::string error = "Internal CMake error when trying to open file: ";
  199. error += fileName.c_str();
  200. error += " for writing.";
  201. this->SetError(error.c_str());
  202. return false;
  203. }
  204. file << message;
  205. file.close();
  206. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  207. return true;
  208. }
  209. //----------------------------------------------------------------------------
  210. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  211. {
  212. if ( args.size() < 3 )
  213. {
  214. this->SetError("READ must be called with at least two additional "
  215. "arguments");
  216. return false;
  217. }
  218. cmCommandArgumentsHelper argHelper;
  219. cmCommandArgumentGroup group;
  220. cmCAString readArg (&argHelper, "READ");
  221. cmCAString fileNameArg (&argHelper, 0);
  222. cmCAString resultArg (&argHelper, 0);
  223. cmCAString offsetArg (&argHelper, "OFFSET", &group);
  224. cmCAString limitArg (&argHelper, "LIMIT", &group);
  225. cmCAEnabler hexOutputArg (&argHelper, "HEX", &group);
  226. readArg.Follows(0);
  227. fileNameArg.Follows(&readArg);
  228. resultArg.Follows(&fileNameArg);
  229. group.Follows(&resultArg);
  230. argHelper.Parse(&args, 0);
  231. std::string fileName = fileNameArg.GetString();
  232. if ( !cmsys::SystemTools::FileIsFullPath(fileName.c_str()) )
  233. {
  234. fileName = this->Makefile->GetCurrentDirectory();
  235. fileName += "/" + fileNameArg.GetString();
  236. }
  237. std::string variable = resultArg.GetString();
  238. // Open the specified file.
  239. #if defined(_WIN32) || defined(__CYGWIN__)
  240. std::ifstream file(fileName.c_str(), std::ios::in |
  241. (hexOutputArg.IsEnabled() ? std::ios::binary : std::ios::in));
  242. #else
  243. std::ifstream file(fileName.c_str(), std::ios::in);
  244. #endif
  245. if ( !file )
  246. {
  247. std::string error = "Internal CMake error when trying to open file: ";
  248. error += fileName.c_str();
  249. error += " for reading.";
  250. this->SetError(error.c_str());
  251. return false;
  252. }
  253. // is there a limit?
  254. long sizeLimit = -1;
  255. if (limitArg.GetString().size() > 0)
  256. {
  257. sizeLimit = atoi(limitArg.GetCString());
  258. }
  259. // is there an offset?
  260. long offset = 0;
  261. if (offsetArg.GetString().size() > 0)
  262. {
  263. offset = atoi(offsetArg.GetCString());
  264. }
  265. file.seekg(offset);
  266. std::string output;
  267. if (hexOutputArg.IsEnabled())
  268. {
  269. // Convert part of the file into hex code
  270. int c;
  271. while((sizeLimit != 0) && (c = file.get(), file))
  272. {
  273. char hex[4];
  274. sprintf(hex, "%x", c&0xff);
  275. output += hex;
  276. if (sizeLimit > 0)
  277. {
  278. sizeLimit--;
  279. }
  280. }
  281. }
  282. else
  283. {
  284. std::string line;
  285. bool has_newline = false;
  286. while (sizeLimit != 0 &&
  287. cmSystemTools::GetLineFromStream(file, line, &has_newline,
  288. sizeLimit) )
  289. {
  290. if (sizeLimit > 0)
  291. {
  292. sizeLimit = sizeLimit - static_cast<long>(line.size());
  293. if (has_newline)
  294. {
  295. sizeLimit--;
  296. }
  297. if (sizeLimit < 0)
  298. {
  299. sizeLimit = 0;
  300. }
  301. }
  302. output += line;
  303. if ( has_newline )
  304. {
  305. output += "\n";
  306. }
  307. }
  308. }
  309. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  310. return true;
  311. }
  312. //----------------------------------------------------------------------------
  313. bool cmFileCommand::HandleStringsCommand(std::vector<std::string> const& args)
  314. {
  315. if(args.size() < 3)
  316. {
  317. this->SetError("STRINGS requires a file name and output variable");
  318. return false;
  319. }
  320. // Get the file to read.
  321. std::string fileName = args[1];
  322. if(!cmsys::SystemTools::FileIsFullPath(fileName.c_str()))
  323. {
  324. fileName = this->Makefile->GetCurrentDirectory();
  325. fileName += "/" + args[1];
  326. }
  327. // Get the variable in which to store the results.
  328. std::string outVar = args[2];
  329. // Parse the options.
  330. enum { arg_none,
  331. arg_limit_input,
  332. arg_limit_output,
  333. arg_limit_count,
  334. arg_length_minimum,
  335. arg_length_maximum,
  336. arg__maximum,
  337. arg_regex };
  338. unsigned int minlen = 0;
  339. unsigned int maxlen = 0;
  340. int limit_input = -1;
  341. int limit_output = -1;
  342. unsigned int limit_count = 0;
  343. cmsys::RegularExpression regex;
  344. bool have_regex = false;
  345. bool newline_consume = false;
  346. bool hex_conversion_enabled = true;
  347. int arg_mode = arg_none;
  348. for(unsigned int i=3; i < args.size(); ++i)
  349. {
  350. if(args[i] == "LIMIT_INPUT")
  351. {
  352. arg_mode = arg_limit_input;
  353. }
  354. else if(args[i] == "LIMIT_OUTPUT")
  355. {
  356. arg_mode = arg_limit_output;
  357. }
  358. else if(args[i] == "LIMIT_COUNT")
  359. {
  360. arg_mode = arg_limit_count;
  361. }
  362. else if(args[i] == "LENGTH_MINIMUM")
  363. {
  364. arg_mode = arg_length_minimum;
  365. }
  366. else if(args[i] == "LENGTH_MAXIMUM")
  367. {
  368. arg_mode = arg_length_maximum;
  369. }
  370. else if(args[i] == "REGEX")
  371. {
  372. arg_mode = arg_regex;
  373. }
  374. else if(args[i] == "NEWLINE_CONSUME")
  375. {
  376. newline_consume = true;
  377. arg_mode = arg_none;
  378. }
  379. else if(args[i] == "NO_HEX_CONVERSION")
  380. {
  381. hex_conversion_enabled = false;
  382. arg_mode = arg_none;
  383. }
  384. else if(arg_mode == arg_limit_input)
  385. {
  386. if(sscanf(args[i].c_str(), "%d", &limit_input) != 1 ||
  387. limit_input < 0)
  388. {
  389. cmOStringStream e;
  390. e << "STRINGS option LIMIT_INPUT value \""
  391. << args[i] << "\" is not an unsigned integer.";
  392. this->SetError(e.str().c_str());
  393. return false;
  394. }
  395. arg_mode = arg_none;
  396. }
  397. else if(arg_mode == arg_limit_output)
  398. {
  399. if(sscanf(args[i].c_str(), "%d", &limit_output) != 1 ||
  400. limit_output < 0)
  401. {
  402. cmOStringStream e;
  403. e << "STRINGS option LIMIT_OUTPUT value \""
  404. << args[i] << "\" is not an unsigned integer.";
  405. this->SetError(e.str().c_str());
  406. return false;
  407. }
  408. arg_mode = arg_none;
  409. }
  410. else if(arg_mode == arg_limit_count)
  411. {
  412. int count;
  413. if(sscanf(args[i].c_str(), "%d", &count) != 1 || count < 0)
  414. {
  415. cmOStringStream e;
  416. e << "STRINGS option LIMIT_COUNT value \""
  417. << args[i] << "\" is not an unsigned integer.";
  418. this->SetError(e.str().c_str());
  419. return false;
  420. }
  421. limit_count = count;
  422. arg_mode = arg_none;
  423. }
  424. else if(arg_mode == arg_length_minimum)
  425. {
  426. int len;
  427. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  428. {
  429. cmOStringStream e;
  430. e << "STRINGS option LENGTH_MINIMUM value \""
  431. << args[i] << "\" is not an unsigned integer.";
  432. this->SetError(e.str().c_str());
  433. return false;
  434. }
  435. minlen = len;
  436. arg_mode = arg_none;
  437. }
  438. else if(arg_mode == arg_length_maximum)
  439. {
  440. int len;
  441. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  442. {
  443. cmOStringStream e;
  444. e << "STRINGS option LENGTH_MAXIMUM value \""
  445. << args[i] << "\" is not an unsigned integer.";
  446. this->SetError(e.str().c_str());
  447. return false;
  448. }
  449. maxlen = len;
  450. arg_mode = arg_none;
  451. }
  452. else if(arg_mode == arg_regex)
  453. {
  454. if(!regex.compile(args[i].c_str()))
  455. {
  456. cmOStringStream e;
  457. e << "STRINGS option REGEX value \""
  458. << args[i] << "\" could not be compiled.";
  459. this->SetError(e.str().c_str());
  460. return false;
  461. }
  462. have_regex = true;
  463. arg_mode = arg_none;
  464. }
  465. else
  466. {
  467. cmOStringStream e;
  468. e << "STRINGS given unknown argument \""
  469. << args[i] << "\"";
  470. this->SetError(e.str().c_str());
  471. return false;
  472. }
  473. }
  474. if (hex_conversion_enabled)
  475. {
  476. // TODO: should work without temp file, but just on a memory buffer
  477. std::string binaryFileName = this->Makefile->GetCurrentOutputDirectory();
  478. binaryFileName += cmake::GetCMakeFilesDirectory();
  479. binaryFileName += "/FileCommandStringsBinaryFile";
  480. if(cmHexFileConverter::TryConvert(fileName.c_str(),binaryFileName.c_str()))
  481. {
  482. fileName = binaryFileName;
  483. }
  484. }
  485. // Open the specified file.
  486. #if defined(_WIN32) || defined(__CYGWIN__)
  487. std::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
  488. #else
  489. std::ifstream fin(fileName.c_str(), std::ios::in);
  490. #endif
  491. if(!fin)
  492. {
  493. cmOStringStream e;
  494. e << "STRINGS file \"" << fileName << "\" cannot be read.";
  495. this->SetError(e.str().c_str());
  496. return false;
  497. }
  498. // Parse strings out of the file.
  499. int output_size = 0;
  500. std::vector<std::string> strings;
  501. std::string s;
  502. int c;
  503. while((!limit_count || strings.size() < limit_count) &&
  504. (limit_input < 0 || static_cast<int>(fin.tellg()) < limit_input) &&
  505. (c = fin.get(), fin))
  506. {
  507. if(c == '\0')
  508. {
  509. // A terminating null character has been found. Check if the
  510. // current string matches the requirements. Since it was
  511. // terminated by a null character, we require that the length be
  512. // at least one no matter what the user specified.
  513. if(s.length() >= minlen && s.length() >= 1 &&
  514. (!have_regex || regex.find(s.c_str())))
  515. {
  516. output_size += static_cast<int>(s.size()) + 1;
  517. if(limit_output >= 0 && output_size >= limit_output)
  518. {
  519. s = "";
  520. break;
  521. }
  522. strings.push_back(s);
  523. }
  524. // Reset the string to empty.
  525. s = "";
  526. }
  527. else if(c == '\n' && !newline_consume)
  528. {
  529. // The current line has been terminated. Check if the current
  530. // string matches the requirements. The length may now be as
  531. // low as zero since blank lines are allowed.
  532. if(s.length() >= minlen &&
  533. (!have_regex || regex.find(s.c_str())))
  534. {
  535. output_size += static_cast<int>(s.size()) + 1;
  536. if(limit_output >= 0 && output_size >= limit_output)
  537. {
  538. s = "";
  539. break;
  540. }
  541. strings.push_back(s);
  542. }
  543. // Reset the string to empty.
  544. s = "";
  545. }
  546. else if(c == '\r')
  547. {
  548. // Ignore CR character to make output always have UNIX newlines.
  549. }
  550. else if(c >= 0x20 && c < 0x7F || c == '\t' || c == '\f' ||
  551. (c == '\n' && newline_consume))
  552. {
  553. // This is an ASCII character that may be part of a string.
  554. s += c;
  555. }
  556. else
  557. {
  558. // This is a non-string character. Reset the string to emtpy.
  559. s = "";
  560. }
  561. // Terminate a string if the maximum length is reached.
  562. if(maxlen > 0 && s.size() == maxlen)
  563. {
  564. if(s.length() >= minlen &&
  565. (!have_regex || regex.find(s.c_str())))
  566. {
  567. output_size += static_cast<int>(s.size()) + 1;
  568. if(limit_output >= 0 && output_size >= limit_output)
  569. {
  570. s = "";
  571. break;
  572. }
  573. strings.push_back(s);
  574. }
  575. s = "";
  576. }
  577. }
  578. // If there is a non-empty current string we have hit the end of the
  579. // input file or the input size limit. Check if the current string
  580. // matches the requirements.
  581. if((!limit_count || strings.size() < limit_count) &&
  582. !s.empty() && s.length() >= minlen &&
  583. (!have_regex || regex.find(s.c_str())))
  584. {
  585. output_size += static_cast<int>(s.size()) + 1;
  586. if(limit_output < 0 || output_size < limit_output)
  587. {
  588. strings.push_back(s);
  589. }
  590. }
  591. // Encode the result in a CMake list.
  592. const char* sep = "";
  593. std::string output;
  594. for(std::vector<std::string>::const_iterator si = strings.begin();
  595. si != strings.end(); ++si)
  596. {
  597. // Separate the strings in the output to make it a list.
  598. output += sep;
  599. sep = ";";
  600. // Store the string in the output, but escape semicolons to
  601. // make sure it is a list.
  602. std::string const& sr = *si;
  603. for(unsigned int i=0; i < sr.size(); ++i)
  604. {
  605. if(sr[i] == ';')
  606. {
  607. output += '\\';
  608. }
  609. output += sr[i];
  610. }
  611. }
  612. // Save the output in a makefile variable.
  613. this->Makefile->AddDefinition(outVar.c_str(), output.c_str());
  614. return true;
  615. }
  616. //----------------------------------------------------------------------------
  617. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  618. bool recurse)
  619. {
  620. if ( args.size() < 2 )
  621. {
  622. this->SetError("GLOB requires at least a variable name");
  623. return false;
  624. }
  625. std::vector<std::string>::const_iterator i = args.begin();
  626. i++; // Get rid of subcommand
  627. std::string variable = *i;
  628. i++;
  629. cmsys::Glob g;
  630. g.SetRecurse(recurse);
  631. bool explicitFollowSymlinks = false;
  632. cmPolicies::PolicyStatus status =
  633. this->Makefile->GetPolicyStatus(cmPolicies::CMP0009);
  634. if(recurse)
  635. {
  636. switch(status)
  637. {
  638. case cmPolicies::NEW:
  639. g.RecurseThroughSymlinksOff();
  640. break;
  641. case cmPolicies::OLD:
  642. case cmPolicies::WARN:
  643. case cmPolicies::REQUIRED_IF_USED:
  644. case cmPolicies::REQUIRED_ALWAYS:
  645. g.RecurseThroughSymlinksOn();
  646. break;
  647. }
  648. }
  649. std::string output = "";
  650. bool first = true;
  651. for ( ; i != args.end(); ++i )
  652. {
  653. if ( recurse && (*i == "FOLLOW_SYMLINKS") )
  654. {
  655. explicitFollowSymlinks = true;
  656. g.RecurseThroughSymlinksOn();
  657. ++i;
  658. if ( i == args.end() )
  659. {
  660. this->SetError(
  661. "GLOB_RECURSE requires a glob expression after FOLLOW_SYMLINKS");
  662. return false;
  663. }
  664. }
  665. if ( *i == "RELATIVE" )
  666. {
  667. ++i; // skip RELATIVE
  668. if ( i == args.end() )
  669. {
  670. this->SetError("GLOB requires a directory after the RELATIVE tag");
  671. return false;
  672. }
  673. g.SetRelative(i->c_str());
  674. ++i;
  675. if(i == args.end())
  676. {
  677. this->SetError("GLOB requires a glob expression after the directory");
  678. return false;
  679. }
  680. }
  681. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  682. {
  683. std::string expr = this->Makefile->GetCurrentDirectory();
  684. // Handle script mode
  685. if ( expr.size() > 0 )
  686. {
  687. expr += "/" + *i;
  688. g.FindFiles(expr);
  689. }
  690. else
  691. {
  692. g.FindFiles(*i);
  693. }
  694. }
  695. else
  696. {
  697. g.FindFiles(*i);
  698. }
  699. std::vector<std::string>::size_type cc;
  700. std::vector<std::string>& files = g.GetFiles();
  701. for ( cc = 0; cc < files.size(); cc ++ )
  702. {
  703. if ( !first )
  704. {
  705. output += ";";
  706. }
  707. output += files[cc];
  708. first = false;
  709. }
  710. }
  711. if(recurse && !explicitFollowSymlinks)
  712. {
  713. switch (status)
  714. {
  715. case cmPolicies::NEW:
  716. // Correct behavior, yay!
  717. break;
  718. case cmPolicies::OLD:
  719. // Probably not really the expected behavior, but the author explicitly
  720. // asked for the old behavior... no warning.
  721. case cmPolicies::WARN:
  722. // Possibly unexpected old behavior *and* we actually traversed
  723. // symlinks without being explicitly asked to: warn the author.
  724. if(g.GetFollowedSymlinkCount() != 0)
  725. {
  726. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING,
  727. this->Makefile->GetPolicies()->
  728. GetPolicyWarning(cmPolicies::CMP0009));
  729. }
  730. break;
  731. case cmPolicies::REQUIRED_IF_USED:
  732. case cmPolicies::REQUIRED_ALWAYS:
  733. this->SetError("policy CMP0009 error");
  734. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  735. this->Makefile->GetPolicies()->
  736. GetRequiredPolicyError(cmPolicies::CMP0009));
  737. return false;
  738. }
  739. }
  740. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  741. return true;
  742. }
  743. //----------------------------------------------------------------------------
  744. bool cmFileCommand::HandleMakeDirectoryCommand(
  745. std::vector<std::string> const& args)
  746. {
  747. if(args.size() < 2 )
  748. {
  749. this->SetError("called with incorrect number of arguments");
  750. return false;
  751. }
  752. std::vector<std::string>::const_iterator i = args.begin();
  753. i++; // Get rid of subcommand
  754. std::string expr;
  755. for ( ; i != args.end(); ++i )
  756. {
  757. const std::string* cdir = &(*i);
  758. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  759. {
  760. expr = this->Makefile->GetCurrentDirectory();
  761. expr += "/" + *i;
  762. cdir = &expr;
  763. }
  764. if ( !this->Makefile->CanIWriteThisFile(cdir->c_str()) )
  765. {
  766. std::string e = "attempted to create a directory: " + *cdir
  767. + " into a source directory.";
  768. this->SetError(e.c_str());
  769. cmSystemTools::SetFatalErrorOccured();
  770. return false;
  771. }
  772. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  773. {
  774. std::string error = "problem creating directory: " + *cdir;
  775. this->SetError(error.c_str());
  776. return false;
  777. }
  778. }
  779. return true;
  780. }
  781. //----------------------------------------------------------------------------
  782. bool
  783. cmFileCommand::HandleDifferentCommand(std::vector<std::string> const& args)
  784. {
  785. /*
  786. FILE(DIFFERENT <variable> FILES <lhs> <rhs>)
  787. */
  788. // Evaluate arguments.
  789. const char* file_lhs = 0;
  790. const char* file_rhs = 0;
  791. const char* var = 0;
  792. enum Doing { DoingNone, DoingVar, DoingFileLHS, DoingFileRHS };
  793. Doing doing = DoingVar;
  794. for(unsigned int i=1; i < args.size(); ++i)
  795. {
  796. if(args[i] == "FILES")
  797. {
  798. doing = DoingFileLHS;
  799. }
  800. else if(doing == DoingVar)
  801. {
  802. var = args[i].c_str();
  803. doing = DoingNone;
  804. }
  805. else if(doing == DoingFileLHS)
  806. {
  807. file_lhs = args[i].c_str();
  808. doing = DoingFileRHS;
  809. }
  810. else if(doing == DoingFileRHS)
  811. {
  812. file_rhs = args[i].c_str();
  813. doing = DoingNone;
  814. }
  815. else
  816. {
  817. cmOStringStream e;
  818. e << "DIFFERENT given unknown argument " << args[i];
  819. this->SetError(e.str().c_str());
  820. return false;
  821. }
  822. }
  823. if(!var)
  824. {
  825. this->SetError("DIFFERENT not given result variable name.");
  826. return false;
  827. }
  828. if(!file_lhs || !file_rhs)
  829. {
  830. this->SetError("DIFFERENT not given FILES option with two file names.");
  831. return false;
  832. }
  833. // Compare the files.
  834. const char* result =
  835. cmSystemTools::FilesDiffer(file_lhs, file_rhs)? "1" : "0";
  836. this->Makefile->AddDefinition(var, result);
  837. return true;
  838. }
  839. //----------------------------------------------------------------------------
  840. // File installation helper class.
  841. struct cmFileInstaller
  842. {
  843. // Methods to actually install files.
  844. bool InstallFile(const char* fromFile, const char* toFile, bool always);
  845. bool InstallDirectory(const char* source, const char* destination,
  846. bool always);
  847. // All instances need the file command and makefile using them.
  848. cmFileInstaller(cmFileCommand* fc, cmMakefile* mf):
  849. FileCommand(fc), Makefile(mf), DestDirLength(0), MatchlessFiles(true)
  850. {
  851. // Get the current manifest.
  852. this->Manifest =
  853. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  854. }
  855. ~cmFileInstaller()
  856. {
  857. // Save the updated install manifest.
  858. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  859. this->Manifest.c_str());
  860. }
  861. private:
  862. cmFileCommand* FileCommand;
  863. cmMakefile* Makefile;
  864. cmFileTimeComparison FileTimes;
  865. public:
  866. // The length of the destdir setting.
  867. int DestDirLength;
  868. // Whether to install a file not matching any expression.
  869. bool MatchlessFiles;
  870. // The current file manifest (semicolon separated list).
  871. std::string Manifest;
  872. // Permissions for files and directories installed by this object.
  873. mode_t FilePermissions;
  874. mode_t DirPermissions;
  875. // Properties set by pattern and regex match rules.
  876. struct MatchProperties
  877. {
  878. bool Exclude;
  879. mode_t Permissions;
  880. MatchProperties(): Exclude(false), Permissions(0) {}
  881. };
  882. struct MatchRule
  883. {
  884. cmsys::RegularExpression Regex;
  885. MatchProperties Properties;
  886. std::string RegexString;
  887. MatchRule(std::string const& regex):
  888. Regex(regex.c_str()), RegexString(regex) {}
  889. };
  890. std::vector<MatchRule> MatchRules;
  891. // Get the properties from rules matching this input file.
  892. MatchProperties CollectMatchProperties(const char* file,
  893. bool isDirectory)
  894. {
  895. // Match rules are case-insensitive on some platforms.
  896. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  897. std::string lower = cmSystemTools::LowerCase(file);
  898. file = lower.c_str();
  899. #endif
  900. // Collect properties from all matching rules.
  901. bool matched = false;
  902. MatchProperties result;
  903. for(std::vector<MatchRule>::iterator mr = this->MatchRules.begin();
  904. mr != this->MatchRules.end(); ++mr)
  905. {
  906. if(mr->Regex.find(file))
  907. {
  908. matched = true;
  909. result.Exclude |= mr->Properties.Exclude;
  910. result.Permissions |= mr->Properties.Permissions;
  911. }
  912. }
  913. if(!matched && !this->MatchlessFiles && !isDirectory)
  914. {
  915. result.Exclude = true;
  916. }
  917. return result;
  918. }
  919. // Append a file to the installation manifest.
  920. void ManifestAppend(std::string const& file)
  921. {
  922. this->Manifest += ";";
  923. this->Manifest += file.substr(this->DestDirLength);
  924. }
  925. // Translate an argument to a permissions bit.
  926. bool CheckPermissions(std::string const& arg, mode_t& permissions)
  927. {
  928. if(arg == "OWNER_READ") { permissions |= mode_owner_read; }
  929. else if(arg == "OWNER_WRITE") { permissions |= mode_owner_write; }
  930. else if(arg == "OWNER_EXECUTE") { permissions |= mode_owner_execute; }
  931. else if(arg == "GROUP_READ") { permissions |= mode_group_read; }
  932. else if(arg == "GROUP_WRITE") { permissions |= mode_group_write; }
  933. else if(arg == "GROUP_EXECUTE") { permissions |= mode_group_execute; }
  934. else if(arg == "WORLD_READ") { permissions |= mode_world_read; }
  935. else if(arg == "WORLD_WRITE") { permissions |= mode_world_write; }
  936. else if(arg == "WORLD_EXECUTE") { permissions |= mode_world_execute; }
  937. else if(arg == "SETUID") { permissions |= mode_setuid; }
  938. else if(arg == "SETGID") { permissions |= mode_setgid; }
  939. else
  940. {
  941. cmOStringStream e;
  942. e << "INSTALL given invalid permission \"" << arg << "\".";
  943. this->FileCommand->SetError(e.str().c_str());
  944. return false;
  945. }
  946. return true;
  947. }
  948. private:
  949. bool InstallSymlink(const char* fromFile, const char* toFile, bool always);
  950. };
  951. //----------------------------------------------------------------------------
  952. bool cmFileInstaller::InstallSymlink(const char* fromFile, const char* toFile,
  953. bool always)
  954. {
  955. // Read the original symlink.
  956. std::string symlinkTarget;
  957. if(!cmSystemTools::ReadSymlink(fromFile, symlinkTarget))
  958. {
  959. cmOStringStream e;
  960. e << "INSTALL cannot read symlink \"" << fromFile
  961. << "\" to duplicate at \"" << toFile << "\".";
  962. this->FileCommand->SetError(e.str().c_str());
  963. return false;
  964. }
  965. // Compare the symlink value to that at the destination if not
  966. // always installing.
  967. bool copy = true;
  968. if(!always)
  969. {
  970. std::string oldSymlinkTarget;
  971. if(cmSystemTools::ReadSymlink(toFile, oldSymlinkTarget))
  972. {
  973. if(symlinkTarget == oldSymlinkTarget)
  974. {
  975. copy = false;
  976. }
  977. }
  978. }
  979. // Inform the user about this file installation.
  980. std::string message = (copy? "Installing: " : "Up-to-date: ");
  981. message += toFile;
  982. this->Makefile->DisplayStatus(message.c_str(), -1);
  983. if(copy)
  984. {
  985. // Remove the destination file so we can always create the symlink.
  986. cmSystemTools::RemoveFile(toFile);
  987. // Create the symlink.
  988. if(!cmSystemTools::CreateSymlink(symlinkTarget.c_str(), toFile))
  989. {
  990. cmOStringStream e;
  991. e << "INSTALL cannot duplicate symlink \"" << fromFile
  992. << "\" at \"" << toFile << "\".";
  993. this->FileCommand->SetError(e.str().c_str());
  994. return false;
  995. }
  996. }
  997. // Add the file to the manifest.
  998. this->ManifestAppend(toFile);
  999. return true;
  1000. }
  1001. //----------------------------------------------------------------------------
  1002. bool cmFileInstaller::InstallFile(const char* fromFile, const char* toFile,
  1003. bool always)
  1004. {
  1005. // Collect any properties matching this file name.
  1006. MatchProperties match_properties =
  1007. this->CollectMatchProperties(fromFile, false);
  1008. // Skip the file if it is excluded.
  1009. if(match_properties.Exclude)
  1010. {
  1011. return true;
  1012. }
  1013. // Short-circuit for symbolic links.
  1014. if(cmSystemTools::FileIsSymlink(fromFile))
  1015. {
  1016. return this->InstallSymlink(fromFile, toFile, always);
  1017. }
  1018. // Determine whether we will copy the file.
  1019. bool copy = true;
  1020. if(!always)
  1021. {
  1022. // If both files exist with the same time do not copy.
  1023. if(!this->FileTimes.FileTimesDiffer(fromFile, toFile))
  1024. {
  1025. copy = false;
  1026. }
  1027. }
  1028. // Inform the user about this file installation.
  1029. std::string message = (copy? "Installing: " : "Up-to-date: ");
  1030. message += toFile;
  1031. this->Makefile->DisplayStatus(message.c_str(), -1);
  1032. // Copy the file.
  1033. if(copy && !cmSystemTools::CopyAFile(fromFile, toFile, true, false))
  1034. {
  1035. cmOStringStream e;
  1036. e << "INSTALL cannot copy file \"" << fromFile
  1037. << "\" to \"" << toFile << "\".";
  1038. this->FileCommand->SetError(e.str().c_str());
  1039. return false;
  1040. }
  1041. // Add the file to the manifest.
  1042. this->ManifestAppend(toFile);
  1043. // Set the file modification time of the destination file.
  1044. if(copy && !always)
  1045. {
  1046. if (!cmSystemTools::CopyFileTime(fromFile, toFile))
  1047. {
  1048. cmOStringStream e;
  1049. e << "Problem setting modification time on file \"" << toFile << "\"";
  1050. this->FileCommand->SetError(e.str().c_str());
  1051. return false;
  1052. }
  1053. }
  1054. // Set permissions of the destination file.
  1055. mode_t permissions = (match_properties.Permissions?
  1056. match_properties.Permissions : this->FilePermissions);
  1057. if(!permissions)
  1058. {
  1059. // No permissions were explicitly provided but the user requested
  1060. // that the source file permissions be used.
  1061. cmSystemTools::GetPermissions(fromFile, permissions);
  1062. }
  1063. if(permissions && !cmSystemTools::SetPermissions(toFile, permissions))
  1064. {
  1065. cmOStringStream e;
  1066. e << "Problem setting permissions on file \"" << toFile << "\"";
  1067. this->FileCommand->SetError(e.str().c_str());
  1068. return false;
  1069. }
  1070. return true;
  1071. }
  1072. //----------------------------------------------------------------------------
  1073. bool cmFileInstaller::InstallDirectory(const char* source,
  1074. const char* destination,
  1075. bool always)
  1076. {
  1077. // Collect any properties matching this directory name.
  1078. MatchProperties match_properties =
  1079. this->CollectMatchProperties(source, true);
  1080. // Skip the directory if it is excluded.
  1081. if(match_properties.Exclude)
  1082. {
  1083. return true;
  1084. }
  1085. // Short-circuit for symbolic links.
  1086. if(cmSystemTools::FileIsSymlink(source))
  1087. {
  1088. return this->InstallSymlink(source, destination, always);
  1089. }
  1090. // Inform the user about this directory installation.
  1091. std::string message = "Installing: ";
  1092. message += destination;
  1093. this->Makefile->DisplayStatus(message.c_str(), -1);
  1094. // Make sure the destination directory exists.
  1095. if(!cmSystemTools::MakeDirectory(destination))
  1096. {
  1097. return false;
  1098. }
  1099. // Compute the requested permissions for the destination directory.
  1100. mode_t permissions = (match_properties.Permissions?
  1101. match_properties.Permissions : this->DirPermissions);
  1102. if(!permissions)
  1103. {
  1104. // No permissions were explicitly provided but the user requested
  1105. // that the source directory permissions be used.
  1106. cmSystemTools::GetPermissions(source, permissions);
  1107. }
  1108. // Compute the set of permissions required on this directory to
  1109. // recursively install files and subdirectories safely.
  1110. mode_t required_permissions =
  1111. mode_owner_read | mode_owner_write | mode_owner_execute;
  1112. // If the required permissions are specified it is safe to set the
  1113. // final permissions now. Otherwise we must add the required
  1114. // permissions temporarily during file installation.
  1115. mode_t permissions_before = 0;
  1116. mode_t permissions_after = 0;
  1117. if((permissions & required_permissions) == required_permissions)
  1118. {
  1119. permissions_before = permissions;
  1120. }
  1121. else
  1122. {
  1123. permissions_before = permissions | required_permissions;
  1124. permissions_after = permissions;
  1125. }
  1126. // Set the required permissions of the destination directory.
  1127. if(permissions_before &&
  1128. !cmSystemTools::SetPermissions(destination, permissions_before))
  1129. {
  1130. cmOStringStream e;
  1131. e << "Problem setting permissions on directory \""
  1132. << destination << "\"";
  1133. this->FileCommand->SetError(e.str().c_str());
  1134. return false;
  1135. }
  1136. // Load the directory contents to traverse it recursively.
  1137. cmsys::Directory dir;
  1138. if(source && *source)
  1139. {
  1140. dir.Load(source);
  1141. }
  1142. unsigned long numFiles = static_cast<unsigned long>(dir.GetNumberOfFiles());
  1143. for(unsigned long fileNum = 0; fileNum < numFiles; ++fileNum)
  1144. {
  1145. if(!(strcmp(dir.GetFile(fileNum), ".") == 0 ||
  1146. strcmp(dir.GetFile(fileNum), "..") == 0))
  1147. {
  1148. cmsys_stl::string fromPath = source;
  1149. fromPath += "/";
  1150. fromPath += dir.GetFile(fileNum);
  1151. if(cmSystemTools::FileIsDirectory(fromPath.c_str()))
  1152. {
  1153. cmsys_stl::string toDir = destination;
  1154. toDir += "/";
  1155. toDir += dir.GetFile(fileNum);
  1156. if(!this->InstallDirectory(fromPath.c_str(), toDir.c_str(), always))
  1157. {
  1158. return false;
  1159. }
  1160. }
  1161. else
  1162. {
  1163. // Install this file.
  1164. std::string toFile = destination;
  1165. toFile += "/";
  1166. toFile += dir.GetFile(fileNum);
  1167. if(!this->InstallFile(fromPath.c_str(), toFile.c_str(), always))
  1168. {
  1169. return false;
  1170. }
  1171. }
  1172. }
  1173. }
  1174. // Set the requested permissions of the destination directory.
  1175. if(permissions_after &&
  1176. !cmSystemTools::SetPermissions(destination, permissions_after))
  1177. {
  1178. cmOStringStream e;
  1179. e << "Problem setting permissions on directory \"" << destination << "\"";
  1180. this->FileCommand->SetError(e.str().c_str());
  1181. return false;
  1182. }
  1183. return true;
  1184. }
  1185. //----------------------------------------------------------------------------
  1186. void cmFileCommand::HandleInstallPermissions(cmFileInstaller& installer,
  1187. mode_t& permissions_file,
  1188. mode_t& permissions_dir,
  1189. int itype,
  1190. bool use_given_permissions_file,
  1191. bool use_given_permissions_dir,
  1192. bool use_source_permissions) const
  1193. {
  1194. // Choose a default for shared library permissions.
  1195. bool install_so_no_exe = this->Makefile->IsOn("CMAKE_INSTALL_SO_NO_EXE");
  1196. // If file permissions were not specified set default permissions
  1197. // for this target type.
  1198. if(!use_given_permissions_file && !use_source_permissions)
  1199. {
  1200. switch(itype)
  1201. {
  1202. case cmTarget::SHARED_LIBRARY:
  1203. case cmTarget::MODULE_LIBRARY:
  1204. if(install_so_no_exe)
  1205. {
  1206. // Use read/write permissions.
  1207. permissions_file = 0;
  1208. permissions_file |= mode_owner_read;
  1209. permissions_file |= mode_owner_write;
  1210. permissions_file |= mode_group_read;
  1211. permissions_file |= mode_world_read;
  1212. break;
  1213. }
  1214. case cmTarget::EXECUTABLE:
  1215. case cmTarget::INSTALL_PROGRAMS:
  1216. // Use read/write/executable permissions.
  1217. permissions_file = 0;
  1218. permissions_file |= mode_owner_read;
  1219. permissions_file |= mode_owner_write;
  1220. permissions_file |= mode_owner_execute;
  1221. permissions_file |= mode_group_read;
  1222. permissions_file |= mode_group_execute;
  1223. permissions_file |= mode_world_read;
  1224. permissions_file |= mode_world_execute;
  1225. break;
  1226. default:
  1227. // Use read/write permissions.
  1228. permissions_file = 0;
  1229. permissions_file |= mode_owner_read;
  1230. permissions_file |= mode_owner_write;
  1231. permissions_file |= mode_group_read;
  1232. permissions_file |= mode_world_read;
  1233. break;
  1234. }
  1235. }
  1236. // If directory permissions were not specified set default permissions.
  1237. if(!use_given_permissions_dir && !use_source_permissions)
  1238. {
  1239. // Use read/write/executable permissions.
  1240. permissions_dir = 0;
  1241. permissions_dir |= mode_owner_read;
  1242. permissions_dir |= mode_owner_write;
  1243. permissions_dir |= mode_owner_execute;
  1244. permissions_dir |= mode_group_read;
  1245. permissions_dir |= mode_group_execute;
  1246. permissions_dir |= mode_world_read;
  1247. permissions_dir |= mode_world_execute;
  1248. }
  1249. // Set the installer permissions.
  1250. installer.FilePermissions = permissions_file;
  1251. installer.DirPermissions = permissions_dir;
  1252. }
  1253. //----------------------------------------------------------------------------
  1254. void cmFileCommand
  1255. ::GetTargetTypeFromString(const std::string& stype, int& itype) const
  1256. {
  1257. if ( stype == "EXECUTABLE" )
  1258. {
  1259. itype = cmTarget::EXECUTABLE;
  1260. }
  1261. else if ( stype == "PROGRAM" )
  1262. {
  1263. itype = cmTarget::INSTALL_PROGRAMS;
  1264. }
  1265. else if ( stype == "STATIC_LIBRARY" )
  1266. {
  1267. itype = cmTarget::STATIC_LIBRARY;
  1268. }
  1269. else if ( stype == "SHARED_LIBRARY" )
  1270. {
  1271. itype = cmTarget::SHARED_LIBRARY;
  1272. }
  1273. else if ( stype == "MODULE" )
  1274. {
  1275. itype = cmTarget::MODULE_LIBRARY;
  1276. }
  1277. else if ( stype == "DIRECTORY" )
  1278. {
  1279. itype = cmTarget::INSTALL_DIRECTORY;
  1280. }
  1281. }
  1282. //----------------------------------------------------------------------------
  1283. bool cmFileCommand::HandleInstallDestination(cmFileInstaller& installer,
  1284. std::string& destination)
  1285. {
  1286. // allow for / to be a valid destination
  1287. if ( destination.size() < 2 && destination != "/" )
  1288. {
  1289. this->SetError("called with inapropriate arguments. "
  1290. "No DESTINATION provided or .");
  1291. return false;
  1292. }
  1293. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  1294. if ( destdir && *destdir )
  1295. {
  1296. std::string sdestdir = destdir;
  1297. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  1298. char ch1 = destination[0];
  1299. char ch2 = destination[1];
  1300. char ch3 = 0;
  1301. if ( destination.size() > 2 )
  1302. {
  1303. ch3 = destination[2];
  1304. }
  1305. int skip = 0;
  1306. if ( ch1 != '/' )
  1307. {
  1308. int relative = 0;
  1309. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  1310. ch2 == ':' )
  1311. {
  1312. // Assume windows
  1313. // let's do some destdir magic:
  1314. skip = 2;
  1315. if ( ch3 != '/' )
  1316. {
  1317. relative = 1;
  1318. }
  1319. }
  1320. else
  1321. {
  1322. relative = 1;
  1323. }
  1324. if ( relative )
  1325. {
  1326. // This is relative path on unix or windows. Since we are doing
  1327. // destdir, this case does not make sense.
  1328. this->SetError("called with relative DESTINATION. This "
  1329. "does not make sense when using DESTDIR. Specify "
  1330. "absolute path or remove DESTDIR environment variable.");
  1331. return false;
  1332. }
  1333. }
  1334. else
  1335. {
  1336. if ( ch2 == '/' )
  1337. {
  1338. // looks like a network path.
  1339. std::string message = "called with network path DESTINATION. This "
  1340. "does not make sense when using DESTDIR. Specify local "
  1341. "absolute path or remove DESTDIR environment variable."
  1342. "\nDESTINATION=\n";
  1343. message += destination;
  1344. this->SetError(message.c_str());
  1345. return false;
  1346. }
  1347. }
  1348. destination = sdestdir + (destination.c_str() + skip);
  1349. installer.DestDirLength = int(sdestdir.size());
  1350. }
  1351. if ( !cmSystemTools::FileExists(destination.c_str()) )
  1352. {
  1353. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  1354. {
  1355. std::string errstring = "cannot create directory: " + destination +
  1356. ". Maybe need administrative privileges.";
  1357. this->SetError(errstring.c_str());
  1358. return false;
  1359. }
  1360. }
  1361. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  1362. {
  1363. std::string errstring = "INSTALL destination: " + destination +
  1364. " is not a directory.";
  1365. this->SetError(errstring.c_str());
  1366. return false;
  1367. }
  1368. return true;
  1369. }
  1370. //----------------------------------------------------------------------------
  1371. bool
  1372. cmFileCommand::HandleRPathChangeCommand(std::vector<std::string> const& args)
  1373. {
  1374. // Evaluate arguments.
  1375. const char* file = 0;
  1376. const char* oldRPath = 0;
  1377. const char* newRPath = 0;
  1378. enum Doing { DoingNone, DoingFile, DoingOld, DoingNew };
  1379. Doing doing = DoingNone;
  1380. for(unsigned int i=1; i < args.size(); ++i)
  1381. {
  1382. if(args[i] == "OLD_RPATH")
  1383. {
  1384. doing = DoingOld;
  1385. }
  1386. else if(args[i] == "NEW_RPATH")
  1387. {
  1388. doing = DoingNew;
  1389. }
  1390. else if(args[i] == "FILE")
  1391. {
  1392. doing = DoingFile;
  1393. }
  1394. else if(doing == DoingFile)
  1395. {
  1396. file = args[i].c_str();
  1397. doing = DoingNone;
  1398. }
  1399. else if(doing == DoingOld)
  1400. {
  1401. oldRPath = args[i].c_str();
  1402. doing = DoingNone;
  1403. }
  1404. else if(doing == DoingNew)
  1405. {
  1406. newRPath = args[i].c_str();
  1407. doing = DoingNone;
  1408. }
  1409. else
  1410. {
  1411. cmOStringStream e;
  1412. e << "RPATH_CHANGE given unknown argument " << args[i];
  1413. this->SetError(e.str().c_str());
  1414. return false;
  1415. }
  1416. }
  1417. if(!file)
  1418. {
  1419. this->SetError("RPATH_CHANGE not given FILE option.");
  1420. return false;
  1421. }
  1422. if(!oldRPath)
  1423. {
  1424. this->SetError("RPATH_CHANGE not given OLD_RPATH option.");
  1425. return false;
  1426. }
  1427. if(!newRPath)
  1428. {
  1429. this->SetError("RPATH_CHANGE not given NEW_RPATH option.");
  1430. return false;
  1431. }
  1432. if(!cmSystemTools::FileExists(file, true))
  1433. {
  1434. cmOStringStream e;
  1435. e << "RPATH_CHANGE given FILE \"" << file << "\" that does not exist.";
  1436. this->SetError(e.str().c_str());
  1437. return false;
  1438. }
  1439. bool success = true;
  1440. cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew();
  1441. bool have_ft = cmSystemTools::FileTimeGet(file, ft);
  1442. std::string emsg;
  1443. bool changed;
  1444. if(!cmSystemTools::ChangeRPath(file, oldRPath, newRPath, &emsg, &changed))
  1445. {
  1446. cmOStringStream e;
  1447. e << "RPATH_CHANGE could not write new RPATH:\n"
  1448. << " " << newRPath << "\n"
  1449. << "to the file:\n"
  1450. << " " << file << "\n"
  1451. << emsg;
  1452. this->SetError(e.str().c_str());
  1453. success = false;
  1454. }
  1455. if(success)
  1456. {
  1457. if(changed)
  1458. {
  1459. std::string message = "Set runtime path of \"";
  1460. message += file;
  1461. message += "\" to \"";
  1462. message += newRPath;
  1463. message += "\"";
  1464. this->Makefile->DisplayStatus(message.c_str(), -1);
  1465. }
  1466. if(have_ft)
  1467. {
  1468. cmSystemTools::FileTimeSet(file, ft);
  1469. }
  1470. }
  1471. cmSystemTools::FileTimeDelete(ft);
  1472. return success;
  1473. }
  1474. //----------------------------------------------------------------------------
  1475. bool
  1476. cmFileCommand::HandleRPathRemoveCommand(std::vector<std::string> const& args)
  1477. {
  1478. // Evaluate arguments.
  1479. const char* file = 0;
  1480. enum Doing { DoingNone, DoingFile };
  1481. Doing doing = DoingNone;
  1482. for(unsigned int i=1; i < args.size(); ++i)
  1483. {
  1484. if(args[i] == "FILE")
  1485. {
  1486. doing = DoingFile;
  1487. }
  1488. else if(doing == DoingFile)
  1489. {
  1490. file = args[i].c_str();
  1491. doing = DoingNone;
  1492. }
  1493. else
  1494. {
  1495. cmOStringStream e;
  1496. e << "RPATH_REMOVE given unknown argument " << args[i];
  1497. this->SetError(e.str().c_str());
  1498. return false;
  1499. }
  1500. }
  1501. if(!file)
  1502. {
  1503. this->SetError("RPATH_REMOVE not given FILE option.");
  1504. return false;
  1505. }
  1506. if(!cmSystemTools::FileExists(file, true))
  1507. {
  1508. cmOStringStream e;
  1509. e << "RPATH_REMOVE given FILE \"" << file << "\" that does not exist.";
  1510. this->SetError(e.str().c_str());
  1511. return false;
  1512. }
  1513. bool success = true;
  1514. cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew();
  1515. bool have_ft = cmSystemTools::FileTimeGet(file, ft);
  1516. std::string emsg;
  1517. bool removed;
  1518. if(!cmSystemTools::RemoveRPath(file, &emsg, &removed))
  1519. {
  1520. cmOStringStream e;
  1521. e << "RPATH_REMOVE could not remove RPATH from file:\n"
  1522. << " " << file << "\n"
  1523. << emsg;
  1524. this->SetError(e.str().c_str());
  1525. success = false;
  1526. }
  1527. if(success)
  1528. {
  1529. if(removed)
  1530. {
  1531. std::string message = "Removed runtime path from \"";
  1532. message += file;
  1533. message += "\"";
  1534. this->Makefile->DisplayStatus(message.c_str(), -1);
  1535. }
  1536. if(have_ft)
  1537. {
  1538. cmSystemTools::FileTimeSet(file, ft);
  1539. }
  1540. }
  1541. cmSystemTools::FileTimeDelete(ft);
  1542. return success;
  1543. }
  1544. //----------------------------------------------------------------------------
  1545. bool
  1546. cmFileCommand::HandleRPathCheckCommand(std::vector<std::string> const& args)
  1547. {
  1548. // Evaluate arguments.
  1549. const char* file = 0;
  1550. const char* rpath = 0;
  1551. enum Doing { DoingNone, DoingFile, DoingRPath };
  1552. Doing doing = DoingNone;
  1553. for(unsigned int i=1; i < args.size(); ++i)
  1554. {
  1555. if(args[i] == "RPATH")
  1556. {
  1557. doing = DoingRPath;
  1558. }
  1559. else if(args[i] == "FILE")
  1560. {
  1561. doing = DoingFile;
  1562. }
  1563. else if(doing == DoingFile)
  1564. {
  1565. file = args[i].c_str();
  1566. doing = DoingNone;
  1567. }
  1568. else if(doing == DoingRPath)
  1569. {
  1570. rpath = args[i].c_str();
  1571. doing = DoingNone;
  1572. }
  1573. else
  1574. {
  1575. cmOStringStream e;
  1576. e << "RPATH_CHECK given unknown argument " << args[i];
  1577. this->SetError(e.str().c_str());
  1578. return false;
  1579. }
  1580. }
  1581. if(!file)
  1582. {
  1583. this->SetError("RPATH_CHECK not given FILE option.");
  1584. return false;
  1585. }
  1586. if(!rpath)
  1587. {
  1588. this->SetError("RPATH_CHECK not given RPATH option.");
  1589. return false;
  1590. }
  1591. // If the file exists but does not have the desired RPath then
  1592. // delete it. This is used during installation to re-install a file
  1593. // if its RPath will change.
  1594. if(cmSystemTools::FileExists(file, true) &&
  1595. !cmSystemTools::CheckRPath(file, rpath))
  1596. {
  1597. cmSystemTools::RemoveFile(file);
  1598. }
  1599. return true;
  1600. }
  1601. //----------------------------------------------------------------------------
  1602. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  1603. {
  1604. if ( args.size() < 6 )
  1605. {
  1606. this->SetError("called with incorrect number of arguments");
  1607. return false;
  1608. }
  1609. // Construct a file installer object.
  1610. cmFileInstaller installer(this, this->Makefile);
  1611. std::string rename = "";
  1612. std::string destination = "";
  1613. std::vector<std::string> files;
  1614. int itype = cmTarget::INSTALL_FILES;
  1615. bool optional = false;
  1616. bool result = this->ParseInstallArgs(args, installer,
  1617. itype, rename, destination, files,
  1618. optional);
  1619. if (result == true)
  1620. {
  1621. result = this->DoInstall(installer,
  1622. itype, rename, destination, files, optional);
  1623. }
  1624. return result;
  1625. }
  1626. //----------------------------------------------------------------------------
  1627. bool cmFileCommand::ParseInstallArgs(std::vector<std::string> const& args,
  1628. cmFileInstaller& installer,
  1629. int& itype,
  1630. std::string& rename,
  1631. std::string& destination,
  1632. std::vector<std::string>& files,
  1633. bool& optional)
  1634. {
  1635. std::string stype = "FILES";
  1636. enum Doing { DoingNone, DoingFiles,
  1637. DoingPermissionsFile, DoingPermissionsDir,
  1638. DoingPermissionsMatch, DoingSelf24 };
  1639. Doing doing = DoingNone;
  1640. bool use_given_permissions_file = false;
  1641. bool use_given_permissions_dir = false;
  1642. bool use_source_permissions = false;
  1643. mode_t permissions_file = 0;
  1644. mode_t permissions_dir = 0;
  1645. cmFileInstaller::MatchRule* current_match_rule = 0;
  1646. std::vector<std::string>::size_type i = 0;
  1647. i++; // Get rid of subcommand
  1648. for ( ; i != args.size(); ++i )
  1649. {
  1650. const std::string* cstr = &args[i];
  1651. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  1652. {
  1653. if(current_match_rule)
  1654. {
  1655. cmOStringStream e;
  1656. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1657. this->SetError(e.str().c_str());
  1658. return false;
  1659. }
  1660. i++;
  1661. destination = args[i];
  1662. doing = DoingNone;
  1663. }
  1664. else if ( *cstr == "TYPE" && i < args.size()-1 )
  1665. {
  1666. if(current_match_rule)
  1667. {
  1668. cmOStringStream e;
  1669. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1670. this->SetError(e.str().c_str());
  1671. return false;
  1672. }
  1673. i++;
  1674. stype = args[i];
  1675. if ( args[i+1] == "OPTIONAL" )
  1676. {
  1677. i++;
  1678. optional = true;
  1679. }
  1680. doing = DoingNone;
  1681. }
  1682. else if ( *cstr == "RENAME" && i < args.size()-1 )
  1683. {
  1684. if(current_match_rule)
  1685. {
  1686. cmOStringStream e;
  1687. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1688. this->SetError(e.str().c_str());
  1689. return false;
  1690. }
  1691. i++;
  1692. rename = args[i];
  1693. doing = DoingNone;
  1694. }
  1695. else if ( *cstr == "REGEX" && i < args.size()-1 )
  1696. {
  1697. i++;
  1698. installer.MatchRules.push_back(cmFileInstaller::MatchRule(args[i]));
  1699. current_match_rule = &*(installer.MatchRules.end()-1);
  1700. if(!current_match_rule->Regex.is_valid())
  1701. {
  1702. cmOStringStream e;
  1703. e << "INSTALL could not compile REGEX \"" << args[i] << "\".";
  1704. this->SetError(e.str().c_str());
  1705. return false;
  1706. }
  1707. doing = DoingNone;
  1708. }
  1709. else if ( *cstr == "EXCLUDE" )
  1710. {
  1711. // Add this property to the current match rule.
  1712. if(!current_match_rule)
  1713. {
  1714. cmOStringStream e;
  1715. e << "INSTALL does not allow \""
  1716. << *cstr << "\" before a REGEX is given.";
  1717. this->SetError(e.str().c_str());
  1718. return false;
  1719. }
  1720. current_match_rule->Properties.Exclude = true;
  1721. doing = DoingPermissionsMatch;
  1722. }
  1723. else if ( *cstr == "PROPERTIES" )
  1724. {
  1725. cmOStringStream e;
  1726. e << "INSTALL called with old-style PROPERTIES argument. "
  1727. << "This script was generated with an older version of CMake. "
  1728. << "Re-run this cmake version on your build tree.";
  1729. this->SetError(e.str().c_str());
  1730. return false;
  1731. }
  1732. else if ( *cstr == "PERMISSIONS" )
  1733. {
  1734. if(current_match_rule)
  1735. {
  1736. doing = DoingPermissionsMatch;
  1737. }
  1738. else
  1739. {
  1740. doing = DoingPermissionsFile;
  1741. use_given_permissions_file = true;
  1742. }
  1743. }
  1744. else if ( *cstr == "DIR_PERMISSIONS" )
  1745. {
  1746. if(current_match_rule)
  1747. {
  1748. cmOStringStream e;
  1749. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1750. this->SetError(e.str().c_str());
  1751. return false;
  1752. }
  1753. use_given_permissions_dir = true;
  1754. doing = DoingPermissionsDir;
  1755. }
  1756. else if ( *cstr == "USE_SOURCE_PERMISSIONS" )
  1757. {
  1758. if(current_match_rule)
  1759. {
  1760. cmOStringStream e;
  1761. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1762. this->SetError(e.str().c_str());
  1763. return false;
  1764. }
  1765. doing = DoingNone;
  1766. use_source_permissions = true;
  1767. }
  1768. else if ( *cstr == "FILES_MATCHING" )
  1769. {
  1770. if(current_match_rule)
  1771. {
  1772. cmOStringStream e;
  1773. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1774. this->SetError(e.str().c_str());
  1775. return false;
  1776. }
  1777. doing = DoingNone;
  1778. installer.MatchlessFiles = false;
  1779. }
  1780. else if ( *cstr == "COMPONENTS" )
  1781. {
  1782. if(this->Makefile->IsOn("CMAKE_INSTALL_SELF_2_4"))
  1783. {
  1784. // When CMake 2.4 builds this CMake version we need to support
  1785. // the install scripts it generates since it asks this CMake
  1786. // to install itself using the rules it generated.
  1787. doing = DoingSelf24;
  1788. continue;
  1789. }
  1790. cmOStringStream e;
  1791. e << "INSTALL called with old-style COMPONENTS argument. "
  1792. << "This script was generated with an older version of CMake. "
  1793. << "Re-run this cmake version on your build tree.";
  1794. this->SetError(e.str().c_str());
  1795. return false;
  1796. }
  1797. else if ( *cstr == "CONFIGURATIONS" )
  1798. {
  1799. cmOStringStream e;
  1800. e << "INSTALL called with old-style CONFIGURATIONS argument. "
  1801. << "This script was generated with an older version of CMake. "
  1802. << "Re-run this cmake version on your build tree.";
  1803. this->SetError(e.str().c_str());
  1804. return false;
  1805. }
  1806. else if(*cstr == "FILES" && doing != DoingFiles)
  1807. {
  1808. if(current_match_rule)
  1809. {
  1810. cmOStringStream e;
  1811. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1812. this->SetError(e.str().c_str());
  1813. return false;
  1814. }
  1815. doing = DoingFiles;
  1816. }
  1817. else if(doing == DoingFiles)
  1818. {
  1819. files.push_back(*cstr);
  1820. }
  1821. else if(doing == DoingPermissionsFile)
  1822. {
  1823. if(!installer.CheckPermissions(args[i], permissions_file))
  1824. {
  1825. return false;
  1826. }
  1827. }
  1828. else if(doing == DoingPermissionsDir)
  1829. {
  1830. if(!installer.CheckPermissions(args[i], permissions_dir))
  1831. {
  1832. return false;
  1833. }
  1834. }
  1835. else if(doing == DoingPermissionsMatch)
  1836. {
  1837. if(!installer.CheckPermissions(
  1838. args[i], current_match_rule->Properties.Permissions))
  1839. {
  1840. return false;
  1841. }
  1842. }
  1843. else if(doing == DoingSelf24)
  1844. {
  1845. // Ignore these arguments for compatibility. This should be
  1846. // reached only when CMake 2.4 is installing the current
  1847. // CMake. It can be removed when CMake 2.6 or higher is
  1848. // required to build CMake.
  1849. }
  1850. else
  1851. {
  1852. this->SetError("called with inappropriate arguments");
  1853. return false;
  1854. }
  1855. }
  1856. // now check and postprocess what has been parsed
  1857. if ( files.size() == 0 )
  1858. {
  1859. // nothing to do, no files were listed.
  1860. // if this is handled as error, INSTALL_FILES() creates an invalid
  1861. // cmake_install.cmake script with no FILES() arguments if no files were
  1862. // given to INSTALL_FILES(). This was accepted with CMake 2.4.x.
  1863. return true;
  1864. }
  1865. // Check rename form.
  1866. if(!rename.empty())
  1867. {
  1868. if(itype != cmTarget::INSTALL_FILES &&
  1869. itype != cmTarget::INSTALL_PROGRAMS)
  1870. {
  1871. this->SetError("INSTALL option RENAME may be used only with "
  1872. "FILES or PROGRAMS.");
  1873. return false;
  1874. }
  1875. if(files.size() > 1)
  1876. {
  1877. this->SetError("INSTALL option RENAME may be used only with "
  1878. "one file.");
  1879. return false;
  1880. }
  1881. }
  1882. if (this->HandleInstallDestination(installer, destination) == false)
  1883. {
  1884. return false;
  1885. }
  1886. this->GetTargetTypeFromString(stype, itype);
  1887. this->HandleInstallPermissions(installer,
  1888. permissions_file,
  1889. permissions_dir,
  1890. itype,
  1891. use_given_permissions_file,
  1892. use_given_permissions_dir,
  1893. use_source_permissions);
  1894. return true;
  1895. }
  1896. //----------------------------------------------------------------------------
  1897. bool cmFileCommand::DoInstall( cmFileInstaller& installer,
  1898. const int itype,
  1899. const std::string& rename,
  1900. const std::string& destination,
  1901. const std::vector<std::string>& files,
  1902. const bool optional)
  1903. {
  1904. typedef std::set<cmStdString>::const_iterator iter_type;
  1905. // Check whether files should be copied always or only if they have
  1906. // changed.
  1907. bool copy_always =
  1908. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  1909. // Handle each file listed.
  1910. for (std::vector<std::string>::size_type i = 0; i < files.size(); i ++ )
  1911. {
  1912. // Split the input file into its directory and name components.
  1913. std::vector<std::string> fromPathComponents;
  1914. cmSystemTools::SplitPath(files[i].c_str(), fromPathComponents);
  1915. std::string fromName = *(fromPathComponents.end()-1);
  1916. std::string fromDir = cmSystemTools::JoinPath(fromPathComponents.begin(),
  1917. fromPathComponents.end()-1);
  1918. // Compute the full path to the destination file.
  1919. std::string toFile = destination;
  1920. std::string const& toName = rename.empty()? fromName : rename;
  1921. if(!toName.empty())
  1922. {
  1923. toFile += "/";
  1924. toFile += toName;
  1925. }
  1926. // Construct the full path to the source file. The file name may
  1927. // have been changed above.
  1928. std::string fromFile = fromDir;
  1929. if(!fromName.empty())
  1930. {
  1931. fromFile += "/";
  1932. fromFile += fromName;
  1933. }
  1934. std::string message;
  1935. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  1936. {
  1937. if(itype == cmTarget::INSTALL_DIRECTORY &&
  1938. (fromFile.empty() ||
  1939. cmSystemTools::FileIsDirectory(fromFile.c_str())))
  1940. {
  1941. // Try installing this directory.
  1942. if(!installer.InstallDirectory(fromFile.c_str(), toFile.c_str(),
  1943. copy_always))
  1944. {
  1945. return false;
  1946. }
  1947. }
  1948. else if(cmSystemTools::FileExists(fromFile.c_str()))
  1949. {
  1950. // Install this file.
  1951. if(!installer.InstallFile(fromFile.c_str(), toFile.c_str(),
  1952. copy_always))
  1953. {
  1954. return false;
  1955. }
  1956. }
  1957. else if(!optional)
  1958. {
  1959. // The input file does not exist and installation is not optional.
  1960. cmOStringStream e;
  1961. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  1962. this->SetError(e.str().c_str());
  1963. return false;
  1964. }
  1965. }
  1966. }
  1967. return true;
  1968. }
  1969. //----------------------------------------------------------------------------
  1970. bool cmFileCommand::HandleRelativePathCommand(
  1971. std::vector<std::string> const& args)
  1972. {
  1973. if(args.size() != 4 )
  1974. {
  1975. this->SetError("called with incorrect number of arguments");
  1976. return false;
  1977. }
  1978. const std::string& outVar = args[1];
  1979. const std::string& directoryName = args[2];
  1980. const std::string& fileName = args[3];
  1981. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  1982. {
  1983. std::string errstring =
  1984. "RelativePath must be passed a full path to the directory: "
  1985. + directoryName;
  1986. this->SetError(errstring.c_str());
  1987. return false;
  1988. }
  1989. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  1990. {
  1991. std::string errstring =
  1992. "RelativePath must be passed a full path to the file: "
  1993. + fileName;
  1994. this->SetError(errstring.c_str());
  1995. return false;
  1996. }
  1997. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  1998. fileName.c_str());
  1999. this->Makefile->AddDefinition(outVar.c_str(),
  2000. res.c_str());
  2001. return true;
  2002. }
  2003. //----------------------------------------------------------------------------
  2004. bool cmFileCommand::HandleRename(std::vector<std::string> const& args)
  2005. {
  2006. if(args.size() != 3)
  2007. {
  2008. this->SetError("given incorrect number of arguments.");
  2009. return false;
  2010. }
  2011. // Compute full path for old and new names.
  2012. std::string oldname = args[1];
  2013. if(!cmsys::SystemTools::FileIsFullPath(oldname.c_str()))
  2014. {
  2015. oldname = this->Makefile->GetCurrentDirectory();
  2016. oldname += "/" + args[1];
  2017. }
  2018. std::string newname = args[2];
  2019. if(!cmsys::SystemTools::FileIsFullPath(newname.c_str()))
  2020. {
  2021. newname = this->Makefile->GetCurrentDirectory();
  2022. newname += "/" + args[2];
  2023. }
  2024. if(!cmSystemTools::RenameFile(oldname.c_str(), newname.c_str()))
  2025. {
  2026. std::string err = cmSystemTools::GetLastSystemError();
  2027. cmOStringStream e;
  2028. e << "RENAME failed to rename\n"
  2029. << " " << oldname << "\n"
  2030. << "to\n"
  2031. << " " << newname << "\n"
  2032. << "because: " << err << "\n";
  2033. this->SetError(e.str().c_str());
  2034. return false;
  2035. }
  2036. return true;
  2037. }
  2038. //----------------------------------------------------------------------------
  2039. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  2040. bool recurse)
  2041. {
  2042. std::string message;
  2043. std::vector<std::string>::const_iterator i = args.begin();
  2044. i++; // Get rid of subcommand
  2045. for(;i != args.end(); ++i)
  2046. {
  2047. std::string fileName = *i;
  2048. if(!cmsys::SystemTools::FileIsFullPath(fileName.c_str()))
  2049. {
  2050. fileName = this->Makefile->GetCurrentDirectory();
  2051. fileName += "/" + *i;
  2052. }
  2053. if(cmSystemTools::FileIsDirectory(fileName.c_str()) && recurse)
  2054. {
  2055. cmSystemTools::RemoveADirectory(fileName.c_str());
  2056. }
  2057. else
  2058. {
  2059. cmSystemTools::RemoveFile(fileName.c_str());
  2060. }
  2061. }
  2062. return true;
  2063. }
  2064. //----------------------------------------------------------------------------
  2065. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  2066. const& args,
  2067. bool nativePath)
  2068. {
  2069. std::vector<std::string>::const_iterator i = args.begin();
  2070. if(args.size() != 3)
  2071. {
  2072. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  2073. "only three arguments.");
  2074. return false;
  2075. }
  2076. i++; // Get rid of subcommand
  2077. #if defined(_WIN32) && !defined(__CYGWIN__)
  2078. char pathSep = ';';
  2079. #else
  2080. char pathSep = ':';
  2081. #endif
  2082. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  2083. pathSep);
  2084. i++;
  2085. const char* var = i->c_str();
  2086. std::string value;
  2087. for(std::vector<cmsys::String>::iterator j = path.begin();
  2088. j != path.end(); ++j)
  2089. {
  2090. if(j != path.begin())
  2091. {
  2092. value += ";";
  2093. }
  2094. if(!nativePath)
  2095. {
  2096. cmSystemTools::ConvertToUnixSlashes(*j);
  2097. }
  2098. else
  2099. {
  2100. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  2101. // remove double quotes in the path
  2102. cmsys::String& s = *j;
  2103. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  2104. {
  2105. s = s.substr(1,s.size()-2);
  2106. }
  2107. }
  2108. value += *j;
  2109. }
  2110. this->Makefile->AddDefinition(var, value.c_str());
  2111. return true;
  2112. }
  2113. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2114. // Stuff for curl download
  2115. typedef std::vector<char> cmFileCommandVectorOfChar;
  2116. namespace{
  2117. size_t
  2118. cmFileCommandWriteMemoryCallback(void *ptr, size_t size, size_t nmemb,
  2119. void *data)
  2120. {
  2121. register int realsize = (int)(size * nmemb);
  2122. std::ofstream* fout = static_cast<std::ofstream*>(data);
  2123. const char* chPtr = static_cast<char*>(ptr);
  2124. fout->write(chPtr, realsize);
  2125. return realsize;
  2126. }
  2127. static size_t
  2128. cmFileCommandCurlDebugCallback(CURL *, curl_infotype, char *chPtr,
  2129. size_t size, void *data)
  2130. {
  2131. cmFileCommandVectorOfChar *vec
  2132. = static_cast<cmFileCommandVectorOfChar*>(data);
  2133. vec->insert(vec->end(), chPtr, chPtr + size);
  2134. return size;
  2135. }
  2136. }
  2137. #endif
  2138. bool
  2139. cmFileCommand::HandleDownloadCommand(std::vector<std::string>
  2140. const& args)
  2141. {
  2142. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2143. std::vector<std::string>::const_iterator i = args.begin();
  2144. if(args.size() < 3)
  2145. {
  2146. this->SetError("FILE(DOWNLOAD url file) must be called with "
  2147. "at least three arguments.");
  2148. return false;
  2149. }
  2150. i++; // Get rid of subcommand
  2151. std::string url = *i;
  2152. i++;
  2153. std::string file = *i;
  2154. i++;
  2155. double timeout = 0;
  2156. std::string verboseLog;
  2157. std::string statusVar;
  2158. while(i != args.end())
  2159. {
  2160. if(*i == "TIMEOUT")
  2161. {
  2162. i++;
  2163. if(i != args.end())
  2164. {
  2165. timeout = atof(i->c_str());
  2166. }
  2167. else
  2168. {
  2169. this->SetError("FILE(DOWNLOAD url file TIMEOUT time) missing "
  2170. "time for TIMEOUT.");
  2171. return false;
  2172. }
  2173. }
  2174. else if(*i == "LOG")
  2175. {
  2176. i++;
  2177. if( i == args.end())
  2178. {
  2179. this->SetError("FILE(DOWNLOAD url file LOG VAR) missing "
  2180. "VAR for LOG.");
  2181. return false;
  2182. }
  2183. verboseLog = *i;
  2184. }
  2185. else if(*i == "STATUS")
  2186. {
  2187. i++;
  2188. if( i == args.end())
  2189. {
  2190. this->SetError("FILE(DOWNLOAD url file STATUS VAR) missing "
  2191. "VAR for STATUS.");
  2192. return false;
  2193. }
  2194. statusVar = *i;
  2195. }
  2196. i++;
  2197. }
  2198. std::string dir = cmSystemTools::GetFilenamePath(file.c_str());
  2199. if(!cmSystemTools::FileExists(dir.c_str()) &&
  2200. !cmSystemTools::MakeDirectory(dir.c_str()))
  2201. {
  2202. std::string errstring = "FILE(DOWNLOAD ) error; cannot create directory: "
  2203. + dir + ". Maybe need administrative privileges.";
  2204. this->SetError(errstring.c_str());
  2205. return false;
  2206. }
  2207. std::ofstream fout(file.c_str(), std::ios::binary);
  2208. if(!fout)
  2209. {
  2210. this->SetError("FILE(DOWNLOAD url file TIMEOUT time) can not open "
  2211. "file for write.");
  2212. return false;
  2213. }
  2214. CURL *curl;
  2215. curl_global_init(CURL_GLOBAL_DEFAULT);
  2216. curl = curl_easy_init();
  2217. if(!curl)
  2218. {
  2219. this->SetError("FILE(DOWNLOAD ) error "
  2220. "initializing curl.");
  2221. return false;
  2222. }
  2223. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  2224. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  2225. cmFileCommandWriteMemoryCallback);
  2226. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  2227. cmFileCommandCurlDebugCallback);
  2228. cmFileCommandVectorOfChar chunkDebug;
  2229. ::curl_easy_setopt(curl, CURLOPT_FILE, (void *)&fout);
  2230. ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *)&chunkDebug);
  2231. if(verboseLog.size())
  2232. {
  2233. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  2234. }
  2235. if(timeout > 0)
  2236. {
  2237. curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout );
  2238. }
  2239. CURLcode res = curl_easy_perform(curl);
  2240. /* always cleanup */
  2241. curl_easy_cleanup(curl);
  2242. if(statusVar.size())
  2243. {
  2244. cmOStringStream result;
  2245. result << (int)res << ";\"" << curl_easy_strerror(res) << "\"";
  2246. this->Makefile->AddDefinition(statusVar.c_str(),
  2247. result.str().c_str());
  2248. }
  2249. curl_global_cleanup();
  2250. if(chunkDebug.size())
  2251. {
  2252. chunkDebug.push_back(0);
  2253. if(CURLE_OPERATION_TIMEOUTED == res)
  2254. {
  2255. std::string output = &*chunkDebug.begin();
  2256. if(verboseLog.size())
  2257. {
  2258. this->Makefile->AddDefinition(verboseLog.c_str(),
  2259. &*chunkDebug.begin());
  2260. }
  2261. }
  2262. this->Makefile->AddDefinition(verboseLog.c_str(),
  2263. &*chunkDebug.begin());
  2264. }
  2265. return true;
  2266. #else
  2267. this->SetError("FILE(DOWNLOAD ) "
  2268. "not supported in bootstrap cmake ");
  2269. return false;
  2270. #endif
  2271. }