source: trunk/debbuild@ 109

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

/trunk

Allow comments and blank lines to remain in scriptlets and other bits;
only skip them completely in the preamble.

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