source: trunk/debbuild@ 104

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

/trunk

Expand %if logic support to basic multi-operator support

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