cmFileCommand.cxx 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmFileCommand.h"
  14. #include "cmake.h"
  15. #include "cmHexFileConverter.h"
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <cmsys/Directory.hxx>
  19. #include <cmsys/Glob.hxx>
  20. #include <cmsys/RegularExpression.hxx>
  21. // Table of permissions flags.
  22. #if defined(_WIN32) && !defined(__CYGWIN__)
  23. static mode_t mode_owner_read = S_IREAD;
  24. static mode_t mode_owner_write = S_IWRITE;
  25. static mode_t mode_owner_execute = S_IEXEC;
  26. static mode_t mode_group_read = 0;
  27. static mode_t mode_group_write = 0;
  28. static mode_t mode_group_execute = 0;
  29. static mode_t mode_world_read = 0;
  30. static mode_t mode_world_write = 0;
  31. static mode_t mode_world_execute = 0;
  32. static mode_t mode_setuid = 0;
  33. static mode_t mode_setgid = 0;
  34. #else
  35. static mode_t mode_owner_read = S_IRUSR;
  36. static mode_t mode_owner_write = S_IWUSR;
  37. static mode_t mode_owner_execute = S_IXUSR;
  38. static mode_t mode_group_read = S_IRGRP;
  39. static mode_t mode_group_write = S_IWGRP;
  40. static mode_t mode_group_execute = S_IXGRP;
  41. static mode_t mode_world_read = S_IROTH;
  42. static mode_t mode_world_write = S_IWOTH;
  43. static mode_t mode_world_execute = S_IXOTH;
  44. static mode_t mode_setuid = S_ISUID;
  45. static mode_t mode_setgid = S_ISGID;
  46. #endif
  47. // cmLibraryCommand
  48. bool cmFileCommand::InitialPass(std::vector<std::string> const& args)
  49. {
  50. if(args.size() < 2 )
  51. {
  52. this->SetError("must be called with at least two arguments.");
  53. return false;
  54. }
  55. std::string subCommand = args[0];
  56. if ( subCommand == "WRITE" )
  57. {
  58. return this->HandleWriteCommand(args, false);
  59. }
  60. else if ( subCommand == "APPEND" )
  61. {
  62. return this->HandleWriteCommand(args, true);
  63. }
  64. else if ( subCommand == "READ" )
  65. {
  66. return this->HandleReadCommand(args);
  67. }
  68. else if ( subCommand == "STRINGS" )
  69. {
  70. return this->HandleStringsCommand(args);
  71. }
  72. /* else if ( subCommand == "HEX_TO_BIN" )
  73. {
  74. return this->HandleHex2BinCommand(args);
  75. }*/
  76. else if ( subCommand == "GLOB" )
  77. {
  78. return this->HandleGlobCommand(args, false);
  79. }
  80. else if ( subCommand == "GLOB_RECURSE" )
  81. {
  82. return this->HandleGlobCommand(args, true);
  83. }
  84. else if ( subCommand == "MAKE_DIRECTORY" )
  85. {
  86. return this->HandleMakeDirectoryCommand(args);
  87. }
  88. else if ( subCommand == "REMOVE" )
  89. {
  90. return this->HandleRemove(args, false);
  91. }
  92. else if ( subCommand == "REMOVE_RECURSE" )
  93. {
  94. return this->HandleRemove(args, true);
  95. }
  96. else if ( subCommand == "INSTALL" )
  97. {
  98. return this->HandleInstallCommand(args);
  99. }
  100. else if ( subCommand == "RELATIVE_PATH" )
  101. {
  102. return this->HandleRelativePathCommand(args);
  103. }
  104. else if ( subCommand == "TO_CMAKE_PATH" )
  105. {
  106. return this->HandleCMakePathCommand(args, false);
  107. }
  108. else if ( subCommand == "TO_NATIVE_PATH" )
  109. {
  110. return this->HandleCMakePathCommand(args, true);
  111. }
  112. std::string e = "does not recognize sub-command "+subCommand;
  113. this->SetError(e.c_str());
  114. return false;
  115. }
  116. //----------------------------------------------------------------------------
  117. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  118. bool append)
  119. {
  120. std::string message;
  121. std::vector<std::string>::const_iterator i = args.begin();
  122. i++; // Get rid of subcommand
  123. std::string fileName = *i;
  124. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  125. {
  126. fileName = this->Makefile->GetCurrentDirectory();
  127. fileName += "/" + *i;
  128. }
  129. i++;
  130. for(;i != args.end(); ++i)
  131. {
  132. message += *i;
  133. }
  134. if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
  135. {
  136. std::string e
  137. = "attempted to write a file: " + fileName +
  138. " into a source directory.";
  139. this->SetError(e.c_str());
  140. cmSystemTools::SetFatalErrorOccured();
  141. return false;
  142. }
  143. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  144. cmSystemTools::MakeDirectory(dir.c_str());
  145. mode_t mode =
  146. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  147. S_IREAD | S_IWRITE
  148. #elif defined( __BORLANDC__ )
  149. S_IRUSR | S_IWUSR
  150. #else
  151. S_IRUSR | S_IWUSR |
  152. S_IRGRP |
  153. S_IROTH
  154. #endif
  155. ;
  156. // Set permissions to writable
  157. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  158. {
  159. cmSystemTools::SetPermissions(fileName.c_str(),
  160. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  161. S_IREAD | S_IWRITE
  162. #else
  163. S_IRUSR | S_IWUSR
  164. #endif
  165. );
  166. }
  167. // If GetPermissions fails, pretend like it is ok. File open will fail if
  168. // the file is not writable
  169. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  170. if ( !file )
  171. {
  172. std::string error = "Internal CMake error when trying to open file: ";
  173. error += fileName.c_str();
  174. error += " for writing.";
  175. this->SetError(error.c_str());
  176. return false;
  177. }
  178. file << message;
  179. file.close();
  180. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  181. this->Makefile->AddWrittenFile(fileName.c_str());
  182. return true;
  183. }
  184. //----------------------------------------------------------------------------
  185. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  186. {
  187. if ( args.size() < 3 )
  188. {
  189. this->SetError("READ must be called with at least two additional "
  190. "arguments");
  191. return false;
  192. }
  193. std::string fileName = args[1];
  194. if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
  195. {
  196. fileName = this->Makefile->GetCurrentDirectory();
  197. fileName += "/" + args[1];
  198. }
  199. std::string variable = args[2];
  200. std::ifstream file(fileName.c_str(), std::ios::in);
  201. if ( !file )
  202. {
  203. std::string error = "Internal CMake error when trying to open file: ";
  204. error += fileName.c_str();
  205. error += " for reading.";
  206. this->SetError(error.c_str());
  207. return false;
  208. }
  209. // if there a limit?
  210. long sizeLimit = -1;
  211. if (args.size() >= 5 && args[3] == "LIMIT")
  212. {
  213. sizeLimit = atoi(args[4].c_str());
  214. }
  215. std::string output;
  216. std::string line;
  217. bool has_newline = false;
  218. while (sizeLimit != 0 &&
  219. cmSystemTools::GetLineFromStream(file, line, &has_newline,
  220. sizeLimit) )
  221. {
  222. if (sizeLimit > 0)
  223. {
  224. sizeLimit = sizeLimit - static_cast<long>(line.size());
  225. if (has_newline)
  226. {
  227. sizeLimit--;
  228. }
  229. if (sizeLimit < 0)
  230. {
  231. sizeLimit = 0;
  232. }
  233. }
  234. output += line;
  235. if ( has_newline )
  236. {
  237. output += "\n";
  238. }
  239. }
  240. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  241. return true;
  242. }
  243. //----------------------------------------------------------------------------
  244. /*bool cmFileCommand::HandleHex2BinCommand(std::vector<std::string> const& args)
  245. {
  246. if(args.size() != 3)
  247. {
  248. this->SetError("HEX_TO_BIN requires an input and an output file name");
  249. return false;
  250. }
  251. // Get the file to read.
  252. std::string inFileName = args[1];
  253. if(!cmsys::SystemTools::FileIsFullPath(inFileName.c_str()))
  254. {
  255. inFileName = this->Makefile->GetCurrentDirectory();
  256. inFileName += "/" + args[1];
  257. }
  258. // Get the file to write.
  259. std::string outFileName = args[2];
  260. if(!cmsys::SystemTools::FileIsFullPath(outFileName.c_str()))
  261. {
  262. outFileName = this->Makefile->GetCurrentDirectory();
  263. outFileName += "/" + args[2];
  264. }
  265. if ( !this->Makefile->CanIWriteThisFile(outFileName.c_str()) )
  266. {
  267. std::string e
  268. = "attempted to write a file: " + outFileName +
  269. " into a source directory.";
  270. this->SetError(e.c_str());
  271. cmSystemTools::SetFatalErrorOccured();
  272. return false;
  273. }
  274. std::string dir = cmSystemTools::GetFilenamePath(outFileName);
  275. cmSystemTools::MakeDirectory(dir.c_str());
  276. bool success = cmHexFileConverter::TryConvert(inFileName.c_str(),
  277. outFileName.c_str());
  278. if (!success)
  279. {
  280. success = cmSystemTools::CopyFileAlways(inFileName.c_str(),
  281. outFileName.c_str());
  282. }
  283. return success;
  284. } */
  285. //----------------------------------------------------------------------------
  286. bool cmFileCommand::HandleStringsCommand(std::vector<std::string> const& args)
  287. {
  288. if(args.size() < 3)
  289. {
  290. this->SetError("STRINGS requires a file name and output variable");
  291. return false;
  292. }
  293. // Get the file to read.
  294. std::string fileName = args[1];
  295. if(!cmsys::SystemTools::FileIsFullPath(fileName.c_str()))
  296. {
  297. fileName = this->Makefile->GetCurrentDirectory();
  298. fileName += "/" + args[1];
  299. }
  300. // Get the variable in which to store the results.
  301. std::string outVar = args[2];
  302. // Parse the options.
  303. enum { arg_none,
  304. arg_limit_input,
  305. arg_limit_output,
  306. arg_limit_count,
  307. arg_length_minimum,
  308. arg_length_maximum,
  309. arg__maximum,
  310. arg_regex };
  311. unsigned int minlen = 0;
  312. unsigned int maxlen = 0;
  313. int limit_input = -1;
  314. int limit_output = -1;
  315. unsigned int limit_count = 0;
  316. cmsys::RegularExpression regex;
  317. bool have_regex = false;
  318. bool newline_consume = false;
  319. bool hex_conversion_enabled = true;
  320. int arg_mode = arg_none;
  321. for(unsigned int i=3; i < args.size(); ++i)
  322. {
  323. if(args[i] == "LIMIT_INPUT")
  324. {
  325. arg_mode = arg_limit_input;
  326. }
  327. else if(args[i] == "LIMIT_OUTPUT")
  328. {
  329. arg_mode = arg_limit_output;
  330. }
  331. else if(args[i] == "LIMIT_COUNT")
  332. {
  333. arg_mode = arg_limit_count;
  334. }
  335. else if(args[i] == "LENGTH_MINIMUM")
  336. {
  337. arg_mode = arg_length_minimum;
  338. }
  339. else if(args[i] == "LENGTH_MAXIMUM")
  340. {
  341. arg_mode = arg_length_maximum;
  342. }
  343. else if(args[i] == "REGEX")
  344. {
  345. arg_mode = arg_regex;
  346. }
  347. else if(args[i] == "NEWLINE_CONSUME")
  348. {
  349. newline_consume = true;
  350. arg_mode = arg_none;
  351. }
  352. else if(args[i] == "NO_HEX_CONVERSION")
  353. {
  354. hex_conversion_enabled = false;
  355. arg_mode = arg_none;
  356. }
  357. else if(arg_mode == arg_limit_input)
  358. {
  359. if(sscanf(args[i].c_str(), "%d", &limit_input) != 1 ||
  360. limit_input < 0)
  361. {
  362. cmOStringStream e;
  363. e << "STRINGS option LIMIT_INPUT value \""
  364. << args[i] << "\" is not an unsigned integer.";
  365. this->SetError(e.str().c_str());
  366. return false;
  367. }
  368. arg_mode = arg_none;
  369. }
  370. else if(arg_mode == arg_limit_output)
  371. {
  372. if(sscanf(args[i].c_str(), "%d", &limit_output) != 1 ||
  373. limit_output < 0)
  374. {
  375. cmOStringStream e;
  376. e << "STRINGS option LIMIT_OUTPUT value \""
  377. << args[i] << "\" is not an unsigned integer.";
  378. this->SetError(e.str().c_str());
  379. return false;
  380. }
  381. arg_mode = arg_none;
  382. }
  383. else if(arg_mode == arg_limit_count)
  384. {
  385. int count;
  386. if(sscanf(args[i].c_str(), "%d", &count) != 1 || count < 0)
  387. {
  388. cmOStringStream e;
  389. e << "STRINGS option LIMIT_COUNT value \""
  390. << args[i] << "\" is not an unsigned integer.";
  391. this->SetError(e.str().c_str());
  392. return false;
  393. }
  394. limit_count = count;
  395. arg_mode = arg_none;
  396. }
  397. else if(arg_mode == arg_length_minimum)
  398. {
  399. int len;
  400. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  401. {
  402. cmOStringStream e;
  403. e << "STRINGS option LENGTH_MINIMUM value \""
  404. << args[i] << "\" is not an unsigned integer.";
  405. this->SetError(e.str().c_str());
  406. return false;
  407. }
  408. minlen = len;
  409. arg_mode = arg_none;
  410. }
  411. else if(arg_mode == arg_length_maximum)
  412. {
  413. int len;
  414. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  415. {
  416. cmOStringStream e;
  417. e << "STRINGS option LENGTH_MAXIMUM value \""
  418. << args[i] << "\" is not an unsigned integer.";
  419. this->SetError(e.str().c_str());
  420. return false;
  421. }
  422. maxlen = len;
  423. arg_mode = arg_none;
  424. }
  425. else if(arg_mode == arg_regex)
  426. {
  427. if(!regex.compile(args[i].c_str()))
  428. {
  429. cmOStringStream e;
  430. e << "STRINGS option REGEX value \""
  431. << args[i] << "\" could not be compiled.";
  432. this->SetError(e.str().c_str());
  433. return false;
  434. }
  435. have_regex = true;
  436. arg_mode = arg_none;
  437. }
  438. else
  439. {
  440. cmOStringStream e;
  441. e << "STRINGS given unknown argument \""
  442. << args[i] << "\"";
  443. this->SetError(e.str().c_str());
  444. return false;
  445. }
  446. }
  447. if (hex_conversion_enabled)
  448. {
  449. // TODO: should work without temp file, but just on a memory buffer
  450. std::string binaryFileName = this->Makefile->GetCurrentOutputDirectory();
  451. binaryFileName += cmake::GetCMakeFilesDirectory();
  452. binaryFileName += "/FileCommandStringsBinaryFile";
  453. if (cmHexFileConverter::TryConvert(fileName.c_str(), binaryFileName.c_str()))
  454. {
  455. fileName = binaryFileName;
  456. }
  457. }
  458. // Open the specified file.
  459. #if defined(_WIN32) || defined(__CYGWIN__)
  460. std::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
  461. #else
  462. std::ifstream fin(fileName.c_str(), std::ios::in);
  463. #endif
  464. if(!fin)
  465. {
  466. cmOStringStream e;
  467. e << "STRINGS file \"" << fileName << "\" cannot be read.";
  468. this->SetError(e.str().c_str());
  469. return false;
  470. }
  471. // Parse strings out of the file.
  472. int output_size = 0;
  473. std::vector<std::string> strings;
  474. std::string s;
  475. int c;
  476. while((!limit_count || strings.size() < limit_count) &&
  477. (limit_input < 0 || static_cast<int>(fin.tellg()) < limit_input) &&
  478. (c = fin.get(), fin))
  479. {
  480. if(c == '\0')
  481. {
  482. // A terminating null character has been found. Check if the
  483. // current string matches the requirements. Since it was
  484. // terminated by a null character, we require that the length be
  485. // at least one no matter what the user specified.
  486. if(s.length() >= minlen && s.length() >= 1 &&
  487. (!have_regex || regex.find(s.c_str())))
  488. {
  489. output_size += static_cast<int>(s.size()) + 1;
  490. if(limit_output >= 0 && output_size >= limit_output)
  491. {
  492. s = "";
  493. break;
  494. }
  495. strings.push_back(s);
  496. }
  497. // Reset the string to empty.
  498. s = "";
  499. }
  500. else if(c == '\n' && !newline_consume)
  501. {
  502. // The current line has been terminated. Check if the current
  503. // string matches the requirements. The length may now be as
  504. // low as zero since blank lines are allowed.
  505. if(s.length() >= minlen &&
  506. (!have_regex || regex.find(s.c_str())))
  507. {
  508. output_size += static_cast<int>(s.size()) + 1;
  509. if(limit_output >= 0 && output_size >= limit_output)
  510. {
  511. s = "";
  512. break;
  513. }
  514. strings.push_back(s);
  515. }
  516. // Reset the string to empty.
  517. s = "";
  518. }
  519. else if(c == '\r')
  520. {
  521. // Ignore CR character to make output always have UNIX newlines.
  522. }
  523. else if(c >= 0x20 && c < 0x7F || c == '\t' ||
  524. (c == '\n' && newline_consume))
  525. {
  526. // This is an ASCII character that may be part of a string.
  527. s += c;
  528. }
  529. else
  530. {
  531. // This is a non-string character. Reset the string to emtpy.
  532. s = "";
  533. }
  534. // Terminate a string if the maximum length is reached.
  535. if(maxlen > 0 && s.size() == maxlen)
  536. {
  537. if(s.length() >= minlen &&
  538. (!have_regex || regex.find(s.c_str())))
  539. {
  540. output_size += static_cast<int>(s.size()) + 1;
  541. if(limit_output >= 0 && output_size >= limit_output)
  542. {
  543. s = "";
  544. break;
  545. }
  546. strings.push_back(s);
  547. }
  548. s = "";
  549. }
  550. }
  551. // If there is a non-empty current string we have hit the end of the
  552. // input file or the input size limit. Check if the current string
  553. // matches the requirements.
  554. if((!limit_count || strings.size() < limit_count) &&
  555. !s.empty() && s.length() >= minlen &&
  556. (!have_regex || regex.find(s.c_str())))
  557. {
  558. output_size += static_cast<int>(s.size()) + 1;
  559. if(limit_output < 0 || output_size < limit_output)
  560. {
  561. strings.push_back(s);
  562. }
  563. }
  564. // Encode the result in a CMake list.
  565. const char* sep = "";
  566. std::string output;
  567. for(std::vector<std::string>::const_iterator si = strings.begin();
  568. si != strings.end(); ++si)
  569. {
  570. // Separate the strings in the output to make it a list.
  571. output += sep;
  572. sep = ";";
  573. // Store the string in the output, but escape semicolons to
  574. // make sure it is a list.
  575. std::string const& sr = *si;
  576. for(unsigned int i=0; i < sr.size(); ++i)
  577. {
  578. if(sr[i] == ';')
  579. {
  580. output += '\\';
  581. }
  582. output += sr[i];
  583. }
  584. }
  585. // Save the output in a makefile variable.
  586. this->Makefile->AddDefinition(outVar.c_str(), output.c_str());
  587. return true;
  588. }
  589. //----------------------------------------------------------------------------
  590. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  591. bool recurse)
  592. {
  593. if ( args.size() < 2 )
  594. {
  595. this->SetError("GLOB requires at least a variable name");
  596. return false;
  597. }
  598. std::vector<std::string>::const_iterator i = args.begin();
  599. i++; // Get rid of subcommand
  600. std::string variable = *i;
  601. i++;
  602. cmsys::Glob g;
  603. g.SetRecurse(recurse);
  604. std::string output = "";
  605. bool first = true;
  606. for ( ; i != args.end(); ++i )
  607. {
  608. if ( *i == "RELATIVE" )
  609. {
  610. ++i; // skip RELATIVE
  611. if ( i == args.end() )
  612. {
  613. this->SetError("GLOB requires a directory after the RELATIVE tag");
  614. return false;
  615. }
  616. g.SetRelative(i->c_str());
  617. ++i;
  618. if(i == args.end())
  619. {
  620. this->SetError("GLOB requires a glob expression after the directory");
  621. return false;
  622. }
  623. }
  624. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  625. {
  626. std::string expr = this->Makefile->GetCurrentDirectory();
  627. // Handle script mode
  628. if ( expr.size() > 0 )
  629. {
  630. expr += "/" + *i;
  631. g.FindFiles(expr);
  632. }
  633. else
  634. {
  635. g.FindFiles(*i);
  636. }
  637. }
  638. else
  639. {
  640. g.FindFiles(*i);
  641. }
  642. std::vector<std::string>::size_type cc;
  643. std::vector<std::string>& files = g.GetFiles();
  644. for ( cc = 0; cc < files.size(); cc ++ )
  645. {
  646. if ( !first )
  647. {
  648. output += ";";
  649. }
  650. output += files[cc];
  651. first = false;
  652. }
  653. }
  654. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  655. return true;
  656. }
  657. //----------------------------------------------------------------------------
  658. bool cmFileCommand::HandleMakeDirectoryCommand(
  659. std::vector<std::string> const& args)
  660. {
  661. if(args.size() < 2 )
  662. {
  663. this->SetError("called with incorrect number of arguments");
  664. return false;
  665. }
  666. std::vector<std::string>::const_iterator i = args.begin();
  667. i++; // Get rid of subcommand
  668. std::string expr;
  669. for ( ; i != args.end(); ++i )
  670. {
  671. const std::string* cdir = &(*i);
  672. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  673. {
  674. expr = this->Makefile->GetCurrentDirectory();
  675. expr += "/" + *i;
  676. cdir = &expr;
  677. }
  678. if ( !this->Makefile->CanIWriteThisFile(cdir->c_str()) )
  679. {
  680. std::string e = "attempted to create a directory: " + *cdir
  681. + " into a source directory.";
  682. this->SetError(e.c_str());
  683. cmSystemTools::SetFatalErrorOccured();
  684. return false;
  685. }
  686. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  687. {
  688. std::string error = "problem creating directory: " + *cdir;
  689. this->SetError(error.c_str());
  690. return false;
  691. }
  692. }
  693. return true;
  694. }
  695. //----------------------------------------------------------------------------
  696. // File installation helper class.
  697. struct cmFileInstaller
  698. {
  699. // Methods to actually install files.
  700. bool InstallFile(const char* fromFile, const char* toFile, bool always);
  701. bool InstallDirectory(const char* source, const char* destination,
  702. bool always);
  703. // All instances need the file command and makefile using them.
  704. cmFileInstaller(cmFileCommand* fc, cmMakefile* mf):
  705. FileCommand(fc), Makefile(mf), DestDirLength(0)
  706. {
  707. // Get the current manifest.
  708. this->Manifest =
  709. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  710. }
  711. ~cmFileInstaller()
  712. {
  713. // Save the updated install manifest.
  714. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  715. this->Manifest.c_str());
  716. }
  717. private:
  718. cmFileCommand* FileCommand;
  719. cmMakefile* Makefile;
  720. public:
  721. // The length of the destdir setting.
  722. int DestDirLength;
  723. // The current file manifest (semicolon separated list).
  724. std::string Manifest;
  725. // Permissions for files and directories installed by this object.
  726. mode_t FilePermissions;
  727. mode_t DirPermissions;
  728. // Properties set by pattern and regex match rules.
  729. struct MatchProperties
  730. {
  731. bool Exclude;
  732. mode_t Permissions;
  733. MatchProperties(): Exclude(false), Permissions(0) {}
  734. };
  735. struct MatchRule
  736. {
  737. cmsys::RegularExpression Regex;
  738. MatchProperties Properties;
  739. std::string RegexString;
  740. MatchRule(std::string const& regex):
  741. Regex(regex.c_str()), RegexString(regex) {}
  742. };
  743. std::vector<MatchRule> MatchRules;
  744. // Get the properties from rules matching this input file.
  745. MatchProperties CollectMatchProperties(const char* file)
  746. {
  747. // Match rules are case-insensitive on some platforms.
  748. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  749. std::string lower = cmSystemTools::LowerCase(file);
  750. file = lower.c_str();
  751. #endif
  752. // Collect properties from all matching rules.
  753. MatchProperties result;
  754. for(std::vector<MatchRule>::iterator mr = this->MatchRules.begin();
  755. mr != this->MatchRules.end(); ++mr)
  756. {
  757. if(mr->Regex.find(file))
  758. {
  759. result.Exclude |= mr->Properties.Exclude;
  760. result.Permissions |= mr->Properties.Permissions;
  761. }
  762. }
  763. return result;
  764. }
  765. // Append a file to the installation manifest.
  766. void ManifestAppend(std::string const& file)
  767. {
  768. this->Manifest += ";";
  769. this->Manifest += file.substr(this->DestDirLength);
  770. }
  771. // Translate an argument to a permissions bit.
  772. bool CheckPermissions(std::string const& arg, mode_t& permissions)
  773. {
  774. if(arg == "OWNER_READ") { permissions |= mode_owner_read; }
  775. else if(arg == "OWNER_WRITE") { permissions |= mode_owner_write; }
  776. else if(arg == "OWNER_EXECUTE") { permissions |= mode_owner_execute; }
  777. else if(arg == "GROUP_READ") { permissions |= mode_group_read; }
  778. else if(arg == "GROUP_WRITE") { permissions |= mode_group_write; }
  779. else if(arg == "GROUP_EXECUTE") { permissions |= mode_group_execute; }
  780. else if(arg == "WORLD_READ") { permissions |= mode_world_read; }
  781. else if(arg == "WORLD_WRITE") { permissions |= mode_world_write; }
  782. else if(arg == "WORLD_EXECUTE") { permissions |= mode_world_execute; }
  783. else if(arg == "SETUID") { permissions |= mode_setuid; }
  784. else if(arg == "SETGID") { permissions |= mode_setgid; }
  785. else
  786. {
  787. cmOStringStream e;
  788. e << "INSTALL given invalid permission \"" << arg << "\".";
  789. this->FileCommand->SetError(e.str().c_str());
  790. return false;
  791. }
  792. return true;
  793. }
  794. private:
  795. bool InstallSymlink(const char* fromFile, const char* toFile, bool always);
  796. };
  797. //----------------------------------------------------------------------------
  798. bool cmFileInstaller::InstallSymlink(const char* fromFile, const char* toFile,
  799. bool always)
  800. {
  801. // Inform the user about this file installation.
  802. std::string message = "Installing ";
  803. message += toFile;
  804. this->Makefile->DisplayStatus(message.c_str(), -1);
  805. // Read the original symlink.
  806. std::string symlinkTarget;
  807. if(!cmSystemTools::ReadSymlink(fromFile, symlinkTarget))
  808. {
  809. cmOStringStream e;
  810. e << "INSTALL cannot read symlink \"" << fromFile
  811. << "\" to duplicate at \"" << toFile << "\".";
  812. this->FileCommand->SetError(e.str().c_str());
  813. return false;
  814. }
  815. // Compare the symlink value to that at the destination if not
  816. // always installing.
  817. if(!always)
  818. {
  819. std::string oldSymlinkTarget;
  820. if(cmSystemTools::ReadSymlink(toFile, oldSymlinkTarget))
  821. {
  822. if(symlinkTarget == oldSymlinkTarget)
  823. {
  824. return true;
  825. }
  826. }
  827. }
  828. // Remove the destination file so we can always create the symlink.
  829. cmSystemTools::RemoveFile(toFile);
  830. // Create the symlink.
  831. if(!cmSystemTools::CreateSymlink(symlinkTarget.c_str(), toFile))
  832. {
  833. cmOStringStream e;
  834. e << "INSTALL cannot duplicate symlink \"" << fromFile
  835. << "\" at \"" << toFile << "\".";
  836. this->FileCommand->SetError(e.str().c_str());
  837. return false;
  838. }
  839. // Add the file to the manifest.
  840. this->ManifestAppend(toFile);
  841. return true;
  842. }
  843. //----------------------------------------------------------------------------
  844. bool cmFileInstaller::InstallFile(const char* fromFile, const char* toFile,
  845. bool always)
  846. {
  847. // Collect any properties matching this file name.
  848. MatchProperties match_properties = this->CollectMatchProperties(fromFile);
  849. // Skip the file if it is excluded.
  850. if(match_properties.Exclude)
  851. {
  852. return true;
  853. }
  854. // Short-circuit for symbolic links.
  855. if(cmSystemTools::FileIsSymlink(fromFile))
  856. {
  857. return this->InstallSymlink(fromFile, toFile, always);
  858. }
  859. // Inform the user about this file installation.
  860. std::string message = "Installing ";
  861. message += toFile;
  862. this->Makefile->DisplayStatus(message.c_str(), -1);
  863. // Copy the file.
  864. if(!cmSystemTools::CopyAFile(fromFile, toFile, always))
  865. {
  866. cmOStringStream e;
  867. e << "INSTALL cannot copy file \"" << fromFile
  868. << "\" to \"" << toFile << "\".";
  869. this->FileCommand->SetError(e.str().c_str());
  870. return false;
  871. }
  872. // Add the file to the manifest.
  873. this->ManifestAppend(toFile);
  874. // Set permissions of the destination file.
  875. mode_t permissions = (match_properties.Permissions?
  876. match_properties.Permissions : this->FilePermissions);
  877. if(!permissions)
  878. {
  879. // No permissions were explicitly provided but the user requested
  880. // that the source file permissions be used.
  881. cmSystemTools::GetPermissions(fromFile, permissions);
  882. }
  883. if(permissions && !cmSystemTools::SetPermissions(toFile, permissions))
  884. {
  885. cmOStringStream e;
  886. e << "Problem setting permissions on file \"" << toFile << "\"";
  887. this->FileCommand->SetError(e.str().c_str());
  888. return false;
  889. }
  890. return true;
  891. }
  892. //----------------------------------------------------------------------------
  893. bool cmFileInstaller::InstallDirectory(const char* source,
  894. const char* destination,
  895. bool always)
  896. {
  897. // Collect any properties matching this directory name.
  898. MatchProperties match_properties = this->CollectMatchProperties(source);
  899. // Skip the directory if it is excluded.
  900. if(match_properties.Exclude)
  901. {
  902. return true;
  903. }
  904. // Short-circuit for symbolic links.
  905. if(cmSystemTools::FileIsSymlink(source))
  906. {
  907. return this->InstallSymlink(source, destination, always);
  908. }
  909. // Inform the user about this directory installation.
  910. std::string message = "Installing ";
  911. message += destination;
  912. this->Makefile->DisplayStatus(message.c_str(), -1);
  913. // Make sure the destination directory exists.
  914. if(!cmSystemTools::MakeDirectory(destination))
  915. {
  916. return false;
  917. }
  918. // Compute the requested permissions for the destination directory.
  919. mode_t permissions = (match_properties.Permissions?
  920. match_properties.Permissions : this->DirPermissions);
  921. if(!permissions)
  922. {
  923. // No permissions were explicitly provided but the user requested
  924. // that the source directory permissions be used.
  925. cmSystemTools::GetPermissions(source, permissions);
  926. }
  927. // Compute the set of permissions required on this directory to
  928. // recursively install files and subdirectories safely.
  929. mode_t required_permissions =
  930. mode_owner_read | mode_owner_write | mode_owner_execute;
  931. // If the required permissions are specified it is safe to set the
  932. // final permissions now. Otherwise we must add the required
  933. // permissions temporarily during file installation.
  934. mode_t permissions_before = 0;
  935. mode_t permissions_after = 0;
  936. if(permissions & required_permissions)
  937. {
  938. permissions_before = permissions;
  939. }
  940. else
  941. {
  942. permissions_before = permissions | required_permissions;
  943. permissions_after = permissions;
  944. }
  945. // Set the required permissions of the destination directory.
  946. if(permissions_before &&
  947. !cmSystemTools::SetPermissions(destination, permissions_before))
  948. {
  949. cmOStringStream e;
  950. e << "Problem setting permissions on directory \""
  951. << destination << "\"";
  952. this->FileCommand->SetError(e.str().c_str());
  953. return false;
  954. }
  955. // Load the directory contents to traverse it recursively.
  956. cmsys::Directory dir;
  957. if(source && *source)
  958. {
  959. dir.Load(source);
  960. }
  961. unsigned long numFiles = static_cast<unsigned long>(dir.GetNumberOfFiles());
  962. for(unsigned long fileNum = 0; fileNum < numFiles; ++fileNum)
  963. {
  964. if(!(strcmp(dir.GetFile(fileNum), ".") == 0 ||
  965. strcmp(dir.GetFile(fileNum), "..") == 0))
  966. {
  967. cmsys_stl::string fromPath = source;
  968. fromPath += "/";
  969. fromPath += dir.GetFile(fileNum);
  970. if(cmSystemTools::FileIsDirectory(fromPath.c_str()))
  971. {
  972. cmsys_stl::string toDir = destination;
  973. toDir += "/";
  974. toDir += dir.GetFile(fileNum);
  975. if(!this->InstallDirectory(fromPath.c_str(), toDir.c_str(), always))
  976. {
  977. return false;
  978. }
  979. }
  980. else
  981. {
  982. // Install this file.
  983. std::string toFile = destination;
  984. toFile += "/";
  985. toFile += dir.GetFile(fileNum);
  986. if(!this->InstallFile(fromPath.c_str(), toFile.c_str(), always))
  987. {
  988. return false;
  989. }
  990. }
  991. }
  992. }
  993. // Set the requested permissions of the destination directory.
  994. if(permissions_after &&
  995. !cmSystemTools::SetPermissions(destination, permissions_after))
  996. {
  997. cmOStringStream e;
  998. e << "Problem setting permissions on directory \"" << destination << "\"";
  999. this->FileCommand->SetError(e.str().c_str());
  1000. return false;
  1001. }
  1002. return true;
  1003. }
  1004. //----------------------------------------------------------------------------
  1005. void cmFileCommand::HandleInstallPermissions(cmFileInstaller& installer,
  1006. mode_t& permissions_file,
  1007. mode_t& permissions_dir,
  1008. int itype,
  1009. bool use_given_permissions_file,
  1010. bool use_given_permissions_dir,
  1011. bool use_source_permissions) const
  1012. {
  1013. // Choose a default for shared library permissions.
  1014. bool install_so_no_exe = this->Makefile->IsOn("CMAKE_INSTALL_SO_NO_EXE");
  1015. // If file permissions were not specified set default permissions
  1016. // for this target type.
  1017. if(!use_given_permissions_file && !use_source_permissions)
  1018. {
  1019. switch(itype)
  1020. {
  1021. case cmTarget::SHARED_LIBRARY:
  1022. case cmTarget::MODULE_LIBRARY:
  1023. if(install_so_no_exe)
  1024. {
  1025. // Use read/write permissions.
  1026. permissions_file = 0;
  1027. permissions_file |= mode_owner_read;
  1028. permissions_file |= mode_owner_write;
  1029. permissions_file |= mode_group_read;
  1030. permissions_file |= mode_world_read;
  1031. break;
  1032. }
  1033. case cmTarget::EXECUTABLE:
  1034. case cmTarget::INSTALL_PROGRAMS:
  1035. // Use read/write/executable permissions.
  1036. permissions_file = 0;
  1037. permissions_file |= mode_owner_read;
  1038. permissions_file |= mode_owner_write;
  1039. permissions_file |= mode_owner_execute;
  1040. permissions_file |= mode_group_read;
  1041. permissions_file |= mode_group_execute;
  1042. permissions_file |= mode_world_read;
  1043. permissions_file |= mode_world_execute;
  1044. break;
  1045. default:
  1046. // Use read/write permissions.
  1047. permissions_file = 0;
  1048. permissions_file |= mode_owner_read;
  1049. permissions_file |= mode_owner_write;
  1050. permissions_file |= mode_group_read;
  1051. permissions_file |= mode_world_read;
  1052. break;
  1053. }
  1054. }
  1055. // If directory permissions were not specified set default permissions.
  1056. if(!use_given_permissions_dir && !use_source_permissions)
  1057. {
  1058. // Use read/write/executable permissions.
  1059. permissions_dir = 0;
  1060. permissions_dir |= mode_owner_read;
  1061. permissions_dir |= mode_owner_write;
  1062. permissions_dir |= mode_owner_execute;
  1063. permissions_dir |= mode_group_read;
  1064. permissions_dir |= mode_group_execute;
  1065. permissions_dir |= mode_world_read;
  1066. permissions_dir |= mode_world_execute;
  1067. }
  1068. // Set the installer permissions.
  1069. installer.FilePermissions = permissions_file;
  1070. installer.DirPermissions = permissions_dir;
  1071. }
  1072. //----------------------------------------------------------------------------
  1073. void cmFileCommand
  1074. ::GetTargetTypeFromString(const std::string& stype, int& itype) const
  1075. {
  1076. if ( stype == "EXECUTABLE" )
  1077. {
  1078. itype = cmTarget::EXECUTABLE;
  1079. }
  1080. else if ( stype == "PROGRAM" )
  1081. {
  1082. itype = cmTarget::INSTALL_PROGRAMS;
  1083. }
  1084. else if ( stype == "STATIC_LIBRARY" )
  1085. {
  1086. itype = cmTarget::STATIC_LIBRARY;
  1087. }
  1088. else if ( stype == "SHARED_LIBRARY" )
  1089. {
  1090. itype = cmTarget::SHARED_LIBRARY;
  1091. }
  1092. else if ( stype == "MODULE" )
  1093. {
  1094. itype = cmTarget::MODULE_LIBRARY;
  1095. }
  1096. else if ( stype == "DIRECTORY" )
  1097. {
  1098. itype = cmTarget::INSTALL_DIRECTORY;
  1099. }
  1100. }
  1101. //----------------------------------------------------------------------------
  1102. bool cmFileCommand::HandleInstallDestination(cmFileInstaller& installer,
  1103. std::string& destination)
  1104. {
  1105. if ( destination.size() < 2 )
  1106. {
  1107. this->SetError("called with inapropriate arguments. "
  1108. "No DESTINATION provided or .");
  1109. return false;
  1110. }
  1111. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  1112. if ( destdir && *destdir )
  1113. {
  1114. std::string sdestdir = destdir;
  1115. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  1116. char ch1 = destination[0];
  1117. char ch2 = destination[1];
  1118. char ch3 = 0;
  1119. if ( destination.size() > 2 )
  1120. {
  1121. ch3 = destination[2];
  1122. }
  1123. int skip = 0;
  1124. if ( ch1 != '/' )
  1125. {
  1126. int relative = 0;
  1127. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  1128. ch2 == ':' )
  1129. {
  1130. // Assume windows
  1131. // let's do some destdir magic:
  1132. skip = 2;
  1133. if ( ch3 != '/' )
  1134. {
  1135. relative = 1;
  1136. }
  1137. }
  1138. else
  1139. {
  1140. relative = 1;
  1141. }
  1142. if ( relative )
  1143. {
  1144. // This is relative path on unix or windows. Since we are doing
  1145. // destdir, this case does not make sense.
  1146. this->SetError("called with relative DESTINATION. This "
  1147. "does not make sense when using DESTDIR. Specify "
  1148. "absolute path or remove DESTDIR environment variable.");
  1149. return false;
  1150. }
  1151. }
  1152. else
  1153. {
  1154. if ( ch2 == '/' )
  1155. {
  1156. // looks like a network path.
  1157. this->SetError("called with network path DESTINATION. This "
  1158. "does not make sense when using DESTDIR. Specify local "
  1159. "absolute path or remove DESTDIR environment variable.");
  1160. return false;
  1161. }
  1162. }
  1163. destination = sdestdir + (destination.c_str() + skip);
  1164. installer.DestDirLength = int(sdestdir.size());
  1165. }
  1166. if ( !cmSystemTools::FileExists(destination.c_str()) )
  1167. {
  1168. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  1169. {
  1170. std::string errstring = "cannot create directory: " + destination +
  1171. ". Maybe need administrative privileges.";
  1172. this->SetError(errstring.c_str());
  1173. return false;
  1174. }
  1175. }
  1176. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  1177. {
  1178. std::string errstring = "INSTALL destination: " + destination +
  1179. " is not a directory.";
  1180. this->SetError(errstring.c_str());
  1181. return false;
  1182. }
  1183. return true;
  1184. }
  1185. //----------------------------------------------------------------------------
  1186. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  1187. {
  1188. if ( args.size() < 6 )
  1189. {
  1190. this->SetError("called with incorrect number of arguments");
  1191. return false;
  1192. }
  1193. // Construct a file installer object.
  1194. cmFileInstaller installer(this, this->Makefile);
  1195. std::string rename = "";
  1196. std::string destination = "";
  1197. std::set<cmStdString> components;
  1198. std::set<cmStdString> configurations;
  1199. std::vector<std::string> files;
  1200. int itype = cmTarget::INSTALL_FILES;
  1201. std::map<cmStdString, const char*> properties;
  1202. bool optional = false;
  1203. bool result = this->ParseInstallArgs(args, installer, components,
  1204. configurations, properties,
  1205. itype, rename, destination, files,
  1206. optional);
  1207. if (result == true)
  1208. {
  1209. result = this->DoInstall(installer, components, configurations, properties,
  1210. itype, rename, destination, files, optional);
  1211. }
  1212. return result;
  1213. }
  1214. //----------------------------------------------------------------------------
  1215. bool cmFileCommand::ParseInstallArgs(std::vector<std::string> const& args,
  1216. cmFileInstaller& installer,
  1217. std::set<cmStdString>& components,
  1218. std::set<cmStdString>& configurations,
  1219. std::map<cmStdString, const char*>& properties,
  1220. int& itype,
  1221. std::string& rename,
  1222. std::string& destination,
  1223. std::vector<std::string>& files,
  1224. bool& optional)
  1225. {
  1226. std::string stype = "FILES";
  1227. bool doing_files = false;
  1228. bool doing_properties = false;
  1229. bool doing_permissions_file = false;
  1230. bool doing_permissions_dir = false;
  1231. bool doing_permissions_match = false;
  1232. bool doing_components = false;
  1233. bool doing_configurations = false;
  1234. bool use_given_permissions_file = false;
  1235. bool use_given_permissions_dir = false;
  1236. bool use_source_permissions = false;
  1237. mode_t permissions_file = 0;
  1238. mode_t permissions_dir = 0;
  1239. cmFileInstaller::MatchRule* current_match_rule = 0;
  1240. std::vector<std::string>::size_type i = 0;
  1241. i++; // Get rid of subcommand
  1242. for ( ; i != args.size(); ++i )
  1243. {
  1244. const std::string* cstr = &args[i];
  1245. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  1246. {
  1247. if(current_match_rule)
  1248. {
  1249. cmOStringStream e;
  1250. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1251. this->SetError(e.str().c_str());
  1252. return false;
  1253. }
  1254. i++;
  1255. destination = args[i];
  1256. doing_files = false;
  1257. doing_properties = false;
  1258. doing_permissions_file = false;
  1259. doing_permissions_dir = false;
  1260. doing_components = false;
  1261. doing_configurations = false;
  1262. }
  1263. else if ( *cstr == "TYPE" && i < args.size()-1 )
  1264. {
  1265. if(current_match_rule)
  1266. {
  1267. cmOStringStream e;
  1268. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1269. this->SetError(e.str().c_str());
  1270. return false;
  1271. }
  1272. i++;
  1273. stype = args[i];
  1274. if ( args[i+1] == "OPTIONAL" )
  1275. {
  1276. i++;
  1277. optional = true;
  1278. }
  1279. doing_properties = false;
  1280. doing_files = false;
  1281. doing_permissions_file = false;
  1282. doing_permissions_dir = false;
  1283. doing_components = false;
  1284. doing_configurations = false;
  1285. }
  1286. else if ( *cstr == "RENAME" && i < args.size()-1 )
  1287. {
  1288. if(current_match_rule)
  1289. {
  1290. cmOStringStream e;
  1291. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1292. this->SetError(e.str().c_str());
  1293. return false;
  1294. }
  1295. i++;
  1296. rename = args[i];
  1297. doing_properties = false;
  1298. doing_files = false;
  1299. doing_permissions_file = false;
  1300. doing_permissions_dir = false;
  1301. doing_components = false;
  1302. doing_configurations = false;
  1303. }
  1304. else if ( *cstr == "REGEX" && i < args.size()-1 )
  1305. {
  1306. i++;
  1307. installer.MatchRules.push_back(cmFileInstaller::MatchRule(args[i]));
  1308. current_match_rule = &*(installer.MatchRules.end()-1);
  1309. if(!current_match_rule->Regex.is_valid())
  1310. {
  1311. cmOStringStream e;
  1312. e << "INSTALL could not compile REGEX \"" << args[i] << "\".";
  1313. this->SetError(e.str().c_str());
  1314. return false;
  1315. }
  1316. doing_properties = false;
  1317. doing_files = false;
  1318. doing_permissions_file = false;
  1319. doing_permissions_dir = false;
  1320. doing_components = false;
  1321. doing_configurations = false;
  1322. }
  1323. else if ( *cstr == "EXCLUDE" )
  1324. {
  1325. // Add this property to the current match rule.
  1326. if(!current_match_rule)
  1327. {
  1328. cmOStringStream e;
  1329. e << "INSTALL does not allow \""
  1330. << *cstr << "\" before a REGEX is given.";
  1331. this->SetError(e.str().c_str());
  1332. return false;
  1333. }
  1334. current_match_rule->Properties.Exclude = true;
  1335. doing_permissions_match = true;
  1336. }
  1337. else if ( *cstr == "PROPERTIES" )
  1338. {
  1339. if(current_match_rule)
  1340. {
  1341. cmOStringStream e;
  1342. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1343. this->SetError(e.str().c_str());
  1344. return false;
  1345. }
  1346. doing_properties = true;
  1347. doing_files = false;
  1348. doing_permissions_file = false;
  1349. doing_permissions_dir = false;
  1350. doing_components = false;
  1351. doing_configurations = false;
  1352. }
  1353. else if ( *cstr == "PERMISSIONS" )
  1354. {
  1355. if(current_match_rule)
  1356. {
  1357. doing_permissions_match = true;
  1358. doing_permissions_file = false;
  1359. }
  1360. else
  1361. {
  1362. doing_permissions_match = false;
  1363. doing_permissions_file = true;
  1364. use_given_permissions_file = true;
  1365. }
  1366. doing_properties = false;
  1367. doing_files = false;
  1368. doing_permissions_dir = false;
  1369. doing_components = false;
  1370. doing_configurations = false;
  1371. }
  1372. else if ( *cstr == "DIR_PERMISSIONS" )
  1373. {
  1374. if(current_match_rule)
  1375. {
  1376. cmOStringStream e;
  1377. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1378. this->SetError(e.str().c_str());
  1379. return false;
  1380. }
  1381. use_given_permissions_dir = true;
  1382. doing_properties = false;
  1383. doing_files = false;
  1384. doing_permissions_file = false;
  1385. doing_permissions_dir = true;
  1386. doing_components = false;
  1387. doing_configurations = false;
  1388. }
  1389. else if ( *cstr == "USE_SOURCE_PERMISSIONS" )
  1390. {
  1391. if(current_match_rule)
  1392. {
  1393. cmOStringStream e;
  1394. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1395. this->SetError(e.str().c_str());
  1396. return false;
  1397. }
  1398. doing_properties = false;
  1399. doing_files = false;
  1400. doing_permissions_file = false;
  1401. doing_permissions_dir = false;
  1402. doing_components = false;
  1403. doing_configurations = false;
  1404. use_source_permissions = true;
  1405. }
  1406. else if ( *cstr == "COMPONENTS" )
  1407. {
  1408. if(current_match_rule)
  1409. {
  1410. cmOStringStream e;
  1411. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1412. this->SetError(e.str().c_str());
  1413. return false;
  1414. }
  1415. doing_properties = false;
  1416. doing_files = false;
  1417. doing_permissions_file = false;
  1418. doing_permissions_dir = false;
  1419. doing_components = true;
  1420. doing_configurations = false;
  1421. }
  1422. else if ( *cstr == "CONFIGURATIONS" )
  1423. {
  1424. if(current_match_rule)
  1425. {
  1426. cmOStringStream e;
  1427. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1428. this->SetError(e.str().c_str());
  1429. return false;
  1430. }
  1431. doing_properties = false;
  1432. doing_files = false;
  1433. doing_permissions_file = false;
  1434. doing_permissions_dir = false;
  1435. doing_components = false;
  1436. doing_configurations = true;
  1437. }
  1438. else if ( *cstr == "FILES" && !doing_files)
  1439. {
  1440. if(current_match_rule)
  1441. {
  1442. cmOStringStream e;
  1443. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1444. this->SetError(e.str().c_str());
  1445. return false;
  1446. }
  1447. doing_files = true;
  1448. doing_properties = false;
  1449. doing_permissions_file = false;
  1450. doing_permissions_dir = false;
  1451. doing_components = false;
  1452. doing_configurations = false;
  1453. }
  1454. else if ( doing_properties && i < args.size()-1 )
  1455. {
  1456. properties[args[i]] = args[i+1].c_str();
  1457. i++;
  1458. }
  1459. else if ( doing_files )
  1460. {
  1461. files.push_back(*cstr);
  1462. }
  1463. else if ( doing_components )
  1464. {
  1465. components.insert(*cstr);
  1466. }
  1467. else if ( doing_configurations )
  1468. {
  1469. configurations.insert(cmSystemTools::UpperCase(*cstr));
  1470. }
  1471. else if(doing_permissions_file)
  1472. {
  1473. if(!installer.CheckPermissions(args[i], permissions_file))
  1474. {
  1475. return false;
  1476. }
  1477. }
  1478. else if(doing_permissions_dir)
  1479. {
  1480. if(!installer.CheckPermissions(args[i], permissions_dir))
  1481. {
  1482. return false;
  1483. }
  1484. }
  1485. else if(doing_permissions_match)
  1486. {
  1487. if(!installer.CheckPermissions(
  1488. args[i], current_match_rule->Properties.Permissions))
  1489. {
  1490. return false;
  1491. }
  1492. }
  1493. else
  1494. {
  1495. this->SetError("called with inappropriate arguments");
  1496. return false;
  1497. }
  1498. }
  1499. // now check and postprocess what has been parsed
  1500. if ( files.size() == 0 )
  1501. {
  1502. this->SetError(
  1503. "called with inapropriate arguments. No FILES provided.");
  1504. return false;
  1505. }
  1506. // Check rename form.
  1507. if(!rename.empty())
  1508. {
  1509. if(itype != cmTarget::INSTALL_FILES &&
  1510. itype != cmTarget::INSTALL_PROGRAMS)
  1511. {
  1512. this->SetError("INSTALL option RENAME may be used only with "
  1513. "FILES or PROGRAMS.");
  1514. return false;
  1515. }
  1516. if(files.size() > 1)
  1517. {
  1518. this->SetError("INSTALL option RENAME may be used only with "
  1519. "one file.");
  1520. return false;
  1521. }
  1522. }
  1523. if (this->HandleInstallDestination(installer, destination) == false)
  1524. {
  1525. return false;
  1526. }
  1527. this->GetTargetTypeFromString(stype, itype);
  1528. this->HandleInstallPermissions(installer,
  1529. permissions_file,
  1530. permissions_dir,
  1531. itype,
  1532. use_given_permissions_file,
  1533. use_given_permissions_dir,
  1534. use_source_permissions);
  1535. return true;
  1536. }
  1537. //----------------------------------------------------------------------------
  1538. bool cmFileCommand::DoInstall( cmFileInstaller& installer,
  1539. const std::set<cmStdString>& components,
  1540. const std::set<cmStdString>& configurations,
  1541. std::map<cmStdString, const char*>& properties,
  1542. const int itype,
  1543. const std::string& rename,
  1544. const std::string& destination,
  1545. const std::vector<std::string>& files,
  1546. const bool optional)
  1547. {
  1548. typedef std::set<cmStdString>::const_iterator iter_type;
  1549. // Check for component-specific installation.
  1550. const char* cmake_install_component =
  1551. this->Makefile->GetDefinition("CMAKE_INSTALL_COMPONENT");
  1552. if(cmake_install_component && *cmake_install_component)
  1553. {
  1554. // This install rule applies only if it is associated with the
  1555. // current component.
  1556. if(iter_type(components.find(cmake_install_component)) ==
  1557. components.end())
  1558. {
  1559. return true;
  1560. }
  1561. }
  1562. // Check for configuration-specific installation.
  1563. if(!configurations.empty())
  1564. {
  1565. std::string cmake_install_configuration = cmSystemTools::UpperCase(
  1566. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_CONFIG_NAME"));
  1567. if(cmake_install_configuration.empty())
  1568. {
  1569. // No configuration specified for installation but this install
  1570. // rule is configuration-specific. Skip it.
  1571. return true;
  1572. }
  1573. else if(iter_type(configurations.find(cmake_install_configuration)) ==
  1574. configurations.end())
  1575. {
  1576. // This rule is specific to a configuration not being installed.
  1577. return true;
  1578. }
  1579. }
  1580. // Check whether files should be copied always or only if they have
  1581. // changed.
  1582. bool copy_always =
  1583. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  1584. // Handle each file listed.
  1585. for (std::vector<std::string>::size_type i = 0; i < files.size(); i ++ )
  1586. {
  1587. // Split the input file into its directory and name components.
  1588. std::vector<std::string> fromPathComponents;
  1589. cmSystemTools::SplitPath(files[i].c_str(), fromPathComponents);
  1590. std::string fromName = *(fromPathComponents.end()-1);
  1591. std::string fromDir = cmSystemTools::JoinPath(fromPathComponents.begin(),
  1592. fromPathComponents.end()-1);
  1593. // Compute the full path to the destination file.
  1594. std::string toFile = destination;
  1595. std::string const& toName = rename.empty()? fromName : rename;
  1596. if(!toName.empty())
  1597. {
  1598. toFile += "/";
  1599. toFile += toName;
  1600. }
  1601. // Handle type-specific installation details.
  1602. switch(itype)
  1603. {
  1604. case cmTarget::MODULE_LIBRARY:
  1605. case cmTarget::STATIC_LIBRARY:
  1606. case cmTarget::SHARED_LIBRARY:
  1607. {
  1608. // Handle shared library versioning
  1609. const char* lib_version = 0;
  1610. const char* lib_soversion = 0;
  1611. if ( properties.find("VERSION") != properties.end() )
  1612. {
  1613. lib_version = properties["VERSION"];
  1614. }
  1615. if ( properties.find("SOVERSION") != properties.end() )
  1616. {
  1617. lib_soversion = properties["SOVERSION"];
  1618. }
  1619. if ( !lib_version && lib_soversion )
  1620. {
  1621. lib_version = lib_soversion;
  1622. }
  1623. if ( !lib_soversion && lib_version )
  1624. {
  1625. lib_soversion = lib_version;
  1626. }
  1627. if ( lib_version && lib_soversion )
  1628. {
  1629. std::string libname = toFile;
  1630. std::string soname = toFile;
  1631. std::string soname_nopath = fromName;
  1632. this->ComputeVersionedName(soname, lib_soversion);
  1633. this->ComputeVersionedName(soname_nopath, lib_soversion);
  1634. this->ComputeVersionedName(fromName, lib_version);
  1635. this->ComputeVersionedName(toFile, lib_version);
  1636. cmSystemTools::RemoveFile(soname.c_str());
  1637. cmSystemTools::RemoveFile(libname.c_str());
  1638. if (!cmSystemTools::CreateSymlink(soname_nopath.c_str(),
  1639. libname.c_str()) )
  1640. {
  1641. std::string errstring = "error when creating symlink from: "
  1642. + libname + " to " + soname_nopath;
  1643. this->SetError(errstring.c_str());
  1644. return false;
  1645. }
  1646. installer.ManifestAppend(libname);
  1647. if ( toFile != soname )
  1648. {
  1649. if ( !cmSystemTools::CreateSymlink(fromName.c_str(),
  1650. soname.c_str()) )
  1651. {
  1652. std::string errstring = "error when creating symlink from: "
  1653. + soname + " to " + fromName;
  1654. this->SetError(errstring.c_str());
  1655. return false;
  1656. }
  1657. installer.ManifestAppend(soname);
  1658. }
  1659. }
  1660. }
  1661. break;
  1662. case cmTarget::EXECUTABLE:
  1663. {
  1664. // Handle executable versioning
  1665. const char* exe_version = 0;
  1666. if ( properties.find("VERSION") != properties.end() )
  1667. {
  1668. exe_version = properties["VERSION"];
  1669. }
  1670. if ( exe_version )
  1671. {
  1672. std::string exename = toFile;
  1673. std::string exename_nopath = fromName;
  1674. exename_nopath += "-";
  1675. exename_nopath += exe_version;
  1676. fromName += "-";
  1677. fromName += exe_version;
  1678. toFile += "-";
  1679. toFile += exe_version;
  1680. cmSystemTools::RemoveFile(exename.c_str());
  1681. if (!cmSystemTools::CreateSymlink(exename_nopath.c_str(),
  1682. exename.c_str()) )
  1683. {
  1684. std::string errstring = "error when creating symlink from: "
  1685. + exename + " to " + exename_nopath;
  1686. this->SetError(errstring.c_str());
  1687. return false;
  1688. }
  1689. installer.ManifestAppend(exename);
  1690. }
  1691. }
  1692. break;
  1693. }
  1694. // Construct the full path to the source file. The file name may
  1695. // have been changed above.
  1696. std::string fromFile = fromDir;
  1697. if(!fromName.empty())
  1698. {
  1699. fromFile += "/";
  1700. fromFile += fromName;
  1701. }
  1702. std::string message;
  1703. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  1704. {
  1705. if(itype == cmTarget::INSTALL_DIRECTORY &&
  1706. (fromFile.empty() ||
  1707. cmSystemTools::FileIsDirectory(fromFile.c_str())))
  1708. {
  1709. // Try installing this directory.
  1710. if(!installer.InstallDirectory(fromFile.c_str(), toFile.c_str(),
  1711. copy_always))
  1712. {
  1713. return false;
  1714. }
  1715. }
  1716. else if(cmSystemTools::FileExists(fromFile.c_str()))
  1717. {
  1718. // Install this file.
  1719. if(!installer.InstallFile(fromFile.c_str(), toFile.c_str(),
  1720. copy_always))
  1721. {
  1722. return false;
  1723. }
  1724. }
  1725. else if(!optional)
  1726. {
  1727. // The input file does not exist and installation is not optional.
  1728. cmOStringStream e;
  1729. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  1730. this->SetError(e.str().c_str());
  1731. return false;
  1732. }
  1733. }
  1734. }
  1735. return true;
  1736. }
  1737. //----------------------------------------------------------------------------
  1738. void cmFileCommand::ComputeVersionedName(std::string& name,
  1739. const char* version)
  1740. {
  1741. #if defined(__APPLE__)
  1742. std::string ext;
  1743. cmsys_stl::string::size_type dot_pos = name.rfind(".");
  1744. if(dot_pos != name.npos)
  1745. {
  1746. ext = name.substr(dot_pos, name.npos);
  1747. name = name.substr(0, dot_pos);
  1748. }
  1749. #endif
  1750. name += ".";
  1751. name += version;
  1752. #if defined(__APPLE__)
  1753. name += ext;
  1754. #endif
  1755. }
  1756. //----------------------------------------------------------------------------
  1757. bool cmFileCommand::HandleRelativePathCommand(
  1758. std::vector<std::string> const& args)
  1759. {
  1760. if(args.size() != 4 )
  1761. {
  1762. this->SetError("called with incorrect number of arguments");
  1763. return false;
  1764. }
  1765. const std::string& outVar = args[1];
  1766. const std::string& directoryName = args[2];
  1767. const std::string& fileName = args[3];
  1768. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  1769. {
  1770. std::string errstring =
  1771. "RelativePath must be passed a full path to the directory: "
  1772. + directoryName;
  1773. this->SetError(errstring.c_str());
  1774. return false;
  1775. }
  1776. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  1777. {
  1778. std::string errstring =
  1779. "RelativePath must be passed a full path to the file: "
  1780. + fileName;
  1781. this->SetError(errstring.c_str());
  1782. return false;
  1783. }
  1784. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  1785. fileName.c_str());
  1786. this->Makefile->AddDefinition(outVar.c_str(),
  1787. res.c_str());
  1788. return true;
  1789. }
  1790. //----------------------------------------------------------------------------
  1791. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  1792. bool recurse)
  1793. {
  1794. std::string message;
  1795. std::vector<std::string>::const_iterator i = args.begin();
  1796. i++; // Get rid of subcommand
  1797. for(;i != args.end(); ++i)
  1798. {
  1799. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  1800. {
  1801. cmSystemTools::RemoveADirectory(i->c_str());
  1802. }
  1803. else
  1804. {
  1805. cmSystemTools::RemoveFile(i->c_str());
  1806. }
  1807. }
  1808. return true;
  1809. }
  1810. //----------------------------------------------------------------------------
  1811. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  1812. const& args,
  1813. bool nativePath)
  1814. {
  1815. std::vector<std::string>::const_iterator i = args.begin();
  1816. if(args.size() != 3)
  1817. {
  1818. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  1819. "only three arguments.");
  1820. return false;
  1821. }
  1822. i++; // Get rid of subcommand
  1823. #if defined(_WIN32) && !defined(__CYGWIN__)
  1824. char pathSep = ';';
  1825. #else
  1826. char pathSep = ':';
  1827. #endif
  1828. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  1829. pathSep);
  1830. i++;
  1831. const char* var = i->c_str();
  1832. std::string value;
  1833. for(std::vector<cmsys::String>::iterator j = path.begin();
  1834. j != path.end(); ++j)
  1835. {
  1836. if(j != path.begin())
  1837. {
  1838. value += ";";
  1839. }
  1840. if(!nativePath)
  1841. {
  1842. cmSystemTools::ConvertToUnixSlashes(*j);
  1843. }
  1844. else
  1845. {
  1846. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  1847. // remove double quotes in the path
  1848. cmsys::String& s = *j;
  1849. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  1850. {
  1851. s = s.substr(1,s.size()-2);
  1852. }
  1853. }
  1854. value += *j;
  1855. }
  1856. this->Makefile->AddDefinition(var, value.c_str());
  1857. return true;
  1858. }