source: trunk/debbuild@ 102

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

/trunk

Add global "debdist" and "debver" definitions, derived from the
version of the base-files package. /etc/debian_version should have
similar info, but may not be as specific/accurate.

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