cmFileCommand.cxx 69 KB

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