PageRenderTime 313ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 1ms

/Source/cmFileCommand.cxx

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