cmCPackDebGenerator.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. // 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.c_str())
  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->c_str());
  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. cmd += " \"";
  376. cmd += cmakeExecutable;
  377. cmd += "\" -E tar cfz data.tar.gz ";
  378. // now add all directories which have to be compressed
  379. // collect all top level install dirs for that
  380. // e.g. /opt/bin/foo, /usr/bin/bar and /usr/bin/baz would give /usr and /opt
  381. size_t topLevelLength = std::string(this->GetOption("WDIR")).length();
  382. cmCPackLogger(cmCPackLog::LOG_DEBUG, "WDIR: \"" << this->GetOption("WDIR")
  383. << "\", length = " << topLevelLength
  384. << std::endl);
  385. std::set<std::string> installDirs;
  386. for (std::vector<std::string>::const_iterator fileIt =
  387. packageFiles.begin();
  388. fileIt != packageFiles.end(); ++ fileIt )
  389. {
  390. cmCPackLogger(cmCPackLog::LOG_DEBUG, "FILEIT: \"" << *fileIt << "\""
  391. << std::endl);
  392. std::string::size_type slashPos = fileIt->find('/', topLevelLength+1);
  393. std::string relativeDir = fileIt->substr(topLevelLength,
  394. slashPos - topLevelLength);
  395. cmCPackLogger(cmCPackLog::LOG_DEBUG, "RELATIVEDIR: \"" << relativeDir
  396. << "\"" << std::endl);
  397. if (installDirs.find(relativeDir) == installDirs.end())
  398. {
  399. installDirs.insert(relativeDir);
  400. cmd += " .";
  401. cmd += relativeDir;
  402. }
  403. }
  404. std::string output;
  405. int retval = -1;
  406. int res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
  407. &retval, this->GetOption("WDIR"), this->GeneratorVerbose, 0);
  408. if ( !res || retval )
  409. {
  410. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  411. tmpFile += "/Deb.log";
  412. cmGeneratedFileStream ofs(tmpFile.c_str());
  413. ofs << "# Run command: " << cmd.c_str() << std::endl
  414. << "# Working directory: " << toplevel << std::endl
  415. << "# Output:" << std::endl
  416. << output.c_str() << std::endl;
  417. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running tar command: "
  418. << cmd.c_str() << std::endl
  419. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  420. return 0;
  421. }
  422. std::string md5filename;
  423. md5filename = this->GetOption("WDIR");
  424. md5filename += "/md5sums";
  425. { // the scope is needed for cmGeneratedFileStream
  426. cmGeneratedFileStream out(md5filename.c_str());
  427. std::vector<std::string>::const_iterator fileIt;
  428. // std::string topLevelWithTrailingSlash = toplevel;
  429. std::string topLevelWithTrailingSlash =
  430. this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  431. topLevelWithTrailingSlash += '/';
  432. for ( fileIt = packageFiles.begin();
  433. fileIt != packageFiles.end(); ++ fileIt )
  434. {
  435. cmd = "\"";
  436. cmd += cmakeExecutable;
  437. cmd += "\" -E md5sum \"";
  438. cmd += *fileIt;
  439. cmd += "\"";
  440. //std::string output;
  441. //int retVal = -1;
  442. res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
  443. &retval, toplevel.c_str(), this->GeneratorVerbose, 0);
  444. // debian md5sums entries are like this:
  445. // 014f3604694729f3bf19263bac599765 usr/bin/ccmake
  446. // thus strip the full path (with the trailing slash)
  447. cmSystemTools::ReplaceString(output,
  448. topLevelWithTrailingSlash.c_str(), "");
  449. out << output;
  450. }
  451. // each line contains a eol.
  452. // Do not end the md5sum file with yet another (invalid)
  453. }
  454. cmd = "";
  455. if (NULL != this->GetOption("CPACK_DEBIAN_FAKEROOT_EXECUTABLE"))
  456. {
  457. cmd = this->GetOption("CPACK_DEBIAN_FAKEROOT_EXECUTABLE");
  458. }
  459. cmd += " \"";
  460. cmd += cmakeExecutable;
  461. cmd += "\" -E tar cfz control.tar.gz ./control ./md5sums";
  462. const char* controlExtra =
  463. this->GetOption("CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA");
  464. if( controlExtra )
  465. {
  466. std::vector<std::string> controlExtraList;
  467. cmSystemTools::ExpandListArgument(controlExtra, controlExtraList);
  468. for(std::vector<std::string>::iterator i =
  469. controlExtraList.begin(); i != controlExtraList.end(); ++i)
  470. {
  471. std::string filenamename =
  472. cmsys::SystemTools::GetFilenameName(*i);
  473. std::string localcopy = this->GetOption("WDIR");
  474. localcopy += "/";
  475. localcopy += filenamename;
  476. // if we can copy the file, it means it does exist, let's add it:
  477. if( cmsys::SystemTools::CopyFileIfDifferent(
  478. i->c_str(), localcopy.c_str()) )
  479. {
  480. // debian is picky and need relative to ./ path in the tar.gz
  481. cmd += " ./";
  482. cmd += filenamename;
  483. }
  484. }
  485. }
  486. res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
  487. &retval, this->GetOption("WDIR"), this->GeneratorVerbose, 0);
  488. if ( !res || retval )
  489. {
  490. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  491. tmpFile += "/Deb.log";
  492. cmGeneratedFileStream ofs(tmpFile.c_str());
  493. ofs << "# Run command: " << cmd.c_str() << std::endl
  494. << "# Working directory: " << toplevel << std::endl
  495. << "# Output:" << std::endl
  496. << output.c_str() << std::endl;
  497. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running tar command: "
  498. << cmd.c_str() << std::endl
  499. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  500. return 0;
  501. }
  502. // ar -r your-package-name.deb debian-binary control.tar.gz data.tar.gz
  503. // since debian packages require BSD ar (most Linux distros and even
  504. // FreeBSD and NetBSD ship GNU ar) we use a copy of OpenBSD ar here.
  505. std::vector<std::string> arFiles;
  506. std::string topLevelString = this->GetOption("WDIR");
  507. topLevelString += "/";
  508. arFiles.push_back(topLevelString + "debian-binary");
  509. arFiles.push_back(topLevelString + "control.tar.gz");
  510. arFiles.push_back(topLevelString + "data.tar.gz");
  511. std::string outputFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  512. outputFileName += "/";
  513. outputFileName += this->GetOption("CPACK_OUTPUT_FILE_NAME");
  514. res = ar_append(outputFileName.c_str(), arFiles);
  515. if ( res!=0 )
  516. {
  517. std::string tmpFile = this->GetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME");
  518. tmpFile += "/Deb.log";
  519. cmGeneratedFileStream ofs(tmpFile.c_str());
  520. ofs << "# Problem creating archive using: " << res << std::endl;
  521. return 0;
  522. }
  523. return 1;
  524. }
  525. bool cmCPackDebGenerator::SupportsComponentInstallation() const
  526. {
  527. if (IsOn("CPACK_DEB_COMPONENT_INSTALL"))
  528. {
  529. return true;
  530. }
  531. else
  532. {
  533. return false;
  534. }
  535. }
  536. std::string cmCPackDebGenerator::GetComponentInstallDirNameSuffix(
  537. const std::string& componentName)
  538. {
  539. if (componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  540. return componentName;
  541. }
  542. if (componentPackageMethod == ONE_PACKAGE) {
  543. return std::string("ALL_COMPONENTS_IN_ONE");
  544. }
  545. // We have to find the name of the COMPONENT GROUP
  546. // the current COMPONENT belongs to.
  547. std::string groupVar = "CPACK_COMPONENT_" +
  548. cmSystemTools::UpperCase(componentName) + "_GROUP";
  549. if (NULL != GetOption(groupVar))
  550. {
  551. return std::string(GetOption(groupVar));
  552. }
  553. else
  554. {
  555. return componentName;
  556. }
  557. }
  558. // The following code is taken from OpenBSD ar:
  559. // http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ar/
  560. // It has been slightly modified:
  561. // -return error codes instead exit() in functions
  562. // -use the stdio file I/O functions instead the file descriptor based ones
  563. // -merged into one cxx file
  564. // -no additional options supported
  565. // The coding style hasn't been modified.
  566. /*-
  567. * Copyright (c) 1990, 1993, 1994
  568. * The Regents of the University of California. All rights reserved.
  569. *
  570. * This code is derived from software contributed to Berkeley by
  571. * Hugh Smith at The University of Guelph.
  572. *
  573. * Redistribution and use in source and binary forms, with or without
  574. * modification, are permitted provided that the following conditions
  575. * are met:
  576. * 1. Redistributions of source code must retain the above copyright
  577. * notice, this list of conditions and the following disclaimer.
  578. * 2. Redistributions in binary form must reproduce the above copyright
  579. * notice, this list of conditions and the following disclaimer in the
  580. * documentation and/or other materials provided with the distribution.
  581. * 3. Neither the name of the University nor the names of its contributors
  582. * may be used to endorse or promote products derived from this software
  583. * without specific prior written permission.
  584. *
  585. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  586. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  587. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  588. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  589. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  590. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  591. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  592. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  593. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  594. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  595. * SUCH DAMAGE.
  596. */
  597. #include <sys/types.h>
  598. #include <sys/stat.h>
  599. #include <stdio.h>
  600. #include <string.h>
  601. #include <stdlib.h>
  602. #define ARMAG "!<arch>\n" /* ar "magic number" */
  603. #define SARMAG 8 /* strlen(ARMAG); */
  604. #define AR_EFMT1 "#1/" /* extended format #1 */
  605. #define ARFMAG "`\n"
  606. /* Header format strings. */
  607. #define HDR1 "%s%-13d%-12ld%-6u%-6u%-8o%-10lld%2s"
  608. #define HDR2 "%-16.16s%-12ld%-6u%-6u%-8o%-10lld%2s"
  609. struct ar_hdr {
  610. char ar_name[16]; /* name */
  611. char ar_date[12]; /* modification time */
  612. char ar_uid[6]; /* user id */
  613. char ar_gid[6]; /* group id */
  614. char ar_mode[8]; /* octal file permissions */
  615. char ar_size[10]; /* size in bytes */
  616. char ar_fmag[2]; /* consistency check */
  617. };
  618. /* Set up file copy. */
  619. #define SETCF(from, fromname, to, toname, pad) { \
  620. cf.rFile = from; \
  621. cf.rname = fromname; \
  622. cf.wFile = to; \
  623. cf.wname = toname; \
  624. cf.flags = pad; \
  625. }
  626. /* File copy structure. */
  627. typedef struct {
  628. FILE* rFile; /* read file descriptor */
  629. const char *rname; /* read name */
  630. FILE* wFile; /* write file descriptor */
  631. const char *wname; /* write name */
  632. #define NOPAD 0x00 /* don't pad */
  633. #define WPAD 0x02 /* pad on writes */
  634. unsigned int flags; /* pad flags */
  635. } CF;
  636. /* misc.c */
  637. static const char * ar_rname(const char *path)
  638. {
  639. const char *ind = strrchr(path, '/');
  640. return (ind ) ? ind + 1 : path;
  641. }
  642. /* archive.c */
  643. typedef struct ar_hdr HDR;
  644. static char ar_hb[sizeof(HDR) + 1]; /* real header */
  645. static size_t ar_already_written;
  646. /* copy_ar --
  647. * Copy size bytes from one file to another - taking care to handle the
  648. * extra byte (for odd size files) when reading archives and writing an
  649. * extra byte if necessary when adding files to archive. The length of
  650. * the object is the long name plus the object itself; the variable
  651. * already_written gets set if a long name was written.
  652. *
  653. * The padding is really unnecessary, and is almost certainly a remnant
  654. * of early archive formats where the header included binary data which
  655. * a PDP-11 required to start on an even byte boundary. (Or, perhaps,
  656. * because 16-bit word addressed copies were faster?) Anyhow, it should
  657. * have been ripped out long ago.
  658. */
  659. static int copy_ar(CF *cfp, off_t size)
  660. {
  661. static char pad = '\n';
  662. off_t sz = size;
  663. size_t nr, nw;
  664. char buf[8*1024];
  665. if (sz == 0)
  666. return 0;
  667. FILE* from = cfp->rFile;
  668. FILE* to = cfp->wFile;
  669. while (sz &&
  670. (nr = fread(buf, 1, sz < static_cast<off_t>(sizeof(buf))
  671. ? static_cast<size_t>(sz) : sizeof(buf), from ))
  672. > 0) {
  673. sz -= nr;
  674. for (size_t off = 0; off < nr; nr -= off, off += nw)
  675. if ((nw = fwrite(buf + off, 1, nr, to)) < nr)
  676. return -1;
  677. }
  678. if (sz)
  679. return -2;
  680. if (cfp->flags & WPAD && (size + ar_already_written) & 1
  681. && fwrite(&pad, 1, 1, to) != 1)
  682. return -4;
  683. return 0;
  684. }
  685. /* put_arobj -- Write an archive member to a file. */
  686. static int put_arobj(CF *cfp, struct stat *sb)
  687. {
  688. int result = 0;
  689. struct ar_hdr *hdr;
  690. /* If passed an sb structure, reading a file from disk. Get stat(2)
  691. * information, build a name and construct a header. (Files are named
  692. * by their last component in the archive.) */
  693. const char* name = ar_rname(cfp->rname);
  694. (void)stat(cfp->rname, sb);
  695. /* If not truncating names and the name is too long or contains
  696. * a space, use extended format 1. */
  697. size_t lname = strlen(name);
  698. uid_t uid = sb->st_uid;
  699. gid_t gid = sb->st_gid;
  700. if (uid > USHRT_MAX) {
  701. uid = USHRT_MAX;
  702. }
  703. if (gid > USHRT_MAX) {
  704. gid = USHRT_MAX;
  705. }
  706. if (lname > sizeof(hdr->ar_name) || strchr(name, ' '))
  707. (void)sprintf(ar_hb, HDR1, AR_EFMT1, (int)lname,
  708. (long int)sb->st_mtime, (unsigned)uid, (unsigned)gid,
  709. sb->st_mode, (long long)sb->st_size + lname, ARFMAG);
  710. else {
  711. lname = 0;
  712. (void)sprintf(ar_hb, HDR2, name,
  713. (long int)sb->st_mtime, (unsigned)uid, (unsigned)gid,
  714. sb->st_mode, (long long)sb->st_size, ARFMAG);
  715. }
  716. off_t size = sb->st_size;
  717. if (fwrite(ar_hb, 1, sizeof(HDR), cfp->wFile) != sizeof(HDR))
  718. return -1;
  719. if (lname) {
  720. if (fwrite(name, 1, lname, cfp->wFile) != lname)
  721. return -2;
  722. ar_already_written = lname;
  723. }
  724. result = copy_ar(cfp, size);
  725. ar_already_written = 0;
  726. return result;
  727. }
  728. /* append.c */
  729. /* append --
  730. * Append files to the archive - modifies original archive or creates
  731. * a new archive if named archive does not exist.
  732. */
  733. static int ar_append(const char* archive,const std::vector<std::string>& files)
  734. {
  735. int eval = 0;
  736. FILE* aFile = cmSystemTools::Fopen(archive, "wb+");
  737. if (aFile!=NULL) {
  738. fwrite(ARMAG, SARMAG, 1, aFile);
  739. if (fseek(aFile, 0, SEEK_END) != -1) {
  740. CF cf;
  741. struct stat sb;
  742. /* Read from disk, write to an archive; pad on write. */
  743. SETCF(NULL, 0, aFile, archive, WPAD);
  744. for(std::vector<std::string>::const_iterator fileIt = files.begin();
  745. fileIt!=files.end(); ++fileIt) {
  746. const char* filename = fileIt->c_str();
  747. FILE* file = cmSystemTools::Fopen(filename, "rb");
  748. if (file == NULL) {
  749. eval = -1;
  750. continue;
  751. }
  752. cf.rFile = file;
  753. cf.rname = filename;
  754. int result = put_arobj(&cf, &sb);
  755. (void)fclose(file);
  756. if (result!=0) {
  757. eval = -2;
  758. break;
  759. }
  760. }
  761. }
  762. else {
  763. eval = -3;
  764. }
  765. fclose(aFile);
  766. }
  767. else {
  768. eval = -4;
  769. }
  770. return eval;
  771. }