source: trunk/debbuild@ 191

Last change on this file since 191 was 191, checked in by kdeugau, 9 years ago

/trunk

Review, revise, and update automatic OS detection with much input from
Neal Gompa. Legacy Unbuntu verions have been refined, current LTS
releases and Debian releases have been added, and now that sanity has
prevailed we can get the base OS, OS release, and OS version all from
a handy file.

Note that debbuild diverges from rpmbuild in that it provides a
reasonable default for %{dist}, similar to usage found in RHEL and
Fedora packages (eg "el6" or "fc20"). rpmbuild relies on --define's
in the build environment call to set this. If specified with --define,
the command line argument will take precedence over the autodetection,
although the custom %{debver} and %{debdist} macros will remain as set
from the autodetection.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 78.4 KB
Line 
1#!/usr/bin/perl -w
2# debbuild script
3# Shamelessly steals interface from rpm's "rpmbuild" to create
4# Debian packages. Please note that such packages are highly
5# unlikely to conform to "Debian Policy".
6###
7# SVN revision info
8# $Date: 2015-07-14 01:14:26 +0000 (Tue, 14 Jul 2015) $
9# SVN revision $Rev: 191 $
10# Last update by $Author: kdeugau $
11###
12# Copyright (C) 2005-2013 Kris Deugau <kdeugau@deepnet.cx>
13#
14# This program is free software; you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation; either version 2 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
28use strict;
29use warnings;
30use Fcntl; # for sysopen flags
31use Cwd 'abs_path'; # for finding where files really are
32use Config;
33
34my $version = "0.10.1"; #VERSION#
35
36# regex debugger
37#use re "debug";
38
39# Behavioural compatibility FTW! Yes, rpmbuild does this too.
40die "No .spec file to work with! Exiting.\n" if scalar(@ARGV) == 0;
41
42# Program flow:
43# -> Parse/execute "system" config/macros (if any - should be rare)
44# -> Parse/execute "user" config/macros (if any - *my* requirement is %_topdir)
45# -> Parse command line for options, spec file/tarball/.src.deb (NB - also accept .src.rpm)
46
47sub expandmacros;
48
49# User's prefs for dirs, environment, etc,etc,etc.
50# config file ~/.debmacros
51# Default ordered search paths for config/macros:
52# /usr/lib/rpm/rpmrc /usr/lib/rpm/redhat/rpmrc /etc/rpmrc ~/.rpmrc
53# /usr/lib/rpm/macros /usr/lib/rpm/redhat/macros /etc/rpm/macros ~/.rpmmacros
54# **NOTE: May be possible to (ab)use bits of debhelper
55
56# Build tree
57# default is /usr/src/debian/{BUILD,SOURCES,SPECS,DEBS,SDEBS}
58
59# Globals
60my $finalmessages = ''; # A place to stuff messages that I want printed at the *very* end of any processing.
61my $specfile;
62my $tarball;
63my $srcpkg;
64my $cmdbuildroot;
65my $tarballdir = '%{name}-%{version}'; # We do this in case of a spec file not using %setup...
66my %specglobals; # For %define's in specfile, among other things.
67
68$specglobals{'_vendor'} = 'debbuild';
69# this can be changed by the Vendor: header in the spec file
70$specglobals{'vendor'} = 'debbuild';
71
72# Initialized globals
73my $verbosity = 0;
74my $NoAutoReq = 0;
75my %cmdopts = (type => '',
76 stage => 'a',
77 short => 'n'
78 );
79my $topdir = "/usr/src/debian";
80#my $specglobals{buildroot} = "%{_tmppath}/%{name}-%{version}-%{release}.root".int(rand(99998)+1);
81$specglobals{buildroot} = '%{_topdir}/BUILDROOT/%{name}-%{version}-%{release}';
82
83# "Constants"
84my %targets = ('p' => 'Prep',
85 'c' => 'Compile',
86 'i' => 'Install',
87 'l' => 'Verify %files',
88 'a' => 'Build binary and source',
89 'b' => 'Build binary',
90 's' => 'Build source'
91 );
92# Ah, the joys of multiple architectures. :( Feh.
93# As copied from rpm
94my %optflags = ( 'i386' => '-O2 -g -march=i386 -mcpu=i686',
95 'amd64' => '-O2 -g'
96 );
97my $hostarch; # we set this later...
98my $scriptletbase =
99q(#!/bin/bash
100
101 RPM_SOURCE_DIR="%{_topdir}/SOURCES"
102 RPM_BUILD_DIR="%{_topdir}/BUILD"
103 RPM_OPT_FLAGS="%{optflags}"
104 RPM_ARCH="%{_arch}"
105 RPM_OS="linux"
106 export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS
107 RPM_DOC_DIR="/usr/share/doc"
108 export RPM_DOC_DIR
109 RPM_PACKAGE_NAME="%{name}"
110 RPM_PACKAGE_VERSION="%{version}"
111 RPM_PACKAGE_RELEASE="%{release}"
112 export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE
113 LANG=C
114 export LANG
115 unset CDPATH DISPLAY ||:
116 RPM_BUILD_ROOT="%{buildroot}"
117 export RPM_BUILD_ROOT
118
119 PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/usr/lib64/pkgconfig:/usr/share/pkgconfig"
120 export PKG_CONFIG_PATH
121);
122
123if (-e '/usr/bin/dpkg-architecture') {
124 foreach (`dpkg-architecture`) {
125 s/=(.+)/="$1"/;
126 $scriptletbase .= " $_";
127 ($hostarch) = (/^DEB_HOST_ARCH="(.+)"$/) if /DEB_HOST_ARCH=/;
128 }
129} else {
130 warn "Missing dpkg-architecture, DEB_HOST_ARCH will not be set!\n";
131}
132
133$scriptletbase .=
134q(
135 set -x
136 umask 022
137 cd "%{_topdir}/BUILD"
138);
139
140# Hackery to try to bring some semblance of sanity to packages built for more
141# than one Debian version at the same time. Whee.
142# /etc/debian-version and/or version of base-files package
143my %distmap = (
144# legacy Unbuntu
145 "3.1.9ubuntu" => "dapper",
146 "6.06" => "dapper",
147 "4ubuntu" => "feisty",
148 "7.04" => "fiesty",
149 "4.0.1ubuntu" => "hardy",
150 "8.04" => "hardy",
151# Note we do NOT support identification of "subrelease" versions (ie semimajor updates
152# to a given release). If your dependencies are really that tight, and you can't rely
153# on the versions of the actual dependencies, you're already in over your head, and
154# should probably only ship a tarballed installer.
155# only supporting LTS releases
156# base-files version doesn't map to the Ubuntu version the way Debian versions do, thus the doubled entries
157# Ubuntu 12.04.5 LTS (Precise Pangolin)
158 "6.5ubuntu" => "precise",
159 "12.04" => "precise",
160# Ubuntu 14.04.2 LTS (Trusty Tahr)
161 "7.2ubuntu" => "trusty",
162 "14.04" => "trusty",
163# Debian releases
164 "3.0" => "woody",
165 "3.1" => "sarge",
166 "4" => "etch",
167 "5" => "lenny",
168 "6" => "squeeze",
169 "7" => "wheezy",
170 "8" => "jessie",
171 "9" => "stretch",
172 "99" => "sid",
173);
174
175# Map Ubuntu base-files versions to the nominal public versions
176
177my %ubnt_vermap = (
178 "6.5ubuntu6" => "12.04",
179 "7.2ubuntu5" => "14.04",
180);
181
182# Enh. There doesn't seem to be any better way to do this... :(
183{
184# could theoretically also do something with this... it's about as stable as "dpkg-query ..." below... :(
185# my $releasever = qx { cat /etc/debian_version };
186# chomp $releasever;
187 my $basefiles;
188 my $basever;
189 my $baseos;
190
191 # Funny thing how files like this have become useful...
192 # Check for /etc/os-release. If that doesn't exist, try /etc/lsb-release. If that doesn't exist,
193 # check for existence of dpkg-query, and either call it Debian Woody (if missing), or fall through
194 # to guessing based on the version of the base-files package.
195 if (-e '/etc/os-release') {
196 open OSREL, "</etc/os-release";
197 # Look for ID and VERSION_ID lines.
198 while (<OSREL>) {
199 $baseos = $1 if /^ID=(\w+)/;
200 $basever = $1 if /^VERSION_ID="?([\d.]+)/;
201 }
202 close OSREL;
203
204 } elsif (-e '/etc/lsb-release') {
205 open LSBREL, "</etc/lsb-release";
206 # Look for DISTRIB_ID and DISTRIB_RELEASE lines.
207 # We could also theoretically extract the dist codename, but since we have to hand-map
208 # it in so many other cases there's little point.
209 while (<LSBREL>) {
210 $baseos = $1 if /^DISTRIB_ID=(\w+)/;
211 $basever = $1 if /^DISTRIB_RELEASE=([\d.]+)/;
212 }
213 close LSBREL;
214
215 } elsif ( ! -e '/usr/bin/dpkg-query' ) {
216 # call it woody, since sarge and newer have dpkg-query, and we don't much care about obsolete^n releases
217 $basever = "3.0";
218 $baseos = 'debian';
219
220 } else {
221# *eyeroll* there *really* has to be a better way to go about this. You
222# can't sanely build packages for multiple distro targets if you can't
223# programmatically figure out which one you're building on.
224
225# note that we care only about major release numbers; tracking minor or point
226# releases would be... exponentially more painful.
227
228 my $majver;
229 # for the lazy copy-paster: dpkg-query --showformat '${version}\n' -W base-files
230 # avoid shellisms
231 if (open BASEGETTER, "-|", "dpkg-query", "--showformat", '${version}', "-W", "base-files") {
232 $basever = <BASEGETTER>;
233 close BASEGETTER;
234
235 if ($basever =~ /ubuntu/) {
236 # Ubuntu, at least until they upset their versioning scheme again
237 # note that we remap the basefiles version to the public release number, to match the
238 # behaviour for Debian, and to match the unofficial standard for RHEL/Centos etc and Fedora
239 ($basefiles,$basever) = ($basever =~ /^(([\d.]+)ubuntu)\d/);
240 $baseos = 'ubuntu';
241 } else {
242 # Debian, or more "pure" derivative
243 $baseos = 'debian';
244 ($basever,$majver) = ($basever =~ /^((\d+)(?:\.\d)?)/);
245 if ($majver > 3) {
246 $basever = $majver;
247 }
248 }
249 }
250
251 if (!$basever) {
252 # Your llama is on fire
253 $basever = '99';
254 print "Warning: couldn't autodetect OS version, assuming sid/unstable\n";
255 }
256 } # got dpkg-query?
257
258 # Set some legacy globals.
259 $specglobals{"debdist"} = $distmap{$basever};
260 $specglobals{"debver"} = $basever; # this may have trouble with Ubuntu versions?
261
262 # Set the standard generic OS-class globals;
263 $baseos = lc($baseos);
264 $specglobals{"ubuntu"} = $basever if $baseos eq 'ubuntu';
265 $specglobals{"debian"} = $basever if $baseos eq 'debian';
266
267 # Default %{dist} to something marginally sane. Note this should be overrideable by --define.
268 # This has been chosen to most closely follow the usage in RHEL/CentOS and Fedora, ie "el5" or "fc20".
269 $specglobals{"dist"} = $baseos.$basever;
270
271} # done trying to set debian dist/version
272
273# Package data
274# This is the form of $pkgdata{pkgname}{meta}
275# meta includes Summary, Name, Version, Release, Group, Copyright,
276# Source, URL, Packager, BuildRoot, Description, BuildRequires,
277# Requires, Provides
278# 10/31/2005 Maybe this should be flatter? -kgd
279my %pkgdata = (main => {source => ''});
280my @pkglist = ('main'); #sigh
281# Files listing. Embedding this in %pkgdata would be, um, messy.
282my %filelist;
283my %doclist;
284my $buildreq = '';
285
286# Scriptlets
287my $prepscript = '';
288my $buildscript = '';
289# %install doesn't need the full treatment from %clean; just an empty place to install to.
290# NB - rpm doesn't do this; is it really necessary?
291my $installscript = '[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT'."\n";
292my $cleanscript = '';
293
294# Snag some environment data
295my $tmpdir;
296if (defined $ENV{TMP} && $ENV{TMP} =~ /^(\/var)?\/tmp$/) {
297 $tmpdir = $ENV{TMP};
298} else {
299 $tmpdir = "/var/tmp";
300}
301
302##main
303
304load_userconfig();
305parse_cmd();
306
307if ($cmdopts{install}) {
308 install_sdeb();
309 exit 0;
310}
311
312# output stage of --showpkgs
313if ($cmdopts{type} eq 'd') {
314 parse_spec();
315 foreach my $pkg (@pkglist) {
316 $pkgdata{$pkg}{name} =~ tr/_/-/;
317
318 my $pkgfullname = "$pkgdata{$pkg}{name}_".
319 (defined($pkgdata{main}{epoch}) ? "$pkgdata{main}{epoch}:" : '').
320 "$pkgdata{$pkg}{version}-$pkgdata{main}{release}_$pkgdata{$pkg}{arch}.deb";
321
322 print "$pkgfullname\n" if $filelist{$pkg};
323
324 }
325 # Source package
326 print "$pkgdata{main}{name}-".
327 (defined($pkgdata{main}{epoch}) ? "$pkgdata{main}{epoch}:" : '').
328 "$pkgdata{main}{version}-$pkgdata{main}{release}.sdeb\n";
329 exit 0;
330}
331
332# Stick --rebuild handling in here - basically install_sdeb()
333# followed by tweaking options to run with -ba
334if ($cmdopts{type} eq 's') {
335 if ($srcpkg =~ /\.src\.rpm$/) {
336 my @srclist = qx { rpm -qlp $srcpkg };
337 foreach (@srclist) {
338 chomp;
339 $specfile = "$topdir/SPECS/$_" if /\.spec$/;
340 }
341 qx { rpm -i $srcpkg };
342 } else {
343 install_sdeb();
344 my @srclist = qx { pax < $srcpkg };
345 foreach (@srclist) {
346 chomp;
347 $specfile = "$topdir/$_" if /SPECS/;
348 }
349 }
350 $cmdopts{type} = 'b';
351 $cmdopts{stage} = 'a';
352}
353
354if ($cmdopts{type} eq 'b') {
355 # Need to read the spec file to find the tarball. Note that
356 # this also generates most of the shell script required.
357 parse_spec();
358 die "Can't build $pkgdata{main}{name}: build requirements not met.\n"
359 if !checkbuildreq();
360}
361
362if ($cmdopts{type} eq 't') {
363 # Need to unpack the tarball to find the spec file. Sort of the inverse of -b above.
364 # zcat $tarball |tar -t |grep .spec
365 # collect some info about the tarball
366 $specfile = "$topdir/BUILD/". qx { zcat $tarball |tar -t |grep -e '[\.]spec\$' };
367 chomp $specfile;
368 my ($fileonly, $dirname) = ($tarball =~ /(([a-zA-Z0-9._-]+)\.tar\.(?:gz|bz2|xz))$/);
369
370 $tarball = abs_path($tarball);
371##fixme: use macro expansions for the executables
372 my $unpackcmd = "cd $topdir/BUILD; ".
373 ( $tarball =~ /\.tar\.gz$/ ? "/bin/gzip" : "" ).
374 ( $tarball =~ /\.tar\.bz2$/ ? "/bin/bzip2" : "" ).
375 ( $tarball =~ /\.tar\.(?:gz|Z)$/ ? "/usr/bin/xz" : "" ).
376 " -dc $tarball |/bin/tar -xf -";
377
378 system "$unpackcmd";
379 system "cp $tarball $topdir/SOURCES/$fileonly";
380 system "cp $specfile $topdir/SPECS/";
381 parse_spec();
382 die "Can't build $pkgdata{main}{name}: build requirements not met.\n"
383 if !checkbuildreq();
384}
385
386# -> srcpkg if -.s
387if ($cmdopts{stage} eq 's') {
388 srcpackage();
389 print $finalmessages;
390 exit 0;
391}
392
393# Hokay. Need to:
394# -> prep if -.p OR (-.[cilabs] AND !--short-circuit)
395if ($cmdopts{stage} eq 'p' || ($cmdopts{stage} =~ /[cilabs]/ && $cmdopts{short} ne 'y')) {
396 prep();
397}
398# -> build if -.c OR (-.[ilabs] AND !--short-circuit)
399if ($cmdopts{stage} eq 'c' || ($cmdopts{stage} =~ /[ilabs]/ && $cmdopts{short} ne 'y')) {
400 build();
401}
402# -> install if -.[ilabs]
403#if ($cmdopts{stage} eq 'i' || ($cmdopts{stage} =~ /[labs]/ && $cmdopts{short} ne 'y')) {
404if ($cmdopts{stage} =~ /[ilabs]/) {
405 install();
406#foreach my $pkg (@pkglist) {
407# print "files in $pkg:\n ".$filelist{$pkg}."\n";
408#}
409
410}
411# -> binpkg and srcpkg if -.a
412if ($cmdopts{stage} eq 'a') {
413 binpackage();
414 srcpackage();
415 clean();
416}
417# -> binpkg if -.b
418if ($cmdopts{stage} eq 'b') {
419 binpackage();
420 clean();
421}
422
423# Spit out any closing remarks
424print $finalmessages;
425
426# Just in case.
427exit 0;
428
429
430## load_userconfig()
431# Loads user configuration (if any)
432# Currently only handles .debmacros
433# Needs to handle "other files"
434sub load_userconfig {
435 my $homedir = (getpwuid($<))[7];
436 if (-e "$homedir/.debmacros") {
437 open USERMACROS,"<$homedir/.debmacros";
438 while (<USERMACROS>) {
439 # And we also only handle a few macros at the moment.
440 if (/^\%_topdir/) {
441 my (undef,$tmp) = split /\s+/, $_;
442 $topdir = $tmp;
443 }
444 next if /^\%_/;
445 # Allow arbitrary definitions. Note that we're only doing simple defs here for now.
446 if (/^\%([a-z0-9]+)\s+(.+)$/) {
447 $specglobals{$1} = $2;
448 }
449 }
450 }
451} # end load_userconfig()
452
453
454## parse_cmd()
455# Parses command line into global hash %cmdopts, other globals
456# Options based on rpmbuild's options
457sub parse_cmd {
458 # Don't feel like coding my own option parser...
459 #use Getopt::Long;
460 # ... but I may have to: (OTOH, rpm uses popt, so maybe we can too.)
461 #use Getopt::Popt qw(:all);
462 # Or not. >:( Stupid Debian lack of findable Perl module names in packages.
463
464 # ... also note, single-character options are mostly not flags, so they can't be
465 # sanely combined the way most other programs' single-character flag options can.
466
467 # Stuff it.
468 my $prevopt = '';
469 foreach (@ARGV) {
470 chomp;
471
472 # Is it an option?
473 if (/^-/) {
474
475 # Is it a long option?
476 if (/^--/) {
477 if (/^--short-circuit/) {
478 $cmdopts{short} = 'y';
479 } elsif (/^--rebuild/) {
480 $cmdopts{type} = 's';
481 } elsif (/^--showpkgs/) {
482 $cmdopts{type} = 'd'; # d for 'diagnostic' or 'debug' or 'dump'
483 } elsif (/^--help/) {
484 print "debbuild v$version".q{
485Copyright 2005-2012 Kris Deugau <kdeugau@deepnet.cx>
486
487Build .deb packages from RPM-style .spec files
488debbuild supports most package-building options rpmbuild does.
489
490Build options with [ <specfile> | <tarball> | <source package> ]:
491 -bp build through %prep (unpack sources and apply patches) from <specfile>
492 -bc build through %build (%prep, then compile) from <specfile>
493 -bi build through %install (%prep, %build, then install) from <specfile>
494 -bl verify %files section from <specfile>
495 -ba build source and binary packages from <specfile>
496 -bb build binary package only from <specfile>
497 -bs build source package only from <specfile>
498 -tp build through %prep (unpack sources and apply patches) from <tarball>
499 -tc build through %build (%prep, then compile) from <tarball>
500 -ti build through %install (%prep, %build, then install) from <tarball>
501 -ta build source and binary packages from <tarball>
502 -tb build binary package only from <tarball>
503 -ts build source package only from <tarball>
504 --rebuild build binary package from <source package>
505 --buildroot=DIRECTORY override build root
506 --short-circuit skip straight to specified stage (only for c,i)
507
508Common options:
509 -D, --define='MACRO EXPR' define MACRO with value EXPR
510
511debbuild-specific options:
512 -i Unpack a .sdeb in the %_topdir tree
513 --showpkgs Show package names that would be built. Only works with .spec files.
514
515};
516 exit;
517
518 } elsif (/^--define/) {
519 # nothing to do? Can't see anything needed, we handle the actual definition later.
520##fixme
521# add --self-package here
522# deps build-essential pax fakeroot
523 } else {
524 print "Long option $_ not handled\n";
525 }
526 } else {
527 # Not a long option
528 if (/^-[bt]/) {
529 if ($cmdopts{stage} eq 's') {
530 # Mutually exclusive options.
531 die "Can't use $_ with --rebuild\n";
532 } else {
533 # Capture the type (from "bare" files or tarball) and the stage (prep, build, etc)
534 ($cmdopts{stage}) = (/^-[bt]([pcilabs])/);
535 ($cmdopts{type}) = (/^-([bt])[pcilabs]/);
536 }
537 } elsif (/^-v/) {
538 # bump verbosity. Not sure what I'll actually do here...
539 } elsif (/^-i/) {
540 $cmdopts{install} = 1;
541 $prevopt = '-i';
542 } elsif (/^-D/) {
543 # --define. nothing to do on this round; actual work is done next time.
544 } else {
545 die "Bad option $_\n";
546 }
547 }
548
549 } else { # Not an option argument
550
551 # --buildroot is the only option that takes an argument
552 # Therefore, any *other* bare arguments are the spec file,
553 # tarball, or source package we're operating on - depending
554 # on which one we meet.
555 if ($prevopt eq '--buildroot') {
556 $cmdbuildroot = $_;
557 } elsif ($prevopt eq '--define' || $prevopt eq '-D') {
558 my ($macro,$value) = (/([a-z0-9_.-]+)(?:\s+(.+))?/i);
559 if ($value ne '') {
560 $specglobals{$macro} = $value;
561 } else {
562 warn "WARNING: missing value for macro $macro in --define! Ignoring.\n";
563 }
564 } elsif ($prevopt eq '-i') {
565 $srcpkg = $_;
566 } else {
567 if ($cmdopts{type} eq 's') {
568 # Source package
569 if (!/(sdeb|\.src\.rpm)$/) {
570 die "Can't --rebuild with $_\n";
571 }
572 $srcpkg = $_;
573 } elsif ($cmdopts{type} eq 'b' || $cmdopts{type} eq 'd') {
574 # Spec file
575 $specfile = $_;
576 } else {
577 # Tarball build. Need to extract tarball to find spec file. Whee.
578 $tarball = $_;
579 }
580 }
581 }
582 $prevopt = $_;
583 } # foreach @ARGV
584
585 # Some cross-checks. rpmbuild limits --short-circuit to just
586 # the "compile" and "install" targets - with good reason IMO.
587 # Note that --short-circuit with -.p is not really an error, just redundant.
588 # NB - this is NOT fatal, just ignored!
589 if ($cmdopts{short} eq 'y' && $cmdopts{stage} =~ /[labs]/) {
590 warn "Can't use --short-circuit for $targets{$cmdopts{stage}} stage. Ignoring.\n";
591 $cmdopts{short} = 'n';
592 }
593
594 # Valid options, with example arguments (if any):
595# Build from .spec file; mutually exclusive:
596 # -bp
597 # -bc
598 # -bi
599 # -bl
600 # -ba
601 # -bb
602 # -bs
603# Build from tarball; mutually exclusive:
604 # -tp
605 # -tc
606 # -ti
607 # -ta
608 # -tb
609 # -ts
610# Build from .src.(deb|rpm)
611 # --rebuild
612 # --recompile
613
614# General options
615 # --buildroot=DIRECTORY
616 # --clean
617 # --nobuild
618 # --nodeps
619 # --nodirtokens
620 # --rmsource
621 # --rmspec
622 # --short-circuit
623 # --target=CPU-VENDOR-OS
624
625 #my $popt = new Getopt::Popt(argv => \@ARGV, options => \@optionsTable);
626
627} # end parse_cmd()
628
629
630## parse_spec()
631# Parse the .spec file.
632sub parse_spec {
633 die "No .spec file specified! Exiting.\n" if !$specfile;
634 open SPECFILE,"<$specfile" or die "specfile ($specfile) barfed: $!";
635
636 my $iflevel = 0;
637 my $buildarch = $hostarch;
638 $pkgdata{main}{arch} = $hostarch;
639
640 my $stage = 'preamble';
641 my $subname = 'main';
642 my $scriptlet;
643
644# Basic algorithm:
645# For each line
646# if it's a member of an %if construct, branch and see which segment of the
647# spec file we need to parse and which one gets discarded, then
648# short-circuit back to the top of the loop.
649# if it's a %section, bump the stage. Preparse addons to the %section line
650# (eg subpackage) and stuff them in suitable loop-global variables, then
651# short-circuit back to the top of the loop.
652# Otherwise, parse the line according to which section we're supposedly
653# parsing right now
654
655LINE: while (<SPECFILE>) {
656 next if /^#/ && $stage eq 'preamble'; # Ignore comments...
657 next if /^\s*$/ && $stage eq 'preamble'; # ... and blank lines.
658
659# no sense in continuing if we find something we don't grok
660 # Yes, this is really horribly fugly. But it's a cheap crosscheck against invalid
661 # %-tags which also make rpmbuild barf. In theory.
662# notes: some of these are not *entirely* case-sensitive (%ifxxx), but most are.
663 # Extracted from the Maximum RPM online doc via:
664 # grep -h %[a-z] *|perl -e 'while (<>) { /\%([a-z0-9]+)\b/; print "$1|\n"; }'|sort -u
665 if (/^%[a-z]/ &&
666 $_ !~ /^%(?:attr|build|changelog|check|clean|config|configure|defattr|define|description|
667 dir|doc|docdir|else|endif|files|ghost|if|ifarch|ifn|ifnarch|ifnos|ifnxxx|fos|ifxxx|
668 install|makeinstall|package|patch\d+|post|postun|pre|prep|preun|readme|setup[^\s]*|
669 triggerin|triggerpostun|triggerun|verify|verifyscript)\b/x
670 ) {
671 my ($badtag) = (/^%([a-z]+)/i);
672 die "Unknown tag \%$badtag at line $. of $specfile\n";
673 }
674
675# preprocess %define's
676 if (my ($key, $def) = (/^\%define\s+([^\s]+)\s+(.+)$/) ) {
677 $specglobals{$key} = expandmacros($def,'g');
678 }
679
680 if (/^\%if/) {
681 s/^\%if//;
682 chomp;
683 my $expr = expandmacros($_, 'g');
684 $iflevel++;
685
686 if ($expr !~ /^\s*\d+\s*$/) {
687 # gots a logic statement we want to turn into a 1 or a 0. most likely by eval'ing it.
688
689 $expr =~ s/\s+//g;
690
691# For Great w00tness! New and Improved multilayered logic handling.
692
693 my @bits = split /\b/, $expr;
694 $expr = '';
695 foreach my $bit (@bits) {
696 next if $bit eq '"';
697 $bit =~ s/"//g;
698 $expr .= qq("$bit") if $bit =~ /^\w+$/;
699 $expr .= $bit if $bit !~ /^\w+$/;
700 }
701
702 # Done in this order so we don't cascade incorrectly. Yes, those spaces ARE correct in the replacements!
703 $expr =~ s/==/ eq /g;
704 $expr =~ s/!=/ ne /g;
705 $expr =~ s/<=>/ cmp /g;
706 $expr =~ s/<=/ le /g;
707 $expr =~ s/>=/ ge /g;
708 $expr =~ s/</ lt /g;
709 $expr =~ s/>/ gt /g;
710
711 # Turn it into something that eval's to a number. Maybe not needed? O_o
712 #$expr = "( $expr ? 1 : 0 )";
713
714 $expr = eval $expr;
715 }
716
717 next LINE if $expr != 0; # This appears to be the only case we call false.
718 while (<SPECFILE>) {
719 if (/^\%endif/) {
720 $iflevel--;
721 next LINE;
722 } elsif (/^\%else/) {
723 next LINE;
724 }
725 }
726 }
727 if (/^\%else/) {
728 while (<SPECFILE>) {
729 if (/^\%endif/) {
730 $iflevel--;
731 next LINE;
732 }
733 }
734 }
735 if (/^\%endif/) {
736 $iflevel--;
737 next LINE;
738 } # %if/%else/%endif
739
740 if (/^\%{echo:(.+)}/) {
741 my $output = expandmacros($1, 'gp');
742 print "$output";
743 next LINE;
744 }
745
746# now we pick out the sections and set "state" to parse that section. Fugly but I can't see a better way. >:(
747
748 if (/^\%description(?:\s+(?:-n\s+)?(.+))?/) {
749 $stage = 'desc';
750 $subname = "main";
751 if ($1) { # Magic to add entries to the right package
752 my $tmp = expandmacros("$1", 'gp');
753 if (/-n/) { $subname = $tmp; } else { $subname = "$pkgdata{main}{name}-$tmp"; }
754 }
755 next LINE;
756 } # %description
757
758 if (/^\%package\s+(?:-n\s+)?(.+)/) {
759 $stage = 'package';
760 if ($1) { # Magic to add entries to the right package
761 my $tmp = expandmacros("$1", 'gp');
762 if (/-n/) { $subname = $tmp; } else { $subname = "$pkgdata{main}{name}-$tmp"; }
763 }
764 push @pkglist, $subname;
765 $pkgdata{$subname}{name} = $subname;
766 $pkgdata{$subname}{version} = $pkgdata{main}{version};
767 # Build "same arch as previous package found" by default. Where rpm just picks the
768 # *very* last one, we want to allow arch<native>+arch-all
769 # (eg, Apache is i386, but apache-manual is all)
770 $pkgdata{$subname}{arch} = $buildarch; # Since it's likely subpackages will NOT have a BuildArch line...
771 next LINE;
772 } # %package
773
774 if (/^\%prep/) {
775 $stage = 'prep';
776 # Replace some core macros
777 $pkgdata{main}{source} = expandmacros($pkgdata{main}{source},'gp');
778 next LINE;
779 } # %prep
780
781 if (/^\%build/) {
782 $stage = 'build';
783 $buildscript .= "cd '$tarballdir'\n" if $pkgdata{main}{hassetup};
784 next LINE;
785 } # %build
786
787 if (/^\%install/) {
788 $stage = 'install';
789 $installscript .= "cd '$tarballdir'\n" if $pkgdata{main}{hassetup};
790 next LINE;
791 } # %install
792
793 if (/^\%clean/) {
794 $stage = 'clean';
795 $cleanscript .= "cd '$tarballdir'\n" if $pkgdata{main}{hassetup};
796 next LINE;
797 } # %clean
798
799 if (/^\%(pre|post|preun|postun)\b(?:\s+(?:-n\s+)?(.+))?/i) {
800 $stage = 'prepost';
801 $scriptlet = lc $1;
802 $subname = 'main';
803 if ($2) { # Magic to add entries to the right package
804 my $tmp = expandmacros("$2", 'g');
805 if (/-n/) { $subname = $tmp; } else { $subname = "$pkgdata{main}{name}-$tmp"; }
806 }
807 next LINE;
808 } # %pre/%post/%preun/%postun
809
810 if (/^\%files(?:\s+(?:-n\s+)?(.+))?/) {
811 $stage = 'files';
812 $subname = 'main';
813 if ($1) { # Magic to add entries to the right list of files
814 my $tmp = expandmacros("$1", 'gp');
815 if (/-n/) { $subname = $tmp; } else { $subname = "$pkgdata{main}{name}-$tmp"; }
816 }
817 next LINE;
818 } # %files
819
820 if (/^\%changelog/) {
821 $stage = 'changelog';
822 $pkgdata{main}{changelog} = '';
823 next LINE;
824 }
825
826# now we handle individual lines from the various sections
827
828 if ($stage eq 'desc') {
829 next LINE if /^\s*#/;
830 $pkgdata{$subname}{desc} .= " $_";
831 } # description
832
833 if ($stage eq 'package') {
834 # gotta expand %defines here. Whee.
835# Note that we look for the Debian-specific Recommends, Suggests, and Replaces,
836# although they will have to be wrapped in '%if %{_vendor} == "debbuild"' for
837# an rpmbuild-compatible .spec file
838# NB: NOT going to support Pre-Depends, since it's a "Don't Use" (mis)feature, and
839# RPM's support for a similar tag (PreReq) has been recently dropped.
840 if (my ($dname,$dvalue) = (/^(Recommends|Suggests|Enhances|Replaces|Summary|Group|Version|Requires|Conflicts|Provides|BuildArch(?:itecture)?):\s+(.+)$/i)) {
841 $dname =~ tr/[A-Z]/[a-z]/;
842 if ($dname =~ /^BuildArch/i) {
843 $dvalue =~ s/^noarch/all/ig;
844 $buildarch = $dvalue; # Emulate rpm's behaviour to a degree
845 $dname = 'arch';
846 }
847 if ($dname =~ /recommends|suggests|enhances|replaces|requires|conflicts|provides/) {
848 $pkgdata{$subname}{$dname} .= ", ".expandmacros($dvalue, 'gp');
849 } else {
850 $pkgdata{$subname}{$dname} = expandmacros($dvalue, 'gp');
851 }
852 }
853 } # package
854
855 if ($stage eq 'prep') {
856 # Actual handling for %prep section. May have %setup macro; may
857 # include %patch tags, may be just a bare shell script.
858 if (/^\%setup/) {
859 $pkgdata{main}{hassetup} = 1; # flag the fact that we've got %setup
860 # Parse out the %setup macro.
861 chomp;
862 # rpmbuild doesn't complain about gibberish immediately following %setup, but we will
863 warn "Suspect \%setup tag '$_', continuing\n" if ! /^\%setup(?:\s|$)/;
864
865 # Prepare some flags
866 my $changedir = 0;
867 my $deldir = 1;
868 my $quietunpack = 0;
869 my $deftarball = 1;
870 my $snum = '';
871 my $sbefore;
872 my $safter;
873 my $altsource = 0;
874
875 # yes, really. It's not clear how this cascades down to %build or %install
876 my $thistarballdir = $tarballdir;
877
878 my @sbits = split /\s+/;
879 shift @sbits;
880 while (my $sopt = shift @sbits) {
881 if ($sopt eq '-n') {
882 $thistarballdir = shift @sbits;
883 } elsif ($sopt eq '-a') {
884 # shift, next opt must be numeric (which sourcenn:)
885 $sopt = shift @sbits;
886 die "Argument for \%setup -a must be numeric at $specfile line $.\n" if $sopt !~ /^\d+$/;
887 $safter = $sopt;
888 $snum = $sopt;
889 $altsource = 1;
890 } elsif ($sopt eq '-b') {
891 # shift, next opt must be numeric (which sourcenn:)
892 $sopt = shift @sbits;
893 die "Argument for \%setup -b must be numeric at $specfile line $.\n" if $sopt !~ /^\d+$/;
894 $sbefore = $sopt;
895 $snum = $sopt;
896 $altsource = 2;
897 } elsif ($sopt eq '-c') {
898 # flag, create and change directory before unpack
899 $changedir = 1;
900 } elsif ($sopt eq '-D') {
901 # flag, do not delete directory before unpack
902 $deldir = 0;
903 } elsif ($sopt eq '-T') {
904 # flag, do not unpack first source
905 $deftarball = 0;
906 } elsif ($sopt eq '-q') {
907 # SSSH! Unpack quietly
908 $quietunpack = 1;
909 }
910 } # $sopt = shift @sbits
911
912# Note that this is an incomplete match to rpmbuild's full %setup expression.
913# Still known to do:
914# - Match implementation of -a and -b if both are specified.
915# Looks to be "expand -a into $aftersource and expand -b into $beforesource inside the while()"
916# instead of treating them as flags like the other arguments.
917# - -q appears to be somewhat positional
918
919 $prepscript .= "cd '$topdir/BUILD'\n";
920 $tarballdir = expandmacros($tarballdir,'gp');
921 $prepscript .= "rm -rf '$thistarballdir'\n" if $deldir;
922 $prepscript .= "/bin/mkdir -p $thistarballdir\n" if $changedir;
923
924 if ($deftarball) {
925 $prepscript .= "cd '$thistarballdir'\n" if $changedir;
926 $prepscript .=
927 ( $pkgdata{main}{source} =~ /\.tar\.gz$/ ? "/bin/gzip" : "" ).
928 ( $pkgdata{main}{source} =~ /\.tar\.bz2$/ ? "/bin/bzip2" : "" ).
929 ( $pkgdata{main}{source} =~ /\.tar\.xz$/ ? "/usr/bin/xz" : "" ).
930 " -dc '$topdir/SOURCES/$pkgdata{main}{source}' | /bin/tar -x".
931 ( $quietunpack ? '' : 'vv' )."f -\n".
932 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n);
933 }
934
935 if ($altsource) {
936 if ($altsource == 1) { # -a
937 $prepscript .= "cd '$thistarballdir'\n";
938 $prepscript .=
939 ( $pkgdata{sources}{$snum} =~ /\.tar\.gz$/ ? "/bin/gzip" : "" ).
940 ( $pkgdata{sources}{$snum} =~ /\.tar\.bz2$/ ? "/bin/bzip2" : "" ).
941 ( $pkgdata{sources}{$snum} =~ /\.tar\.xz$/ ? "/usr/bin/xz" : "" ).
942 " -dc '$topdir/SOURCES/$pkgdata{sources}{$snum}' | /bin/tar -x".
943 ( $quietunpack ? '' : 'vv' )."f -\n".
944 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n);
945 } # $altsource == 1
946
947 if ($altsource == 2) { # -b
948 $prepscript .=
949 ( $pkgdata{sources}{$snum} =~ /\.tar\.gz$/ ? "/bin/gzip" : "" ).
950 ( $pkgdata{sources}{$snum} =~ /\.tar\.bz2$/ ? "/bin/bzip2" : "" ).
951 ( $pkgdata{sources}{$snum} =~ /\.tar\.xz$/ ? "/usr/bin/xz" : "" ).
952 " -dc '$topdir/SOURCES/$pkgdata{sources}{$snum}' | /bin/tar -x".
953 ( $quietunpack ? '' : 'vv' )."f -\n".
954 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n);
955 $prepscript .= "cd '$thistarballdir'\n";
956 } # $altsource == 2
957 }
958
959# rpm doesn't seem to do the chowns any more
960# qq([ `/usr/bin/id -u` = '0' ] && /bin/chown -Rhf root .\n).
961# qq([ `/usr/bin/id -u` = '0' ] && /bin/chgrp -Rhf root .\n).
962 $prepscript .=
963 qq(/bin/chmod -Rf a+rX,u+w,g-w,o-w .\n);
964
965 } elsif ( my ($patchnum,$patchopts) = (/^\%patch([^\s]+)(\s+.+)?$/) ) {
966 chomp $patchnum;
967 $prepscript .= qq(echo "Patch #$patchnum ($pkgdata{main}{"patch$patchnum"}):"\n);
968 # If there are options passed, use'em.
969 # Otherwise, catch a bare %patch and ASS-U-ME it's '-p0'-able.
970 # Will break on options that don't provide -pnn, but what the hell.
971 # Need to decompress .bz2 and .gz patches on the fly. Not sure why one
972 # would ever have such things, but there they are... and rpmbuild supports 'em
973 # patch originally suggested by Lucian Cristian for .bz2
974 # reworked and expanded to support .Z/.gz/.xz as well
975 if ( $pkgdata{main}{"patch$patchnum"} =~ /\.(?:Z|gz|bz2|xz)$/ ) {
976 my $decompressor;
977 $decompressor = 'gzip' if $pkgdata{main}{"patch$patchnum"} =~ /\.(?:Z|gz)$/;
978 $decompressor = 'bzip2' if $pkgdata{main}{"patch$patchnum"} =~ /\.bz2$/;
979 $decompressor = 'xz' if $pkgdata{main}{"patch$patchnum"} =~ /\.xz$/;
980 $prepscript .= qq(/bin/$decompressor -d < $topdir/SOURCES/$pkgdata{main}{"patch$patchnum"} | patch );
981 $prepscript .= $patchopts if $patchopts;
982 $prepscript .= "-p0" if !$patchopts;
983 $prepscript .= qq( -s\nSTATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n);
984 } else {
985 $prepscript .= "patch ";
986 $prepscript .= $patchopts if $patchopts;
987 $prepscript .= "-p0" if !$patchopts;
988 $prepscript .= " -s <$topdir/SOURCES/".$pkgdata{main}{"patch$patchnum"}."\n";
989 }
990 } else {
991 $prepscript .= expandmacros($_,'gp');
992 }
993 next LINE;
994 } # prep
995
996 if ($stage eq 'build') {
997 # %build. This is pretty much just a shell script. There
998 # aren't many local macros to deal with.
999 if (/^\%configure/) {
1000 $buildscript .= expandmacros($_,'cgbp');
1001 } elsif (/^\%\{__make\}/) {
1002 $buildscript .= expandmacros($_,'mgbp');
1003 } else {
1004 $buildscript .= expandmacros($_,'gp');
1005 }
1006 next LINE;
1007 } # build
1008
1009 if ($stage eq 'install') {
1010 if (/^\%makeinstall/) {
1011 $installscript .= expandmacros($_,'igbp');
1012 } else {
1013 $installscript .= expandmacros($_,'gp');
1014 }
1015 next LINE;
1016 } # install
1017
1018 if ($stage eq 'clean') {
1019 $cleanscript .= expandmacros($_,'gp');
1020 next LINE;
1021 } # clean
1022
1023 if ($stage eq 'prepost') {
1024 $pkgdata{$subname}{$scriptlet} .= expandmacros($_,'gp');
1025 next LINE;
1026 } # prepost
1027
1028 if ($stage eq 'files') {
1029 # need to deal with these someday
1030 next LINE if /^\%dir/;
1031 next LINE if /^\%defattr/;
1032 next LINE if /^\%verify/;
1033 # dunno what to do with this; not sure if there's space in Debian's package structure for it.
1034 next LINE if /^\%ghost/;
1035 # Debian dpkg doesn't speak "%docdir". Meh.
1036 next LINE if /^\%docdir/;
1037# my $singleton = 0; # don't recall what this was for
1038 next LINE if /^\s*#/;
1039 next LINE if /^\s*$/;
1040
1041# create and initialize flags
1042 my ($perms, $owner, $group, $conf, $filesline);
1043 $perms = $owner = $group = $conf = '-';
1044
1045 $filesline = $_;
1046
1047 # strip and flag %attr constructs
1048 if ($filesline =~ /\%attr\b/) {
1049 # Extract %attr...
1050 my ($args) = (/(\%attr\s*\(\s*[\d-]+\s*,\s*["a-zA-Z0-9-]+\s*,\s*["a-zA-Z0-9-]+\s*\))/);
1051 $args =~ s/\s+//g;
1052 $args =~ s/"//g; # don't think quotes are ever necessary, but they're *allowed*
1053 # ... and parse it ...
1054 ($perms,$owner,$group) = ($args =~ /\(([\d-]+),([a-zA-Z0-9-]+),([a-zA-Z0-9-]+)/);
1055 # ... and wipe it when we're done.
1056 $filesline =~ s/\%attr\s*\(\s*[\d-]+\s*,\s*["a-zA-Z0-9-]+\s*,\s*["a-zA-Z0-9-]+\s*\)//;
1057 }
1058
1059 # Conffiles. Note that Debian and RH have similar, but not
1060 # *quite* identical ideas of what constitutes a conffile. Nrgh.
1061 # Note that dpkg will always ask if you want to replace the file - noreplace
1062 # is more or less permanently enabled.
1063##fixme
1064# also need to handle missingok (file that doesn't exist, but should be removed on uninstall)
1065# hmm. not sure if such is **POSSIBLE** with Debian... maybe an addition to %post?
1066 if ($filesline =~ /\%config\b(?:\s*\(\s*noreplace\s*\)\s*)?/) {
1067 $pkgdata{$subname}{conffiles} = 1; # Flag it for later
1068 $conf = 'y';
1069 $filesline =~ s/\%config\b(?:\s*\(\s*noreplace\s*\)\s*)?//;
1070 }
1071
1072 # %doc needs extra processing, because it can be a space-separated list, and may
1073 # include both full and partial pathnames. The partial pathnames must be fiddled
1074 # into place in the %install script, because Debian doesn't really have the concept
1075 # of "documentation file" that rpm does. (Debian "documentation files" are files
1076 # in /usr/share/doc/<packagename>.)
1077##fixme: unhandled case: %doc %defattr. Eeep.
1078# don't really know what to do with %defattr, generally. :(
1079 if ($filesline =~ /\%doc\b/) {
1080 $filesline =~ s/\s*\%doc\s+//;
1081
1082# this could probably go elsewhere.
1083 my $pkgname = $pkgdata{$subname}{name};
1084 $pkgname =~ tr/_/-/;
1085
1086 # have to extract the partial pathnames that %doc installs automagically
1087 foreach (split /\s+/, $filesline) {
1088 if (! (/^\%/ or m|^/|) ) {
1089 $doclist{$subname} .= " $_";
1090 my ($element) = m|([^/\s]+/?)$|;
1091 $filesline =~ s|$_|\%{_docdir}/$pkgname/$element|;
1092 }
1093 }
1094 } # $filesline =~ /\%doc\b/
1095
1096 $filesline =~ s/^\s*//; # Just In Case. For, uh, neatness.
1097
1098# due to Debian's total lack of real permissions-processing in its actual package
1099# handling component (dpkg-deb), this can't really be done "properly". We'll have
1100# to add chown/chmod commands to the postinst instead. Feh.
1101 $pkgdata{$subname}{'post'} .= "chown $owner $filesline\n" if $owner ne '-';
1102 $pkgdata{$subname}{'post'} .= "chgrp $group $filesline\n" if $group ne '-';
1103 $pkgdata{$subname}{'post'} .= "chmod $perms $filesline\n" if $perms ne '-';
1104
1105##fixme
1106# need hackery to assure only one filespec per %config. NB: "*" is one filespec. <g>
1107 push @{$pkgdata{$subname}{conflist}}, $filesline if $conf ne '-';
1108
1109 # now that we've got the specials out of the way, we can add things to the appropriate list of files.
1110 # ... and finally everything else
1111 $filelist{$subname} .= " $filesline";
1112
1113 next LINE;
1114 } # files
1115
1116 if ($stage eq 'changelog') {
1117 # this is one of the few places we do NOT generally want to replace macros...
1118 $pkgdata{main}{changelog} .= $_;
1119 }
1120
1121 if ($stage eq 'preamble') {
1122 if (/^summary:\s*(.+)/i) {
1123 $pkgdata{main}{summary} = $1;
1124 } elsif (/^name:\s*(.+)/i) {
1125 $pkgdata{main}{name} = expandmacros($1,'g');
1126 } elsif (/^epoch:\s*(.+)/i) {
1127 $pkgdata{main}{epoch} = expandmacros($1,'g');
1128 } elsif (/^version:\s*(.+)/i) {
1129 $pkgdata{main}{version} = expandmacros($1,'g');
1130 } elsif (/^release:\s*(.+)/i) {
1131 $pkgdata{main}{release} = expandmacros($1,'g');
1132 } elsif (/^group:\s*(.+)/i) {
1133 $pkgdata{main}{group} = $1;
1134 } elsif (/^copyright:\s*(.+)/i) {
1135 $pkgdata{main}{copyright} = $1;
1136 } elsif (/^url:\s*(.+)/i) {
1137 $pkgdata{main}{url} = $1;
1138 } elsif (/^packager:\s*(.+)/i) {
1139 $pkgdata{main}{packager} = $1;
1140 } elsif (/^vendor:\s*(.+)/i) {
1141 $specglobals{vendor} = $1;
1142 } elsif (/^buildroot:\s*(.+)/i) {
1143 $specglobals{buildroot} = $1;
1144 } elsif (my ($sval) = /^source0?:\s*(.+)/i) {
1145 $sval =~ s/\s*$//;
1146 $pkgdata{main}{source} = $sval;
1147 # Do this here since we don't use any URL forms elsewhere (maybe some other time)
1148 $pkgdata{main}{source} =~ s|^.+/([^/]+)$|$1|;
1149 $pkgdata{sources}{0} = $pkgdata{main}{source};
1150 # .tar, .tar.gz, .tar.bz2, .tgz, .xz
1151 die "Unknown Source: header format $sval\n" if $sval !~ /\.(?:tar(?:\.(?:gz|bz2|xz))?|tgz)$/;
1152 } elsif (my ($srcnum, $src) = (/^source([0-9]+):\s*(.+)/i)) {
1153 $src =~ s/\s*$//;
1154 $pkgdata{sources}{$srcnum} = $src;
1155 # Do this here since we don't use any URL forms elsewhere (maybe some other time)
1156 $pkgdata{sources}{$srcnum} =~ s|^.+/([^/]+)$|$1|;
1157 } elsif (/^patch([^:]+):\s*(.+)$/i) {
1158 my $patchname = "patch$1";
1159 $pkgdata{main}{$patchname} = $2;
1160 if ($pkgdata{main}{$patchname} =~ /\//) {
1161 # URL-style patch. Rare but not unheard-of.
1162 my @patchbits = split '/', $pkgdata{main}{$patchname};
1163 $pkgdata{main}{$patchname} = $patchbits[$#patchbits];
1164 }
1165 chomp $pkgdata{main}{$patchname};
1166 } elsif (/^buildarch(?:itecture)?:\s*(.+)/i) {
1167 $pkgdata{main}{arch} = $1;
1168 $pkgdata{main}{arch} =~ s/^noarch$/all/;
1169 $buildarch = $pkgdata{main}{arch};
1170 } elsif (/^buildrequires:\s*(.+)/i) {
1171 $buildreq .= ", $1";
1172 } elsif (/^requires:\s*(.+)/i) {
1173 $pkgdata{main}{requires} .= ", ".expandmacros("$1", 'gp');
1174 } elsif (/^provides:\s*(.+)/i) {
1175 $pkgdata{main}{provides} .= ", $1";
1176 } elsif (/^conflicts:\s*(.+)/i) {
1177 $pkgdata{main}{conflicts} .= ", $1";
1178 } elsif (/^recommends:\s*(.+)/i) {
1179 $pkgdata{main}{recommends} .= ", $1";
1180 warn "Warning: Debian-specific 'Recommends:' outside \%if wrapper\n" if $iflevel == 0;
1181# As of sometime between RHEL 6 and RHEL 7 or so, support was added for Recommends: and Enhances:,
1182# along with shiny new tag Supplements:. We'll continue to warn about them for a while.
1183 } elsif (/^suggests:\s*(.+)/i) {
1184 $pkgdata{main}{suggests} .= ", $1";
1185 warn "Warning: 'Suggests:' outside \%if wrapper\n" if $iflevel == 0;
1186 } elsif (/^enhances:\s*(.+)/i) {
1187 $pkgdata{main}{enhances} .= ", $1";
1188 warn "Warning: 'Enhances:' outside \%if wrapper\n" if $iflevel == 0;
1189 } elsif (/^supplements:\s*(.+)/i) {
1190 $pkgdata{main}{enhances} .= ", $1";
1191 warn "Warning: 'Supplements:' is not natively supported by .deb packages. Downgrading relationship to Enhances:.\n";
1192 } elsif (/^replaces:\s*(.+)/i) {
1193 $pkgdata{main}{replaces} .= ", $1";
1194 warn "Warning: 'Replaces:' outside \%if wrapper\n" if $iflevel == 0;
1195 } elsif (/^obsoletes:\s*(.+)/i) {
1196 $pkgdata{main}{replaces} .= ", $1";
1197 } elsif (/^autoreq(?:prov)?:\s*(.+)/i) {
1198 # we don't handle auto-provides (yet)
1199 $NoAutoReq = 1 if $1 =~ /(?:no|0)/i;
1200 }
1201 next LINE;
1202 } # preamble
1203
1204 } # while <SPEC>
1205
1206 # Parse and replace some more macros. More will be replaced even later.
1207
1208 # Expand macros as necessary.
1209 $scriptletbase = expandmacros($scriptletbase,'gp');
1210
1211 $cleanscript = expandmacros($cleanscript,'gp');
1212
1213 $specglobals{buildroot} = $cmdbuildroot if $cmdbuildroot;
1214 $specglobals{buildroot} = expandmacros($specglobals{buildroot},'gp');
1215
1216 close SPECFILE;
1217} # end parse_spec()
1218
1219
1220## prep()
1221# Writes and executes the %prep script (mostly) built while reading the spec file.
1222sub prep {
1223 # Replace some things here just to make sure.
1224 $prepscript = expandmacros($prepscript,'gp');
1225
1226 # create script filename
1227 my $prepscriptfile = "$tmpdir/deb-tmp.prep.".int(rand(99998)+1);
1228 sysopen(PREPSCRIPT, $prepscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
1229 or die "Can't open/create prep script file $prepscriptfile: $!\n";
1230 print PREPSCRIPT $scriptletbase;
1231 print PREPSCRIPT $prepscript;
1232 close PREPSCRIPT;
1233
1234 # execute
1235 print "Calling \%prep script $prepscriptfile...\n";
1236 system("/bin/sh -e $prepscriptfile") == 0
1237 or die "Can't exec: $!\n";
1238
1239 # and clean up
1240 unlink $prepscriptfile;
1241} # end prep()
1242
1243
1244## build()
1245# Writes and executes the %build script (mostly) built while reading the spec file.
1246sub build {
1247 # Expand the macros
1248 $buildscript = expandmacros($buildscript,'cgbp');
1249
1250 # create script filename
1251 my $buildscriptfile = "$tmpdir/deb-tmp.build.".int(rand(99998)+1);
1252 sysopen(BUILDSCRIPT, $buildscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
1253 or die "Can't open/create build script file $buildscriptfile: $!\n";
1254 print BUILDSCRIPT $scriptletbase;
1255 print BUILDSCRIPT $buildscript;
1256 close BUILDSCRIPT;
1257
1258 # execute
1259 print "Calling \%build script $buildscriptfile...\n";
1260 system("/bin/sh -e $buildscriptfile") == 0
1261 or die "Can't exec: $!\n";
1262
1263 # and clean up
1264 unlink $buildscriptfile;
1265} # end build()
1266
1267
1268## install()
1269# Writes and executes the %install script (mostly) built while reading the spec file.
1270sub install {
1271
1272 # munge %doc entries into place
1273 # rpm handles this with a separate executed %doc script, we're not going to bother.
1274 foreach my $docpkg (keys %doclist) {
1275 my $pkgname = $pkgdata{$docpkg}{name};
1276 $pkgname =~ s/_/-/g;
1277
1278 $installscript .= "DOCDIR=\$RPM_BUILD_ROOT\%{_docdir}/$pkgname\nexport DOCDIR\n";
1279 $installscript .= "mkdir -p \$DOCDIR\n";
1280 $doclist{$docpkg} =~ s/^\s*//;
1281 foreach (split(' ',$doclist{$docpkg})) {
1282 $installscript .= "cp -pr $_ \$DOCDIR/\n";
1283 }
1284 }
1285
1286 # Expand the macros
1287 $installscript = expandmacros($installscript,'igbp');
1288
1289 # create script filename
1290 my $installscriptfile = "$tmpdir/deb-tmp.inst.".int(rand(99998)+1);
1291 sysopen(INSTSCRIPT, $installscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
1292 or die "Can't open/create install script file $installscriptfile: $!\n";
1293 print INSTSCRIPT $scriptletbase;
1294 print INSTSCRIPT $installscript;
1295 close INSTSCRIPT;
1296
1297 # execute
1298 print "Calling \%install script $installscriptfile...\n";
1299 system("/bin/sh -e $installscriptfile") == 0
1300 or die "Can't exec: $!\n";
1301
1302 # and clean up
1303 unlink $installscriptfile;
1304
1305 # final bit: compress manpages if present
1306 # done here cuz I don't grok shell well
1307 # should probably error-check all kinds of things. <g>
1308 if (opendir MANROOT, "$specglobals{buildroot}/usr/share/man") {
1309 my @mansects = readdir MANROOT;
1310 closedir MANROOT;
1311 foreach my $mandir (@mansects) {
1312 next if $mandir !~ /^man/;
1313 opendir MANPAGES, "$specglobals{buildroot}/usr/share/man/$mandir";
1314 my @pagelist = readdir MANPAGES;
1315 closedir MANPAGES; # Slightly safer to to this; no accidental recursion. O_o
1316 foreach my $manpage (@pagelist) {
1317 $manpage = "$specglobals{buildroot}/usr/share/man/$mandir/$manpage";
1318 if ( -f $manpage) {
1319 if ($manpage =~ /^(.+)\.(?:Z|gz|bz2)\n?$/) {
1320 my $newpage = $1;
1321 `gunzip $manpage` if $manpage =~ /\.(?:Z|gz)$/;
1322 `bunzip2 $manpage` if $manpage =~ /\.bz2$/;
1323 $manpage = $newpage;
1324 }
1325 `gzip -9 -n $manpage`;
1326 } elsif ( -l $manpage) {
1327 my $linkdest = readlink $manpage;
1328 $linkdest =~ s/\.(?:Z|gz|bz2)//;
1329 unlink $manpage;
1330 $manpage =~ s/\.(?:Z|gz|bz2)//;
1331 symlink "$linkdest.gz", "$manpage.gz" or print "DEBUG: wibble: symlinking manpage failed: $!\n";
1332 }
1333 }
1334 }
1335 } # if opendir MANROOT
1336} # end install()
1337
1338
1339## binpackage()
1340# Creates the binary .deb package from the installed tree in $specglobals{buildroot}.
1341# Writes and executes a shell script to do so.
1342# Creates miscellaneous files required by dpkg-deb to actually build the package file.
1343# Should handle simple subpackages
1344sub binpackage {
1345
1346 foreach my $pkg (@pkglist) {
1347
1348 $pkgdata{$pkg}{arch} = $hostarch if !$pkgdata{$pkg}{arch}; # Just In Case.
1349
1350 # Make sure we have somewhere to write the .deb file
1351 if (!-e "$topdir/DEBS/$pkgdata{$pkg}{arch}") {
1352 mkdir "$topdir/DEBS/$pkgdata{$pkg}{arch}";
1353 }
1354
1355 # Skip building a package that doesn't have any files or dependencies. True
1356 # metapackages don't have any files, but they depend on a bunch of things.
1357 # Packages with neither have, essentially, no content.
1358 next if
1359 (!$filelist{$pkg} or $filelist{$pkg} =~ /^\s*$/) &&
1360 (!$pkgdata{$pkg}{requires});
1361 $filelist{$pkg} = '' if !$filelist{$pkg};
1362
1363 # Gotta do this first, otherwise we don't have a place to move files from %files
1364 mkdir "$specglobals{buildroot}/$pkg";
1365
1366 # Eliminate any lingering % macros
1367 $filelist{$pkg} = expandmacros $filelist{$pkg}, 'gp';
1368
1369 my @pkgfilelist = split ' ', $filelist{$pkg};
1370 foreach my $pkgfile (@pkgfilelist) {
1371 $pkgfile = expandmacros($pkgfile, 'gp');
1372
1373 # Feh. Manpages don't **NEED** to be gzipped, but rpmbuild does, and so shall we.
1374 # ... and your little info page too!
1375 if ($pkgfile =~ m{/usr/share/(?:man/man|info)}) {
1376 # need to check to see if manpage is gzipped
1377 if (-e "$specglobals{buildroot}$pkgfile") {
1378 # if we've just been pointed to a manpage section with "many" pages,
1379 # we need to gzip them all.
1380 # fortunately, we do NOT need to explicitly track each file for the
1381 # purpose of stuffing them in the package... the original %files
1382 # entry will do just fine.
1383 if ( -d "$specglobals{buildroot}$pkgfile") {
1384 foreach my $globfile (glob("$specglobals{buildroot}$pkgfile/*")) {
1385 gzip $globfile if $globfile !~ m|\.gz$|;
1386 }
1387 } else {
1388 if ($pkgfile !~ m|\.gz$|) {
1389 qx { gzip $specglobals{buildroot}$pkgfile };
1390 $pkgfile .= ".gz";
1391 }
1392 }
1393 } else {
1394 if ($pkgfile !~ m|\.gz$|) {
1395 $pkgfile .= ".gz";
1396 } else {
1397 $pkgfile =~ s/\.gz$//;
1398 qx { gzip $specglobals{buildroot}$pkgfile };
1399 $pkgfile .= ".gz";
1400 }
1401 }
1402 }
1403
1404 my ($fpath,$fname) = ($pkgfile =~ m|(.+?/?)?([^/]+/?)$|); # We don't need $fname now, but we might.
1405 qx { mkdir -p $specglobals{buildroot}/$pkg$fpath }
1406 if $fpath && $fpath ne '';
1407 qx { mv $specglobals{buildroot}$pkgfile $specglobals{buildroot}/$pkg$fpath };
1408 }
1409
1410 # Get the "Depends" (Requires) a la RPM. Ish. We strip the leading
1411 # comma and space here (if needed) in case there were "Requires" specified
1412 # in the spec file - those would precede these.
1413 $pkgdata{$pkg}{requires} .= getreqs("$specglobals{buildroot}/$pkg") if ! $NoAutoReq;
1414
1415 # magic needed to properly version dependencies...
1416 # only provided deps will really be included
1417 $pkgdata{$pkg}{requires} =~ s/^, //; # Still have to do this here.
1418 $pkgdata{$pkg}{requires} =~ s/\s+//g;
1419 my @deps = split /,/, $pkgdata{$pkg}{requires};
1420 my $tmp = '';
1421 foreach my $dep (@deps) {
1422 # Hack up the perl(Class::SubClass) deps into something dpkg can understand.
1423 # May or may not be versioned.
1424 # We do this first so the version rewriter can do its magic next.
1425 if (my ($mod,$ver) = ($dep =~ /^perl\(([A-Za-z0-9\:\-]+)\)([><=]+.+)?/) ) {
1426 $mod =~ s/^perl\(//;
1427 $mod =~ s/\)$//;
1428 $mod =~ s/::/-/g;
1429 $mod =~ tr/A-Z/a-z/;
1430 $mod = "lib$mod-perl";
1431 $mod .= $ver if $ver;
1432 $dep = $mod;
1433 }
1434 if (my ($name,$rel,$value) = ($dep =~ /^([a-zA-Z0-9._-]+)([><=]+)([a-zA-Z0-9._-]+)$/)) {
1435 $tmp .= ", $name ($rel $value)";
1436 } else {
1437 $tmp .= ", $dep";
1438 }
1439 }
1440 ($pkgdata{$pkg}{requires} = $tmp) =~ s/^, //;
1441
1442 # Do this here since we're doing {depends}...
1443 if (defined($pkgdata{$pkg}{provides})) {
1444 $pkgdata{$pkg}{provides} =~ s/^, //;
1445 $pkgdata{$pkg}{provides} = expandmacros($pkgdata{$pkg}{provides},'gp');
1446 }
1447 if (defined($pkgdata{$pkg}{conflicts})) {
1448 $pkgdata{$pkg}{conflicts} =~ s/^, //;
1449 $pkgdata{$pkg}{conflicts} = expandmacros($pkgdata{$pkg}{conflicts},'gp');
1450 }
1451
1452# These are Debian-specific!
1453 if (defined($pkgdata{$pkg}{recommends})) {
1454 $pkgdata{$pkg}{recommends} =~ s/^, //;
1455 $pkgdata{$pkg}{recommends} = expandmacros($pkgdata{$pkg}{recommends},'gp');
1456 }
1457 if (defined($pkgdata{$pkg}{suggests})) {
1458 $pkgdata{$pkg}{suggests} =~ s/^, //;
1459 $pkgdata{$pkg}{suggests} = expandmacros($pkgdata{$pkg}{suggests},'gp');
1460 }
1461 if (defined($pkgdata{$pkg}{enhances})) {
1462 $pkgdata{$pkg}{enhances} =~ s/^, //;
1463 $pkgdata{$pkg}{enhances} = expandmacros($pkgdata{$pkg}{enhances},'gp');
1464 }
1465 if (defined($pkgdata{$pkg}{replaces})) {
1466 $pkgdata{$pkg}{replaces} =~ s/^, //;
1467 $pkgdata{$pkg}{replaces} = expandmacros($pkgdata{$pkg}{replaces},'gp');
1468 }
1469
1470 # Gotta do this next, otherwise the control file has nowhere to go. >:(
1471 mkdir "$specglobals{buildroot}/$pkg/DEBIAN";
1472
1473 # Hack the filename for the package into a Debian-tool-compatible format. GRRRRRR!!!!!
1474 # Have I mentioned I hate Debian Policy?
1475 $pkgdata{$pkg}{name} =~ tr/_/-/;
1476
1477 # create script filename
1478 my $debscriptfile = "$tmpdir/deb-tmp.pkg.".int(rand(99998)+1);
1479 sysopen(DEBSCRIPT, $debscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
1480 or die "Can't open/create package-creation script file $debscriptfile: $!\n";
1481 print DEBSCRIPT $scriptletbase;
1482 print DEBSCRIPT "fakeroot -- dpkg-deb -b $specglobals{buildroot}/$pkg $topdir/DEBS/$pkgdata{$pkg}{arch}/".
1483 "$pkgdata{$pkg}{name}_".
1484 (defined($pkgdata{main}{epoch}) ? "$pkgdata{main}{epoch}:" : '').
1485 "$pkgdata{$pkg}{version}-$pkgdata{main}{release}_$pkgdata{$pkg}{arch}.deb\n";
1486 # %$&$%@#@@#%@@@ Debian and their horrible ugly package names. >:(
1487 close DEBSCRIPT;
1488
1489 $pkgdata{$pkg}{summary} = expandmacros($pkgdata{$pkg}{summary}, 'gp');
1490 my $control = "Package: $pkgdata{$pkg}{name}\n".
1491 "Version: ".(defined($pkgdata{main}{epoch}) ? "$pkgdata{main}{epoch}:" : '').
1492 "$pkgdata{$pkg}{version}-$pkgdata{main}{release}\n".
1493 "Section: ".($pkgdata{$pkg}{group} ? $pkgdata{$pkg}{group} : $pkgdata{main}{group})."\n".
1494 "Priority: optional\n".
1495 "Architecture: $pkgdata{$pkg}{arch}\n".
1496 "Maintainer: $pkgdata{main}{packager}\n".
1497 ( $pkgdata{$pkg}{requires} ne '' ? "Depends: $pkgdata{$pkg}{requires}\n" : '' ).
1498 ( defined($pkgdata{$pkg}{provides}) ? "Provides: $pkgdata{$pkg}{provides}\n" : '' ).
1499 ( defined($pkgdata{$pkg}{conflicts}) ? "Conflicts: $pkgdata{$pkg}{conflicts}\n" : '' ).
1500 ( defined($pkgdata{$pkg}{recommends}) ? "Recommends: $pkgdata{$pkg}{recommends}\n" : '' ).
1501 ( defined($pkgdata{$pkg}{suggests}) ? "Suggests: $pkgdata{$pkg}{suggests}\n" : '' ).
1502 ( defined($pkgdata{$pkg}{enhances}) ? "Enhances: $pkgdata{$pkg}{enhances}\n" : '' ).
1503 ( defined($pkgdata{$pkg}{replaces}) ? "Replaces: $pkgdata{$pkg}{replaces}\n" : '' ).
1504 "Description: $pkgdata{$pkg}{summary}\n";
1505 $pkgdata{$pkg}{desc} = expandmacros($pkgdata{$pkg}{desc}, 'gp');
1506 # Munge things so that Debian tools don't choke on errant blank lines
1507 $pkgdata{$pkg}{desc} =~ s/\s+$//g; # Trim trailing blanks
1508 $pkgdata{$pkg}{desc} =~ s/^ $/ ./mg; # Replace lines consisting of " \n" with " .\n"
1509 $control .= "$pkgdata{$pkg}{desc}\n";
1510
1511 open CONTROL, ">$specglobals{buildroot}/$pkg/DEBIAN/control";
1512 print CONTROL $control;
1513 close CONTROL;
1514
1515 # Iff there are conffiles (as specified in the %files list(s), add'em
1516 # in so dpkg-deb can tag them.
1517 if ($pkgdata{$pkg}{conffiles}) {
1518 open CONFLIST, ">$specglobals{buildroot}/$pkg/DEBIAN/conffiles";
1519 foreach my $conffile (@{$pkgdata{$pkg}{conflist}}) {
1520 $conffile = expandmacros($conffile, 'g');
1521 my @tmp = glob "$specglobals{buildroot}/$pkg/$conffile";
1522 foreach (@tmp) {
1523 s|$specglobals{buildroot}/$pkg/||g; # nrgl. gotta be a better way to do this...
1524 s/\s+/\n/g; # Not gonna support spaces in filenames. Ewww.
1525 print CONFLIST "$_\n";
1526 }
1527 }
1528 close CONFLIST;
1529 }
1530
1531 # found the point of scripts on subpackages.
1532 if ($pkgdata{$pkg}{'pre'}) {
1533 $pkgdata{$pkg}{'pre'} = expandmacros($pkgdata{$pkg}{'pre'},'gp');
1534 open PREINST, ">$specglobals{buildroot}/$pkg/DEBIAN/preinst";
1535 print PREINST "#!/bin/sh\nset -e\n\n";
1536 print PREINST $pkgdata{$pkg}{'pre'};
1537 close PREINST;
1538 chmod 0755, "$specglobals{buildroot}/$pkg/DEBIAN/preinst";
1539 }
1540 if ($pkgdata{$pkg}{'post'}) {
1541 $pkgdata{$pkg}{'post'} = expandmacros($pkgdata{$pkg}{'post'},'gp');
1542 open POSTINST, ">$specglobals{buildroot}/$pkg/DEBIAN/postinst";
1543 print POSTINST "#!/bin/sh\nset -e\n\n";
1544 print POSTINST $pkgdata{$pkg}{'post'};
1545 close POSTINST;
1546 chmod 0755, "$specglobals{buildroot}/$pkg/DEBIAN/postinst";
1547 }
1548 if ($pkgdata{$pkg}{'preun'}) {
1549 $pkgdata{$pkg}{'pre'} = expandmacros($pkgdata{$pkg}{'preun'},'gp');
1550 open PREUNINST, ">$specglobals{buildroot}/$pkg/DEBIAN/prerm";
1551 print PREUNINST "#!/bin/sh\nset -e\n\n";
1552 print PREUNINST $pkgdata{$pkg}{'preun'};
1553 close PREUNINST;
1554 chmod 0755, "$specglobals{buildroot}/$pkg/DEBIAN/prerm";
1555 }
1556 if ($pkgdata{$pkg}{'postun'}) {
1557 $pkgdata{$pkg}{'postun'} = expandmacros($pkgdata{$pkg}{'postun'},'gp');
1558 open POSTUNINST, ">$specglobals{buildroot}/$pkg/DEBIAN/postrm";
1559 print POSTUNINST "#!/bin/sh\nset -e\n\n";
1560 print POSTUNINST $pkgdata{$pkg}{'postun'};
1561 close POSTUNINST;
1562 chmod 0755, "$specglobals{buildroot}/$pkg/DEBIAN/postrm";
1563 }
1564
1565 # execute
1566 print "Calling package creation script $debscriptfile for $pkgdata{$pkg}{name}...\n";
1567 system("/bin/sh -e $debscriptfile") == 0
1568 or die "Can't exec: $!\n";
1569
1570 $finalmessages .= "Wrote binary package ".
1571 "$pkgdata{$pkg}{name}_".
1572 (defined($pkgdata{main}{epoch}) ? "$pkgdata{main}{epoch}:" : '').
1573 "$pkgdata{$pkg}{version}-$pkgdata{main}{release}_$pkgdata{$pkg}{arch}.deb".
1574 " in $topdir/DEBS/$pkgdata{$pkg}{arch}\n";
1575 # and clean up
1576 unlink $debscriptfile;
1577
1578 } # subpackage loop
1579
1580} # end binpackage()
1581
1582
1583## srcpackage()
1584# Builds a .src.deb source package. Note that Debian's idea of
1585# a "source package" is seriously flawed IMO, because you can't
1586# easily copy it as-is.
1587# Not quite identical to RPM, but Good Enough (TM).
1588sub srcpackage {
1589 # In case we were called with -bs.
1590 $pkgdata{main}{name} =~ tr/_/-/;
1591 my $pkgsrcname = "$pkgdata{main}{name}-".
1592 (defined($pkgdata{main}{epoch}) ? "$pkgdata{main}{epoch}:" : '').
1593 "$pkgdata{main}{version}-$pkgdata{main}{release}.sdeb";
1594
1595 my $paxcmd;
1596
1597 # We'll definitely need this later, and *may* need it sooner.
1598 (my $barespec = $specfile) =~ s|.+/([^/]+)$|$1|;
1599
1600 # Copy the specfile to the build tree, but only if it's not there already.
1601##buglet: need to deal with silly case where silly user has put the spec
1602# file in a subdir of %{_topdir}/SPECS. Ewww. Silly user!
1603 if (abs_path($specfile) !~ /^$topdir\/SPECS/) {
1604 $paxcmd .= "cp $specfile %{_topdir}/SPECS/; \n"
1605 }
1606
1607 # use pax -w [file] [file] ... >outfile.sdeb
1608 $paxcmd = "cd $topdir; pax -w ";
1609
1610 # some packages may not have a "main" source.
1611 $paxcmd .= "SOURCES/$pkgdata{main}{source} " if $pkgdata{main}{source};
1612
1613 # create file list: Source[nn], Patch[nn]
1614 foreach my $specbit (keys %{$pkgdata{main}} ) {
1615 next if $specbit eq 'source';
1616 $paxcmd .= "SOURCES/$pkgdata{main}{$specbit} " if $specbit =~ /^patch/;
1617##buglet: need to deal with case where patches are listed as URLs?
1618# or other extended pathnames? Silly !@$%^&!%%!%!! user!
1619 }
1620
1621 foreach my $source (keys %{$pkgdata{sources}}) {
1622 # Skip Source0, since it's also $pkgdata{main}{source}. Could arguably
1623 # just remove that instead, but I think that might backfire.
1624 next if $source eq '0';
1625 $paxcmd .= "SOURCES/$pkgdata{sources}{$source} ";
1626 }
1627
1628 # add the spec file, source package destination, and cd back where we came from.
1629 $paxcmd .= "SPECS/$barespec > $topdir/SDEBS/$pkgsrcname; cd -";
1630
1631 # In case of %-macros...
1632 $paxcmd = expandmacros($paxcmd,'gp');
1633
1634 system "$paxcmd";
1635 $finalmessages .= "Wrote source package $pkgsrcname in $topdir/SDEBS.\n";
1636} # end srcpackage()
1637
1638
1639## clean()
1640# Writes and executes the %clean script (mostly) built while reading the spec file.
1641sub clean {
1642 # Replace some things here just to make sure.
1643 $cleanscript = expandmacros($cleanscript,'gp');
1644
1645 # create script filename
1646 my $cleanscriptfile = "$tmpdir/deb-tmp.clean.".int(rand(99998)+1);
1647 sysopen(CLEANSCRIPT, $cleanscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
1648 or die $!;
1649 print CLEANSCRIPT $scriptletbase;
1650 print CLEANSCRIPT $cleanscript;
1651 close CLEANSCRIPT;
1652
1653 # execute
1654 print "Calling \%clean script $cleanscriptfile...\n";
1655 system("/bin/sh -e $cleanscriptfile") == 0
1656 or die "Can't exec: $!\n";
1657
1658 # and clean up
1659 unlink $cleanscriptfile;
1660} # end clean()
1661
1662
1663## checkbuildreq()
1664# Checks the build requirements (if any)
1665# Spits out a rude warning and returns a true-false error if any
1666# requirements are not met.
1667sub checkbuildreq {
1668 return 1 if $buildreq eq ''; # No use doing extra work.
1669
1670 # expand macros
1671 $buildreq = expandmacros($buildreq,'gp');
1672
1673 if ( ! -e "/usr/bin/dpkg-query" ) {
1674 print "**WARNING** dpkg-query not found. Can't check build-deps.\n".
1675 " Required for sucessful build:\n".$buildreq."\n".
1676 " Continuing anyway.\n";
1677 return 1;
1678 }
1679
1680 my $reqflag = 1; # unset iff a buildreq is missing
1681
1682 $buildreq =~ s/^, //; # Strip the leading comma and space
1683 my @reqlist = split /,\s+/, $buildreq;
1684
1685 my @missinglist;
1686 foreach my $req (@reqlist) {
1687 # from rpmbuild error message
1688 # Dependency tokens must begin with alpha-numeric, '_' or '/'
1689##fixme: check for suitable whitespace around $rel
1690 my ($pkg,$rel,$ver);
1691
1692 # We have two classes of requirements - versioned and unversioned.
1693 if ($req =~ /[><=]/) {
1694 # Pick up the details of versioned buildreqs
1695 ($pkg,$rel,$ver) = ($req =~ /([a-z0-9._-]+)\s+([><=]+)\s+([a-z0-9._-]+)/);
1696 } else {
1697 # And the unversioned ones.
1698 $pkg = $req;
1699 $rel = '>=';
1700 $ver = 0;
1701 }
1702
1703## Apparently a package that has been installed, then uninstalled, has a "known" dpkg status of
1704## "unknown ok not-installed" vs a package that has never been installed which returns nothing. O_o
1705## Virtual packages, of course, *also* show as "not installed" like this (WTF?)
1706## This causes real packages to be misdetected as installed "possible virtual packages" instead of "missing
1707## packages". I don't think there's really a solution until/unless Debian's tools properly register virtual
1708## packages as installed.
1709 my @pkglist = qx { dpkg-query --showformat '\${status}\t\${version}\n' -W $pkg };
1710 if (!$pkglist[0]) {
1711 print " * Missing build-dependency $pkg!\n $pkglist[0]";
1712 $reqflag = 0;
1713 push @missinglist, $pkg;
1714 } else {
1715# real package, installed
1716#kdeugau:~$ dpkg-query --showformat '${status}\t${version}\n' -W libc-client2007e-dev 2>&1
1717#install ok installed 8:2007f~dfsg-1
1718# virtual package, provided by ???
1719#kdeugau:~$ dpkg-query --showformat '${status}\t${version}\n' -W libc-client-dev 2>&1
1720#unknown ok not-installed
1721# real package or virtual package not installed or provided
1722#kdeugau:~$ dpkg-query --showformat '${status}\t${version}\n' -W libdb4.8-dbg 2>&1
1723#dpkg-query: no packages found matching libdb4.8-dbg
1724
1725 my ($reqstat,undef,undef,$reqver) = split /\s+/, $pkglist[0];
1726 if ($reqstat =~ /^unknown/) {
1727 # this seems to be a virtual package.
1728 print " * Warning: $pkg is probably installed but seems to be a virtual package.\n";
1729 } elsif ($reqstat =~ /^install/) {
1730 my ($resp) = qx { dpkg --compare-versions $reqver '$rel' $ver && echo "ok" };
1731 if ($resp !~ /^ok/) {
1732 $reqflag = 0;
1733 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n"
1734 }
1735 } else {
1736 # whatever state it's in, it's not completely installed, therefore it's missing.
1737 print " * Missing build-dependency $pkg!\n $pkglist[0]";
1738 $reqflag = 0;
1739 push @missinglist, $pkg;
1740 } # end not installed/installed check
1741 }
1742 } # end req loop
1743
1744 print "To install all missing dependencies, run 'apt-get install ".join(' ', @missinglist)."'.\n" unless $reqflag;
1745
1746 return $reqflag;
1747} # end checkbuildreq()
1748
1749
1750## getreqs()
1751# Find out which libraries/packages are required for any
1752# executables and libs in a given file tree.
1753# (Debian doesn't have soname-level deps; just package-level)
1754# Returns an empty string if the tree contains no binaries.
1755# Doesn't work well on shell scripts. but those *should* be
1756# fine anyway. (Yeah, right...)
1757sub getreqs() {
1758 my $pkgtree = $_[0];
1759
1760 print "Checking library requirements...\n";
1761 my @binlist = qx { find $pkgtree -type f -perm 755 };
1762
1763 if (scalar(@binlist) == 0) {
1764 return '';
1765 }
1766
1767 my @reqlist;
1768 foreach (@binlist) {
1769 push @reqlist, qx { LANG=C ldd $_ };
1770 }
1771
1772 # Get the list of libs provided by this package. Still doesn't
1773 # handle the case where the lib gets stuffed into a subpackage. :/
1774 my @intprovlist = qx { find $pkgtree -type f -name "*.so*" };
1775 my $provlist = '';
1776 foreach (@intprovlist) {
1777 s/$pkgtree//;
1778 $provlist .= "$_";
1779 }
1780
1781 my %reqs;
1782 my $reqlibs = '';
1783 my %reqliblist;
1784
1785 foreach (@reqlist) {
1786 next if /^$pkgtree/;
1787 next if /not a dynamic executable/;
1788 next if m|/lib(?:64)?/ld-linux|; # Hack! Hack! PTHBTT! (libc suxx0rz)
1789 next if /linux-gate.so/; # Kernel hackery for teh W1n!!1!1eleventy-one!1 (Don't ask. Feh.)
1790 next if /linux-vdso.so/; # More kernel hackery. Whee!
1791
1792 # Whee! more hackery to detect provided-here libs. Some apparently return from ldd as "not found".
1793 my ($a,$b) = split / => /;
1794 $a =~ s/\s*//g;
1795 if ($b =~ /not found/) {
1796 next if qx { find $specglobals{buildroot} -name "*$a" };
1797 }
1798
1799 my ($req) = (m|=\>\s+([a-zA-Z0-9._/+-]+)|); # dig out the actual library (so)name.
1800 # And feh, we need the *path*, since I've discovered a new edge case where
1801 # the same libnnn.1.2.3 *file*name is found across *several* lib dirs. >:(
1802
1803 # Ignore libs provided by this package. Note that we don't match
1804 # on word-boundary at the *end* of the lib we're looking for, as the
1805 # looked-for lib may not have the full soname version. (ie, it may
1806 # "just" point to one of the symlinks that get created somewhere.)
1807 next if $provlist =~ /\b$req/;
1808
1809 $reqlibs .= " $req";
1810 $reqliblist{$req} = 1;
1811 }
1812
1813 if (%reqliblist) {
1814 foreach my $rlib (keys %reqliblist) {
1815 my $libpkg = qx { dpkg -S $rlib };
1816 ($libpkg,undef) = split /:/, $libpkg;
1817 $reqs{$libpkg} = 1;
1818 }
1819 }
1820
1821 my $deplist = '';
1822 foreach (keys %reqs) {
1823 $deplist .= ", $_";
1824 }
1825
1826# For now, we're done. We're not going to meddle with versions yet.
1827# Among other things, it's messier than handling "simple" yes/no "do
1828# we have this lib?" deps. >:(
1829
1830 return $deplist;
1831} # end getreqs()
1832
1833
1834## install_sdeb()
1835# Extracts .sdeb contents to %_topdir as appropriate
1836sub install_sdeb {
1837 $srcpkg = abs_path($srcpkg);
1838
1839 my $paxcmd = "cd $topdir; pax -r <$srcpkg; cd -";
1840
1841 # In case of %-macros...
1842 $paxcmd = expandmacros($paxcmd,'gp');
1843
1844 system "$paxcmd";
1845 print "Extracted source package $srcpkg to $topdir.\n";
1846} # end install_sdeb()
1847
1848
1849## expandmacros()
1850# Expands all %{blah} macros in the passed string
1851# Split up a bit with some sections so we don't spend time trying to
1852# expand macros that are only used in a few specific places.
1853sub expandmacros {
1854 my $macrostring = shift;
1855 my $section = shift;
1856
1857 # To allow the FHS-ish %configure and %makeinstall to work The Right Way.
1858 # (Without clobbering the global $specglobals{buildroot}.)
1859 my $prefix = '';
1860
1861 if ($section =~ /c/) {
1862 # %configure macro
1863# Don't know what it's for, don't have a useful default replacement
1864# --program-prefix=%{_program_prefix} \
1865 $macrostring =~ s'%configure'./configure --host=$DEB_HOST_GNU_TYPE \
1866 --build=$DEB_BUILD_GNU_TYPE \
1867 --prefix=%{_prefix} \
1868 --exec-prefix=%{_exec_prefix} \
1869 --bindir=%{_bindir} \
1870 --sbindir=%{_sbindir} \
1871 --sysconfdir=%{_sysconfdir} \
1872 --datadir=%{_datadir} \
1873 --includedir=%{_includedir} \
1874 --libdir=%{_libdir} \
1875 --libexecdir=%{_libexecdir} \
1876 --localstatedir=%{_localstatedir} \
1877 --sharedstatedir=%{_sharedstatedir} \
1878 --mandir=%{_mandir} \
1879 --infodir=%{_infodir} ';
1880 } # done %configure
1881
1882 if ($section =~ /m/) {
1883 $macrostring =~ s'%{__make}'make ';
1884 } # done make
1885
1886# %makeinstall expands with recursive macros in rpm. whee.
1887 if ($section =~ /i/) {
1888 # This is where we need to mangle $prefix.
1889 $macrostring =~ s'%makeinstall'make \
1890 prefix=%{?buildroot:%{buildroot}}%{_prefix} \
1891 exec_prefix=%{?buildroot:%{buildroot}}%{_exec_prefix} \
1892 bindir=%{?buildroot:%{buildroot}}%{_bindir} \
1893 sbindir=%{?buildroot:%{buildroot}}%{_sbindir} \
1894 sysconfdir=%{?buildroot:%{buildroot}}%{_sysconfdir} \
1895 datadir=%{?buildroot:%{buildroot}}%{_datadir} \
1896 includedir=%{?buildroot:%{buildroot}}%{_includedir} \
1897 libdir=%{?buildroot:%{buildroot}}%{_libdir} \
1898 libexecdir=%{?buildroot:%{buildroot}}%{_libexecdir} \
1899 localstatedir=%{?buildroot:%{buildroot}}%{_localstatedir} \
1900 sharedstatedir=%{?buildroot:%{buildroot}}%{_sharedstatedir} \
1901 mandir=%{?buildroot:%{buildroot}}%{_mandir} \
1902 infodir=%{?buildroot:%{buildroot}}%{_infodir} \
1903 install ';
1904 $prefix = $specglobals{buildroot};
1905 } # done %install and/or %makeinstall
1906
1907 # Build data
1908 # Note that these are processed in reverse order to get the substitution order right
1909 if ($section =~ /b/) {
1910# $macrostring =~ s'%{fhs}'host=$DEB_HOST_GNU_TYPE \
1911# build=$DEB_BUILD_GNU_TYPE \
1912 $macrostring =~ s'%{fhs}'prefix=%{_prefix} \
1913 exec-prefix=%{_exec_prefix} \
1914 bindir=%{_bindir} \
1915 sbindir=%{_sbindir} \
1916 sysconfdir=%{_sysconfdir} \
1917 datadir=%{_datadir} \
1918 includedir=%{_includedir} \
1919 libdir=%{_libdir} \
1920 libexecdir=%{_libexecdir} \
1921 localstatedir=%{_localstatedir} \
1922 sharedstatedir=%{_sharedstatedir} \
1923 mandir=%{_mandir} \
1924 infodir=%{_infodir} \
1925';
1926
1927 # Note that the above regex terminates with the extra space
1928 # "Just In Case" of user additions, which will then get neatly
1929 # tagged on the end where they take precedence (supposedly)
1930 # over the "default" ones.
1931
1932 # Now we cascade the macros introduced above. >_<
1933 # Wot ot to go theah:
1934 $macrostring =~ s|%{_mandir}|%{_datadir}/man|g; #/usr/share/man
1935 $macrostring =~ s|%{_infodir}|%{_datadir}/info|g; #/usr/share/info
1936 $macrostring =~ s|%{_oldincludedir}|/usr/include|g; #/usr/include
1937 $macrostring =~ s|%{_includedir}|%{_prefix}/include|g; #/usr/include
1938 $macrostring =~ s|%{_libdir}|%{_exec_prefix}/%{_lib}|g; #/usr/lib
1939 $macrostring =~ s|%{_lib}|lib|g; #?
1940 $macrostring =~ s|%{_localstatedir}|/var|g; #/var
1941 $macrostring =~ s|%{_sharedstatedir}|%{_prefix}/com|g; #/usr/com WTF?
1942 $macrostring =~ s|%{_sysconfdir}|/etc|g; #/etc
1943 $macrostring =~ s|%{_datadir}|%{_prefix}/share|g; #/usr/share
1944 $macrostring =~ s|%{_libexecdir}|%{_exec_prefix}/libexec|g; #/usr/libexec
1945 $macrostring =~ s|%{_sbindir}|%{_exec_prefix}/sbin|g; #/usr/sbin
1946 $macrostring =~ s|%{_bindir}|%{_exec_prefix}/bin|g; #/usr/bin
1947 $macrostring =~ s|%{_exec_prefix}|%{_prefix}|g; #/usr
1948 $macrostring =~ s|%{_prefix}|/usr|g; #/usr
1949 } # done with config section
1950
1951 # Package data
1952 if ($section =~ /p/) {
1953 $macrostring =~ s/\%\{buildroot\}/$specglobals{buildroot}/gi;
1954 $macrostring =~ s/\%\{source0?}/$topdir\/SOURCES\/$pkgdata{main}{source}/gi;
1955 foreach my $source (keys %{$pkgdata{sources}}) {
1956 $macrostring =~ s/\%\{source$source\}/$topdir\/SOURCES\/$pkgdata{sources}{$source}/gi;
1957 }
1958 $macrostring =~ s/\%\{name\}/$pkgdata{main}{name}/gi;
1959 $macrostring =~ s/\%\{version\}/$pkgdata{main}{version}/gi;
1960 $macrostring =~ s/\%\{release\}/$pkgdata{main}{release}/gi;
1961 }
1962
1963 # Globals, and not-so-globals
1964 if ($section =~ /g/) {
1965
1966 $macrostring =~ s|%{_builddir}|%{_topdir}/BUILD|g;
1967# no longer a valid macro/tag
1968# $macrostring =~ s|%{buildsubdir}|$tarballdir|g;
1969 $macrostring =~ s|%{_topdir}|$topdir|g;
1970 $macrostring =~ s|%{_tmppath}|$tmpdir|g;
1971 $macrostring =~ s'%{_docdir}'%{_datadir}/doc'g;
1972
1973 # Standard FHS locations. More or less.
1974 $macrostring =~ s'%{_bindir}'/usr/bin'g;
1975 $macrostring =~ s'%{_sbindir}'/usr/sbin'g;
1976 $macrostring =~ s'%{_mandir}'%{_datadir}/man'g;
1977 $macrostring =~ s'%{_infodir}'%{_datadir}/info'g;
1978 $macrostring =~ s'%{_includedir}'/usr/include'g;
1979 $macrostring =~ s'%{_libdir}'/usr/lib'g;
1980 $macrostring =~ s'%{_sysconfdir}'/etc'g;
1981 $macrostring =~ s'%{_localstatedir}'/var'g;
1982 $macrostring =~ s'%{_prefix}'/usr'g;
1983
1984 # FHS-ish locations that aren't quite actually FHS-specified.
1985 $macrostring =~ s'%{_datadir}'/usr/share'g;
1986
1987 # special %define's. Handle the general case where we eval anything.
1988 # Even more general: %(...) is a spec-parse-time shell code wrapper.
1989 # Prime example:
1990 #%define perl_vendorlib %(eval "`perl -V:installvendorlib`"; echo $installvendorlib)
1991 if ($macrostring =~ /\%\((.+)\)/) {
1992 my $shellstr = $1;
1993 # Oy vey this gets silly for the perl bits. Executing a shell to
1994 # call Perl to get the vendorlib/sitelib/whatever "core" globals.
1995 # This can do more, but... eww.
1996 $shellstr = qx { /bin/sh -c '$shellstr' }; # Yay! ' characters apparently get properly exscapededed.
1997 $macrostring =~ s/\%\(.+\)/$shellstr/;
1998 }
1999
2000 # support for **some** %if constructs. Note that this breaks somewhat if
2001 # there's no value specified... but so does rpm.
2002
2003 my $tmpcount = 0; # runaway shutdown. Not really sure how else to avoid getting stuck.
2004 while ($macrostring =~ m/\%\{([?!]+)([a-z0-9_.-]+)(?:\:([a-z0-9_.\/\-\\]+)?)?\}/g) { #Whew....
2005 my $qex = $1;
2006 my $macro = $2;
2007 my $value = (defined($3) ? $3 : '');
2008
2009 my $neg = '1' if $qex =~ /\!/;
2010 if ($specglobals{$macro}) {
2011 $value = '' if $neg;
2012 } else {
2013 $value = '' if !$neg;
2014 }
2015 $macrostring =~ s/\%\{[?!]+[a-z0-9_.-]+(?:\:([a-z0-9_.\/\-\\]+)?)?\}/$value/;
2016
2017# not certain about this, but I don't want to run away. It *can* happen if planned carefully. :/
2018$tmpcount++;
2019# %makeinstall has 13 occurrences. D'Oh!
2020die "excessive recursive macro replacement; dying.\n" if $tmpcount > 14;
2021
2022 } # while()
2023
2024 # Misc expansions
2025 $macrostring =~ s|%{_arch}|$hostarch|g;
2026 $macrostring =~ s|%{optflags}|$optflags{$hostarch}|g;
2027 $macrostring =~ s|%{_vendor}|$specglobals{'_vendor'}|g;
2028
2029 # system programs. RPM uses a global config file for these; we'll just
2030 # ASS-U-ME and make life a little simpler.
2031 if ($macrostring =~ /\%\{\_\_([a-z0-9_-]+)\}/) {
2032# see /usr/lib/rpm/macros for most of the list. we should arguably just parse that file, but.. ow.
2033 $macrostring =~ s|%{__([a-z0-9_-]+)}|$1|g;
2034 }
2035
2036# should probably stick a "no runaway" flag in here... Just In Case...
2037 # %define's
2038# note to self: find out valid chars for %define name/label so we can parse them
2039 while (my ($key) = ($macrostring =~ /%{([a-z0-9_]+)}/i) ) {
2040# hrm. This needs thinking.
2041#die "A horrible death! \%{$key}, '$macrostring'\n" if !$specglobals{$key};
2042 $macrostring =~ s|%{$key}|$specglobals{$key}|g;
2043 # wanna limit this to "... if $specglobals{$key}", but need more magic
2044 }
2045
2046 # Perl @INC/...lib locations, and other related bits.
2047 $macrostring =~ s|%{perl_archlib}|$Config{installarchlib}|g;
2048 $macrostring =~ s|%{perl_sitelib}|$Config{installsitelib}|g;
2049 $macrostring =~ s|%{perl_sitearch}|$Config{installsitearch}|g;
2050 $macrostring =~ s|%{perl_vendorlib}|$Config{installvendorlib}|g;
2051 $macrostring =~ s|%{perl_vendorarch}|$Config{installvendorarch}|g;
2052
2053 } # done with globals section
2054
2055 return $macrostring;
2056} # end expandmacros()
2057
2058
2059
2060__END__
2061
2062
2063
2064=head1 NAME
2065
2066debbuild - Build Debian-compatible .deb packages from RPM .spec files
2067
2068=head1 SYNOPSIS
2069
2070 debbuild {-ba|-bb|-bp|-bc|-bi|-bl|-bs} [build-options] file.spec
2071
2072 debbuild {-ta|-tb|-tp|-tc|-ti|-tl|-ts} [build-options] file.tar.{gz|bz2|xz}
2073
2074 debbuild --rebuild file.{src.rpm|sdeb}
2075
2076 debbuild --showpkgs
2077
2078 debbuild -i foo.sdeb
2079
2080=head1 DESCRIPTION
2081
2082debbuild attempts to build Debian-friendly semi-native packages from RPM spec files,
2083RPM-friendly tarballs, and RPM source packages (.src.rpm files). It accepts I<most> of the
2084options rpmbuild does, and should be able to interpret most spec files usefully. Perl
2085modules should be handled via CPAN+dh-make-perl instead as it's simpler than even tweaking
2086a .spec template.
2087
2088As far as possible, the command-line options are identical to those from rpmbuild, although
2089several rpmbuild options are not supported:
2090
2091 --recompile
2092 --showrc
2093 --buildroot
2094 --clean
2095 --nobuild
2096 --rmsource
2097 --rmspec
2098 --sign
2099 --target
2100
2101Some of these could probably be trivially added. Feel free to send me a patch. ;)
2102
2103Complex spec files will most likely not work well, if at all. Rewrite them from scratch -
2104you'll have to make heavy modifications anyway.
2105
2106If you see something you don't like, mail me. Send a patch if you feel inspired. I don't
2107promise I'll do anything other than say "Yup, that's broken" or "Got your message".
2108
2109The source package container I invented for debbuild, the .sdeb file, can be "installed"
2110with debbuild -i exactly the same way as a .src.rpm can be installed with "rpm -i". Both
2111will unpack the file and place the source(s) and .spec file in the appropriate places in
2112%_topdir/SOURCES and %_topdir/SPECS respectively.
2113
2114=head1 ASSUMPTIONS
2115
2116As with rpmbuild, debbuild makes some assumptions about your system.
2117
2118=over 4
2119
2120=item *
2121
2122Either you have rights to do as you please under /usr/src/debian, or you have created a file
2123~/.debmacros containing a suitable %_topdir definition.
2124
2125Both rpmbuild and debbuild require the directories %_topdir/{BUILD,SOURCES,SPECS}. However,
2126where rpmbuild requires the %_topdir/{RPMS,SRPMS} directories, debbuild
2127requires %_topdir/{DEBS,SDEBS} instead. Create them in advance;
2128some subdirectories are created automatically as needed, but most are not.
2129
2130=item *
2131
2132/var/tmp must allow script execution - rpmbuild and debbuild both rely on creating and
2133executing shell scripts for much of their functionality. By default, debbuild also creates
2134install trees under /var/tmp - however this is (almost) entirely under the control of the
2135package's .spec file.
2136
2137=item *
2138
2139If you wish to --rebuild a .src.rpm, your %_topdir for both debbuild and rpmbuild must either
2140match, or be suitably symlinked one direction or another so that both programs are effectively
2141working in the same tree. (Or you could just manually wrestle files around your system.)
2142
2143You could symlink ~/.rpmmacros to ~/.debmacros (or vice versa) and save yourself some hassle
2144if you need to rebuild .src.rpm packages on a regular basis. Currently debbuild only uses the
2145%_topdir macro definition, although there are many more things that rpmbuild can use from
2146~/.rpmmacros.
2147
2148=back
2149
2150=head1 ERRATA
2151
2152debbuild deliberately does a few things differently from rpm.
2153
2154=head2 BuildArch or BuildArchitecture
2155
2156rpm takes the last BuildArch entry it finds in the .spec file, whatever it is, and runs with
2157that for all packages. Debian's repository system is fairly heavily designed around the
2158assumption that a single source package may generate small binary (executable) packages
2159for each arch, and large binary arch-all packages containing shared data.
2160
2161debbuild allows this by using the architecture specified by (in order of preference):
2162
2163=over 4
2164
2165=item * Host architecture
2166
2167=item * BuildArch specified in .spec file preamble
2168
2169=item * "Last specified" BuildArch for packages with several subpackages
2170
2171=item * BuildArch specified in the %package section for that subpackage
2172
2173=back
2174
2175=head2 Finding out what packages should be built (--showpkgs)
2176
2177rpmbuild does not include any convenient method I know of to list the packages a spec file
2178will produce. Since I needed this ability for another tool, I added it.
2179
2180It requires the .spec file for the package, and produces a list of full package filenames
2181(without path data) that would be generated by one of --rebuild, -ta, -tb, -ba, or -bb.
2182This includes the .sdeb source package.
2183
2184=head1 AUTHOR
2185
2186debbuild was written by Kris Deugau <kdeugau@deepnet.cx>. A version that approximates
2187current is available at http://www.deepnet.cx/debbuild/.
2188
2189=head1 BUGS
2190
2191Funky Things Happen if you forget a command-line option or two. I've been too lazy to bother
2192fixing this.
2193
2194Many macro expansions are unsupported or incompletely supported.
2195
2196The generated scriptlets don't quite match those from rpmbuild exactly. There are extra
2197environment variables and preprocessing that I haven't needed (yet).
2198
2199Dcumentation, such as it is, will likely remain perpetually out of date.
2200
2201%_topdir and the five "working" directories under %_topdir could arguably be created by
2202debbuild. However, rpmbuild doesn't create these directories either.
2203
2204debbuild's %setup -q is arguably less buggy than rpmbuild's implementation; with rpmbuild
2205it must appear before -a or -b at least to be effective. debbuild does not care.
2206
2207%setup -c, -a, and -b should arguably throw errors if specified together; otherwise the
2208results are almost certainly not what you want to happen.
2209
2210=head1 SEE ALSO
2211
2212rpm(8), rpmbuild(8), and pretty much any document describing how to write a .spec file.
2213
2214=cut
Note: See TracBrowser for help on using the repository browser.