cmCPackDebGenerator.cxx 28 KB

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