cmCPackDebGenerator.cxx 30 KB

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