source: trunk/debbuild@ 95

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

/trunk

Add support for basic %if constructs:

%if <expression>
<parse this bit of spec file>
%else
<parse this bit of spec file>
%endif

<expression> will usually be %{?macro:0}%{?!macro:1}, but the
"execute-shell-code" %(<shell code>) expansion can also be used if the
output is suitable. %else is optional. %if's should be nestable
very deeply - probably 231-1 at least.

Also includes a bugfix on the initialization of $tarballdir.

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