source: trunk/debbuild@ 77

Last change on this file since 77 was 77, checked in by kdeugau, 18 years ago

/trunk

Cosmetic hackery to get some messages spit out at the very end of any processing

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