source: trunk/debbuild@ 92

Last change on this file since 92 was 92, checked in by kdeugau, 17 years ago

/trunk

Fix smallish bug in install_sdeb() - absolute-ify the source package
pathname we're given so we can actually find it.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 47.4 KB
Line 
1#!/usr/bin/perl
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: 2007-04-18 15:27:31 +0000 (Wed, 18 Apr 2007) $
9# SVN revision $Rev: 92 $
10# Last update by $Author: kdeugau $
11###
12# Copyright 2005-2007 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
32
33# regex debugger
34#use re "debug";
35
36# Program flow:
37# -> Parse/execute "system" config/macros (if any - should be rare)
38# -> Parse/execute "user" config/macros (if any - *my* requirement is %_topdir)
39# -> Parse command line for options, spec file/tarball/.src.deb (NB - also accept .src.rpm)
40
41sub expandmacros;
42
43# User's prefs for dirs, environment, etc,etc,etc.
44# config file ~/.debmacros
45# Default ordered search paths for config/macros:
46# /usr/lib/rpm/rpmrc /usr/lib/rpm/redhat/rpmrc /etc/rpmrc ~/.rpmrc
47# /usr/lib/rpm/macros /usr/lib/rpm/redhat/macros /etc/rpm/macros ~/.rpmmacros
48# **NOTE: May be possible to (ab)use bits of debhelper
49
50# Build tree
51# default is /usr/src/debian/{BUILD,SOURCES,SPECS,DEBS,SDEBS}
52
53# Globals
54my $finalmessages = ''; # A place to stuff messages that I want printed at the *very* end of any processing.
55my $specfile;
56my $tarball;
57my $srcpkg;
58my $cmdbuildroot;
59my $tarballdir; # This should really be initialized, but the coding makes it, um, ugly.
60my %specglobals; # For %define's in specfile, among other things.
61
62# Initialized globals
63my $verbosity = 0;
64my %cmdopts = (type => '',
65 stage => 'a',
66 short => 'n'
67 );
68my $topdir = "/usr/src/debian";
69my $buildroot = "%{_tmppath}/%{name}-%{version}-%{release}.root".int(rand(99998)+1);
70
71# "Constants"
72my %targets = ('p' => 'Prep',
73 'c' => 'Compile',
74 'i' => 'Install',
75 'l' => 'Verify %files',
76 'a' => 'Build binary and source',
77 'b' => 'Build binary',
78 's' => 'Build source'
79 );
80# Ah, the joys of multiple architectures. :( Feh.
81# As copied from rpm
82my %optflags = ( 'i386' => '-O2 -g -march=i386 -mcpu=i686',
83 'amd64' => '-O2 -g'
84 );
85my $hostarch; # we set this later...
86my $scriptletbase =
87q(#!/bin/sh
88
89 RPM_SOURCE_DIR="%{_topdir}/SOURCES"
90 RPM_BUILD_DIR="%{_topdir}/BUILD"
91 RPM_OPT_FLAGS="%{optflags}"
92 RPM_ARCH="%{_arch}"
93 RPM_OS="linux"
94 export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS
95 RPM_DOC_DIR="/usr/share/doc"
96 export RPM_DOC_DIR
97 RPM_PACKAGE_NAME="%{name}"
98 RPM_PACKAGE_VERSION="%{version}"
99 RPM_PACKAGE_RELEASE="%{release}"
100 export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE
101 RPM_BUILD_ROOT="%{buildroot}"
102 export RPM_BUILD_ROOT
103);
104foreach (`dpkg-architecture`) {
105 s/=(.+)/="$1"/;
106 $scriptletbase .= " $_";
107 ($hostarch) = (/^DEB_HOST_ARCH="(.+)"$/) if /DEB_HOST_ARCH=/;
108}
109$scriptletbase .=
110q(
111 set -x
112 umask 022
113 cd %{_topdir}/BUILD
114);
115
116# Package data
117# This is the form of $pkgdata{pkgname}{meta}
118# meta includes Summary, Name, Version, Release, Group, Copyright,
119# Source, URL, Packager, BuildRoot, Description, BuildReq(uires),
120# Requires, Provides
121# 10/31/2005 Maybe this should be flatter? -kgd
122my %pkgdata;
123my @pkglist = ('main'); #sigh
124# Files listing. Embedding this in %pkgdata would be, um, messy.
125my %filelist;
126my $buildreq = '';
127
128# Scriptlets
129my $prepscript = '';
130my $buildscript = '';
131# %install doesn't need the full treatment from %clean; just an empty place to install to.
132# NB - rpm doesn't do this; is it really necessary?
133my $installscript = '[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT'."\n";
134my $cleanscript = '';
135
136die "Not enough arguments\n" if #$argv == 0;
137
138# Snag some environment data
139my $tmpdir;
140if (defined $ENV{TMP} && $ENV{TMP} =~ /^(\/var)?\/tmp$/) {
141 $tmpdir = $ENV{TMP};
142} else {
143 $tmpdir = "/var/tmp";
144}
145
146##main
147
148load_userconfig();
149parse_cmd();
150
151if ($cmdopts{install}) {
152 install_sdeb();
153 exit 0;
154}
155
156# output stage of --showpkgs
157if ($cmdopts{type} eq 'd') {
158 parse_spec();
159 foreach my $pkg (@pkglist) {
160 $pkgdata{$pkg}{name} =~ tr/_/-/;
161
162 my $pkgfullname = "$pkgdata{$pkg}{name}_$pkgdata{$pkg}{version}-$pkgdata{main}{release}_$pkgdata{$pkg}{arch}.deb";
163
164 print "$pkgfullname\n" if $filelist{$pkg};
165
166 }
167 # Source package
168 print "$pkgdata{main}{name}-$pkgdata{main}{version}-$pkgdata{main}{release}.sdeb\n";
169 exit 0;
170}
171
172# Stick --rebuild handling in here - basically install_sdeb()
173# followed by tweaking options to run with -ba
174if ($cmdopts{type} eq 's') {
175 if ($srcpkg =~ /\.src\.rpm$/) {
176 my @srclist = qx { rpm -qlp $srcpkg };
177 foreach (@srclist) {
178 chomp;
179 $specfile = "$topdir/SPECS/$_" if /\.spec$/;
180 }
181 qx { rpm -i $srcpkg };
182 } else {
183 install_sdeb();
184 my @srclist = qx { pax < $srcpkg };
185 foreach (@srclist) {
186 chomp;
187 $specfile = "$topdir/$_" if /SPECS/;
188 }
189 }
190 $cmdopts{type} = 'b';
191 $cmdopts{stage} = 'a';
192}
193
194if ($cmdopts{type} eq 'b') {
195 # Need to read the spec file to find the tarball. Note that
196 # this also generates most of the shell script required.
197 parse_spec();
198 die "Can't build $pkgdata{main}{name}: build requirements not met.\n"
199 if !checkbuildreq();
200}
201
202if ($cmdopts{type} eq 't') {
203 # Need to unpack the tarball to find the spec file. Sort of the inverse of -b above.
204 # zcat $tarball |tar -t |grep .spec
205 # collect some info about the tarball
206 $specfile = "$topdir/BUILD/". qx { zcat $tarball |tar -t |grep .spec\$ };
207 chomp $specfile;
208 my ($fileonly, $dirname) = ($tarball =~ /(([a-zA-Z0-9._-]+)\.tar\.(?:gz|bz2))$/);
209
210 $tarball = abs_path($tarball);
211 my $unpackcmd = "cd $topdir/BUILD; tar -".
212 ( $tarball =~ /\.tar\.gz$/ ? "z" : "" ).
213 ( $tarball =~ /\.tar\.bz2$/ ? "j" : "" ). "xf $tarball";
214 system "$unpackcmd";
215 system "cp $tarball $topdir/SOURCES/$fileonly";
216 system "cp $specfile $topdir/SPECS/";
217 parse_spec();
218 die "Can't build $pkgdata{main}{name}: build requirements not met.\n"
219 if !checkbuildreq();
220}
221
222# -> srcpkg if -.s
223if ($cmdopts{stage} eq 's') {
224 srcpackage();
225 exit 0;
226}
227
228# Hokay. Need to:
229# -> prep if -.p OR (-.[cilabs] AND !--short-circuit)
230if ($cmdopts{stage} eq 'p' || ($cmdopts{stage} =~ /[cilabs]/ && $cmdopts{short} ne 'y')) {
231 prep();
232}
233# -> build if -.c OR (-.[ilabs] AND !--short-circuit)
234if ($cmdopts{stage} eq 'c' || ($cmdopts{stage} =~ /[ilabs]/ && $cmdopts{short} ne 'y')) {
235 build();
236}
237# -> install if -.[ilabs]
238#if ($cmdopts{stage} eq 'i' || ($cmdopts{stage} =~ /[labs]/ && $cmdopts{short} ne 'y')) {
239if ($cmdopts{stage} =~ /[ilabs]/) {
240 install();
241#foreach my $pkg (@pkglist) {
242# print "files in $pkg:\n ".$filelist{$pkg}."\n";
243#}
244
245}
246# -> binpkg and srcpkg if -.a
247if ($cmdopts{stage} eq 'a') {
248 binpackage();
249 srcpackage();
250}
251# -> binpkg if -.b
252if ($cmdopts{stage} eq 'b') {
253 binpackage();
254}
255
256# Spit out any closing remarks
257print $finalmessages;
258
259# Just in case.
260exit 0;
261
262
263## load_userconfig()
264# Loads user configuration (if any)
265# Currently only handles .debmacros
266# Needs to handle "other files"
267sub load_userconfig {
268 my (undef,undef,undef,undef,undef,undef,undef,$homedir,undef) = getpwuid($<);
269 if (-e "$homedir/.debmacros") {
270 open USERMACROS,"<$homedir/.debmacros";
271 while (<USERMACROS>) {
272 # And we also only handle a few macros at the moment.
273 if (/^\%_topdir/) {
274 my (undef,$tmp) = split /\s+/, $_;
275 $topdir = $tmp;
276 }
277 }
278 }
279} # end load_userconfig()
280
281
282## parse_cmd()
283# Parses command line into global hash %cmdopts, other globals
284# Options based on rpmbuild's options
285sub parse_cmd {
286 # Don't feel like coding my own option parser...
287 #use Getopt::Long;
288 # ... but I may have to: (OTOH, rpm uses popt, so maybe we can too.)
289 #use Getopt::Popt qw(:all);
290 # Or not. >:( Stupid Debian lack of findable Perl module names in packages.
291
292 # Stuff it.
293 my $prevopt = '';
294 foreach (@ARGV) {
295 chomp;
296
297 # Is it an option?
298 if (/^-/) {
299
300 # Is it a long option?
301 if (/^--/) {
302 if (/^--short-circuit/) {
303 $cmdopts{short} = 'y';
304 } elsif (/^--rebuild/) {
305 $cmdopts{type} = 's';
306 } elsif (/^--showpkgs/) {
307 $cmdopts{type} = 'd'; # d for 'diagnostic' or 'debug' or 'dump'
308 } else {
309 print "Long opt $_\n";
310 }
311 } else {
312 # Not a long option
313 if (/^-[bt]/) {
314 if ($cmdopts{stage} eq 's') {
315 # Mutually exclusive options.
316 die "Can't use $_ with --rebuild\n";
317 } else {
318 # Capture the type (from "bare" files or tarball) and the stage (prep, build, etc)
319 ($cmdopts{stage}) = (/^-[bt]([pcilabs])/);
320 ($cmdopts{type}) = (/^-([bt])[pcilabs]/);
321 }
322 } elsif (/^-v/) {
323 # bump verbosity. Not sure what I'll actually do here...
324 } elsif (/^-i/) {
325 $cmdopts{install} = 1;
326 $prevopt = '-i';
327 } else {
328 die "Bad option $_\n";
329 }
330 }
331
332 } else { # Not an option argument
333
334 # --buildroot is the only option that takes an argument
335 # Therefore, any *other* bare arguments are the spec file,
336 # tarball, or source package we're operating on - depending
337 # on which one we meet.
338 if ($prevopt eq '--buildroot') {
339 $cmdbuildroot = $_;
340 } elsif ($prevopt eq '-i') {
341 $srcpkg = $_;
342 } else {
343 if ($cmdopts{type} eq 's') {
344 # Source package
345 if (!/(sdeb|\.src\.rpm)$/) {
346 die "Can't --rebuild with $_\n";
347 }
348 $srcpkg = $_;
349 } elsif ($cmdopts{type} eq 'b' || $cmdopts{type} eq 'd') {
350 # Spec file
351 $specfile = $_;
352 } else {
353 # Tarball build. Need to extract tarball to find spec file. Whee.
354 $tarball = $_;
355 }
356 }
357 }
358 $prevopt = $_;
359 } # foreach @ARGV
360
361 # Some cross-checks. rpmbuild limits --short-circuit to just
362 # the "compile" and "install" targets - with good reason IMO.
363 # Note that --short-circuit with -.p is not really an error, just redundant.
364 # NB - this is NOT fatal, just ignored!
365 if ($cmdopts{short} eq 'y' && $cmdopts{stage} =~ /[labs]/) {
366 warn "Can't use --short-circuit for $targets{$cmdopts{stage}} stage. Ignoring.\n";
367 $cmdopts{short} = 'n';
368 }
369
370 # Valid options, with example arguments (if any):
371# Build from .spec file; mutually exclusive:
372 # -bp
373 # -bc
374 # -bi
375 # -bl
376 # -ba
377 # -bb
378 # -bs
379# Build from tarball; mutually exclusive:
380 # -tp
381 # -tc
382 # -ti
383 # -ta
384 # -tb
385 # -ts
386# Build from .src.(deb|rpm)
387 # --rebuild
388 # --recompile
389
390# General options
391 # --buildroot=DIRECTORY
392 # --clean
393 # --nobuild
394 # --nodeps
395 # --nodirtokens
396 # --rmsource
397 # --rmspec
398 # --short-circuit
399 # --target=CPU-VENDOR-OS
400
401 #my $popt = new Getopt::Popt(argv => \@ARGV, options => \@optionsTable);
402
403} # end parse_cmd()
404
405
406## parse_spec()
407# Parse the .spec file.
408sub parse_spec {
409 open SPECFILE,"<$specfile" or die "specfile ($specfile) barfed: $!";
410
411 my $buildarch = $hostarch;
412 $pkgdata{main}{arch} = $hostarch;
413
414LINE: while (<SPECFILE>) {
415 next if /^#/; # Ignore comments...
416 next if /^\s+$/; # ... and blank lines.
417
418 if (/^\%/) {
419 # A macro that needs further processing.
420
421 if (my ($key, $def) = (/^\%define\s+([^\s]+)\s+(.+)$/) ) {
422 $specglobals{$key} = expandmacros($def,'g');
423 }
424
425 if (/^\%description(?:\s+(?:-n\s+)?(.+))?/) {
426 my $subname = "main";
427 if ($1) {
428 my $tmp = expandmacros("$1", 'g');
429 if (/-n/) { $subname = $tmp; } else { $subname = "$pkgdata{main}{name}-$tmp"; }
430 }
431 while (<SPECFILE>) {
432 next if /^#/; # Messy. Should be possible to do better. :/
433 redo LINE if /^\%/;
434 $pkgdata{$subname}{desc} .= " $_";
435 }
436 }
437 if (/^\%package\s+(?:-n\s+)?(.+)/) {
438 # gotta expand %defines here. Whee.
439 my $subname = expandmacros("$1", 'g');
440 if (! /-n/) { $subname = "$pkgdata{main}{name}-$1"; }
441 push @pkglist, $subname;
442 $pkgdata{$subname}{name} = $subname;
443 $pkgdata{$subname}{version} = $pkgdata{main}{version};
444 # Build "same arch as previous package found" by default. Where rpm just picks the
445 # *very* last one, we want to allow arch<native>+arch-all
446 # (eg, Apache is i386, but apache-manual is all)
447 $pkgdata{$subname}{arch} = $buildarch; # Since it's likely subpackages will NOT have a BuildArch line...
448 while (<SPECFILE>) {
449 redo LINE if /^\%/;
450 if (my ($dname,$dvalue) = (/^(Summary|Group|Version|Requires|Provides|BuildArch(?:itecture)?):\s+(.+)$/i)) {
451 $dname =~ tr/[A-Z]/[a-z]/;
452 if ($dname =~ /^BuildArch/i) {
453 $dvalue =~ s/^noarch/all/ig;
454 $buildarch = $dvalue; # Emulate rpm's behaviour to a degree
455 $dname = 'arch';
456 }
457 $pkgdata{$subname}{$dname} = expandmacros($dvalue, 'gp');
458 }
459 }
460 }
461
462 if (/^\%prep/) {
463 # %prep section. May have %setup macro; may include %patch tags,
464 # may be just a bare shell script.
465
466 # This really should be local-ish, but we need just the filename for the source
467 $pkgdata{main}{source} =~ s|.+/([^/]+)$|$1|;
468
469 # Replace some core macros
470 $pkgdata{main}{source} = expandmacros($pkgdata{main}{source},'gp');
471
472PREPSCRIPT: while (<SPECFILE>) {
473 if (/^\%setup/) {
474 # Parse out the %setup macro. Note that we aren't supporting
475 # many of RPM's %setup features.
476 $prepscript .= "cd $topdir/BUILD\n";
477 if ( /\s+-n\s+([^\s]+)\s+/ ) {
478 $tarballdir = $1;
479 } else {
480 $tarballdir = "$pkgdata{main}{name}-$pkgdata{main}{version}";
481 }
482 $tarballdir = expandmacros($tarballdir,'gp');
483 $prepscript .= "rm -rf $tarballdir\n";
484 if (/\s+-c\s+/) {
485 $prepscript .= "mkdir $tarballdir\ncd $tarballdir\n";
486 }
487 $prepscript .= "tar -".
488 ( $pkgdata{main}{source} =~ /\.tar\.gz$/ ? "z" : "" ).
489 ( $pkgdata{main}{source} =~ /\.tar\.bz2$/ ? "j" : "" ).
490 ( /\s+-q\s+/ ? '' : 'vv' )."xf ".
491 "$topdir/SOURCES/$pkgdata{main}{source}\n".
492 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n).
493 "cd $topdir/BUILD/$tarballdir\n".
494 qq([ `/usr/bin/id -u` = '0' ] && /bin/chown -Rhf root .\n).
495 qq([ `/usr/bin/id -u` = '0' ] && /bin/chgrp -Rhf root .\n).
496 qq(/bin/chmod -Rf a+rX,g-w,o-w .\n);
497 } elsif ( my ($patchnum,$patchopts) = (/^\%patch([^\s]+)(\s+.+)?$/) ) {
498 chomp $patchnum;
499 $prepscript .= qq(echo "Patch #$patchnum ($pkgdata{main}{"patch$patchnum"}):"\n).
500 "patch ";
501 # If there are options passed, use'em.
502 # Otherwise, catch a bare %patch and ASS-U-ME it's '-p0'-able.
503 # Will break on options that don't provide -pnn, but what the hell.
504 $prepscript .= $patchopts if $patchopts;
505 $prepscript .= "-p0" if !$patchopts;
506 $prepscript .= " -s <$topdir/SOURCES/".$pkgdata{main}{"patch$patchnum"}."\n";
507 } else {
508 last PREPSCRIPT if /^\%/;
509 $prepscript .= $_;
510 }
511 }
512 redo LINE;
513 }
514 if (/^\%build/) {
515 # %build. This is pretty much just a shell script. There
516 # *are* a few macros, but we're not going to deal with them yet.
517 $buildscript .= "cd $tarballdir\n";
518BUILDSCRIPT: while (<SPECFILE>) {
519 if (/^\%configure/) {
520 $buildscript .= expandmacros($_,'cgbp');
521 } elsif (/^\%\{__make\}/) {
522 $buildscript .= expandmacros($_,'mgbp');
523 } else {
524 last BUILDSCRIPT if /^\%[^{]/;
525 $buildscript .= $_;
526 }
527 }
528 redo LINE;
529 }
530 if (/^\%install/) {
531 $installscript .= "cd $tarballdir\n";
532INSTALLSCRIPT: while (<SPECFILE>) {
533 if (/^\%makeinstall/) {
534 $installscript .= expandmacros($_,'igbp');
535 } else {
536 last INSTALLSCRIPT if /^\%/;
537 $installscript .= $_;
538 }
539 }
540 redo LINE;
541 }
542 if (/^\%clean/) {
543 while (<SPECFILE>) {
544 redo LINE if /^\%/;
545 $cleanscript .= $_;
546 }
547 $cleanscript = expandmacros($cleanscript,'gp');
548 }
549
550 # pre/post (un)install scripts. Note that we expand macros later anyway, so we'll leave them unexpanded here.
551 if (/^\%(pre|post|preun|postun)\b(?:\s+(?:-n\s+)?(.+))?/i) {
552 my $scriptlet = lc $1;
553 my $pkgname = 'main';
554 if ($2) { # Magic to add entries to the right list of files
555 my $tmp = expandmacros("$2", 'g');
556 if (/-n/) { $pkgname = $tmp; } else { $pkgname = "$pkgdata{main}{name}-$tmp"; }
557 }
558 while (<SPECFILE>) {
559 redo LINE if /^\%/;
560 $pkgdata{$pkgname}{$scriptlet} .= $_;
561 }
562 }
563 # done %pre/%post scripts
564
565 if (/^\%files(?:\s+(?:-n\s+)?(.+))?/) {
566 my $pkgname = 'main';
567 if ($1) { # Magic to add entries to the right list of files
568 my $tmp = expandmacros("$1", 'g');
569 if (/-n/) { $pkgname = $tmp; } else { $pkgname = "$pkgdata{main}{name}-$tmp"; }
570 }
571
572 # Set this now, so it can be flipped a bit later, and used much later.
573 #$pkgdata{$pkgname}{conffiles} = 0;
574
575 while (<SPECFILE>) {
576 chomp;
577 next if /^#/;
578 # need to update this to deal (properly) with %dir, %attr, etc
579 next if /^\%dir/;
580 next if /^\%defattr/;
581
582 # Debian dpkg doesn't speak "%docdir". Meh.
583 next if /^\%docdir/;
584
585##fixme
586# Note that big chunks of this section don't match rpm's behaviour; among other things,
587# rpm accepts more than one %-directive on one line for a file or set of files.
588 # make sure files get suitable permissions and so on
589 if (/^\%attr/) {
590 # We're going to collapse whitespace before processing. PTHBT.
591 # While this breaks pathnames with spaces, anyone expecting command-line
592 # tools with spaces to work (never mind work *properly* or *well*) under
593 # any *nix has their head so far up their ass they can see out their mouth.
594 my ($args,$filelist) = split /\)/;
595 $filelist{$pkgname} .= " $filelist";
596 $args =~ s/\s+//g;
597 $args =~ s/"//g; # don't think quotes are ever necessary, but they're *allowed*
598 my ($perms,$owner,$group) = ($args =~ /\(([\d-]+),([a-zA-Z0-9-]+),([a-zA-Z0-9-]+)/);
599# due to Debian's total lack of real permissions-processing in its actual package
600# handling component (dpkg-deb), this can't really be done "properly". We'll have
601# to add chown/chmod commands to the postinst instead. Feh.
602 $pkgdata{$pkgname}{'post'} .= "chown $owner $filelist\n" if $owner ne '-';
603 $pkgdata{$pkgname}{'post'} .= "chgrp $group $filelist\n" if $group ne '-';
604 $pkgdata{$pkgname}{'post'} .= "chmod $perms $filelist\n" if $perms ne '-';
605 next;
606 }
607
608 # %doc needs extra processing, because it can be a space-separated list.
609 if (/^\%doc/) {
610 s/^\%doc\s+//;
611 foreach (split()) {
612 $filelist{$pkgname} .= " %{_docdir}/$_";
613 }
614 next;
615 }
616
617 # Conffiles. Note that Debian and RH have similar, but not
618 # *quite* identical ideas of what constitutes a conffile. Nrgh.
619 if (/^\%config\s+(.+)$/) {
620 $pkgdata{$pkgname}{conffiles} = 1; # Flag it for later
621 my $tmp = $1; # Now we can mangleificationate it. And we probably need to. :/
622 $tmp = expandmacros($tmp, 'gp'); # Expand common macros
623 if ($tmp !~ /\s+/) {
624 # Simplest case, just a file. Whew.
625 push @{$pkgdata{$pkgname}{conflist}}, $tmp;
626 $filelist{$pkgname} .= " $tmp";
627 } else {
628 # Wot? Spaces? That means extra %-macros. Which, for the most part, can be ignored.
629 ($tmp) = ($tmp =~ /.+\s([^\s]+)/); # Strip everything before the last space
630 push @{$pkgdata{$pkgname}{conflist}}, $tmp;
631 $filelist{$pkgname} .= " $tmp";
632 }
633 next;
634 }
635
636 # and finally we can fall through %{_<FHS>}-prefixed locations...
637 if (/^\%\{_/) {
638 $filelist{$pkgname} .= " $_";
639 next;
640 }
641 # EW. Necessary to clear up %define expansions before we exit with redo.
642 $_ = expandmacros $_, 'g';
643
644 # ... unknown or "next section" % directives ...
645 redo LINE if /^\%/;
646
647 # ... and "normal" files
648 $filelist{$pkgname} .= " $_";
649 }
650 } # done %file section
651
652 if (/^\%changelog/) {
653 $pkgdata{main}{changelog} = '';
654 while (<SPECFILE>) {
655 redo LINE if /^\%/;
656 $pkgdata{main}{changelog} .= $_;
657 }
658 }
659
660 } else { # Data from the spec file "header"
661
662 if (/^summary:\s+(.+)/i) {
663 $pkgdata{main}{summary} = $1;
664 } elsif (/^name:\s+(.+)/i) {
665 $pkgdata{main}{name} = expandmacros($1,'g');
666 } elsif (/^version:\s+(.+)/i) {
667 $pkgdata{main}{version} = expandmacros($1,'g');
668 } elsif (/^release:\s+(.+)/i) {
669 $pkgdata{main}{release} = expandmacros($1,'g');
670 } elsif (/^group:\s+(.+)/i) {
671 $pkgdata{main}{group} = $1;
672 } elsif (/^copyright:\s+(.+)/i) {
673 $pkgdata{main}{copyright} = $1;
674 } elsif (/^url:\s+(.+)/i) {
675 $pkgdata{main}{url} = $1;
676 } elsif (/^packager:\s+(.+)/i) {
677 $pkgdata{main}{packager} = $1;
678 } elsif (/^buildroot:\s+(.+)/i) {
679 $buildroot = $1;
680 } elsif (/^source0?:\s+(.+)/i) {
681 $pkgdata{main}{source} = $1;
682 die "Unknown tarball format $1\n" if $1 !~ /\.tar\.(?:gz|bz2)$/;
683 } elsif (/^source([0-9]+):\s+(.+)/i) {
684 $pkgdata{sources}{$1} = $2;
685 } elsif (/^patch([^:]+):\s+(.+)$/i) {
686 my $patchname = "patch$1";
687 $pkgdata{main}{$patchname} = $2;
688 if ($pkgdata{main}{$patchname} =~ /\//) {
689 # URL-style patch. Rare but not unheard-of.
690 my @patchbits = split '/', $pkgdata{main}{$patchname};
691 $pkgdata{main}{$patchname} = $patchbits[$#patchbits];
692 }
693 chomp $pkgdata{main}{$patchname};
694 } elsif (/^buildarch(?:itecture)?:\s+(.+)/i) {
695 $pkgdata{main}{arch} = $1;
696 $pkgdata{main}{arch} =~ s/^noarch$/all/;
697 $buildarch = $pkgdata{main}{arch};
698 } elsif (/^buildreq(?:uires)?:\s+(.+)/i) {
699 $buildreq .= ", $1";
700 } elsif (/^requires:\s+(.+)/i) {
701 $pkgdata{main}{requires} .= ", ".expandmacros("$1", 'gp');
702 } elsif (/^provides:\s+(.+)/i) {
703 $pkgdata{main}{provides} .= ", $1";
704 } elsif (/^conflicts:\s+(.+)/i) {
705 $pkgdata{main}{conflicts} .= ", $1";
706 }
707#Name: suwrap
708#Version: 0.04
709#Release: 3
710#Group: Applications/System
711#Copyright: WebHart internal ONLY. :(
712#BuildArchitectures: i386
713#BuildRoot: /tmp/%{name}-%{version}
714#Url: http://virtual.webhart.net
715#Packager: Kris Deugau <kdeugau@deepnet.cx>
716#Source: ftp://virtual.webhart.net/%{name}-%{version}.tar.gz
717
718 }
719 }
720
721 # Parse and replace some more macros. More will be replaced even later.
722
723 # Expand macros as necessary.
724 $scriptletbase = expandmacros($scriptletbase,'gp');
725
726 $buildroot = $cmdbuildroot if $cmdbuildroot;
727 $buildroot = expandmacros($buildroot,'gp');
728
729 close SPECFILE;
730} # end parse_spec()
731
732
733## prep()
734# Writes and executes the %prep script (mostly) built while reading the spec file.
735sub prep {
736 # Replace some things here just to make sure.
737 $prepscript = expandmacros($prepscript,'gp');
738
739#print $prepscript; exit 0;
740
741 # create script filename
742 my $prepscriptfile = "$tmpdir/deb-tmp.prep.".int(rand(99998)+1);
743 sysopen(PREPSCRIPT, $prepscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
744 or die $!;
745 print PREPSCRIPT $scriptletbase;
746 print PREPSCRIPT $prepscript;
747 close PREPSCRIPT;
748
749 # execute
750 print "Calling \%prep script $prepscriptfile...\n";
751 system("/bin/sh -e $prepscriptfile") == 0
752 or die "Can't exec: $!\n";
753
754 # and clean up
755 unlink $prepscriptfile;
756} # end prep()
757
758
759## build()
760# Writes and executes the %build script (mostly) built while reading the spec file.
761sub build {
762 # Expand the macros
763 $buildscript = expandmacros($buildscript,'cgbp');
764
765 # create script filename
766 my $buildscriptfile = "$tmpdir/deb-tmp.build.".int(rand(99998)+1);
767 sysopen(BUILDSCRIPT, $buildscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
768 or die $!;
769 print BUILDSCRIPT $scriptletbase;
770 print BUILDSCRIPT $buildscript;
771 close BUILDSCRIPT;
772
773 # execute
774 print "Calling \%build script $buildscriptfile...\n";
775 system("/bin/sh -e $buildscriptfile") == 0
776 or die "Can't exec: $!\n";
777
778 # and clean up
779 unlink $buildscriptfile;
780} # end build()
781
782
783## install()
784# Writes and executes the %install script (mostly) built while reading the spec file.
785sub install {
786 # Expand the macros
787 $installscript = expandmacros($installscript,'igbp');
788
789 # create script filename
790 my $installscriptfile = "$tmpdir/deb-tmp.inst.".int(rand(99998)+1);
791 sysopen(INSTSCRIPT, $installscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
792 or die $!;
793 print INSTSCRIPT $scriptletbase;
794# print INSTSCRIPT $cleanscript; # Clean up our install target before installing into it.
795 print INSTSCRIPT $installscript;
796 close INSTSCRIPT;
797
798 # execute
799 print "Calling \%install script $installscriptfile...\n";
800 system("/bin/sh -e $installscriptfile") == 0
801 or die "Can't exec: $!\n";
802
803 # and clean up
804 unlink $installscriptfile;
805} # end install()
806
807
808## binpackage()
809# Creates the binary .deb package from the installed tree in $buildroot.
810# Writes and executes a shell script to do so.
811# Creates miscellaneous files required by dpkg-deb to actually build the package file.
812# Should handle simple subpackages
813sub binpackage {
814
815 foreach my $pkg (@pkglist) {
816
817 $pkgdata{$pkg}{arch} = $hostarch if !$pkgdata{$pkg}{arch}; # Just In Case.
818
819 # Make sure we have somewhere to write the .deb file
820 if (!-e "$topdir/DEBS/$pkgdata{$pkg}{arch}") {
821 mkdir "$topdir/DEBS/$pkgdata{$pkg}{arch}";
822 }
823
824 # Skip building a package if it doesn't actually have any files. NB: This
825 # differs slightly from rpm's behaviour where a package *will* be built -
826 # even without any files - if %files is specified anywhere. I can think
827 # of odd corner cases where that *may* be desireable.
828 next if (!$filelist{$pkg} or $filelist{$pkg} =~ /^\s*$/);
829
830 # Gotta do this first, otherwise we don't have a place to move files from %files
831 mkdir "$buildroot/$pkg";
832
833 # Eliminate any lingering % macros
834 $filelist{$pkg} = expandmacros $filelist{$pkg}, 'g';
835
836 my @pkgfilelist = split ' ', $filelist{$pkg};
837 foreach my $pkgfile (@pkgfilelist) {
838 $pkgfile = expandmacros($pkgfile, 'gp');
839 my ($fpath,$fname) = ($pkgfile =~ m|(.+?/?)?([^/]+)$|); # We don't need $fname now, but we might.
840 qx { mkdir -p $buildroot/$pkg$fpath }
841 if $fpath && $fpath ne '';
842 qx { mv $buildroot$pkgfile $buildroot/$pkg$fpath };
843 }
844
845 # Get the "Depends" (Requires) a la RPM. Ish. We strip the leading
846 # comma and space here (if needed) in case there were "Requires" specified
847 # in the spec file - those would precede these.
848 $pkgdata{$pkg}{requires} .= getreqs("$buildroot/$pkg");
849
850 # magic needed to properly version dependencies...
851 # only provided deps will really be included
852 $pkgdata{$pkg}{requires} =~ s/^, //; # Still have to do this here.
853 $pkgdata{$pkg}{requires} =~ s/\s+//g;
854 my @deps = split /,/, $pkgdata{$pkg}{requires};
855 my $tmp = '';
856 foreach my $dep (@deps) {
857 # Hack up the perl(Class::SubClass) deps into something dpkg can understand.
858 # May or may not be versioned.
859 # We do this first so the version rewriter can do its magic next.
860 if (my ($mod,$ver) = ($dep =~ /^perl\(([A-Za-z0-9\:\-]+)\)([><=]+.+)?/) ) {
861 $mod =~ s/^perl\(//;
862 $mod =~ s/\)$//;
863 $mod =~ s/::/-/g;
864 $mod =~ tr/A-Z/a-z/;
865 $mod = "lib$mod-perl";
866 $mod .= $ver if $ver;
867 $dep = $mod;
868 }
869 if (my ($name,$rel,$value) = ($dep =~ /^([a-zA-Z0-9._-]+)([><=]+)([a-zA-Z0-9._-]+)$/)) {
870 $tmp .= ", $name ($rel $value)";
871 } else {
872 $tmp .= ", $dep";
873 }
874 }
875 ($pkgdata{$pkg}{requires} = $tmp) =~ s/^, //;
876
877 # Do this here since we're doing {depends}...
878 if (defined($pkgdata{$pkg}{provides})) {
879 $pkgdata{$pkg}{provides} =~ s/^, //;
880 $pkgdata{$pkg}{provides} = expandmacros($pkgdata{$pkg}{provides},'gp');
881 }
882 if (defined($pkgdata{$pkg}{conflicts})) {
883 $pkgdata{$pkg}{conflicts} =~ s/^, //;
884 $pkgdata{$pkg}{conflicts} = expandmacros($pkgdata{$pkg}{conflicts},'gp');
885 }
886
887 # Gotta do this next, otherwise the control file has nowhere to go. >:(
888 mkdir "$buildroot/$pkg/DEBIAN";
889
890 # Hack the filename for the package into a Debian-tool-compatible format. GRRRRRR!!!!!
891 # Have I mentioned I hate Debian Policy?
892 $pkgdata{$pkg}{name} =~ tr/_/-/;
893
894 # create script filename
895 my $debscriptfile = "$tmpdir/deb-tmp.pkg.".int(rand(99998)+1);
896 sysopen(DEBSCRIPT, $debscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
897 or die $!;
898 print DEBSCRIPT $scriptletbase;
899 print DEBSCRIPT "fakeroot dpkg-deb -b $buildroot/$pkg $topdir/DEBS/$pkgdata{$pkg}{arch}/".
900 "$pkgdata{$pkg}{name}_$pkgdata{$pkg}{version}-$pkgdata{main}{release}_$pkgdata{$pkg}{arch}.deb\n";
901 # %$&$%@#@@#%@@@ Debian and their horrible ugly package names. >:(
902 close DEBSCRIPT;
903
904 my $control = "Package: $pkgdata{$pkg}{name}\n".
905 "Version: $pkgdata{$pkg}{version}-$pkgdata{main}{release}\n".
906 "Section: $pkgdata{$pkg}{group}\n".
907 "Priority: optional\n".
908 "Architecture: $pkgdata{$pkg}{arch}\n".
909 "Maintainer: $pkgdata{main}{packager}\n".
910 ( $pkgdata{$pkg}{requires} ne '' ? "Depends: $pkgdata{$pkg}{requires}\n" : '' ).
911 ( defined($pkgdata{$pkg}{provides}) ? "Provides: $pkgdata{$pkg}{provides}\n" : '' ).
912 ( defined($pkgdata{$pkg}{conflicts}) ? "Conflicts: $pkgdata{$pkg}{conflicts}\n" : '' ).
913 "Description: $pkgdata{$pkg}{summary}\n";
914 $control .= "$pkgdata{$pkg}{desc}\n";
915
916 open CONTROL, ">$buildroot/$pkg/DEBIAN/control";
917 print CONTROL $control;
918 close CONTROL;
919
920 # Iff there are conffiles (as specified in the %files list(s), add'em
921 # in so dpkg-deb can tag them.
922 if ($pkgdata{$pkg}{conffiles}) {
923 open CONFLIST, ">$buildroot/$pkg/DEBIAN/conffiles";
924 foreach my $conffile (@{$pkgdata{$pkg}{conflist}}) {
925 print CONFLIST "$conffile\n";
926 }
927 close CONFLIST;
928 }
929
930 # found the point of scripts on subpackages.
931 if ($pkgdata{$pkg}{'pre'}) {
932 $pkgdata{$pkg}{'pre'} = expandmacros($pkgdata{$pkg}{'pre'},'gp');
933 open PREINST, ">$buildroot/$pkg/DEBIAN/preinst";
934 print PREINST "#!/bin/sh\nset -e\n\n";
935 print PREINST $pkgdata{$pkg}{'pre'};
936 close PREINST;
937 `chmod 0755 $buildroot/$pkg/DEBIAN/preinst`;
938 }
939 if ($pkgdata{$pkg}{'post'}) {
940 $pkgdata{$pkg}{'post'} = expandmacros($pkgdata{$pkg}{'post'},'gp');
941 open PREINST, ">$buildroot/$pkg/DEBIAN/postinst";
942 print PREINST "#!/bin/sh\nset -e\n\n";
943 print PREINST $pkgdata{$pkg}{'post'};
944 close PREINST;
945 `chmod 0755 $buildroot/$pkg/DEBIAN/postinst`;
946 }
947 if ($pkgdata{$pkg}{'preun'}) {
948 $pkgdata{$pkg}{'pre'} = expandmacros($pkgdata{$pkg}{'preun'},'gp');
949 open PREINST, ">$buildroot/$pkg/DEBIAN/prerm";
950 print PREINST "#!/bin/sh\nset -e\n\n";
951 print PREINST $pkgdata{$pkg}{'preun'};
952 close PREINST;
953 `chmod 0755 $buildroot/$pkg/DEBIAN/prerm`;
954 }
955 if ($pkgdata{$pkg}{'postun'}) {
956 $pkgdata{$pkg}{'postun'} = expandmacros($pkgdata{$pkg}{'postun'},'gp');
957 open PREINST, ">$buildroot/$pkg/DEBIAN/postrm";
958 print PREINST "#!/bin/sh\nset -e\n\n";
959 print PREINST $pkgdata{$pkg}{'postun'};
960 close PREINST;
961 `chmod 0755 $buildroot/$pkg/DEBIAN/postrm`;
962 }
963
964 # execute
965 print "Calling package creation script $debscriptfile for $pkgdata{$pkg}{name}...\n";
966 system("/bin/sh -e $debscriptfile") == 0
967 or die "Can't exec: $!\n";
968
969 $finalmessages .= "Wrote binary package ".
970 "$pkgdata{$pkg}{name}_$pkgdata{$pkg}{version}-$pkgdata{main}{release}_$pkgdata{$pkg}{arch}.deb".
971 " in $topdir/DEBS/$pkgdata{$pkg}{arch}\n";
972 # and clean up
973 unlink $debscriptfile;
974
975 } # subpackage loop
976
977} # end binpackage()
978
979
980## srcpackage()
981# Builds a .src.deb source package. Note that Debian's idea of
982# a "source package" is seriously flawed IMO, because you can't
983# easily copy it as-is.
984# Not quite identical to RPM, but Good Enough (TM).
985sub srcpackage {
986 # In case we were called with -bs.
987 $pkgdata{main}{name} =~ tr/_/-/;
988 my $pkgsrcname = "$pkgdata{main}{name}-$pkgdata{main}{version}-$pkgdata{main}{release}.sdeb";
989
990 my $paxcmd;
991
992 # We'll definitely need this later, and *may* need it sooner.
993 (my $barespec = $specfile) =~ s|.+/([^/]+)$|$1|;
994
995 # Copy the specfile to the build tree, but only if it's not there already.
996##buglet: need to deal with silly case where silly user has put the spec
997# file in a subdir of %{_topdir}/SPECS. Ewww. Silly user!
998 if (abs_path($specfile) !~ /^$topdir\/SPECS/) {
999 $paxcmd .= "cp $specfile %{_topdir}/SPECS/; \n"
1000 }
1001
1002 # use pax -w [file] [file] ... >outfile.sdeb
1003 $paxcmd = "cd $topdir; pax -w ";
1004
1005# tweak source entry into usable form. Need it locally somewhere along the line.
1006 (my $pkgsrc = $pkgdata{main}{source}) =~ s|.+/([^/]+)$|$1|;
1007 $paxcmd .= "SOURCES/$pkgsrc ";
1008
1009 # create file list: Source[nn], Patch[nn]
1010 foreach my $specbit (keys %{$pkgdata{main}} ) {
1011 next if $specbit eq 'source';
1012 $paxcmd .= "SOURCES/$pkgdata{main}{$specbit} " if $specbit =~ /^patch/;
1013##buglet: need to deal with case where patches are listed as URLs?
1014# or other extended pathnames? Silly !@$%^&!%%!%!! user!
1015 }
1016
1017 foreach my $source (keys %{$pkgdata{sources}}) {
1018 $paxcmd .= "SOURCES/$pkgdata{sources}{$source} ";
1019 }
1020
1021 # add the spec file, source package destination, and cd back where we came from.
1022 $paxcmd .= "SPECS/$barespec > $topdir/SDEBS/$pkgsrcname; cd -";
1023
1024 # In case of %-macros...
1025 $paxcmd = expandmacros($paxcmd,'gp');
1026
1027 system "$paxcmd";
1028 $finalmessages .= "Wrote source package $pkgsrcname in $topdir/SDEBS.\n";
1029}
1030
1031
1032## checkbuildreq()
1033# Checks the build requirements (if any)
1034# Spits out a rude warning and returns a true-false error if any
1035# requirements are not met.
1036sub checkbuildreq {
1037 return 1 if $buildreq eq ''; # No use doing extra work.
1038
1039 if ( ! -e "/usr/bin/dpkg-query" ) {
1040 print "**WARNING** dpkg-query not found. Can't check build-deps.\n".
1041 " Required for sucessful build:\n".$buildreq."\n".
1042 " Continuing anyway.\n";
1043 return 1;
1044 }
1045
1046 my $reqflag = 1; # unset iff a buildreq is missing
1047
1048 $buildreq =~ s/^, //; # Strip the leading comma and space
1049 my @reqlist = split /,\s+/, $buildreq;
1050
1051 foreach my $req (@reqlist) {
1052 my ($pkg,$rel,$ver);
1053
1054 # We have two classes of requirements - versioned and unversioned.
1055 if ($req =~ /[><=]/) {
1056 # Pick up the details of versioned buildreqs
1057 ($pkg,$rel,$ver) = ($req =~ /([a-z0-9._-]+)\s+([><=]+)\s+([a-z0-9._-]+)/);
1058 } else {
1059 # And the unversioned ones.
1060 $pkg = $req;
1061 $rel = '>=';
1062 $ver = 0;
1063 }
1064
1065 my @pkglist = qx { dpkg-query --showformat '\${status}\t\${version}\n' -W $pkg };
1066# need to check if no lines returned - means a bad buildreq
1067 my ($reqstat,undef,undef,$reqver) = split /\s+/, $pkglist[0];
1068 if ($reqstat !~ /install/) {
1069 print " * Missing build-dependency $pkg!\n";
1070 $reqflag = 0;
1071 } else {
1072# gotta be a better way to do this... :/
1073 if ($rel eq '>=' && !($reqver ge $ver)) {
1074 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1075 $reqflag = 0;
1076 }
1077 if ($rel eq '>' && !($reqver gt $ver)) {
1078 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1079 $reqflag = 0;
1080 }
1081 if ($rel eq '<=' && !($reqver le $ver)) {
1082 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1083 $reqflag = 0;
1084 }
1085 if ($rel eq '<' && !($reqver lt $ver)) {
1086 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1087 $reqflag = 0;
1088 }
1089 if ($rel eq '=' && !($reqver eq $ver)) {
1090 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1091 $reqflag = 0;
1092 }
1093 } # end not installed/installed check
1094 } # end req loop
1095
1096 return $reqflag;
1097} # end checkbuildreq()
1098
1099
1100## getreqs()
1101# Find out which libraries/packages are required for any
1102# executables and libs in a given file tree.
1103# (Debian doesn't have soname-level deps; just package-level)
1104# Returns an empty string if the tree contains no binaries.
1105# Doesn't work well on shell scripts. but those *should* be
1106# fine anyway. (Yeah, right...)
1107sub getreqs() {
1108 my $pkgtree = $_[0];
1109
1110 print "Checking library requirements...\n";
1111 my @binlist = qx { find $pkgtree -type f -perm 755 };
1112
1113 if (scalar(@binlist) == 0) {
1114 return '';
1115 }
1116
1117 my @reqlist;
1118 foreach (@binlist) {
1119 push @reqlist, qx { ldd $_ };
1120 }
1121
1122 # Get the list of libs provided by this package. Still doesn't
1123 # handle the case where the lib gets stuffed into a subpackage. :/
1124 my @intprovlist = qx { find $pkgtree -type f -name "*.so*" };
1125 my $provlist = '';
1126 foreach (@intprovlist) {
1127 s/$pkgtree//;
1128 $provlist .= "$_";
1129 }
1130
1131 my %reqs;
1132 my $reqlibs = '';
1133
1134 foreach (@reqlist) {
1135 next if /^$pkgtree/;
1136 next if /not a dynamic executable/;
1137 next if m|/lib/ld-linux.so|; # Hack! Hack! PTHBTT! (libc suxx0rz)
1138
1139 my ($req) = (/^\s+([a-z0-9._-]+)/); # dig out the actual library (so)name
1140
1141 # Ignore libs provided by this package. Note that we don't match
1142 # on word-boundary at the *end* of the lib we're looking for, as the
1143 # looked-for lib may not have the full soname version. (ie, it may
1144 # "just" point to one of the symlinks that get created somewhere.)
1145 next if $provlist =~ /\b$req/;
1146
1147 $reqlibs .= " $req";
1148 }
1149
1150 if ($reqlibs ne '') {
1151 foreach (qx { dpkg -S $reqlibs }) {
1152 my ($libpkg,undef) = split /:\s+/;
1153 $reqs{$libpkg} = 1;
1154 }
1155 }
1156
1157 my $deplist = '';
1158 foreach (keys %reqs) {
1159 $deplist .= ", $_";
1160 }
1161
1162# For now, we're done. We're not going to meddle with versions yet.
1163# Among other things, it's messier than handling "simple" yes/no "do
1164# we have this lib?" deps. >:(
1165
1166 return $deplist;
1167} # end getreqs()
1168
1169
1170## install_sdeb()
1171# Extracts .sdeb contents to %_topdir as appropriate
1172sub install_sdeb {
1173 $srcpkg = abs_path($srcpkg);
1174
1175 my $paxcmd = "cd $topdir; pax -r <$srcpkg; cd -";
1176
1177 # In case of %-macros...
1178 $paxcmd = expandmacros($paxcmd,'gp');
1179
1180 system "$paxcmd";
1181 print "Extracted source package $srcpkg to $topdir.\n";
1182} # end install_sdeb()
1183
1184
1185## expandmacros()
1186# Expands all %{blah} macros in the passed string
1187# Split up a bit with some sections so we don't spend time trying to
1188# expand macros that are only used in a few specific places.
1189sub expandmacros {
1190 my $macrostring = shift;
1191 my $section = shift;
1192
1193 # To allow the FHS-ish %configure and %makeinstall to work The Right Way.
1194 # (Without clobbering the global $buildroot.)
1195 my $prefix = '';
1196
1197 if ($section =~ /c/) {
1198 # %configure macro
1199# Don't know what it's for, don't have a useful default replacement
1200# --program-prefix=%{_program_prefix} \
1201 $macrostring =~ s'%configure'./configure --host=$DEB_HOST_GNU_TYPE \
1202 --build=$DEB_BUILD_GNU_TYPE \
1203 --prefix=%{_prefix} \
1204 --exec-prefix=%{_exec_prefix} \
1205 --bindir=%{_bindir} \
1206 --sbindir=%{_sbindir} \
1207 --sysconfdir=%{_sysconfdir} \
1208 --datadir=%{_datadir} \
1209 --includedir=%{_includedir} \
1210 --libdir=%{_libdir} \
1211 --libexecdir=%{_libexecdir} \
1212 --localstatedir=%{_localstatedir} \
1213 --sharedstatedir=%{_sharedstatedir} \
1214 --mandir=%{_mandir} \
1215 --infodir=%{_infodir} ';
1216 } # done %configure
1217
1218 if ($section =~ /m/) {
1219 $macrostring =~ s'%{__make}'make ';
1220 } # done make
1221
1222 if ($section =~ /i/) {
1223 # This is where we need to mangle $prefix.
1224 $macrostring =~ s'%makeinstall'make %{fhs} install';
1225 $prefix = $buildroot;
1226 } # done %install and/or %makeinstall
1227
1228 # Build data
1229 # Note that these are processed in reverse order to get the substitution order right
1230 if ($section =~ /b/) {
1231# $macrostring =~ s'%{fhs}'host=$DEB_HOST_GNU_TYPE \
1232# build=$DEB_BUILD_GNU_TYPE \
1233 $macrostring =~ s'%{fhs}'prefix=%{_prefix} \
1234 exec-prefix=%{_exec_prefix} \
1235 bindir=%{_bindir} \
1236 sbindir=%{_sbindir} \
1237 sysconfdir=%{_sysconfdir} \
1238 datadir=%{_datadir} \
1239 includedir=%{_includedir} \
1240 libdir=%{_libdir} \
1241 libexecdir=%{_libexecdir} \
1242 localstatedir=%{_localstatedir} \
1243 sharedstatedir=%{_sharedstatedir} \
1244 mandir=%{_mandir} \
1245 infodir=%{_infodir} \
1246';
1247
1248 # Note that the above regex terminates with the extra space
1249 # "Just In Case" of user additions, which will then get neatly
1250 # tagged on the end where they take precedence (supposedly)
1251 # over the "default" ones.
1252
1253 # Now we cascade the macros introduced above. >_<
1254 # Wot ot to go theah:
1255 $macrostring =~ s|%{_mandir}|%{_datadir}/man|g; #/usr/share/man
1256 $macrostring =~ s|%{_infodir}|%{_datadir}/info|g; #/usr/share/info
1257 $macrostring =~ s|%{_oldincludedir}|/usr/include|g; #/usr/include
1258 $macrostring =~ s|%{_includedir}|%{_prefix}/include|g; #/usr/include
1259 $macrostring =~ s|%{_libdir}|%{_exec_prefix}/%{_lib}|g; #/usr/lib
1260 $macrostring =~ s|%{_lib}|lib|g; #?
1261 $macrostring =~ s|%{_localstatedir}|/var|g; #/var
1262 $macrostring =~ s|%{_sharedstatedir}|%{_prefix}/com|g; #/usr/com WTF?
1263 $macrostring =~ s|%{_sysconfdir}|/etc|g; #/etc
1264 $macrostring =~ s|%{_datadir}|%{_prefix}/share|g; #/usr/share
1265 $macrostring =~ s|%{_libexecdir}|%{_exec_prefix}/libexec|g; #/usr/libexec
1266 $macrostring =~ s|%{_sbindir}|%{_exec_prefix}/sbin|g; #/usr/sbin
1267 $macrostring =~ s|%{_bindir}|%{_exec_prefix}/bin|g; #/usr/bin
1268 $macrostring =~ s|%{_exec_prefix}|%{_prefix}|g; #/usr
1269 $macrostring =~ s|%{_prefix}|/usr|g; #/usr
1270 } # done with config section
1271
1272 # Package data
1273 if ($section =~ /p/) {
1274 $macrostring =~ s/\%\{buildroot\}/$buildroot/gi;
1275 foreach my $source (keys %{$pkgdata{sources}}) {
1276 $macrostring =~ s/\%\{source$source\}/$topdir\/SOURCES\/$pkgdata{sources}{$source}/gi;
1277 }
1278 $macrostring =~ s/\%\{name\}/$pkgdata{main}{name}/gi;
1279 $macrostring =~ s/\%\{version\}/$pkgdata{main}{version}/gi;
1280 $macrostring =~ s/\%\{release\}/$pkgdata{main}{release}/gi;
1281 }
1282
1283 # Globals, and not-so-globals
1284 if ($section =~ /g/) {
1285
1286 # special %define's. Handle the general case where we eval anything.
1287 # Prime example:
1288 #%define perl_vendorlib %(eval "`perl -V:installvendorlib`"; echo $installvendorlib)
1289 if ($macrostring =~ /^\%\(eval.+\)$/) {
1290 $macrostring =~ s/^\%\(//;
1291 $macrostring =~ s/\)$//;
1292 # Oy vey this gets silly for the perl bits. Executing a shell to
1293 # call Perl to get the vendorlib/sitelib/whatever "core" globals.
1294 # This can do more, but... eww.
1295 # Next line is non-optimal - what if $macrostring contains ' characters?
1296 $macrostring = qx { /bin/sh -c '$macrostring' };
1297 }
1298
1299 $macrostring =~ s|%{_builddir}|%{_topdir}/BUILD|g;
1300 $macrostring =~ s|%{_topdir}|$topdir|g;
1301 $macrostring =~ s|%{_tmppath}|$tmpdir|g;
1302 $macrostring =~ s'%{_docdir}'%{_datadir}/doc'g;
1303
1304 # Standard FHS locations. More or less.
1305 $macrostring =~ s'%{_bindir}'/usr/bin'g;
1306 $macrostring =~ s'%{_sbindir}'/usr/sbin'g;
1307 $macrostring =~ s'%{_mandir}'%{_datadir}/man'g;
1308 $macrostring =~ s'%{_includedir}'/usr/include'g;
1309 $macrostring =~ s'%{_libdir}'/usr/lib'g;
1310 $macrostring =~ s'%{_sysconfdir}'/etc'g;
1311 $macrostring =~ s'%{_localstatedir}'/var'g;
1312
1313 # FHS-ish locations that aren't quite actually FHS-specified.
1314 $macrostring =~ s'%{_datadir}'/usr/share'g;
1315
1316 # %define's
1317 foreach my $key (keys %specglobals) {
1318 $macrostring =~ s|%{$key}|$specglobals{$key}|g;
1319 }
1320
1321 # system programs. RPM uses a global config file for these; we'll just
1322 # ASS-U-ME and make life a little simpler.
1323 if ($macrostring =~ /\%\{\_\_([a-z0-9_-]+)\}/) {
1324 $macrostring =~ s|%{__([a-z0-9_-]+)}|$1|g;
1325 }
1326
1327 # Misc expansions
1328 $macrostring =~ s|%{_arch}|$hostarch|g;
1329 $macrostring =~ s|%{optflags}|$optflags{$hostarch}|g;
1330
1331 } # done with globals section
1332
1333 return $macrostring;
1334} # end expandmacros()
1335
1336
1337
1338__END__
1339
1340
1341
1342=head1 NAME
1343
1344debbuild - Build Debian-compatible packages from RPM spec files
1345
1346=head1 SYNOPSIS
1347
1348 debbuild {-ba|-bb|-bp|-bc|-bi|-bl|-bs} [build-options] file.spec
1349
1350 debbuild {-ta|-tb|-tp|-tc|-ti|-tl|-ts} [build-options] file.tar.{gz|bz2}
1351
1352 debbuild --rebuild file.{src.rpm|sdeb}
1353
1354 debbuild --showpkgs
1355
1356=head1 DESCRIPTION
1357
1358This script attempts to build Debian-friendly semi-native packages from RPM spec files,
1359RPM-friendly tarballs, and RPM source packages (.src.rpm files). It accepts I<most> of the
1360options rpmbuild does, and should be able to interpret most spec files usefully. Perl
1361modules should be handled via CPAN+dh-make-perl instead; Debian's conventions for such
1362things do not lend themselves to automated conversion.
1363
1364As far as possible, the command-line options are identical to those from rpmbuild, although
1365several rpmbuild options are not supported:
1366
1367 --recompile
1368 --showrc
1369 --buildroot
1370 --clean
1371 --nobuild
1372 --rmsource
1373 --rmspec
1374 --sign
1375 --target
1376
1377Some of these could probably be trivially added. Feel free to send me a patch. ;)
1378
1379Complex spec files will most likely not work well, if at all. Rewrite them from scratch -
1380you'll have to make heavy modifications anyway.
1381
1382If you see something you don't like, mail me. Send a patch if you feel inspired. I don't
1383promise I'll do anything other than say "Yup, that's broken" or "Got your message".
1384
1385=head1 ASSUMPTIONS
1386
1387As with rpmbuild, debbuild makes some assumptions about your system.
1388
1389=over 4
1390
1391=item *
1392
1393Either you have rights to do as you please under /usr/src/debian, or you have created a file
1394~/.debmacros containing a suitable %_topdir definition.
1395
1396Both rpmbuild and debbuild require the directories %_topdir/{BUILD,SOURCES,SPECS}. However,
1397where rpmbuild requires the %_topdir/{RPMS,SRPMS} directories, debbuild
1398requires %_topdir/{DEBS,SDEBS} instead. Create them in advance;
1399some subdirectories are created automatically as needed, but most are not.
1400
1401=item *
1402
1403/var/tmp must allow script execution - rpmbuild and debbuild both rely on creating and
1404executing shell scripts for much of their functionality. By default, debbuild also creates
1405install trees under /var/tmp - however this is (almost) entirely under the control of the
1406package's .spec file.
1407
1408=item *
1409
1410If you wish to --rebuild a .src.rpm, your %_topdir for both debbuild and rpmbuild must either
1411match, or be suitably symlinked one direction or another so that both programs are effectively
1412working in the same tree. (Or you could just manually wrestle files around your system.)
1413
1414You could symlink ~/.rpmmacros to ~/.debmacros (or vice versa) and save yourself some hassle
1415if you need to rebuild .src.rpm packages on a regular basis. Currently debbuild only uses the
1416%_topdir macro definition, although there are many more things that rpmbuild can use from
1417~/.rpmmacros.
1418
1419=back
1420
1421=head1 ERRATA
1422
1423debbuild deliberately does a few things differently from rpm.
1424
1425=head2 BuildArch or BuildArchitecture
1426
1427rpm takes the last BuildArch entry it finds in the .spec file, whatever it is, and runs with
1428that for all packages. Debian's repository system is fairly heavily designed around the
1429assumption that a single source package may generate small binary (executable) packages
1430for each arch, and large binary arch-all packages containing shared data.
1431
1432debbuild allows this by using the architecture specified by (in order of preference):
1433
1434=over 4
1435
1436=item * Host architecture
1437
1438=item * BuildArch specified in .spec file preamble
1439
1440=item * "Last specified" BuildArch for packages with several subpackages
1441
1442=item * BuildArch specified in the %package section for that subpackage
1443
1444=back
1445
1446=head2 Finding out what packages should be built (--showpkgs)
1447
1448rpmbuild does not include any convenient method I know of to list the packages a spec file
1449will produce. Since I needed this ability for another tool, I added it.
1450
1451It requires the .spec file for the package, and produces a list of full package filenames
1452(without path data) that would be generated by one of --rebuild, -ta, -tb, -ba, or -bb.
1453This includes the .sdeb source package.
1454
1455=head1 AUTHOR
1456
1457debbuild was written by Kris Deugau <kdeugau@deepnet.cx>. A version that approximates
1458current is available at http://www.deepnet.cx/debbuild/.
1459
1460=head1 BUGS
1461
1462Funky Things Happen if you forget a command-line option or two. I've been too lazy to bother
1463fixing this.
1464
1465Many macro expansions are unsupported or incompletely supported.
1466
1467The generated scriptlets don't quite match those from rpmbuild exactly. There are extra
1468environment variables and preprocessing that I haven't needed (yet).
1469
1470Dcumentation, such as it is, will likely remain perpetually out of date.
1471
1472%_topdir and the five "working" directories under %_topdir could arguably be created by
1473debbuild. However, rpmbuild doesn't create these directories either.
1474
1475=head1 SEE ALSO
1476
1477rpm(8), rpmbuild(8), and pretty much any document describing how to write a .spec file.
1478
1479=cut
Note: See TracBrowser for help on using the repository browser.