cmFileCommand.cxx 65 KB

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