cmCPackDebGenerator.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCPackDebGenerator.h"
  11. #include "cmSystemTools.h"
  12. #include "cmMakefile.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmCPackLog.h"
  15. #include <cmsys/SystemTools.hxx>
  16. #include <cmsys/Glob.hxx>
  17. #include <limits.h> // USHRT_MAX
  18. // NOTE:
  19. // A debian package .deb is simply an 'ar' archive. The only subtle difference
  20. // is that debian uses the BSD ar style archive whereas most Linux distro have
  21. // a GNU ar.
  22. // See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=161593 for more info
  23. // Therefore we provide our own implementation of a BSD-ar:
  24. static int ar_append(const char*archive,const std::vector<std::string>& files);
  25. //----------------------------------------------------------------------
  26. cmCPackDebGenerator::cmCPackDebGenerator()
  27. {
  28. }
  29. //----------------------------------------------------------------------
  30. cmCPackDebGenerator::~cmCPackDebGenerator()
  31. {
  32. }
  33. //----------------------------------------------------------------------
  34. int cmCPackDebGenerator::InitializeInternal()
  35. {
  36. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/usr");
  37. if (cmSystemTools::IsOff(this->GetOption("CPACK_SET_DESTDIR")))
  38. {
  39. this->SetOption("CPACK_SET_DESTDIR", "I_ON");
  40. }
  41. return this->Superclass::InitializeInternal();
  42. }
  43. //----------------------------------------------------------------------
  44. int cmCPackDebGenerator::PackageOnePack(std::string initialTopLevel,
  45. std::string packageName)
  46. {
  47. int retval = 1;
  48. // Begin the archive for this pack
  49. std::string localToplevel(initialTopLevel);
  50. std::string packageFileName(
  51. cmSystemTools::GetParentDirectory(toplevel)
  52. );
  53. std::string outputFileName(
  54. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))
  55. +"-"+packageName + this->GetOutputExtension()
  56. );
  57. localToplevel += "/"+ packageName;
  58. /* replace the TEMP DIRECTORY with the component one */
  59. this->SetOption("CPACK_TEMPORARY_DIRECTORY",localToplevel.c_str());
  60. packageFileName += "/"+ outputFileName;
  61. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  62. this->SetOption("CPACK_OUTPUT_FILE_NAME",outputFileName.c_str());
  63. /* replace the TEMPORARY package file name */
  64. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  65. packageFileName.c_str());
  66. // Tell CPackDeb.cmake the name of the component GROUP.
  67. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT",packageName.c_str());
  68. // Tell CPackDeb.cmake the path where the component is.
  69. std::string component_path = "/";
  70. component_path += packageName;
  71. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT_PART_PATH",
  72. component_path.c_str());
  73. if (!this->ReadListFile("CPackDeb.cmake"))
  74. {
  75. cmCPackLogger(cmCPackLog::LOG_ERROR,
  76. "Error while execution CPackDeb.cmake" << std::endl);
  77. retval = 0;
  78. return retval;
  79. }
  80. cmsys::Glob gl;
  81. std::string findExpr(this->GetOption("WDIR"));
  82. findExpr += "/*";
  83. gl.RecurseOn();
  84. if ( !gl.FindFiles(findExpr) )
  85. {
  86. cmCPackLogger(cmCPackLog::LOG_ERROR,
  87. "Cannot find any files in the installed directory" << std::endl);
  88. return 0;
  89. }
  90. packageFiles = gl.GetFiles();
  91. int res = createDeb();
  92. if (res != 1)
  93. {
  94. retval = 0;
  95. }
  96. // add the generated package to package file names list
  97. packageFileNames.push_back(packageFileName);
  98. return retval;
  99. }
  100. //----------------------------------------------------------------------
  101. int cmCPackDebGenerator::PackageComponents(bool ignoreGroup)
  102. {
  103. int retval = 1;
  104. /* Reset package file name list it will be populated during the
  105. * component packaging run*/
  106. packageFileNames.clear();
  107. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  108. // The default behavior is to have one package by component group
  109. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  110. if (!ignoreGroup)
  111. {
  112. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  113. for (compGIt=this->ComponentGroups.begin();
  114. compGIt!=this->ComponentGroups.end(); ++compGIt)
  115. {
  116. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  117. << compGIt->first
  118. << std::endl);
  119. // Begin the archive for this group
  120. retval &= PackageOnePack(initialTopLevel,compGIt->first);
  121. }
  122. // Handle Orphan components (components not belonging to any groups)
  123. std::map<std::string, cmCPackComponent>::iterator compIt;
  124. for (compIt=this->Components.begin();
  125. compIt!=this->Components.end(); ++compIt )
  126. {
  127. // Does the component belong to a group?
  128. if (compIt->second.Group==NULL)
  129. {
  130. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  131. "Component <"
  132. << compIt->second.Name
  133. << "> does not belong to any group, package it separately."
  134. << std::endl);
  135. // Begin the archive for this orphan component
  136. retval &= PackageOnePack(initialTopLevel,compIt->first);
  137. }
  138. }
  139. }
  140. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  141. // We build 1 package per component
  142. else
  143. {
  144. std::map<std::string, cmCPackComponent>::iterator compIt;
  145. for (compIt=this->Components.begin();
  146. compIt!=this->Components.end(); ++compIt )
  147. {
  148. retval &= PackageOnePack(initialTopLevel,compIt->first);
  149. }
  150. }
  151. return retval;
  152. }
  153. //----------------------------------------------------------------------
  154. int cmCPackDebGenerator::PackageComponentsAllInOne()
  155. {
  156. int retval = 1;
  157. std::string compInstDirName;
  158. /* Reset package file name list it will be populated during the
  159. * component packaging run*/
  160. packageFileNames.clear();
  161. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  162. compInstDirName = "ALL_COMPONENTS_IN_ONE";
  163. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  164. "Packaging all groups in one package..."
  165. "(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
  166. << std::endl);
  167. // The ALL GROUPS in ONE package case
  168. std::string localToplevel(initialTopLevel);
  169. std::string packageFileName(
  170. cmSystemTools::GetParentDirectory(toplevel)
  171. );
  172. std::string outputFileName(
  173. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))
  174. + this->GetOutputExtension()
  175. );
  176. // all GROUP in one vs all COMPONENT in one
  177. localToplevel += "/"+compInstDirName;
  178. /* replace the TEMP DIRECTORY with the component one */
  179. this->SetOption("CPACK_TEMPORARY_DIRECTORY",localToplevel.c_str());
  180. packageFileName += "/"+ outputFileName;
  181. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  182. this->SetOption("CPACK_OUTPUT_FILE_NAME",outputFileName.c_str());
  183. /* replace the TEMPORARY package file name */
  184. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  185. packageFileName.c_str());
  186. // Tell CPackDeb.cmake the path where the component is.
  187. std::string component_path = "/";
  188. component_path += compInstDirName;
  189. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT_PART_PATH",
  190. component_path.c_str());
  191. if (!this->ReadListFile("CPackDeb.cmake"))
  192. {
  193. cmCPackLogger(cmCPackLog::LOG_ERROR,
  194. "Error while execution CPackDeb.cmake" << std::endl);
  195. retval = 0;
  196. return retval;
  197. }
  198. cmsys::Glob gl;
  199. std::string findExpr(this->GetOption("WDIR"));
  200. findExpr += "/*";
  201. gl.RecurseOn();
  202. if ( !gl.FindFiles(findExpr) )
  203. {
  204. cmCPackLogger(cmCPackLog::LOG_ERROR,
  205. "Cannot find any files in the installed directory" << std::endl);
  206. return 0;
  207. }
  208. packageFiles = gl.GetFiles();
  209. int res = createDeb();
  210. if (res != 1)
  211. {
  212. retval = 0;
  213. }
  214. // add the generated package to package file names list
  215. packageFileNames.push_back(packageFileName);
  216. return retval;
  217. }
  218. //----------------------------------------------------------------------
  219. int cmCPackDebGenerator::PackageFiles()
  220. {
  221. int retval = -1;
  222. /* Are we in the component packaging case */
  223. if (WantsComponentInstallation()) {
  224. // CASE 1 : COMPONENT ALL-IN-ONE package
  225. // If ALL GROUPS or ALL COMPONENTS in ONE package has been requested
  226. // then the package file is unique and should be open here.
  227. if (componentPackageMethod == ONE_PACKAGE)
  228. {
  229. return PackageComponentsAllInOne();
  230. }
  231. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  232. // There will be 1 package for each component group
  233. // however one may require to ignore component group and
  234. // in this case you'll get 1 package for each component.
  235. else
  236. {
  237. return PackageComponents(componentPackageMethod ==
  238. ONE_PACKAGE_PER_COMPONENT);
  239. }
  240. }
  241. // CASE 3 : NON COMPONENT package.
  242. else
  243. {
  244. if (!this->ReadListFile("CPackDeb.cmake"))
  245. {
  246. cmCPackLogger(cmCPackLog::LOG_ERROR,
  247. "Error while execution CPackDeb.cmake" << std::endl);
  248. retval = 0;
  249. }
  250. else
  251. {
  252. packageFiles = files;
  253. return createDeb();
  254. }
  255. }
  256. return retval;
  257. }
  258. int cmCPackDebGenerator::createDeb()
  259. {
  260. const char* cmakeExecutable = this->GetOption("CMAKE_COMMAND");
  261. // debian-binary file
  262. std::string dbfilename;
  263. dbfilename += this->GetOption("WDIR");
  264. dbfilename += "/debian-binary";
  265. { // the scope is needed for cmGeneratedFileStream
  266. cmGeneratedFileStream out(dbfilename.c_str());
  267. out << "2.0";
  268. out << std::endl; // required for valid debian package
  269. }
  270. // control file
  271. std::string ctlfilename;
  272. ctlfilename = this->GetOption("WDIR");
  273. ctlfilename += "/control";
  274. // debian policy enforce lower case for package name
  275. // mandatory entries:
  276. std::string debian_pkg_name = cmsys::SystemTools::LowerCase(
  277. this->GetOption("CPACK_DEBIAN_PACKAGE_NAME") );
  278. const char* debian_pkg_version =
  279. this->GetOption("CPACK_DEBIAN_PACKAGE_VERSION");
  280. const char* debian_pkg_section =
  281. this->GetOption("CPACK_DEBIAN_PACKAGE_SECTION");
  282. const char* debian_pkg_priority =
  283. this->GetOption("CPACK_DEBIAN_PACKAGE_PRIORITY");
  284. const char* debian_pkg_arch =
  285. this->GetOption("CPACK_DEBIAN_PACKAGE_ARCHITECTURE");
  286. const char* maintainer = this->GetOption("CPACK_DEBIAN_PACKAGE_MAINTAINER");
  287. const char* desc = this->GetOption("CPACK_DEBIAN_PACKAGE_DESCRIPTION");
  288. // optional entries
  289. const char* debian_pkg_dep = this->GetOption("CPACK_DEBIAN_PACKAGE_DEPENDS");
  290. const char* debian_pkg_rec =
  291. this->GetOption("CPACK_DEBIAN_PACKAGE_RECOMMENDS");
  292. const char* debian_pkg_sug =
  293. this->GetOption("CPACK_DEBIAN_PACKAGE_SUGGESTS");
  294. const char* debian_pkg_url =
  295. this->GetOption("CPACK_DEBIAN_PACKAGE_HOMEPAGE");
  296. const char* debian_pkg_predep =
  297. this->GetOption("CPACK_DEBIAN_PACKAGE_PREDEPENDS");
  298. const char* debian_pkg_enhances =
  299. this->GetOption("CPACK_DEBIAN_PACKAGE_ENHANCES");
  300. const char* debian_pkg_breaks =
  301. this->GetOption("CPACK_DEBIAN_PACKAGE_BREAKS");
  302. const char* debian_pkg_conflicts =
  303. this->GetOption("CPACK_DEBIAN_PACKAGE_CONFLICTS");
  304. const char* debian_pkg_provides =
  305. this->GetOption("CPACK_DEBIAN_PACKAGE_PROVIDES");
  306. const char* debian_pkg_replaces =
  307. this->GetOption("CPACK_DEBIAN_PACKAGE_REPLACES");
  308. { // the scope is needed for cmGeneratedFileStream
  309. cmGeneratedFileStream out(ctlfilename.c_str());
  310. out << "Package: " << debian_pkg_name << "\n";
  311. out << "Version: " << debian_pkg_version << "\n";
  312. out << "Section: " << debian_pkg_section << "\n";
  313. out << "Priority: " << debian_pkg_priority << "\n";
  314. out << "Architecture: " << debian_pkg_arch << "\n";
  315. if(debian_pkg_dep && *debian_pkg_dep)
  316. {
  317. out << "Depends: " << debian_pkg_dep << "\n";
  318. }
  319. if(debian_pkg_rec && *debian_pkg_rec)
  320. {
  321. out << "Recommends: " << debian_pkg_rec << "\n";
  322. }
  323. if(debian_pkg_sug && *debian_pkg_sug)
  324. {
  325. out << "Suggests: " << debian_pkg_sug << "\n";
  326. }
  327. if(debian_pkg_url && *debian_pkg_url)
  328. {
  329. out << "Homepage: " << debian_pkg_url << "\n";
  330. }
  331. if (debian_pkg_predep && *debian_pkg_predep)
  332. {
  333. out << "Pre-Depends: " << debian_pkg_predep << "\n";
  334. }
  335. if (debian_pkg_enhances && *debian_pkg_enhances)
  336. {
  337. out << "Enhances: " << debian_pkg_enhances << "\n";
  338. }
  339. if (debian_pkg_breaks && *debian_pkg_breaks)
  340. {
  341. out << "Breaks: " << debian_pkg_breaks << "\n";
  342. }
  343. if (debian_pkg_conflicts && *debian_pkg_conflicts)
  344. {
  345. out << "Conflicts: " << debian_pkg_conflicts << "\n";
  346. }
  347. if (debian_pkg_provides && *debian_pkg_provides)
  348. {
  349. out << "Provides: " << debian_pkg_provides << "\n";
  350. }
  351. if (debian_pkg_replaces && *debian_pkg_replaces)
  352. {
  353. out << "Replaces: " << debian_pkg_replaces << "\n";
  354. }
  355. unsigned long totalSize = 0;
  356. {
  357. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  358. dirName += '/';
  359. for (std::vector<std::string>::const_iterator fileIt =
  360. packageFiles.begin();
  361. fileIt != packageFiles.end(); ++ fileIt )
  362. {
  363. totalSize += cmSystemTools::FileLength(*fileIt);
  364. }
  365. }
  366. out << "Installed-Size: " << (totalSize + 1023) / 1024 << "\n";
  367. out << "Maintainer: " << maintainer << "\n";
  368. out << "Description: " << desc << "\n";
  369. out << std::endl;
  370. }
  371. std::string cmd;
  372. if (NULL != this->GetOption("CPACK_DEBIAN_FAKEROOT_EXECUTABLE")) {
  373. cmd += this->GetOption("CPACK_DEBIAN_FAKEROOT_EXECUTABLE");
  374. }
  375. const char* debian_compression_type =
  376. this->GetOption("CPACK_DEBIAN_COMPRESSION_TYPE");
  377. if(!debian_compression_type)
  378. {
  379. debian_compression_type = "gzip";
  380. }
  381. std::string cmake_tar = " ", compression_modifier = "a", compression_suffix;
  382. if(!strcmp(debian_compression_type, "lzma")) {
  383. compression_suffix = ".lzma";
  384. } else if(!strcmp(debian_compression_type, "xz")) {
  385. compression_suffix = ".xz";
  386. } else if(!strcmp(debian_compression_type, "bzip2")) {
  387. compression_suffix = ".bz2";
  388. compression_modifier = "j";
  389. cmake_tar += "\"" + std::string(cmakeExecutable) + "\" -E ";
  390. } else if(!strcmp(debian_compression_type, "gzip")) {
  391. compression_suffix = ".gz";
  392. compression_modifier = "z";
  393. cmake_tar += "\"" + std::string(cmakeExecutable) + "\" -E ";
  394. } else if(!strcmp(debian_compression_type, "none")) {
  395. compression_suffix = "";
  396. compression_modifier = "";
  397. cmake_tar += "\"" + std::string(cmakeExecutable) + "\" -E ";
  398. } else {
  399. cmCPackLogger(cmCPackLog::LOG_ERROR,
  400. "Error unrecognized compression type: "
  401. << debian_compression_type << std::endl);
  402. }
  403. cmd += cmake_tar + "tar c" + compression_modifier + "f data.tar"
  404. + compression_suffix;
  405. // now add all directories which have to be compressed
  406. // collect all top level install dirs for that
  407. // e.g. /opt/bin/foo, /usr/bin/bar and /usr/bin/baz would give /usr and /opt
  408. size_t topLevelLength = std::string(this->GetOption("WDIR")).length();
  409. cmCPackLogger(cmCPackLog::LOG_DEBUG, "WDIR: \"" << this->GetOption("WDIR")
  410. << "\", length = " << topLevelLength
  411. << std::endl);
  412. std::set<std::string> installDirs;
  413. for (std::vector<std::string>::const_iterator fileIt =
  414. packageFiles.begin();
  415. fileIt != packageFiles.end(); ++ fileIt )
  416. {
  417. cmCPackLogger(cmCPackLog::LOG_DEBUG, "FILEIT: \"" << *fileIt << "\""
  418. << std::endl);
  419. std::string::size_type slashPos = fileIt->find('/', topLevelLength+1);
  420. std::string relativeDir = fileIt->substr(topLevelLength,
  421. slashPos - topLevelLength);
  422. cmCPackLogger(cmCPackLog::LOG_DEBUG, "RELATIVEDIR: \"" << relativeDir
  423. << "\"" << std::endl);
  424. if (installDirs.find(relativeDir) == installDirs.end())
  425. {
  426. installDirs.insert(relativeDir);
  427. cmd += " .";
  428. cmd += relativeDir;
  429. }
  430. }
  431. std::string output;
  432. int retval = -1;
  433. int res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output, &output,
  434. &retval, this->GetOption("WDIR"), this->GeneratorVerbose, 0);
  435. if ( !res || retval )
  436. {
  437. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  438. tmpFile += "/Deb.log";
  439. cmGeneratedFileStream ofs(tmpFile.c_str());
  440. ofs << "# Run command: " << cmd << std::endl
  441. << "# Working directory: " << toplevel << std::endl
  442. << "# Output:" << std::endl
  443. << output << std::endl;
  444. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running tar command: "
  445. << cmd << std::endl
  446. << "Please check " << tmpFile << " for errors" << std::endl);
  447. return 0;
  448. }
  449. std::string md5filename;
  450. md5filename = this->GetOption("WDIR");
  451. md5filename += "/md5sums";
  452. { // the scope is needed for cmGeneratedFileStream
  453. cmGeneratedFileStream out(md5filename.c_str());
  454. std::vector<std::string>::const_iterator fileIt;
  455. // std::string topLevelWithTrailingSlash = toplevel;
  456. std::string topLevelWithTrailingSlash =
  457. this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  458. topLevelWithTrailingSlash += '/';
  459. for ( fileIt = packageFiles.begin();
  460. fileIt != packageFiles.end(); ++ fileIt )
  461. {
  462. cmd = "\"";
  463. cmd += cmakeExecutable;
  464. cmd += "\" -E md5sum \"";
  465. cmd += *fileIt;
  466. cmd += "\"";
  467. //std::string output;
  468. //int retVal = -1;
  469. res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output, &output,
  470. &retval, toplevel.c_str(), this->GeneratorVerbose, 0);
  471. if ( !res || retval )
  472. {
  473. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running cmake -E md5sum "
  474. << cmd << std::endl);
  475. }
  476. // debian md5sums entries are like this:
  477. // 014f3604694729f3bf19263bac599765 usr/bin/ccmake
  478. // thus strip the full path (with the trailing slash)
  479. cmSystemTools::ReplaceString(output,
  480. topLevelWithTrailingSlash.c_str(), "");
  481. out << output;
  482. }
  483. // each line contains a eol.
  484. // Do not end the md5sum file with yet another (invalid)
  485. }
  486. cmd = "";
  487. if (NULL != this->GetOption("CPACK_DEBIAN_FAKEROOT_EXECUTABLE"))
  488. {
  489. cmd = this->GetOption("CPACK_DEBIAN_FAKEROOT_EXECUTABLE");
  490. }
  491. cmd += cmake_tar + "tar czf control.tar.gz ./control ./md5sums";
  492. const char* controlExtra =
  493. this->GetOption("CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA");
  494. if( controlExtra )
  495. {
  496. std::vector<std::string> controlExtraList;
  497. cmSystemTools::ExpandListArgument(controlExtra, controlExtraList);
  498. for(std::vector<std::string>::iterator i =
  499. controlExtraList.begin(); i != controlExtraList.end(); ++i)
  500. {
  501. std::string filenamename =
  502. cmsys::SystemTools::GetFilenameName(*i);
  503. std::string localcopy = this->GetOption("WDIR");
  504. localcopy += "/";
  505. localcopy += filenamename;
  506. // if we can copy the file, it means it does exist, let's add it:
  507. if( cmsys::SystemTools::CopyFileIfDifferent(
  508. *i, localcopy) )
  509. {
  510. // debian is picky and need relative to ./ path in the tar.*
  511. cmd += " ./";
  512. cmd += filenamename;
  513. }
  514. }
  515. }
  516. res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output, &output,
  517. &retval, this->GetOption("WDIR"), this->GeneratorVerbose, 0);
  518. if ( !res || retval )
  519. {
  520. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  521. tmpFile += "/Deb.log";
  522. cmGeneratedFileStream ofs(tmpFile.c_str());
  523. ofs << "# Run command: " << cmd << std::endl
  524. << "# Working directory: " << toplevel << std::endl
  525. << "# Output:" << std::endl
  526. << output << std::endl;
  527. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running tar command: "
  528. << cmd << std::endl
  529. << "Please check " << tmpFile << " for errors" << std::endl);
  530. return 0;
  531. }
  532. // ar -r your-package-name.deb debian-binary control.tar.* data.tar.*
  533. // since debian packages require BSD ar (most Linux distros and even
  534. // FreeBSD and NetBSD ship GNU ar) we use a copy of OpenBSD ar here.
  535. std::vector<std::string> arFiles;
  536. std::string topLevelString = this->GetOption("WDIR");
  537. topLevelString += "/";
  538. arFiles.push_back(topLevelString + "debian-binary");
  539. arFiles.push_back(topLevelString + "control.tar.gz");
  540. arFiles.push_back(topLevelString + "data.tar" + compression_suffix);
  541. std::string outputFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  542. outputFileName += "/";
  543. outputFileName += this->GetOption("CPACK_OUTPUT_FILE_NAME");
  544. res = ar_append(outputFileName.c_str(), arFiles);
  545. if ( res!=0 )
  546. {
  547. std::string tmpFile = this->GetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME");
  548. tmpFile += "/Deb.log";
  549. cmGeneratedFileStream ofs(tmpFile.c_str());
  550. ofs << "# Problem creating archive using: " << res << std::endl;
  551. return 0;
  552. }
  553. return 1;
  554. }
  555. bool cmCPackDebGenerator::SupportsComponentInstallation() const
  556. {
  557. if (IsOn("CPACK_DEB_COMPONENT_INSTALL"))
  558. {
  559. return true;
  560. }
  561. else
  562. {
  563. return false;
  564. }
  565. }
  566. std::string cmCPackDebGenerator::GetComponentInstallDirNameSuffix(
  567. const std::string& componentName)
  568. {
  569. if (componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  570. return componentName;
  571. }
  572. if (componentPackageMethod == ONE_PACKAGE) {
  573. return std::string("ALL_COMPONENTS_IN_ONE");
  574. }
  575. // We have to find the name of the COMPONENT GROUP
  576. // the current COMPONENT belongs to.
  577. std::string groupVar = "CPACK_COMPONENT_" +
  578. cmSystemTools::UpperCase(componentName) + "_GROUP";
  579. if (NULL != GetOption(groupVar))
  580. {
  581. return std::string(GetOption(groupVar));
  582. }
  583. else
  584. {
  585. return componentName;
  586. }
  587. }
  588. // The following code is taken from OpenBSD ar:
  589. // http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ar/
  590. // It has been slightly modified:
  591. // -return error codes instead exit() in functions
  592. // -use the stdio file I/O functions instead the file descriptor based ones
  593. // -merged into one cxx file
  594. // -no additional options supported
  595. // The coding style hasn't been modified.
  596. /*-
  597. * Copyright (c) 1990, 1993, 1994
  598. * The Regents of the University of California. All rights reserved.
  599. *
  600. * This code is derived from software contributed to Berkeley by
  601. * Hugh Smith at The University of Guelph.
  602. *
  603. * Redistribution and use in source and binary forms, with or without
  604. * modification, are permitted provided that the following conditions
  605. * are met:
  606. * 1. Redistributions of source code must retain the above copyright
  607. * notice, this list of conditions and the following disclaimer.
  608. * 2. Redistributions in binary form must reproduce the above copyright
  609. * notice, this list of conditions and the following disclaimer in the
  610. * documentation and/or other materials provided with the distribution.
  611. * 3. Neither the name of the University nor the names of its contributors
  612. * may be used to endorse or promote products derived from this software
  613. * without specific prior written permission.
  614. *
  615. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  616. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  617. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  618. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  619. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  620. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  621. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  622. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  623. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  624. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  625. * SUCH DAMAGE.
  626. */
  627. #include <sys/types.h>
  628. #include <sys/stat.h>
  629. #include <stdio.h>
  630. #include <string.h>
  631. #include <stdlib.h>
  632. #define ARMAG "!<arch>\n" /* ar "magic number" */
  633. #define SARMAG 8 /* strlen(ARMAG); */
  634. #define AR_EFMT1 "#1/" /* extended format #1 */
  635. #define ARFMAG "`\n"
  636. /* Header format strings. */
  637. #define HDR1 "%s%-13d%-12ld%-6u%-6u%-8o%-10lld%2s"
  638. #define HDR2 "%-16.16s%-12ld%-6u%-6u%-8o%-10lld%2s"
  639. struct ar_hdr {
  640. char ar_name[16]; /* name */
  641. char ar_date[12]; /* modification time */
  642. char ar_uid[6]; /* user id */
  643. char ar_gid[6]; /* group id */
  644. char ar_mode[8]; /* octal file permissions */
  645. char ar_size[10]; /* size in bytes */
  646. char ar_fmag[2]; /* consistency check */
  647. };
  648. /* Set up file copy. */
  649. #define SETCF(from, fromname, to, toname, pad) { \
  650. cf.rFile = from; \
  651. cf.rname = fromname; \
  652. cf.wFile = to; \
  653. cf.wname = toname; \
  654. cf.flags = pad; \
  655. }
  656. /* File copy structure. */
  657. typedef struct {
  658. FILE* rFile; /* read file descriptor */
  659. const char *rname; /* read name */
  660. FILE* wFile; /* write file descriptor */
  661. const char *wname; /* write name */
  662. #define NOPAD 0x00 /* don't pad */
  663. #define WPAD 0x02 /* pad on writes */
  664. unsigned int flags; /* pad flags */
  665. } CF;
  666. /* misc.c */
  667. static const char * ar_rname(const char *path)
  668. {
  669. const char *ind = strrchr(path, '/');
  670. return (ind ) ? ind + 1 : path;
  671. }
  672. /* archive.c */
  673. typedef struct ar_hdr HDR;
  674. static char ar_hb[sizeof(HDR) + 1]; /* real header */
  675. static size_t ar_already_written;
  676. /* copy_ar --
  677. * Copy size bytes from one file to another - taking care to handle the
  678. * extra byte (for odd size files) when reading archives and writing an
  679. * extra byte if necessary when adding files to archive. The length of
  680. * the object is the long name plus the object itself; the variable
  681. * already_written gets set if a long name was written.
  682. *
  683. * The padding is really unnecessary, and is almost certainly a remnant
  684. * of early archive formats where the header included binary data which
  685. * a PDP-11 required to start on an even byte boundary. (Or, perhaps,
  686. * because 16-bit word addressed copies were faster?) Anyhow, it should
  687. * have been ripped out long ago.
  688. */
  689. static int copy_ar(CF *cfp, off_t size)
  690. {
  691. static char pad = '\n';
  692. off_t sz = size;
  693. size_t nr, nw;
  694. char buf[8*1024];
  695. if (sz == 0)
  696. return 0;
  697. FILE* from = cfp->rFile;
  698. FILE* to = cfp->wFile;
  699. while (sz &&
  700. (nr = fread(buf, 1, sz < static_cast<off_t>(sizeof(buf))
  701. ? static_cast<size_t>(sz) : sizeof(buf), from ))
  702. > 0) {
  703. sz -= nr;
  704. for (size_t off = 0; off < nr; nr -= off, off += nw)
  705. if ((nw = fwrite(buf + off, 1, nr, to)) < nr)
  706. return -1;
  707. }
  708. if (sz)
  709. return -2;
  710. if (cfp->flags & WPAD && (size + ar_already_written) & 1
  711. && fwrite(&pad, 1, 1, to) != 1)
  712. return -4;
  713. return 0;
  714. }
  715. /* put_arobj -- Write an archive member to a file. */
  716. static int put_arobj(CF *cfp, struct stat *sb)
  717. {
  718. int result = 0;
  719. struct ar_hdr *hdr;
  720. /* If passed an sb structure, reading a file from disk. Get stat(2)
  721. * information, build a name and construct a header. (Files are named
  722. * by their last component in the archive.) */
  723. const char* name = ar_rname(cfp->rname);
  724. (void)stat(cfp->rname, sb);
  725. /* If not truncating names and the name is too long or contains
  726. * a space, use extended format 1. */
  727. size_t lname = strlen(name);
  728. uid_t uid = sb->st_uid;
  729. gid_t gid = sb->st_gid;
  730. if (uid > USHRT_MAX) {
  731. uid = USHRT_MAX;
  732. }
  733. if (gid > USHRT_MAX) {
  734. gid = USHRT_MAX;
  735. }
  736. if (lname > sizeof(hdr->ar_name) || strchr(name, ' '))
  737. (void)sprintf(ar_hb, HDR1, AR_EFMT1, (int)lname,
  738. (long int)sb->st_mtime, (unsigned)uid, (unsigned)gid,
  739. (unsigned)sb->st_mode, (long long)sb->st_size + lname,
  740. ARFMAG);
  741. else {
  742. lname = 0;
  743. (void)sprintf(ar_hb, HDR2, name,
  744. (long int)sb->st_mtime, (unsigned)uid, (unsigned)gid,
  745. (unsigned)sb->st_mode, (long long)sb->st_size,
  746. ARFMAG);
  747. }
  748. off_t size = sb->st_size;
  749. if (fwrite(ar_hb, 1, sizeof(HDR), cfp->wFile) != sizeof(HDR))
  750. return -1;
  751. if (lname) {
  752. if (fwrite(name, 1, lname, cfp->wFile) != lname)
  753. return -2;
  754. ar_already_written = lname;
  755. }
  756. result = copy_ar(cfp, size);
  757. ar_already_written = 0;
  758. return result;
  759. }
  760. /* append.c */
  761. /* append --
  762. * Append files to the archive - modifies original archive or creates
  763. * a new archive if named archive does not exist.
  764. */
  765. static int ar_append(const char* archive,const std::vector<std::string>& files)
  766. {
  767. int eval = 0;
  768. FILE* aFile = cmSystemTools::Fopen(archive, "wb+");
  769. if (aFile!=NULL) {
  770. fwrite(ARMAG, SARMAG, 1, aFile);
  771. if (fseek(aFile, 0, SEEK_END) != -1) {
  772. CF cf;
  773. struct stat sb;
  774. /* Read from disk, write to an archive; pad on write. */
  775. SETCF(NULL, 0, aFile, archive, WPAD);
  776. for(std::vector<std::string>::const_iterator fileIt = files.begin();
  777. fileIt!=files.end(); ++fileIt) {
  778. const char* filename = fileIt->c_str();
  779. FILE* file = cmSystemTools::Fopen(filename, "rb");
  780. if (file == NULL) {
  781. eval = -1;
  782. continue;
  783. }
  784. cf.rFile = file;
  785. cf.rname = filename;
  786. int result = put_arobj(&cf, &sb);
  787. (void)fclose(file);
  788. if (result!=0) {
  789. eval = -2;
  790. break;
  791. }
  792. }
  793. }
  794. else {
  795. eval = -3;
  796. }
  797. fclose(aFile);
  798. }
  799. else {
  800. eval = -4;
  801. }
  802. return eval;
  803. }