source: trunk/debbuild@ 151

Last change on this file since 151 was 151, checked in by kdeugau, 14 years ago

/trunk

Update Debian release version mappings
Tweak the Debian version autodetection
Add another kernel-innards "library" to the list to ignore

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