source: trunk/debbuild@ 188

Last change on this file since 188 was 188, checked in by kdeugau, 9 years ago

/trunk

Fine-tune handling of %setup -c, -a, and -b to correctly handle a missed
edge case, as per a report from Andreas Scherer.

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