source: trunk/debbuild@ 103

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

/trunk

Major rewrite of parse_spec(); it should now handle %if constructs in
any location in the spec file. The code is somewhat more spread out
now as well.

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