cmFileCommand.cxx 69 KB

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