cmCPackDebGenerator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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::PackageFiles()
  45. {
  46. this->ReadListFile("CPackDeb.cmake");
  47. const char* cmakeExecutable = this->GetOption("CMAKE_COMMAND");
  48. // debian-binary file
  49. std::string dbfilename;
  50. dbfilename = toplevel;
  51. dbfilename += "/debian-binary";
  52. { // the scope is needed for cmGeneratedFileStream
  53. cmGeneratedFileStream out(dbfilename.c_str());
  54. out << "2.0";
  55. out << std::endl; // required for valid debian package
  56. }
  57. // control file
  58. std::string ctlfilename;
  59. ctlfilename = toplevel;
  60. ctlfilename += "/control";
  61. // debian policy enforce lower case for package name
  62. // mandatory entries:
  63. std::string debian_pkg_name = cmsys::SystemTools::LowerCase(
  64. this->GetOption("CPACK_DEBIAN_PACKAGE_NAME") );
  65. const char* debian_pkg_version =
  66. this->GetOption("CPACK_DEBIAN_PACKAGE_VERSION");
  67. const char* debian_pkg_section =
  68. this->GetOption("CPACK_DEBIAN_PACKAGE_SECTION");
  69. const char* debian_pkg_priority =
  70. this->GetOption("CPACK_DEBIAN_PACKAGE_PRIORITY");
  71. const char* debian_pkg_arch =
  72. this->GetOption("CPACK_DEBIAN_PACKAGE_ARCHITECTURE");
  73. const char* maintainer = this->GetOption("CPACK_DEBIAN_PACKAGE_MAINTAINER");
  74. const char* desc = this->GetOption("CPACK_DEBIAN_PACKAGE_DESCRIPTION");
  75. // optional entries
  76. const char* debian_pkg_dep = this->GetOption("CPACK_DEBIAN_PACKAGE_DEPENDS");
  77. const char* debian_pkg_rec =
  78. this->GetOption("CPACK_DEBIAN_PACKAGE_RECOMMENDS");
  79. const char* debian_pkg_sug =
  80. this->GetOption("CPACK_DEBIAN_PACKAGE_SUGGESTS");
  81. const char* debian_pkg_url =
  82. this->GetOption("CPACK_DEBIAN_PACKAGE_HOMEPAGE");
  83. { // the scope is needed for cmGeneratedFileStream
  84. cmGeneratedFileStream out(ctlfilename.c_str());
  85. out << "Package: " << debian_pkg_name << "\n";
  86. out << "Version: " << debian_pkg_version << "\n";
  87. out << "Section: " << debian_pkg_section << "\n";
  88. out << "Priority: " << debian_pkg_priority << "\n";
  89. out << "Architecture: " << debian_pkg_arch << "\n";
  90. if(debian_pkg_dep)
  91. {
  92. out << "Depends: " << debian_pkg_dep << "\n";
  93. }
  94. if(debian_pkg_rec)
  95. {
  96. out << "Recommends: " << debian_pkg_rec << "\n";
  97. }
  98. if(debian_pkg_sug)
  99. {
  100. out << "Suggests: " << debian_pkg_sug << "\n";
  101. }
  102. if(debian_pkg_url)
  103. {
  104. out << "Homepage: " << debian_pkg_url << "\n";
  105. }
  106. unsigned long totalSize = 0;
  107. {
  108. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  109. dirName += '/';
  110. for (std::vector<std::string>::const_iterator fileIt = files.begin();
  111. fileIt != files.end(); ++ fileIt )
  112. {
  113. totalSize += cmSystemTools::FileLength(fileIt->c_str());
  114. }
  115. }
  116. out << "Installed-Size: " << (totalSize + 1023) / 1024 << "\n";
  117. out << "Maintainer: " << maintainer << "\n";
  118. out << "Description: " << desc << "\n";
  119. out << std::endl;
  120. }
  121. std::string cmd;
  122. cmd = "\"";
  123. cmd += cmakeExecutable;
  124. cmd += "\" -E tar cfz data.tar.gz ";
  125. // now add all directories which have to be compressed
  126. // collect all top level install dirs for that
  127. // e.g. /opt/bin/foo, /usr/bin/bar and /usr/bin/baz would give /usr and /opt
  128. size_t topLevelLength = toplevel.length();
  129. std::set<std::string> installDirs;
  130. for (std::vector<std::string>::const_iterator fileIt = files.begin();
  131. fileIt != files.end(); ++ fileIt )
  132. {
  133. std::string::size_type slashPos = fileIt->find('/', topLevelLength+1);
  134. std::string relativeDir = fileIt->substr(topLevelLength,
  135. slashPos - topLevelLength);
  136. if (installDirs.find(relativeDir) == installDirs.end())
  137. {
  138. installDirs.insert(relativeDir);
  139. cmd += " .";
  140. cmd += relativeDir;
  141. }
  142. }
  143. std::string output;
  144. int retVal = -1;
  145. int res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
  146. &retVal, toplevel.c_str(), this->GeneratorVerbose, 0);
  147. if ( !res || retVal )
  148. {
  149. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  150. tmpFile += "/Deb.log";
  151. cmGeneratedFileStream ofs(tmpFile.c_str());
  152. ofs << "# Run command: " << cmd.c_str() << std::endl
  153. << "# Working directory: " << toplevel << std::endl
  154. << "# Output:" << std::endl
  155. << output.c_str() << std::endl;
  156. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running tar command: "
  157. << cmd.c_str() << std::endl
  158. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  159. return 0;
  160. }
  161. std::string md5filename;
  162. md5filename = toplevel;
  163. md5filename += "/md5sums";
  164. { // the scope is needed for cmGeneratedFileStream
  165. cmGeneratedFileStream out(md5filename.c_str());
  166. std::vector<std::string>::const_iterator fileIt;
  167. std::string topLevelWithTrailingSlash = toplevel;
  168. topLevelWithTrailingSlash += '/';
  169. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  170. {
  171. cmd = "\"";
  172. cmd += cmakeExecutable;
  173. cmd += "\" -E md5sum \"";
  174. cmd += *fileIt;
  175. cmd += "\"";
  176. //std::string output;
  177. //int retVal = -1;
  178. res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
  179. &retVal, toplevel.c_str(), this->GeneratorVerbose, 0);
  180. // debian md5sums entries are like this:
  181. // 014f3604694729f3bf19263bac599765 usr/bin/ccmake
  182. // thus strip the full path (with the trailing slash)
  183. cmSystemTools::ReplaceString(output,
  184. topLevelWithTrailingSlash.c_str(), "");
  185. out << output;
  186. }
  187. // each line contains a eol.
  188. // Do not end the md5sum file with yet another (invalid)
  189. }
  190. cmd = "\"";
  191. cmd += cmakeExecutable;
  192. cmd += "\" -E tar cfz control.tar.gz ./control ./md5sums";
  193. const char* controlExtra =
  194. this->GetOption("CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA");
  195. if( controlExtra )
  196. {
  197. std::vector<std::string> controlExtraList;
  198. cmSystemTools::ExpandListArgument(controlExtra, controlExtraList);
  199. for(std::vector<std::string>::iterator i =
  200. controlExtraList.begin(); i != controlExtraList.end(); ++i)
  201. {
  202. std::string filenamename =
  203. cmsys::SystemTools::GetFilenameName(i->c_str());
  204. std::string localcopy = toplevel;
  205. localcopy += "/";
  206. localcopy += filenamename;
  207. // if we can copy the file, it means it does exist, let's add it:
  208. if( cmsys::SystemTools::CopyFileIfDifferent(
  209. i->c_str(), localcopy.c_str()) )
  210. {
  211. // debian is picky and need relative to ./ path in the tar.gz
  212. cmd += " ./";
  213. cmd += filenamename;
  214. }
  215. }
  216. }
  217. res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
  218. &retVal, toplevel.c_str(), this->GeneratorVerbose, 0);
  219. if ( !res || retVal )
  220. {
  221. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  222. tmpFile += "/Deb.log";
  223. cmGeneratedFileStream ofs(tmpFile.c_str());
  224. ofs << "# Run command: " << cmd.c_str() << std::endl
  225. << "# Working directory: " << toplevel << std::endl
  226. << "# Output:" << std::endl
  227. << output.c_str() << std::endl;
  228. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running tar command: "
  229. << cmd.c_str() << std::endl
  230. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  231. return 0;
  232. }
  233. // ar -r your-package-name.deb debian-binary control.tar.gz data.tar.gz
  234. // since debian packages require BSD ar (most Linux distros and even
  235. // FreeBSD and NetBSD ship GNU ar) we use a copy of OpenBSD ar here.
  236. std::vector<std::string> arFiles;
  237. std::string topLevelString = toplevel;
  238. topLevelString += "/";
  239. arFiles.push_back(topLevelString + "debian-binary");
  240. arFiles.push_back(topLevelString + "control.tar.gz");
  241. arFiles.push_back(topLevelString + "data.tar.gz");
  242. res = ar_append(packageFileNames[0].c_str(), arFiles);
  243. if ( res!=0 )
  244. {
  245. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  246. tmpFile += "/Deb.log";
  247. cmGeneratedFileStream ofs(tmpFile.c_str());
  248. ofs << "# Problem creating archive using: " << res << std::endl;
  249. return 0;
  250. }
  251. return 1;
  252. }
  253. // The following code is taken from OpenBSD ar:
  254. // http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ar/
  255. // It has been slightly modified:
  256. // -return error codes instead exit() in functions
  257. // -use the stdio file I/O functions instead the file descriptor based ones
  258. // -merged into one cxx file
  259. // -no additional options supported
  260. // The coding style hasn't been modified.
  261. /*-
  262. * Copyright (c) 1990, 1993, 1994
  263. * The Regents of the University of California. All rights reserved.
  264. *
  265. * This code is derived from software contributed to Berkeley by
  266. * Hugh Smith at The University of Guelph.
  267. *
  268. * Redistribution and use in source and binary forms, with or without
  269. * modification, are permitted provided that the following conditions
  270. * are met:
  271. * 1. Redistributions of source code must retain the above copyright
  272. * notice, this list of conditions and the following disclaimer.
  273. * 2. Redistributions in binary form must reproduce the above copyright
  274. * notice, this list of conditions and the following disclaimer in the
  275. * documentation and/or other materials provided with the distribution.
  276. * 3. Neither the name of the University nor the names of its contributors
  277. * may be used to endorse or promote products derived from this software
  278. * without specific prior written permission.
  279. *
  280. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  281. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  282. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  283. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  284. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  285. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  286. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  287. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  288. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  289. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  290. * SUCH DAMAGE.
  291. */
  292. #include <sys/types.h>
  293. #include <sys/stat.h>
  294. #include <stdio.h>
  295. #include <string.h>
  296. #include <stdlib.h>
  297. #define ARMAG "!<arch>\n" /* ar "magic number" */
  298. #define SARMAG 8 /* strlen(ARMAG); */
  299. #define AR_EFMT1 "#1/" /* extended format #1 */
  300. #define ARFMAG "`\n"
  301. /* Header format strings. */
  302. #define HDR1 "%s%-13d%-12ld%-6u%-6u%-8o%-10lld%2s"
  303. #define HDR2 "%-16.16s%-12ld%-6u%-6u%-8o%-10lld%2s"
  304. struct ar_hdr {
  305. char ar_name[16]; /* name */
  306. char ar_date[12]; /* modification time */
  307. char ar_uid[6]; /* user id */
  308. char ar_gid[6]; /* group id */
  309. char ar_mode[8]; /* octal file permissions */
  310. char ar_size[10]; /* size in bytes */
  311. char ar_fmag[2]; /* consistency check */
  312. };
  313. /* Set up file copy. */
  314. #define SETCF(from, fromname, to, toname, pad) { \
  315. cf.rFile = from; \
  316. cf.rname = fromname; \
  317. cf.wFile = to; \
  318. cf.wname = toname; \
  319. cf.flags = pad; \
  320. }
  321. /* File copy structure. */
  322. typedef struct {
  323. FILE* rFile; /* read file descriptor */
  324. const char *rname; /* read name */
  325. FILE* wFile; /* write file descriptor */
  326. const char *wname; /* write name */
  327. #define NOPAD 0x00 /* don't pad */
  328. #define WPAD 0x02 /* pad on writes */
  329. unsigned int flags; /* pad flags */
  330. } CF;
  331. /* misc.c */
  332. static const char * ar_rname(const char *path)
  333. {
  334. const char *ind = strrchr(path, '/');
  335. return (ind ) ? ind + 1 : path;
  336. }
  337. /* archive.c */
  338. typedef struct ar_hdr HDR;
  339. static char ar_hb[sizeof(HDR) + 1]; /* real header */
  340. static size_t ar_already_written;
  341. /* copy_ar --
  342. * Copy size bytes from one file to another - taking care to handle the
  343. * extra byte (for odd size files) when reading archives and writing an
  344. * extra byte if necessary when adding files to archive. The length of
  345. * the object is the long name plus the object itself; the variable
  346. * already_written gets set if a long name was written.
  347. *
  348. * The padding is really unnecessary, and is almost certainly a remnant
  349. * of early archive formats where the header included binary data which
  350. * a PDP-11 required to start on an even byte boundary. (Or, perhaps,
  351. * because 16-bit word addressed copies were faster?) Anyhow, it should
  352. * have been ripped out long ago.
  353. */
  354. static int copy_ar(CF *cfp, off_t size)
  355. {
  356. static char pad = '\n';
  357. off_t sz = size;
  358. size_t nr, nw;
  359. char buf[8*1024];
  360. if (sz == 0)
  361. return 0;
  362. FILE* from = cfp->rFile;
  363. FILE* to = cfp->wFile;
  364. while (sz &&
  365. (nr = fread(buf, 1, sz < static_cast<off_t>(sizeof(buf))
  366. ? static_cast<size_t>(sz) : sizeof(buf), from ))
  367. > 0) {
  368. sz -= nr;
  369. for (size_t off = 0; off < nr; nr -= off, off += nw)
  370. if ((nw = fwrite(buf + off, 1, nr, to)) < nr)
  371. return -1;
  372. }
  373. if (sz)
  374. return -2;
  375. if (cfp->flags & WPAD && (size + ar_already_written) & 1
  376. && fwrite(&pad, 1, 1, to) != 1)
  377. return -4;
  378. return 0;
  379. }
  380. /* put_arobj -- Write an archive member to a file. */
  381. static int put_arobj(CF *cfp, struct stat *sb)
  382. {
  383. int result = 0;
  384. struct ar_hdr *hdr;
  385. /* If passed an sb structure, reading a file from disk. Get stat(2)
  386. * information, build a name and construct a header. (Files are named
  387. * by their last component in the archive.) */
  388. const char* name = ar_rname(cfp->rname);
  389. (void)stat(cfp->rname, sb);
  390. /* If not truncating names and the name is too long or contains
  391. * a space, use extended format 1. */
  392. size_t lname = strlen(name);
  393. uid_t uid = sb->st_uid;
  394. gid_t gid = sb->st_gid;
  395. if (uid > USHRT_MAX) {
  396. uid = USHRT_MAX;
  397. }
  398. if (gid > USHRT_MAX) {
  399. gid = USHRT_MAX;
  400. }
  401. if (lname > sizeof(hdr->ar_name) || strchr(name, ' '))
  402. (void)sprintf(ar_hb, HDR1, AR_EFMT1, (int)lname,
  403. (long int)sb->st_mtime, uid, gid, sb->st_mode,
  404. (long long)sb->st_size + lname, ARFMAG);
  405. else {
  406. lname = 0;
  407. (void)sprintf(ar_hb, HDR2, name,
  408. (long int)sb->st_mtime, uid, gid, sb->st_mode,
  409. (long long)sb->st_size, ARFMAG);
  410. }
  411. off_t size = sb->st_size;
  412. if (fwrite(ar_hb, 1, sizeof(HDR), cfp->wFile) != sizeof(HDR))
  413. return -1;
  414. if (lname) {
  415. if (fwrite(name, 1, lname, cfp->wFile) != lname)
  416. return -2;
  417. ar_already_written = lname;
  418. }
  419. result = copy_ar(cfp, size);
  420. ar_already_written = 0;
  421. return result;
  422. }
  423. /* append.c */
  424. /* append --
  425. * Append files to the archive - modifies original archive or creates
  426. * a new archive if named archive does not exist.
  427. */
  428. static int ar_append(const char* archive,const std::vector<std::string>& files)
  429. {
  430. int eval = 0;
  431. FILE* aFile = fopen(archive, "wb+");
  432. if (aFile!=NULL) {
  433. fwrite(ARMAG, SARMAG, 1, aFile);
  434. if (fseek(aFile, 0, SEEK_END) != -1) {
  435. CF cf;
  436. struct stat sb;
  437. /* Read from disk, write to an archive; pad on write. */
  438. SETCF(NULL, 0, aFile, archive, WPAD);
  439. for(std::vector<std::string>::const_iterator fileIt = files.begin();
  440. fileIt!=files.end(); ++fileIt) {
  441. const char* filename = fileIt->c_str();
  442. FILE* file = fopen(filename, "rb");
  443. if (file == NULL) {
  444. eval = -1;
  445. continue;
  446. }
  447. cf.rFile = file;
  448. cf.rname = filename;
  449. int result = put_arobj(&cf, &sb);
  450. (void)fclose(file);
  451. if (result!=0) {
  452. eval = -2;
  453. break;
  454. }
  455. }
  456. }
  457. else {
  458. eval = -3;
  459. }
  460. fclose(aFile);
  461. }
  462. else {
  463. eval = -4;
  464. }
  465. return eval;
  466. }