cmFileCommand.cxx 70 KB

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