source: trunk/debbuild@ 148

Last change on this file since 148 was 148, checked in by kdeugau, 15 years ago

/trunk

Add vendor macro to match _vendor (both seem to be legit in rpm)

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