cmFileCommand.cxx 60 KB

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