cmFileCommand.cxx 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  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. // allow for / to be a valid destination
  1149. if ( destination.size() < 2 && destination != "/" )
  1150. {
  1151. this->SetError("called with inapropriate arguments. "
  1152. "No DESTINATION provided or .");
  1153. return false;
  1154. }
  1155. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  1156. if ( destdir && *destdir )
  1157. {
  1158. std::string sdestdir = destdir;
  1159. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  1160. char ch1 = destination[0];
  1161. char ch2 = destination[1];
  1162. char ch3 = 0;
  1163. if ( destination.size() > 2 )
  1164. {
  1165. ch3 = destination[2];
  1166. }
  1167. int skip = 0;
  1168. if ( ch1 != '/' )
  1169. {
  1170. int relative = 0;
  1171. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  1172. ch2 == ':' )
  1173. {
  1174. // Assume windows
  1175. // let's do some destdir magic:
  1176. skip = 2;
  1177. if ( ch3 != '/' )
  1178. {
  1179. relative = 1;
  1180. }
  1181. }
  1182. else
  1183. {
  1184. relative = 1;
  1185. }
  1186. if ( relative )
  1187. {
  1188. // This is relative path on unix or windows. Since we are doing
  1189. // destdir, this case does not make sense.
  1190. this->SetError("called with relative DESTINATION. This "
  1191. "does not make sense when using DESTDIR. Specify "
  1192. "absolute path or remove DESTDIR environment variable.");
  1193. return false;
  1194. }
  1195. }
  1196. else
  1197. {
  1198. if ( ch2 == '/' )
  1199. {
  1200. // looks like a network path.
  1201. std::string message = "called with network path DESTINATION. This "
  1202. "does not make sense when using DESTDIR. Specify local "
  1203. "absolute path or remove DESTDIR environment variable."
  1204. "\nDESTINATION=\n";
  1205. message += destination;
  1206. this->SetError(message.c_str());
  1207. return false;
  1208. }
  1209. }
  1210. destination = sdestdir + (destination.c_str() + skip);
  1211. installer.DestDirLength = int(sdestdir.size());
  1212. }
  1213. if ( !cmSystemTools::FileExists(destination.c_str()) )
  1214. {
  1215. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  1216. {
  1217. std::string errstring = "cannot create directory: " + destination +
  1218. ". Maybe need administrative privileges.";
  1219. this->SetError(errstring.c_str());
  1220. return false;
  1221. }
  1222. }
  1223. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  1224. {
  1225. std::string errstring = "INSTALL destination: " + destination +
  1226. " is not a directory.";
  1227. this->SetError(errstring.c_str());
  1228. return false;
  1229. }
  1230. return true;
  1231. }
  1232. //----------------------------------------------------------------------------
  1233. bool cmFileCommand::HandleChrpathCommand(std::vector<std::string> const& args)
  1234. {
  1235. // Evaluate arguments.
  1236. const char* file = 0;
  1237. const char* oldRPath = 0;
  1238. const char* newRPath = 0;
  1239. enum Doing { DoingNone, DoingFile, DoingOld, DoingNew };
  1240. Doing doing = DoingNone;
  1241. for(unsigned int i=1; i < args.size(); ++i)
  1242. {
  1243. if(args[i] == "OLD_RPATH")
  1244. {
  1245. doing = DoingOld;
  1246. }
  1247. else if(args[i] == "NEW_RPATH")
  1248. {
  1249. doing = DoingNew;
  1250. }
  1251. else if(args[i] == "FILE")
  1252. {
  1253. doing = DoingFile;
  1254. }
  1255. else if(doing == DoingFile)
  1256. {
  1257. file = args[i].c_str();
  1258. doing = DoingNone;
  1259. }
  1260. else if(doing == DoingOld)
  1261. {
  1262. oldRPath = args[i].c_str();
  1263. doing = DoingNone;
  1264. }
  1265. else if(doing == DoingNew)
  1266. {
  1267. newRPath = args[i].c_str();
  1268. doing = DoingNone;
  1269. }
  1270. else
  1271. {
  1272. cmOStringStream e;
  1273. e << "CHRPATH given unknown argument " << args[i];
  1274. this->SetError(e.str().c_str());
  1275. return false;
  1276. }
  1277. }
  1278. if(!file)
  1279. {
  1280. this->SetError("CHRPATH not given FILE option.");
  1281. return false;
  1282. }
  1283. if(!oldRPath)
  1284. {
  1285. this->SetError("CHRPATH not given OLD_RPATH option.");
  1286. return false;
  1287. }
  1288. if(!newRPath)
  1289. {
  1290. this->SetError("CHRPATH not given NEW_RPATH option.");
  1291. return false;
  1292. }
  1293. if(!cmSystemTools::FileExists(file, true))
  1294. {
  1295. cmOStringStream e;
  1296. e << "CHRPATH given FILE \"" << file << "\" that does not exist.";
  1297. this->SetError(e.str().c_str());
  1298. return false;
  1299. }
  1300. std::string emsg;
  1301. if(cmSystemTools::ChangeRPath(file, oldRPath, newRPath, &emsg))
  1302. {
  1303. return true;
  1304. }
  1305. else
  1306. {
  1307. cmOStringStream e;
  1308. e << "CHRPATH could not write new RPATH:\n"
  1309. << " " << newRPath << "\n"
  1310. << "to the file:\n"
  1311. << " " << file << "\n"
  1312. << emsg;
  1313. this->SetError(e.str().c_str());
  1314. return false;
  1315. }
  1316. }
  1317. //----------------------------------------------------------------------------
  1318. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  1319. {
  1320. if ( args.size() < 6 )
  1321. {
  1322. this->SetError("called with incorrect number of arguments");
  1323. return false;
  1324. }
  1325. // Construct a file installer object.
  1326. cmFileInstaller installer(this, this->Makefile);
  1327. std::string rename = "";
  1328. std::string destination = "";
  1329. std::vector<std::string> files;
  1330. int itype = cmTarget::INSTALL_FILES;
  1331. std::map<cmStdString, const char*> properties;
  1332. bool optional = false;
  1333. bool result = this->ParseInstallArgs(args, installer, properties,
  1334. itype, rename, destination, files,
  1335. optional);
  1336. if (result == true)
  1337. {
  1338. result = this->DoInstall(installer,
  1339. itype, rename, destination, files, optional);
  1340. }
  1341. return result;
  1342. }
  1343. //----------------------------------------------------------------------------
  1344. bool cmFileCommand::ParseInstallArgs(std::vector<std::string> const& args,
  1345. cmFileInstaller& installer,
  1346. std::map<cmStdString, const char*>& properties,
  1347. int& itype,
  1348. std::string& rename,
  1349. std::string& destination,
  1350. std::vector<std::string>& files,
  1351. bool& optional)
  1352. {
  1353. std::string stype = "FILES";
  1354. bool doing_files = false;
  1355. bool doing_properties = false;
  1356. bool doing_permissions_file = false;
  1357. bool doing_permissions_dir = false;
  1358. bool doing_permissions_match = false;
  1359. bool use_given_permissions_file = false;
  1360. bool use_given_permissions_dir = false;
  1361. bool use_source_permissions = false;
  1362. mode_t permissions_file = 0;
  1363. mode_t permissions_dir = 0;
  1364. cmFileInstaller::MatchRule* current_match_rule = 0;
  1365. std::vector<std::string>::size_type i = 0;
  1366. i++; // Get rid of subcommand
  1367. for ( ; i != args.size(); ++i )
  1368. {
  1369. const std::string* cstr = &args[i];
  1370. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  1371. {
  1372. if(current_match_rule)
  1373. {
  1374. cmOStringStream e;
  1375. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1376. this->SetError(e.str().c_str());
  1377. return false;
  1378. }
  1379. i++;
  1380. destination = args[i];
  1381. doing_files = false;
  1382. doing_properties = false;
  1383. doing_permissions_file = false;
  1384. doing_permissions_dir = false;
  1385. }
  1386. else if ( *cstr == "TYPE" && i < args.size()-1 )
  1387. {
  1388. if(current_match_rule)
  1389. {
  1390. cmOStringStream e;
  1391. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1392. this->SetError(e.str().c_str());
  1393. return false;
  1394. }
  1395. i++;
  1396. stype = args[i];
  1397. if ( args[i+1] == "OPTIONAL" )
  1398. {
  1399. i++;
  1400. optional = true;
  1401. }
  1402. doing_properties = false;
  1403. doing_files = false;
  1404. doing_permissions_file = false;
  1405. doing_permissions_dir = false;
  1406. }
  1407. else if ( *cstr == "RENAME" && i < args.size()-1 )
  1408. {
  1409. if(current_match_rule)
  1410. {
  1411. cmOStringStream e;
  1412. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1413. this->SetError(e.str().c_str());
  1414. return false;
  1415. }
  1416. i++;
  1417. rename = args[i];
  1418. doing_properties = false;
  1419. doing_files = false;
  1420. doing_permissions_file = false;
  1421. doing_permissions_dir = false;
  1422. }
  1423. else if ( *cstr == "REGEX" && i < args.size()-1 )
  1424. {
  1425. i++;
  1426. installer.MatchRules.push_back(cmFileInstaller::MatchRule(args[i]));
  1427. current_match_rule = &*(installer.MatchRules.end()-1);
  1428. if(!current_match_rule->Regex.is_valid())
  1429. {
  1430. cmOStringStream e;
  1431. e << "INSTALL could not compile REGEX \"" << args[i] << "\".";
  1432. this->SetError(e.str().c_str());
  1433. return false;
  1434. }
  1435. doing_properties = false;
  1436. doing_files = false;
  1437. doing_permissions_file = false;
  1438. doing_permissions_dir = false;
  1439. }
  1440. else if ( *cstr == "EXCLUDE" )
  1441. {
  1442. // Add this property to the current match rule.
  1443. if(!current_match_rule)
  1444. {
  1445. cmOStringStream e;
  1446. e << "INSTALL does not allow \""
  1447. << *cstr << "\" before a REGEX is given.";
  1448. this->SetError(e.str().c_str());
  1449. return false;
  1450. }
  1451. current_match_rule->Properties.Exclude = true;
  1452. doing_permissions_match = true;
  1453. }
  1454. else if ( *cstr == "PROPERTIES" )
  1455. {
  1456. if(current_match_rule)
  1457. {
  1458. cmOStringStream e;
  1459. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1460. this->SetError(e.str().c_str());
  1461. return false;
  1462. }
  1463. doing_properties = true;
  1464. doing_files = false;
  1465. doing_permissions_file = false;
  1466. doing_permissions_dir = false;
  1467. }
  1468. else if ( *cstr == "PERMISSIONS" )
  1469. {
  1470. if(current_match_rule)
  1471. {
  1472. doing_permissions_match = true;
  1473. doing_permissions_file = false;
  1474. }
  1475. else
  1476. {
  1477. doing_permissions_match = false;
  1478. doing_permissions_file = true;
  1479. use_given_permissions_file = true;
  1480. }
  1481. doing_properties = false;
  1482. doing_files = false;
  1483. doing_permissions_dir = false;
  1484. }
  1485. else if ( *cstr == "DIR_PERMISSIONS" )
  1486. {
  1487. if(current_match_rule)
  1488. {
  1489. cmOStringStream e;
  1490. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1491. this->SetError(e.str().c_str());
  1492. return false;
  1493. }
  1494. use_given_permissions_dir = true;
  1495. doing_properties = false;
  1496. doing_files = false;
  1497. doing_permissions_file = false;
  1498. doing_permissions_dir = true;
  1499. }
  1500. else if ( *cstr == "USE_SOURCE_PERMISSIONS" )
  1501. {
  1502. if(current_match_rule)
  1503. {
  1504. cmOStringStream e;
  1505. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1506. this->SetError(e.str().c_str());
  1507. return false;
  1508. }
  1509. doing_properties = false;
  1510. doing_files = false;
  1511. doing_permissions_file = false;
  1512. doing_permissions_dir = false;
  1513. use_source_permissions = true;
  1514. }
  1515. else if ( *cstr == "FILES_MATCHING" )
  1516. {
  1517. if(current_match_rule)
  1518. {
  1519. cmOStringStream e;
  1520. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1521. this->SetError(e.str().c_str());
  1522. return false;
  1523. }
  1524. doing_properties = false;
  1525. doing_files = false;
  1526. doing_permissions_file = false;
  1527. doing_permissions_dir = false;
  1528. installer.MatchlessFiles = false;
  1529. }
  1530. else if ( *cstr == "COMPONENTS" )
  1531. {
  1532. cmOStringStream e;
  1533. e << "INSTALL called with old-style COMPONENTS argument. "
  1534. << "This script was generated with an older version of CMake. "
  1535. << "Re-run this cmake version on your build tree.";
  1536. this->SetError(e.str().c_str());
  1537. return false;
  1538. }
  1539. else if ( *cstr == "CONFIGURATIONS" )
  1540. {
  1541. cmOStringStream e;
  1542. e << "INSTALL called with old-style CONFIGURATIONS argument. "
  1543. << "This script was generated with an older version of CMake. "
  1544. << "Re-run this cmake version on your build tree.";
  1545. this->SetError(e.str().c_str());
  1546. return false;
  1547. }
  1548. else if ( *cstr == "FILES" && !doing_files)
  1549. {
  1550. if(current_match_rule)
  1551. {
  1552. cmOStringStream e;
  1553. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1554. this->SetError(e.str().c_str());
  1555. return false;
  1556. }
  1557. doing_files = true;
  1558. doing_properties = false;
  1559. doing_permissions_file = false;
  1560. doing_permissions_dir = false;
  1561. }
  1562. else if ( doing_properties && i < args.size()-1 )
  1563. {
  1564. properties[args[i]] = args[i+1].c_str();
  1565. i++;
  1566. }
  1567. else if ( doing_files )
  1568. {
  1569. files.push_back(*cstr);
  1570. }
  1571. else if(doing_permissions_file)
  1572. {
  1573. if(!installer.CheckPermissions(args[i], permissions_file))
  1574. {
  1575. return false;
  1576. }
  1577. }
  1578. else if(doing_permissions_dir)
  1579. {
  1580. if(!installer.CheckPermissions(args[i], permissions_dir))
  1581. {
  1582. return false;
  1583. }
  1584. }
  1585. else if(doing_permissions_match)
  1586. {
  1587. if(!installer.CheckPermissions(
  1588. args[i], current_match_rule->Properties.Permissions))
  1589. {
  1590. return false;
  1591. }
  1592. }
  1593. else
  1594. {
  1595. this->SetError("called with inappropriate arguments");
  1596. return false;
  1597. }
  1598. }
  1599. // now check and postprocess what has been parsed
  1600. if ( files.size() == 0 )
  1601. {
  1602. // nothing to do, no files were listed.
  1603. // if this is handled as error, INSTALL_FILES() creates an invalid
  1604. // cmake_install.cmake script with no FILES() arguments if no files were
  1605. // given to INSTALL_FILES(). This was accepted with CMake 2.4.x.
  1606. return true;
  1607. }
  1608. // Check rename form.
  1609. if(!rename.empty())
  1610. {
  1611. if(itype != cmTarget::INSTALL_FILES &&
  1612. itype != cmTarget::INSTALL_PROGRAMS)
  1613. {
  1614. this->SetError("INSTALL option RENAME may be used only with "
  1615. "FILES or PROGRAMS.");
  1616. return false;
  1617. }
  1618. if(files.size() > 1)
  1619. {
  1620. this->SetError("INSTALL option RENAME may be used only with "
  1621. "one file.");
  1622. return false;
  1623. }
  1624. }
  1625. if (this->HandleInstallDestination(installer, destination) == false)
  1626. {
  1627. return false;
  1628. }
  1629. if(properties.find("VERSION") != properties.end())
  1630. {
  1631. cmOStringStream e;
  1632. e << "INSTALL called with old-style VERSION property. "
  1633. << "This script was generated with an older version of CMake. "
  1634. << "Re-run this cmake version on your build tree.";
  1635. this->SetError(e.str().c_str());
  1636. return false;
  1637. }
  1638. if(properties.find("SOVERSION") != properties.end())
  1639. {
  1640. cmOStringStream e;
  1641. e << "INSTALL called with old-style SOVERSION property. "
  1642. << "This script was generated with an older version of CMake. "
  1643. << "Re-run this cmake version on your build tree.";
  1644. this->SetError(e.str().c_str());
  1645. return false;
  1646. }
  1647. this->GetTargetTypeFromString(stype, itype);
  1648. this->HandleInstallPermissions(installer,
  1649. permissions_file,
  1650. permissions_dir,
  1651. itype,
  1652. use_given_permissions_file,
  1653. use_given_permissions_dir,
  1654. use_source_permissions);
  1655. return true;
  1656. }
  1657. //----------------------------------------------------------------------------
  1658. bool cmFileCommand::DoInstall( cmFileInstaller& installer,
  1659. const int itype,
  1660. const std::string& rename,
  1661. const std::string& destination,
  1662. const std::vector<std::string>& files,
  1663. const bool optional)
  1664. {
  1665. typedef std::set<cmStdString>::const_iterator iter_type;
  1666. // Check whether files should be copied always or only if they have
  1667. // changed.
  1668. bool copy_always =
  1669. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  1670. // Handle each file listed.
  1671. for (std::vector<std::string>::size_type i = 0; i < files.size(); i ++ )
  1672. {
  1673. // Split the input file into its directory and name components.
  1674. std::vector<std::string> fromPathComponents;
  1675. cmSystemTools::SplitPath(files[i].c_str(), fromPathComponents);
  1676. std::string fromName = *(fromPathComponents.end()-1);
  1677. std::string fromDir = cmSystemTools::JoinPath(fromPathComponents.begin(),
  1678. fromPathComponents.end()-1);
  1679. // Compute the full path to the destination file.
  1680. std::string toFile = destination;
  1681. std::string const& toName = rename.empty()? fromName : rename;
  1682. if(!toName.empty())
  1683. {
  1684. toFile += "/";
  1685. toFile += toName;
  1686. }
  1687. // Construct the full path to the source file. The file name may
  1688. // have been changed above.
  1689. std::string fromFile = fromDir;
  1690. if(!fromName.empty())
  1691. {
  1692. fromFile += "/";
  1693. fromFile += fromName;
  1694. }
  1695. std::string message;
  1696. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  1697. {
  1698. if(itype == cmTarget::INSTALL_DIRECTORY &&
  1699. (fromFile.empty() ||
  1700. cmSystemTools::FileIsDirectory(fromFile.c_str())))
  1701. {
  1702. // Try installing this directory.
  1703. if(!installer.InstallDirectory(fromFile.c_str(), toFile.c_str(),
  1704. copy_always))
  1705. {
  1706. return false;
  1707. }
  1708. }
  1709. else if(cmSystemTools::FileExists(fromFile.c_str()))
  1710. {
  1711. // Install this file.
  1712. if(!installer.InstallFile(fromFile.c_str(), toFile.c_str(),
  1713. copy_always))
  1714. {
  1715. return false;
  1716. }
  1717. }
  1718. else if(!optional)
  1719. {
  1720. // The input file does not exist and installation is not optional.
  1721. cmOStringStream e;
  1722. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  1723. this->SetError(e.str().c_str());
  1724. return false;
  1725. }
  1726. }
  1727. }
  1728. return true;
  1729. }
  1730. //----------------------------------------------------------------------------
  1731. bool cmFileCommand::HandleRelativePathCommand(
  1732. std::vector<std::string> const& args)
  1733. {
  1734. if(args.size() != 4 )
  1735. {
  1736. this->SetError("called with incorrect number of arguments");
  1737. return false;
  1738. }
  1739. const std::string& outVar = args[1];
  1740. const std::string& directoryName = args[2];
  1741. const std::string& fileName = args[3];
  1742. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  1743. {
  1744. std::string errstring =
  1745. "RelativePath must be passed a full path to the directory: "
  1746. + directoryName;
  1747. this->SetError(errstring.c_str());
  1748. return false;
  1749. }
  1750. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  1751. {
  1752. std::string errstring =
  1753. "RelativePath must be passed a full path to the file: "
  1754. + fileName;
  1755. this->SetError(errstring.c_str());
  1756. return false;
  1757. }
  1758. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  1759. fileName.c_str());
  1760. this->Makefile->AddDefinition(outVar.c_str(),
  1761. res.c_str());
  1762. return true;
  1763. }
  1764. //----------------------------------------------------------------------------
  1765. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  1766. bool recurse)
  1767. {
  1768. std::string message;
  1769. std::vector<std::string>::const_iterator i = args.begin();
  1770. i++; // Get rid of subcommand
  1771. for(;i != args.end(); ++i)
  1772. {
  1773. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  1774. {
  1775. cmSystemTools::RemoveADirectory(i->c_str());
  1776. }
  1777. else
  1778. {
  1779. cmSystemTools::RemoveFile(i->c_str());
  1780. }
  1781. }
  1782. return true;
  1783. }
  1784. //----------------------------------------------------------------------------
  1785. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  1786. const& args,
  1787. bool nativePath)
  1788. {
  1789. std::vector<std::string>::const_iterator i = args.begin();
  1790. if(args.size() != 3)
  1791. {
  1792. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  1793. "only three arguments.");
  1794. return false;
  1795. }
  1796. i++; // Get rid of subcommand
  1797. #if defined(_WIN32) && !defined(__CYGWIN__)
  1798. char pathSep = ';';
  1799. #else
  1800. char pathSep = ':';
  1801. #endif
  1802. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  1803. pathSep);
  1804. i++;
  1805. const char* var = i->c_str();
  1806. std::string value;
  1807. for(std::vector<cmsys::String>::iterator j = path.begin();
  1808. j != path.end(); ++j)
  1809. {
  1810. if(j != path.begin())
  1811. {
  1812. value += ";";
  1813. }
  1814. if(!nativePath)
  1815. {
  1816. cmSystemTools::ConvertToUnixSlashes(*j);
  1817. }
  1818. else
  1819. {
  1820. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  1821. // remove double quotes in the path
  1822. cmsys::String& s = *j;
  1823. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  1824. {
  1825. s = s.substr(1,s.size()-2);
  1826. }
  1827. }
  1828. value += *j;
  1829. }
  1830. this->Makefile->AddDefinition(var, value.c_str());
  1831. return true;
  1832. }
  1833. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1834. // Stuff for curl download
  1835. typedef std::vector<char> cmFileCommandVectorOfChar;
  1836. namespace{
  1837. size_t
  1838. cmFileCommandWriteMemoryCallback(void *ptr, size_t size, size_t nmemb,
  1839. void *data)
  1840. {
  1841. register int realsize = (int)(size * nmemb);
  1842. std::ofstream* fout = static_cast<std::ofstream*>(data);
  1843. const char* chPtr = static_cast<char*>(ptr);
  1844. fout->write(chPtr, realsize);
  1845. return realsize;
  1846. }
  1847. static size_t
  1848. cmFileCommandCurlDebugCallback(CURL *, curl_infotype, char *chPtr,
  1849. size_t size, void *data)
  1850. {
  1851. cmFileCommandVectorOfChar *vec
  1852. = static_cast<cmFileCommandVectorOfChar*>(data);
  1853. vec->insert(vec->end(), chPtr, chPtr + size);
  1854. return size;
  1855. }
  1856. }
  1857. #endif
  1858. bool
  1859. cmFileCommand::HandleDownloadCommand(std::vector<std::string>
  1860. const& args)
  1861. {
  1862. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1863. std::vector<std::string>::const_iterator i = args.begin();
  1864. if(args.size() < 3)
  1865. {
  1866. this->SetError("FILE(DOWNLOAD url file) must be called with "
  1867. "at least three arguments.");
  1868. return false;
  1869. }
  1870. i++; // Get rid of subcommand
  1871. std::string url = *i;
  1872. i++;
  1873. std::string file = *i;
  1874. i++;
  1875. double timeout = 0;
  1876. std::string verboseLog;
  1877. std::string statusVar;
  1878. while(i != args.end())
  1879. {
  1880. if(*i == "TIMEOUT")
  1881. {
  1882. i++;
  1883. if(i != args.end())
  1884. {
  1885. timeout = atof(i->c_str());
  1886. }
  1887. else
  1888. {
  1889. this->SetError("FILE(DOWNLOAD url file TIMEOUT time) missing "
  1890. "time for TIMEOUT.");
  1891. return false;
  1892. }
  1893. }
  1894. else if(*i == "LOG")
  1895. {
  1896. i++;
  1897. if( i == args.end())
  1898. {
  1899. this->SetError("FILE(DOWNLOAD url file LOG VAR) missing "
  1900. "VAR for LOG.");
  1901. return false;
  1902. }
  1903. verboseLog = *i;
  1904. }
  1905. else if(*i == "STATUS")
  1906. {
  1907. i++;
  1908. if( i == args.end())
  1909. {
  1910. this->SetError("FILE(DOWNLOAD url file STATUS VAR) missing "
  1911. "VAR for STATUS.");
  1912. return false;
  1913. }
  1914. statusVar = *i;
  1915. }
  1916. i++;
  1917. }
  1918. std::string dir = cmSystemTools::GetFilenamePath(file.c_str());
  1919. if(!cmSystemTools::FileExists(dir.c_str()) &&
  1920. !cmSystemTools::MakeDirectory(dir.c_str()))
  1921. {
  1922. std::string errstring = "FILE(DOWNLOAD ) error; cannot create directory: "
  1923. + dir + ". Maybe need administrative privileges.";
  1924. this->SetError(errstring.c_str());
  1925. return false;
  1926. }
  1927. std::ofstream fout(file.c_str(), std::ios::binary);
  1928. if(!fout)
  1929. {
  1930. this->SetError("FILE(DOWNLOAD url file TIMEOUT time) can not open "
  1931. "file for write.");
  1932. return false;
  1933. }
  1934. CURL *curl;
  1935. curl_global_init(CURL_GLOBAL_DEFAULT);
  1936. curl = curl_easy_init();
  1937. if(!curl)
  1938. {
  1939. this->SetError("FILE(DOWNLOAD ) error "
  1940. "initializing curl.");
  1941. return false;
  1942. }
  1943. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  1944. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  1945. cmFileCommandWriteMemoryCallback);
  1946. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  1947. cmFileCommandCurlDebugCallback);
  1948. cmFileCommandVectorOfChar chunkDebug;
  1949. ::curl_easy_setopt(curl, CURLOPT_FILE, (void *)&fout);
  1950. ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *)&chunkDebug);
  1951. if(verboseLog.size())
  1952. {
  1953. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  1954. }
  1955. if(timeout > 0)
  1956. {
  1957. curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout );
  1958. }
  1959. CURLcode res = curl_easy_perform(curl);
  1960. /* always cleanup */
  1961. curl_easy_cleanup(curl);
  1962. if(statusVar.size())
  1963. {
  1964. cmOStringStream result;
  1965. result << (int)res << ";\"" << curl_easy_strerror(res) << "\"";
  1966. this->Makefile->AddDefinition(statusVar.c_str(),
  1967. result.str().c_str());
  1968. }
  1969. curl_global_cleanup();
  1970. if(chunkDebug.size())
  1971. {
  1972. chunkDebug.push_back(0);
  1973. if(CURLE_OPERATION_TIMEOUTED == res)
  1974. {
  1975. std::string output = &*chunkDebug.begin();
  1976. if(verboseLog.size())
  1977. {
  1978. this->Makefile->AddDefinition(verboseLog.c_str(),
  1979. &*chunkDebug.begin());
  1980. }
  1981. }
  1982. this->Makefile->AddDefinition(verboseLog.c_str(),
  1983. &*chunkDebug.begin());
  1984. }
  1985. return true;
  1986. #else
  1987. this->SetError("FILE(DOWNLOAD ) "
  1988. "not supported in bootstrap cmake ");
  1989. return false;
  1990. #endif
  1991. }