cmFileCommand.cxx 71 KB

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