cmFileCommand.cxx 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429
  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);
  845. bool InstallDirectory(const char* source, const char* destination);
  846. // All instances need the file command and makefile using them.
  847. cmFileInstaller(cmFileCommand* command):
  848. FileCommand(command), Makefile(command->GetMakefile()),
  849. Always(false), DestDirLength(0), MatchlessFiles(true)
  850. {
  851. // Check whether to copy files always or only if they have changed.
  852. this->Always =
  853. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  854. // Get the current manifest.
  855. this->Manifest =
  856. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  857. }
  858. ~cmFileInstaller()
  859. {
  860. // Save the updated install manifest.
  861. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  862. this->Manifest.c_str());
  863. }
  864. private:
  865. cmFileCommand* FileCommand;
  866. cmMakefile* Makefile;
  867. bool Always;
  868. cmFileTimeComparison FileTimes;
  869. public:
  870. // The length of the destdir setting.
  871. int DestDirLength;
  872. // Whether to install a file not matching any expression.
  873. bool MatchlessFiles;
  874. // The current file manifest (semicolon separated list).
  875. std::string Manifest;
  876. // Permissions for files and directories installed by this object.
  877. mode_t FilePermissions;
  878. mode_t DirPermissions;
  879. // Properties set by pattern and regex match rules.
  880. struct MatchProperties
  881. {
  882. bool Exclude;
  883. mode_t Permissions;
  884. MatchProperties(): Exclude(false), Permissions(0) {}
  885. };
  886. struct MatchRule
  887. {
  888. cmsys::RegularExpression Regex;
  889. MatchProperties Properties;
  890. std::string RegexString;
  891. MatchRule(std::string const& regex):
  892. Regex(regex.c_str()), RegexString(regex) {}
  893. };
  894. std::vector<MatchRule> MatchRules;
  895. // Get the properties from rules matching this input file.
  896. MatchProperties CollectMatchProperties(const char* file,
  897. bool isDirectory)
  898. {
  899. // Match rules are case-insensitive on some platforms.
  900. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  901. std::string lower = cmSystemTools::LowerCase(file);
  902. file = lower.c_str();
  903. #endif
  904. // Collect properties from all matching rules.
  905. bool matched = false;
  906. MatchProperties result;
  907. for(std::vector<MatchRule>::iterator mr = this->MatchRules.begin();
  908. mr != this->MatchRules.end(); ++mr)
  909. {
  910. if(mr->Regex.find(file))
  911. {
  912. matched = true;
  913. result.Exclude |= mr->Properties.Exclude;
  914. result.Permissions |= mr->Properties.Permissions;
  915. }
  916. }
  917. if(!matched && !this->MatchlessFiles && !isDirectory)
  918. {
  919. result.Exclude = true;
  920. }
  921. return result;
  922. }
  923. // Append a file to the installation manifest.
  924. void ManifestAppend(std::string const& file)
  925. {
  926. this->Manifest += ";";
  927. this->Manifest += file.substr(this->DestDirLength);
  928. }
  929. bool SetPermissions(const char* toFile, mode_t permissions)
  930. {
  931. if(permissions && !cmSystemTools::SetPermissions(toFile, permissions))
  932. {
  933. cmOStringStream e;
  934. e << "INSTALL cannot set permissions on \"" << toFile << "\"";
  935. this->FileCommand->SetError(e.str().c_str());
  936. return false;
  937. }
  938. return true;
  939. }
  940. // Translate an argument to a permissions bit.
  941. bool CheckPermissions(std::string const& arg, mode_t& permissions)
  942. {
  943. if(arg == "OWNER_READ") { permissions |= mode_owner_read; }
  944. else if(arg == "OWNER_WRITE") { permissions |= mode_owner_write; }
  945. else if(arg == "OWNER_EXECUTE") { permissions |= mode_owner_execute; }
  946. else if(arg == "GROUP_READ") { permissions |= mode_group_read; }
  947. else if(arg == "GROUP_WRITE") { permissions |= mode_group_write; }
  948. else if(arg == "GROUP_EXECUTE") { permissions |= mode_group_execute; }
  949. else if(arg == "WORLD_READ") { permissions |= mode_world_read; }
  950. else if(arg == "WORLD_WRITE") { permissions |= mode_world_write; }
  951. else if(arg == "WORLD_EXECUTE") { permissions |= mode_world_execute; }
  952. else if(arg == "SETUID") { permissions |= mode_setuid; }
  953. else if(arg == "SETGID") { permissions |= mode_setgid; }
  954. else
  955. {
  956. cmOStringStream e;
  957. e << "INSTALL given invalid permission \"" << arg << "\".";
  958. this->FileCommand->SetError(e.str().c_str());
  959. return false;
  960. }
  961. return true;
  962. }
  963. private:
  964. bool InstallSymlink(const char* fromFile, const char* toFile);
  965. };
  966. //----------------------------------------------------------------------------
  967. bool cmFileInstaller::InstallSymlink(const char* fromFile, const char* toFile)
  968. {
  969. // Read the original symlink.
  970. std::string symlinkTarget;
  971. if(!cmSystemTools::ReadSymlink(fromFile, symlinkTarget))
  972. {
  973. cmOStringStream e;
  974. e << "INSTALL cannot read symlink \"" << fromFile
  975. << "\" to duplicate at \"" << toFile << "\".";
  976. this->FileCommand->SetError(e.str().c_str());
  977. return false;
  978. }
  979. // Compare the symlink value to that at the destination if not
  980. // always installing.
  981. bool copy = true;
  982. if(!this->Always)
  983. {
  984. std::string oldSymlinkTarget;
  985. if(cmSystemTools::ReadSymlink(toFile, oldSymlinkTarget))
  986. {
  987. if(symlinkTarget == oldSymlinkTarget)
  988. {
  989. copy = false;
  990. }
  991. }
  992. }
  993. // Inform the user about this file installation.
  994. std::string message = (copy? "Installing: " : "Up-to-date: ");
  995. message += toFile;
  996. this->Makefile->DisplayStatus(message.c_str(), -1);
  997. if(copy)
  998. {
  999. // Remove the destination file so we can always create the symlink.
  1000. cmSystemTools::RemoveFile(toFile);
  1001. // Create the symlink.
  1002. if(!cmSystemTools::CreateSymlink(symlinkTarget.c_str(), toFile))
  1003. {
  1004. cmOStringStream e;
  1005. e << "INSTALL cannot duplicate symlink \"" << fromFile
  1006. << "\" at \"" << toFile << "\".";
  1007. this->FileCommand->SetError(e.str().c_str());
  1008. return false;
  1009. }
  1010. }
  1011. // Add the file to the manifest.
  1012. this->ManifestAppend(toFile);
  1013. return true;
  1014. }
  1015. //----------------------------------------------------------------------------
  1016. bool cmFileInstaller::InstallFile(const char* fromFile, const char* toFile)
  1017. {
  1018. // Collect any properties matching this file name.
  1019. MatchProperties match_properties =
  1020. this->CollectMatchProperties(fromFile, false);
  1021. // Skip the file if it is excluded.
  1022. if(match_properties.Exclude)
  1023. {
  1024. return true;
  1025. }
  1026. // Short-circuit for symbolic links.
  1027. if(cmSystemTools::FileIsSymlink(fromFile))
  1028. {
  1029. return this->InstallSymlink(fromFile, toFile);
  1030. }
  1031. // Determine whether we will copy the file.
  1032. bool copy = true;
  1033. if(!this->Always)
  1034. {
  1035. // If both files exist with the same time do not copy.
  1036. if(!this->FileTimes.FileTimesDiffer(fromFile, toFile))
  1037. {
  1038. copy = false;
  1039. }
  1040. }
  1041. // Inform the user about this file installation.
  1042. std::string message = (copy? "Installing: " : "Up-to-date: ");
  1043. message += toFile;
  1044. this->Makefile->DisplayStatus(message.c_str(), -1);
  1045. // Copy the file.
  1046. if(copy && !cmSystemTools::CopyAFile(fromFile, toFile, true, false))
  1047. {
  1048. cmOStringStream e;
  1049. e << "INSTALL cannot copy file \"" << fromFile
  1050. << "\" to \"" << toFile << "\".";
  1051. this->FileCommand->SetError(e.str().c_str());
  1052. return false;
  1053. }
  1054. // Add the file to the manifest.
  1055. this->ManifestAppend(toFile);
  1056. // Set the file modification time of the destination file.
  1057. if(copy && !this->Always)
  1058. {
  1059. if (!cmSystemTools::CopyFileTime(fromFile, toFile))
  1060. {
  1061. cmOStringStream e;
  1062. e << "INSTALL cannot set modification time on \"" << toFile << "\"";
  1063. this->FileCommand->SetError(e.str().c_str());
  1064. return false;
  1065. }
  1066. }
  1067. // Set permissions of the destination file.
  1068. mode_t permissions = (match_properties.Permissions?
  1069. match_properties.Permissions : this->FilePermissions);
  1070. if(!permissions)
  1071. {
  1072. // No permissions were explicitly provided but the user requested
  1073. // that the source file permissions be used.
  1074. cmSystemTools::GetPermissions(fromFile, permissions);
  1075. }
  1076. return this->SetPermissions(toFile, permissions);
  1077. }
  1078. //----------------------------------------------------------------------------
  1079. bool cmFileInstaller::InstallDirectory(const char* source,
  1080. const char* destination)
  1081. {
  1082. // Collect any properties matching this directory name.
  1083. MatchProperties match_properties =
  1084. this->CollectMatchProperties(source, true);
  1085. // Skip the directory if it is excluded.
  1086. if(match_properties.Exclude)
  1087. {
  1088. return true;
  1089. }
  1090. // Short-circuit for symbolic links.
  1091. if(cmSystemTools::FileIsSymlink(source))
  1092. {
  1093. return this->InstallSymlink(source, destination);
  1094. }
  1095. // Inform the user about this directory installation.
  1096. std::string message = "Installing: ";
  1097. message += destination;
  1098. this->Makefile->DisplayStatus(message.c_str(), -1);
  1099. // Make sure the destination directory exists.
  1100. if(!cmSystemTools::MakeDirectory(destination))
  1101. {
  1102. cmOStringStream e;
  1103. e << "INSTALL cannot make directory \"" << destination << "\": "
  1104. << cmSystemTools::GetLastSystemError();
  1105. this->FileCommand->SetError(e.str().c_str());
  1106. return false;
  1107. }
  1108. // Compute the requested permissions for the destination directory.
  1109. mode_t permissions = (match_properties.Permissions?
  1110. match_properties.Permissions : this->DirPermissions);
  1111. if(!permissions)
  1112. {
  1113. // No permissions were explicitly provided but the user requested
  1114. // that the source directory permissions be used.
  1115. cmSystemTools::GetPermissions(source, permissions);
  1116. }
  1117. // Compute the set of permissions required on this directory to
  1118. // recursively install files and subdirectories safely.
  1119. mode_t required_permissions =
  1120. mode_owner_read | mode_owner_write | mode_owner_execute;
  1121. // If the required permissions are specified it is safe to set the
  1122. // final permissions now. Otherwise we must add the required
  1123. // permissions temporarily during file installation.
  1124. mode_t permissions_before = 0;
  1125. mode_t permissions_after = 0;
  1126. if((permissions & required_permissions) == required_permissions)
  1127. {
  1128. permissions_before = permissions;
  1129. }
  1130. else
  1131. {
  1132. permissions_before = permissions | required_permissions;
  1133. permissions_after = permissions;
  1134. }
  1135. // Set the required permissions of the destination directory.
  1136. if(!this->SetPermissions(destination, permissions_before))
  1137. {
  1138. return false;
  1139. }
  1140. // Load the directory contents to traverse it recursively.
  1141. cmsys::Directory dir;
  1142. if(source && *source)
  1143. {
  1144. dir.Load(source);
  1145. }
  1146. unsigned long numFiles = static_cast<unsigned long>(dir.GetNumberOfFiles());
  1147. for(unsigned long fileNum = 0; fileNum < numFiles; ++fileNum)
  1148. {
  1149. if(!(strcmp(dir.GetFile(fileNum), ".") == 0 ||
  1150. strcmp(dir.GetFile(fileNum), "..") == 0))
  1151. {
  1152. cmsys_stl::string fromPath = source;
  1153. fromPath += "/";
  1154. fromPath += dir.GetFile(fileNum);
  1155. if(cmSystemTools::FileIsDirectory(fromPath.c_str()))
  1156. {
  1157. cmsys_stl::string toDir = destination;
  1158. toDir += "/";
  1159. toDir += dir.GetFile(fileNum);
  1160. if(!this->InstallDirectory(fromPath.c_str(), toDir.c_str()))
  1161. {
  1162. return false;
  1163. }
  1164. }
  1165. else
  1166. {
  1167. // Install this file.
  1168. std::string toFile = destination;
  1169. toFile += "/";
  1170. toFile += dir.GetFile(fileNum);
  1171. if(!this->InstallFile(fromPath.c_str(), toFile.c_str()))
  1172. {
  1173. return false;
  1174. }
  1175. }
  1176. }
  1177. }
  1178. // Set the requested permissions of the destination directory.
  1179. return this->SetPermissions(destination, permissions_after);
  1180. }
  1181. //----------------------------------------------------------------------------
  1182. void cmFileCommand::HandleInstallPermissions(cmFileInstaller& installer,
  1183. mode_t& permissions_file,
  1184. mode_t& permissions_dir,
  1185. int itype,
  1186. bool use_given_permissions_file,
  1187. bool use_given_permissions_dir,
  1188. bool use_source_permissions) const
  1189. {
  1190. // Choose a default for shared library permissions.
  1191. bool install_so_no_exe = this->Makefile->IsOn("CMAKE_INSTALL_SO_NO_EXE");
  1192. // If file permissions were not specified set default permissions
  1193. // for this target type.
  1194. if(!use_given_permissions_file && !use_source_permissions)
  1195. {
  1196. switch(itype)
  1197. {
  1198. case cmTarget::SHARED_LIBRARY:
  1199. case cmTarget::MODULE_LIBRARY:
  1200. if(install_so_no_exe)
  1201. {
  1202. // Use read/write permissions.
  1203. permissions_file = 0;
  1204. permissions_file |= mode_owner_read;
  1205. permissions_file |= mode_owner_write;
  1206. permissions_file |= mode_group_read;
  1207. permissions_file |= mode_world_read;
  1208. break;
  1209. }
  1210. case cmTarget::EXECUTABLE:
  1211. case cmTarget::INSTALL_PROGRAMS:
  1212. // Use read/write/executable permissions.
  1213. permissions_file = 0;
  1214. permissions_file |= mode_owner_read;
  1215. permissions_file |= mode_owner_write;
  1216. permissions_file |= mode_owner_execute;
  1217. permissions_file |= mode_group_read;
  1218. permissions_file |= mode_group_execute;
  1219. permissions_file |= mode_world_read;
  1220. permissions_file |= mode_world_execute;
  1221. break;
  1222. default:
  1223. // Use read/write permissions.
  1224. permissions_file = 0;
  1225. permissions_file |= mode_owner_read;
  1226. permissions_file |= mode_owner_write;
  1227. permissions_file |= mode_group_read;
  1228. permissions_file |= mode_world_read;
  1229. break;
  1230. }
  1231. }
  1232. // If directory permissions were not specified set default permissions.
  1233. if(!use_given_permissions_dir && !use_source_permissions)
  1234. {
  1235. // Use read/write/executable permissions.
  1236. permissions_dir = 0;
  1237. permissions_dir |= mode_owner_read;
  1238. permissions_dir |= mode_owner_write;
  1239. permissions_dir |= mode_owner_execute;
  1240. permissions_dir |= mode_group_read;
  1241. permissions_dir |= mode_group_execute;
  1242. permissions_dir |= mode_world_read;
  1243. permissions_dir |= mode_world_execute;
  1244. }
  1245. // Set the installer permissions.
  1246. installer.FilePermissions = permissions_file;
  1247. installer.DirPermissions = permissions_dir;
  1248. }
  1249. //----------------------------------------------------------------------------
  1250. void cmFileCommand
  1251. ::GetTargetTypeFromString(const std::string& stype, int& itype) const
  1252. {
  1253. if ( stype == "EXECUTABLE" )
  1254. {
  1255. itype = cmTarget::EXECUTABLE;
  1256. }
  1257. else if ( stype == "PROGRAM" )
  1258. {
  1259. itype = cmTarget::INSTALL_PROGRAMS;
  1260. }
  1261. else if ( stype == "STATIC_LIBRARY" )
  1262. {
  1263. itype = cmTarget::STATIC_LIBRARY;
  1264. }
  1265. else if ( stype == "SHARED_LIBRARY" )
  1266. {
  1267. itype = cmTarget::SHARED_LIBRARY;
  1268. }
  1269. else if ( stype == "MODULE" )
  1270. {
  1271. itype = cmTarget::MODULE_LIBRARY;
  1272. }
  1273. else if ( stype == "DIRECTORY" )
  1274. {
  1275. itype = cmTarget::INSTALL_DIRECTORY;
  1276. }
  1277. }
  1278. //----------------------------------------------------------------------------
  1279. bool cmFileCommand::HandleInstallDestination(cmFileInstaller& installer,
  1280. std::string& destination)
  1281. {
  1282. // allow for / to be a valid destination
  1283. if ( destination.size() < 2 && destination != "/" )
  1284. {
  1285. this->SetError("called with inapropriate arguments. "
  1286. "No DESTINATION provided or .");
  1287. return false;
  1288. }
  1289. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  1290. if ( destdir && *destdir )
  1291. {
  1292. std::string sdestdir = destdir;
  1293. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  1294. char ch1 = destination[0];
  1295. char ch2 = destination[1];
  1296. char ch3 = 0;
  1297. if ( destination.size() > 2 )
  1298. {
  1299. ch3 = destination[2];
  1300. }
  1301. int skip = 0;
  1302. if ( ch1 != '/' )
  1303. {
  1304. int relative = 0;
  1305. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  1306. ch2 == ':' )
  1307. {
  1308. // Assume windows
  1309. // let's do some destdir magic:
  1310. skip = 2;
  1311. if ( ch3 != '/' )
  1312. {
  1313. relative = 1;
  1314. }
  1315. }
  1316. else
  1317. {
  1318. relative = 1;
  1319. }
  1320. if ( relative )
  1321. {
  1322. // This is relative path on unix or windows. Since we are doing
  1323. // destdir, this case does not make sense.
  1324. this->SetError("called with relative DESTINATION. This "
  1325. "does not make sense when using DESTDIR. Specify "
  1326. "absolute path or remove DESTDIR environment variable.");
  1327. return false;
  1328. }
  1329. }
  1330. else
  1331. {
  1332. if ( ch2 == '/' )
  1333. {
  1334. // looks like a network path.
  1335. std::string message = "called with network path DESTINATION. This "
  1336. "does not make sense when using DESTDIR. Specify local "
  1337. "absolute path or remove DESTDIR environment variable."
  1338. "\nDESTINATION=\n";
  1339. message += destination;
  1340. this->SetError(message.c_str());
  1341. return false;
  1342. }
  1343. }
  1344. destination = sdestdir + (destination.c_str() + skip);
  1345. installer.DestDirLength = int(sdestdir.size());
  1346. }
  1347. if ( !cmSystemTools::FileExists(destination.c_str()) )
  1348. {
  1349. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  1350. {
  1351. std::string errstring = "cannot create directory: " + destination +
  1352. ". Maybe need administrative privileges.";
  1353. this->SetError(errstring.c_str());
  1354. return false;
  1355. }
  1356. }
  1357. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  1358. {
  1359. std::string errstring = "INSTALL destination: " + destination +
  1360. " is not a directory.";
  1361. this->SetError(errstring.c_str());
  1362. return false;
  1363. }
  1364. return true;
  1365. }
  1366. //----------------------------------------------------------------------------
  1367. bool
  1368. cmFileCommand::HandleRPathChangeCommand(std::vector<std::string> const& args)
  1369. {
  1370. // Evaluate arguments.
  1371. const char* file = 0;
  1372. const char* oldRPath = 0;
  1373. const char* newRPath = 0;
  1374. enum Doing { DoingNone, DoingFile, DoingOld, DoingNew };
  1375. Doing doing = DoingNone;
  1376. for(unsigned int i=1; i < args.size(); ++i)
  1377. {
  1378. if(args[i] == "OLD_RPATH")
  1379. {
  1380. doing = DoingOld;
  1381. }
  1382. else if(args[i] == "NEW_RPATH")
  1383. {
  1384. doing = DoingNew;
  1385. }
  1386. else if(args[i] == "FILE")
  1387. {
  1388. doing = DoingFile;
  1389. }
  1390. else if(doing == DoingFile)
  1391. {
  1392. file = args[i].c_str();
  1393. doing = DoingNone;
  1394. }
  1395. else if(doing == DoingOld)
  1396. {
  1397. oldRPath = args[i].c_str();
  1398. doing = DoingNone;
  1399. }
  1400. else if(doing == DoingNew)
  1401. {
  1402. newRPath = args[i].c_str();
  1403. doing = DoingNone;
  1404. }
  1405. else
  1406. {
  1407. cmOStringStream e;
  1408. e << "RPATH_CHANGE given unknown argument " << args[i];
  1409. this->SetError(e.str().c_str());
  1410. return false;
  1411. }
  1412. }
  1413. if(!file)
  1414. {
  1415. this->SetError("RPATH_CHANGE not given FILE option.");
  1416. return false;
  1417. }
  1418. if(!oldRPath)
  1419. {
  1420. this->SetError("RPATH_CHANGE not given OLD_RPATH option.");
  1421. return false;
  1422. }
  1423. if(!newRPath)
  1424. {
  1425. this->SetError("RPATH_CHANGE not given NEW_RPATH option.");
  1426. return false;
  1427. }
  1428. if(!cmSystemTools::FileExists(file, true))
  1429. {
  1430. cmOStringStream e;
  1431. e << "RPATH_CHANGE given FILE \"" << file << "\" that does not exist.";
  1432. this->SetError(e.str().c_str());
  1433. return false;
  1434. }
  1435. bool success = true;
  1436. cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew();
  1437. bool have_ft = cmSystemTools::FileTimeGet(file, ft);
  1438. std::string emsg;
  1439. bool changed;
  1440. if(!cmSystemTools::ChangeRPath(file, oldRPath, newRPath, &emsg, &changed))
  1441. {
  1442. cmOStringStream e;
  1443. e << "RPATH_CHANGE could not write new RPATH:\n"
  1444. << " " << newRPath << "\n"
  1445. << "to the file:\n"
  1446. << " " << file << "\n"
  1447. << emsg;
  1448. this->SetError(e.str().c_str());
  1449. success = false;
  1450. }
  1451. if(success)
  1452. {
  1453. if(changed)
  1454. {
  1455. std::string message = "Set runtime path of \"";
  1456. message += file;
  1457. message += "\" to \"";
  1458. message += newRPath;
  1459. message += "\"";
  1460. this->Makefile->DisplayStatus(message.c_str(), -1);
  1461. }
  1462. if(have_ft)
  1463. {
  1464. cmSystemTools::FileTimeSet(file, ft);
  1465. }
  1466. }
  1467. cmSystemTools::FileTimeDelete(ft);
  1468. return success;
  1469. }
  1470. //----------------------------------------------------------------------------
  1471. bool
  1472. cmFileCommand::HandleRPathRemoveCommand(std::vector<std::string> const& args)
  1473. {
  1474. // Evaluate arguments.
  1475. const char* file = 0;
  1476. enum Doing { DoingNone, DoingFile };
  1477. Doing doing = DoingNone;
  1478. for(unsigned int i=1; i < args.size(); ++i)
  1479. {
  1480. if(args[i] == "FILE")
  1481. {
  1482. doing = DoingFile;
  1483. }
  1484. else if(doing == DoingFile)
  1485. {
  1486. file = args[i].c_str();
  1487. doing = DoingNone;
  1488. }
  1489. else
  1490. {
  1491. cmOStringStream e;
  1492. e << "RPATH_REMOVE given unknown argument " << args[i];
  1493. this->SetError(e.str().c_str());
  1494. return false;
  1495. }
  1496. }
  1497. if(!file)
  1498. {
  1499. this->SetError("RPATH_REMOVE not given FILE option.");
  1500. return false;
  1501. }
  1502. if(!cmSystemTools::FileExists(file, true))
  1503. {
  1504. cmOStringStream e;
  1505. e << "RPATH_REMOVE given FILE \"" << file << "\" that does not exist.";
  1506. this->SetError(e.str().c_str());
  1507. return false;
  1508. }
  1509. bool success = true;
  1510. cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew();
  1511. bool have_ft = cmSystemTools::FileTimeGet(file, ft);
  1512. std::string emsg;
  1513. bool removed;
  1514. if(!cmSystemTools::RemoveRPath(file, &emsg, &removed))
  1515. {
  1516. cmOStringStream e;
  1517. e << "RPATH_REMOVE could not remove RPATH from file:\n"
  1518. << " " << file << "\n"
  1519. << emsg;
  1520. this->SetError(e.str().c_str());
  1521. success = false;
  1522. }
  1523. if(success)
  1524. {
  1525. if(removed)
  1526. {
  1527. std::string message = "Removed runtime path from \"";
  1528. message += file;
  1529. message += "\"";
  1530. this->Makefile->DisplayStatus(message.c_str(), -1);
  1531. }
  1532. if(have_ft)
  1533. {
  1534. cmSystemTools::FileTimeSet(file, ft);
  1535. }
  1536. }
  1537. cmSystemTools::FileTimeDelete(ft);
  1538. return success;
  1539. }
  1540. //----------------------------------------------------------------------------
  1541. bool
  1542. cmFileCommand::HandleRPathCheckCommand(std::vector<std::string> const& args)
  1543. {
  1544. // Evaluate arguments.
  1545. const char* file = 0;
  1546. const char* rpath = 0;
  1547. enum Doing { DoingNone, DoingFile, DoingRPath };
  1548. Doing doing = DoingNone;
  1549. for(unsigned int i=1; i < args.size(); ++i)
  1550. {
  1551. if(args[i] == "RPATH")
  1552. {
  1553. doing = DoingRPath;
  1554. }
  1555. else if(args[i] == "FILE")
  1556. {
  1557. doing = DoingFile;
  1558. }
  1559. else if(doing == DoingFile)
  1560. {
  1561. file = args[i].c_str();
  1562. doing = DoingNone;
  1563. }
  1564. else if(doing == DoingRPath)
  1565. {
  1566. rpath = args[i].c_str();
  1567. doing = DoingNone;
  1568. }
  1569. else
  1570. {
  1571. cmOStringStream e;
  1572. e << "RPATH_CHECK given unknown argument " << args[i];
  1573. this->SetError(e.str().c_str());
  1574. return false;
  1575. }
  1576. }
  1577. if(!file)
  1578. {
  1579. this->SetError("RPATH_CHECK not given FILE option.");
  1580. return false;
  1581. }
  1582. if(!rpath)
  1583. {
  1584. this->SetError("RPATH_CHECK not given RPATH option.");
  1585. return false;
  1586. }
  1587. // If the file exists but does not have the desired RPath then
  1588. // delete it. This is used during installation to re-install a file
  1589. // if its RPath will change.
  1590. if(cmSystemTools::FileExists(file, true) &&
  1591. !cmSystemTools::CheckRPath(file, rpath))
  1592. {
  1593. cmSystemTools::RemoveFile(file);
  1594. }
  1595. return true;
  1596. }
  1597. //----------------------------------------------------------------------------
  1598. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  1599. {
  1600. if ( args.size() < 6 )
  1601. {
  1602. this->SetError("called with incorrect number of arguments");
  1603. return false;
  1604. }
  1605. // Construct a file installer object.
  1606. cmFileInstaller installer(this);
  1607. std::string rename = "";
  1608. std::string destination = "";
  1609. std::vector<std::string> files;
  1610. int itype = cmTarget::INSTALL_FILES;
  1611. bool optional = false;
  1612. bool result = this->ParseInstallArgs(args, installer,
  1613. itype, rename, destination, files,
  1614. optional);
  1615. if (result == true)
  1616. {
  1617. result = this->DoInstall(installer,
  1618. itype, rename, destination, files, optional);
  1619. }
  1620. return result;
  1621. }
  1622. //----------------------------------------------------------------------------
  1623. bool cmFileCommand::ParseInstallArgs(std::vector<std::string> const& args,
  1624. cmFileInstaller& installer,
  1625. int& itype,
  1626. std::string& rename,
  1627. std::string& destination,
  1628. std::vector<std::string>& files,
  1629. bool& optional)
  1630. {
  1631. std::string stype = "FILES";
  1632. enum Doing { DoingNone, DoingFiles,
  1633. DoingPermissionsFile, DoingPermissionsDir,
  1634. DoingPermissionsMatch, DoingSelf24 };
  1635. Doing doing = DoingNone;
  1636. bool use_given_permissions_file = false;
  1637. bool use_given_permissions_dir = false;
  1638. bool use_source_permissions = false;
  1639. mode_t permissions_file = 0;
  1640. mode_t permissions_dir = 0;
  1641. cmFileInstaller::MatchRule* current_match_rule = 0;
  1642. std::vector<std::string>::size_type i = 0;
  1643. i++; // Get rid of subcommand
  1644. for ( ; i != args.size(); ++i )
  1645. {
  1646. const std::string* cstr = &args[i];
  1647. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  1648. {
  1649. if(current_match_rule)
  1650. {
  1651. cmOStringStream e;
  1652. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1653. this->SetError(e.str().c_str());
  1654. return false;
  1655. }
  1656. i++;
  1657. destination = args[i];
  1658. doing = DoingNone;
  1659. }
  1660. else if ( *cstr == "TYPE" && i < args.size()-1 )
  1661. {
  1662. if(current_match_rule)
  1663. {
  1664. cmOStringStream e;
  1665. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1666. this->SetError(e.str().c_str());
  1667. return false;
  1668. }
  1669. i++;
  1670. stype = args[i];
  1671. if ( args[i+1] == "OPTIONAL" )
  1672. {
  1673. i++;
  1674. optional = true;
  1675. }
  1676. doing = DoingNone;
  1677. }
  1678. else if ( *cstr == "RENAME" && i < args.size()-1 )
  1679. {
  1680. if(current_match_rule)
  1681. {
  1682. cmOStringStream e;
  1683. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1684. this->SetError(e.str().c_str());
  1685. return false;
  1686. }
  1687. i++;
  1688. rename = args[i];
  1689. doing = DoingNone;
  1690. }
  1691. else if ( *cstr == "REGEX" && i < args.size()-1 )
  1692. {
  1693. i++;
  1694. installer.MatchRules.push_back(cmFileInstaller::MatchRule(args[i]));
  1695. current_match_rule = &*(installer.MatchRules.end()-1);
  1696. if(!current_match_rule->Regex.is_valid())
  1697. {
  1698. cmOStringStream e;
  1699. e << "INSTALL could not compile REGEX \"" << args[i] << "\".";
  1700. this->SetError(e.str().c_str());
  1701. return false;
  1702. }
  1703. doing = DoingNone;
  1704. }
  1705. else if ( *cstr == "EXCLUDE" )
  1706. {
  1707. // Add this property to the current match rule.
  1708. if(!current_match_rule)
  1709. {
  1710. cmOStringStream e;
  1711. e << "INSTALL does not allow \""
  1712. << *cstr << "\" before a REGEX is given.";
  1713. this->SetError(e.str().c_str());
  1714. return false;
  1715. }
  1716. current_match_rule->Properties.Exclude = true;
  1717. doing = DoingPermissionsMatch;
  1718. }
  1719. else if ( *cstr == "PROPERTIES" )
  1720. {
  1721. cmOStringStream e;
  1722. e << "INSTALL called with old-style PROPERTIES argument. "
  1723. << "This script was generated with an older version of CMake. "
  1724. << "Re-run this cmake version on your build tree.";
  1725. this->SetError(e.str().c_str());
  1726. return false;
  1727. }
  1728. else if ( *cstr == "PERMISSIONS" )
  1729. {
  1730. if(current_match_rule)
  1731. {
  1732. doing = DoingPermissionsMatch;
  1733. }
  1734. else
  1735. {
  1736. doing = DoingPermissionsFile;
  1737. use_given_permissions_file = true;
  1738. }
  1739. }
  1740. else if ( *cstr == "DIR_PERMISSIONS" )
  1741. {
  1742. if(current_match_rule)
  1743. {
  1744. cmOStringStream e;
  1745. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1746. this->SetError(e.str().c_str());
  1747. return false;
  1748. }
  1749. use_given_permissions_dir = true;
  1750. doing = DoingPermissionsDir;
  1751. }
  1752. else if ( *cstr == "USE_SOURCE_PERMISSIONS" )
  1753. {
  1754. if(current_match_rule)
  1755. {
  1756. cmOStringStream e;
  1757. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1758. this->SetError(e.str().c_str());
  1759. return false;
  1760. }
  1761. doing = DoingNone;
  1762. use_source_permissions = true;
  1763. }
  1764. else if ( *cstr == "FILES_MATCHING" )
  1765. {
  1766. if(current_match_rule)
  1767. {
  1768. cmOStringStream e;
  1769. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1770. this->SetError(e.str().c_str());
  1771. return false;
  1772. }
  1773. doing = DoingNone;
  1774. installer.MatchlessFiles = false;
  1775. }
  1776. else if ( *cstr == "COMPONENTS" )
  1777. {
  1778. if(this->Makefile->IsOn("CMAKE_INSTALL_SELF_2_4"))
  1779. {
  1780. // When CMake 2.4 builds this CMake version we need to support
  1781. // the install scripts it generates since it asks this CMake
  1782. // to install itself using the rules it generated.
  1783. doing = DoingSelf24;
  1784. continue;
  1785. }
  1786. cmOStringStream e;
  1787. e << "INSTALL called with old-style COMPONENTS argument. "
  1788. << "This script was generated with an older version of CMake. "
  1789. << "Re-run this cmake version on your build tree.";
  1790. this->SetError(e.str().c_str());
  1791. return false;
  1792. }
  1793. else if ( *cstr == "CONFIGURATIONS" )
  1794. {
  1795. cmOStringStream e;
  1796. e << "INSTALL called with old-style CONFIGURATIONS argument. "
  1797. << "This script was generated with an older version of CMake. "
  1798. << "Re-run this cmake version on your build tree.";
  1799. this->SetError(e.str().c_str());
  1800. return false;
  1801. }
  1802. else if(*cstr == "FILES" && doing != DoingFiles)
  1803. {
  1804. if(current_match_rule)
  1805. {
  1806. cmOStringStream e;
  1807. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1808. this->SetError(e.str().c_str());
  1809. return false;
  1810. }
  1811. doing = DoingFiles;
  1812. }
  1813. else if(doing == DoingFiles)
  1814. {
  1815. files.push_back(*cstr);
  1816. }
  1817. else if(doing == DoingPermissionsFile)
  1818. {
  1819. if(!installer.CheckPermissions(args[i], permissions_file))
  1820. {
  1821. return false;
  1822. }
  1823. }
  1824. else if(doing == DoingPermissionsDir)
  1825. {
  1826. if(!installer.CheckPermissions(args[i], permissions_dir))
  1827. {
  1828. return false;
  1829. }
  1830. }
  1831. else if(doing == DoingPermissionsMatch)
  1832. {
  1833. if(!installer.CheckPermissions(
  1834. args[i], current_match_rule->Properties.Permissions))
  1835. {
  1836. return false;
  1837. }
  1838. }
  1839. else if(doing == DoingSelf24)
  1840. {
  1841. // Ignore these arguments for compatibility. This should be
  1842. // reached only when CMake 2.4 is installing the current
  1843. // CMake. It can be removed when CMake 2.6 or higher is
  1844. // required to build CMake.
  1845. }
  1846. else
  1847. {
  1848. this->SetError("called with inappropriate arguments");
  1849. return false;
  1850. }
  1851. }
  1852. // now check and postprocess what has been parsed
  1853. if ( files.size() == 0 )
  1854. {
  1855. // nothing to do, no files were listed.
  1856. // if this is handled as error, INSTALL_FILES() creates an invalid
  1857. // cmake_install.cmake script with no FILES() arguments if no files were
  1858. // given to INSTALL_FILES(). This was accepted with CMake 2.4.x.
  1859. return true;
  1860. }
  1861. // Check rename form.
  1862. if(!rename.empty())
  1863. {
  1864. if(itype != cmTarget::INSTALL_FILES &&
  1865. itype != cmTarget::INSTALL_PROGRAMS)
  1866. {
  1867. this->SetError("INSTALL option RENAME may be used only with "
  1868. "FILES or PROGRAMS.");
  1869. return false;
  1870. }
  1871. if(files.size() > 1)
  1872. {
  1873. this->SetError("INSTALL option RENAME may be used only with "
  1874. "one file.");
  1875. return false;
  1876. }
  1877. }
  1878. if (this->HandleInstallDestination(installer, destination) == false)
  1879. {
  1880. return false;
  1881. }
  1882. this->GetTargetTypeFromString(stype, itype);
  1883. this->HandleInstallPermissions(installer,
  1884. permissions_file,
  1885. permissions_dir,
  1886. itype,
  1887. use_given_permissions_file,
  1888. use_given_permissions_dir,
  1889. use_source_permissions);
  1890. return true;
  1891. }
  1892. //----------------------------------------------------------------------------
  1893. bool cmFileCommand::DoInstall( cmFileInstaller& installer,
  1894. const int itype,
  1895. const std::string& rename,
  1896. const std::string& destination,
  1897. const std::vector<std::string>& files,
  1898. const bool optional)
  1899. {
  1900. typedef std::set<cmStdString>::const_iterator iter_type;
  1901. // Handle each file listed.
  1902. for (std::vector<std::string>::size_type i = 0; i < files.size(); i ++ )
  1903. {
  1904. // Split the input file into its directory and name components.
  1905. std::vector<std::string> fromPathComponents;
  1906. cmSystemTools::SplitPath(files[i].c_str(), fromPathComponents);
  1907. std::string fromName = *(fromPathComponents.end()-1);
  1908. std::string fromDir = cmSystemTools::JoinPath(fromPathComponents.begin(),
  1909. fromPathComponents.end()-1);
  1910. // Compute the full path to the destination file.
  1911. std::string toFile = destination;
  1912. std::string const& toName = rename.empty()? fromName : rename;
  1913. if(!toName.empty())
  1914. {
  1915. toFile += "/";
  1916. toFile += toName;
  1917. }
  1918. // Construct the full path to the source file. The file name may
  1919. // have been changed above.
  1920. std::string fromFile = fromDir;
  1921. if(!fromName.empty())
  1922. {
  1923. fromFile += "/";
  1924. fromFile += fromName;
  1925. }
  1926. std::string message;
  1927. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  1928. {
  1929. if(itype == cmTarget::INSTALL_DIRECTORY &&
  1930. (fromFile.empty() ||
  1931. cmSystemTools::FileIsDirectory(fromFile.c_str())))
  1932. {
  1933. // Try installing this directory.
  1934. if(!installer.InstallDirectory(fromFile.c_str(), toFile.c_str()))
  1935. {
  1936. return false;
  1937. }
  1938. }
  1939. else if(cmSystemTools::FileExists(fromFile.c_str()))
  1940. {
  1941. // Install this file.
  1942. if(!installer.InstallFile(fromFile.c_str(), toFile.c_str()))
  1943. {
  1944. return false;
  1945. }
  1946. }
  1947. else if(!optional)
  1948. {
  1949. // The input file does not exist and installation is not optional.
  1950. cmOStringStream e;
  1951. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  1952. this->SetError(e.str().c_str());
  1953. return false;
  1954. }
  1955. }
  1956. }
  1957. return true;
  1958. }
  1959. //----------------------------------------------------------------------------
  1960. bool cmFileCommand::HandleRelativePathCommand(
  1961. std::vector<std::string> const& args)
  1962. {
  1963. if(args.size() != 4 )
  1964. {
  1965. this->SetError("called with incorrect number of arguments");
  1966. return false;
  1967. }
  1968. const std::string& outVar = args[1];
  1969. const std::string& directoryName = args[2];
  1970. const std::string& fileName = args[3];
  1971. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  1972. {
  1973. std::string errstring =
  1974. "RelativePath must be passed a full path to the directory: "
  1975. + directoryName;
  1976. this->SetError(errstring.c_str());
  1977. return false;
  1978. }
  1979. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  1980. {
  1981. std::string errstring =
  1982. "RelativePath must be passed a full path to the file: "
  1983. + fileName;
  1984. this->SetError(errstring.c_str());
  1985. return false;
  1986. }
  1987. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  1988. fileName.c_str());
  1989. this->Makefile->AddDefinition(outVar.c_str(),
  1990. res.c_str());
  1991. return true;
  1992. }
  1993. //----------------------------------------------------------------------------
  1994. bool cmFileCommand::HandleRename(std::vector<std::string> const& args)
  1995. {
  1996. if(args.size() != 3)
  1997. {
  1998. this->SetError("given incorrect number of arguments.");
  1999. return false;
  2000. }
  2001. // Compute full path for old and new names.
  2002. std::string oldname = args[1];
  2003. if(!cmsys::SystemTools::FileIsFullPath(oldname.c_str()))
  2004. {
  2005. oldname = this->Makefile->GetCurrentDirectory();
  2006. oldname += "/" + args[1];
  2007. }
  2008. std::string newname = args[2];
  2009. if(!cmsys::SystemTools::FileIsFullPath(newname.c_str()))
  2010. {
  2011. newname = this->Makefile->GetCurrentDirectory();
  2012. newname += "/" + args[2];
  2013. }
  2014. if(!cmSystemTools::RenameFile(oldname.c_str(), newname.c_str()))
  2015. {
  2016. std::string err = cmSystemTools::GetLastSystemError();
  2017. cmOStringStream e;
  2018. e << "RENAME failed to rename\n"
  2019. << " " << oldname << "\n"
  2020. << "to\n"
  2021. << " " << newname << "\n"
  2022. << "because: " << err << "\n";
  2023. this->SetError(e.str().c_str());
  2024. return false;
  2025. }
  2026. return true;
  2027. }
  2028. //----------------------------------------------------------------------------
  2029. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  2030. bool recurse)
  2031. {
  2032. std::string message;
  2033. std::vector<std::string>::const_iterator i = args.begin();
  2034. i++; // Get rid of subcommand
  2035. for(;i != args.end(); ++i)
  2036. {
  2037. std::string fileName = *i;
  2038. if(!cmsys::SystemTools::FileIsFullPath(fileName.c_str()))
  2039. {
  2040. fileName = this->Makefile->GetCurrentDirectory();
  2041. fileName += "/" + *i;
  2042. }
  2043. if(cmSystemTools::FileIsDirectory(fileName.c_str()) && recurse)
  2044. {
  2045. cmSystemTools::RemoveADirectory(fileName.c_str());
  2046. }
  2047. else
  2048. {
  2049. cmSystemTools::RemoveFile(fileName.c_str());
  2050. }
  2051. }
  2052. return true;
  2053. }
  2054. //----------------------------------------------------------------------------
  2055. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  2056. const& args,
  2057. bool nativePath)
  2058. {
  2059. std::vector<std::string>::const_iterator i = args.begin();
  2060. if(args.size() != 3)
  2061. {
  2062. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  2063. "only three arguments.");
  2064. return false;
  2065. }
  2066. i++; // Get rid of subcommand
  2067. #if defined(_WIN32) && !defined(__CYGWIN__)
  2068. char pathSep = ';';
  2069. #else
  2070. char pathSep = ':';
  2071. #endif
  2072. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  2073. pathSep);
  2074. i++;
  2075. const char* var = i->c_str();
  2076. std::string value;
  2077. for(std::vector<cmsys::String>::iterator j = path.begin();
  2078. j != path.end(); ++j)
  2079. {
  2080. if(j != path.begin())
  2081. {
  2082. value += ";";
  2083. }
  2084. if(!nativePath)
  2085. {
  2086. cmSystemTools::ConvertToUnixSlashes(*j);
  2087. }
  2088. else
  2089. {
  2090. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  2091. // remove double quotes in the path
  2092. cmsys::String& s = *j;
  2093. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  2094. {
  2095. s = s.substr(1,s.size()-2);
  2096. }
  2097. }
  2098. value += *j;
  2099. }
  2100. this->Makefile->AddDefinition(var, value.c_str());
  2101. return true;
  2102. }
  2103. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2104. // Stuff for curl download
  2105. typedef std::vector<char> cmFileCommandVectorOfChar;
  2106. namespace{
  2107. size_t
  2108. cmFileCommandWriteMemoryCallback(void *ptr, size_t size, size_t nmemb,
  2109. void *data)
  2110. {
  2111. register int realsize = (int)(size * nmemb);
  2112. std::ofstream* fout = static_cast<std::ofstream*>(data);
  2113. const char* chPtr = static_cast<char*>(ptr);
  2114. fout->write(chPtr, realsize);
  2115. return realsize;
  2116. }
  2117. static size_t
  2118. cmFileCommandCurlDebugCallback(CURL *, curl_infotype, char *chPtr,
  2119. size_t size, void *data)
  2120. {
  2121. cmFileCommandVectorOfChar *vec
  2122. = static_cast<cmFileCommandVectorOfChar*>(data);
  2123. vec->insert(vec->end(), chPtr, chPtr + size);
  2124. return size;
  2125. }
  2126. }
  2127. #endif
  2128. bool
  2129. cmFileCommand::HandleDownloadCommand(std::vector<std::string>
  2130. const& args)
  2131. {
  2132. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2133. std::vector<std::string>::const_iterator i = args.begin();
  2134. if(args.size() < 3)
  2135. {
  2136. this->SetError("FILE(DOWNLOAD url file) must be called with "
  2137. "at least three arguments.");
  2138. return false;
  2139. }
  2140. i++; // Get rid of subcommand
  2141. std::string url = *i;
  2142. i++;
  2143. std::string file = *i;
  2144. i++;
  2145. double timeout = 0;
  2146. std::string verboseLog;
  2147. std::string statusVar;
  2148. while(i != args.end())
  2149. {
  2150. if(*i == "TIMEOUT")
  2151. {
  2152. i++;
  2153. if(i != args.end())
  2154. {
  2155. timeout = atof(i->c_str());
  2156. }
  2157. else
  2158. {
  2159. this->SetError("FILE(DOWNLOAD url file TIMEOUT time) missing "
  2160. "time for TIMEOUT.");
  2161. return false;
  2162. }
  2163. }
  2164. else if(*i == "LOG")
  2165. {
  2166. i++;
  2167. if( i == args.end())
  2168. {
  2169. this->SetError("FILE(DOWNLOAD url file LOG VAR) missing "
  2170. "VAR for LOG.");
  2171. return false;
  2172. }
  2173. verboseLog = *i;
  2174. }
  2175. else if(*i == "STATUS")
  2176. {
  2177. i++;
  2178. if( i == args.end())
  2179. {
  2180. this->SetError("FILE(DOWNLOAD url file STATUS VAR) missing "
  2181. "VAR for STATUS.");
  2182. return false;
  2183. }
  2184. statusVar = *i;
  2185. }
  2186. i++;
  2187. }
  2188. std::string dir = cmSystemTools::GetFilenamePath(file.c_str());
  2189. if(!cmSystemTools::FileExists(dir.c_str()) &&
  2190. !cmSystemTools::MakeDirectory(dir.c_str()))
  2191. {
  2192. std::string errstring = "FILE(DOWNLOAD ) error; cannot create directory: "
  2193. + dir + ". Maybe need administrative privileges.";
  2194. this->SetError(errstring.c_str());
  2195. return false;
  2196. }
  2197. std::ofstream fout(file.c_str(), std::ios::binary);
  2198. if(!fout)
  2199. {
  2200. this->SetError("FILE(DOWNLOAD url file TIMEOUT time) can not open "
  2201. "file for write.");
  2202. return false;
  2203. }
  2204. CURL *curl;
  2205. curl_global_init(CURL_GLOBAL_DEFAULT);
  2206. curl = curl_easy_init();
  2207. if(!curl)
  2208. {
  2209. this->SetError("FILE(DOWNLOAD ) error "
  2210. "initializing curl.");
  2211. return false;
  2212. }
  2213. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  2214. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  2215. cmFileCommandWriteMemoryCallback);
  2216. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  2217. cmFileCommandCurlDebugCallback);
  2218. cmFileCommandVectorOfChar chunkDebug;
  2219. ::curl_easy_setopt(curl, CURLOPT_FILE, (void *)&fout);
  2220. ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *)&chunkDebug);
  2221. if(verboseLog.size())
  2222. {
  2223. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  2224. }
  2225. if(timeout > 0)
  2226. {
  2227. curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout );
  2228. }
  2229. CURLcode res = curl_easy_perform(curl);
  2230. /* always cleanup */
  2231. curl_easy_cleanup(curl);
  2232. if(statusVar.size())
  2233. {
  2234. cmOStringStream result;
  2235. result << (int)res << ";\"" << curl_easy_strerror(res) << "\"";
  2236. this->Makefile->AddDefinition(statusVar.c_str(),
  2237. result.str().c_str());
  2238. }
  2239. curl_global_cleanup();
  2240. if(chunkDebug.size())
  2241. {
  2242. chunkDebug.push_back(0);
  2243. if(CURLE_OPERATION_TIMEOUTED == res)
  2244. {
  2245. std::string output = &*chunkDebug.begin();
  2246. if(verboseLog.size())
  2247. {
  2248. this->Makefile->AddDefinition(verboseLog.c_str(),
  2249. &*chunkDebug.begin());
  2250. }
  2251. }
  2252. this->Makefile->AddDefinition(verboseLog.c_str(),
  2253. &*chunkDebug.begin());
  2254. }
  2255. return true;
  2256. #else
  2257. this->SetError("FILE(DOWNLOAD ) "
  2258. "not supported in bootstrap cmake ");
  2259. return false;
  2260. #endif
  2261. }