source: trunk/debbuild@ 184

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

/trunk

Don't add "Source0" to the .sdeb twice; this surfaced with the changes
in r182.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 73.9 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-06-07 15:49:25 +0000 (Sun, 07 Jun 2015) $
9# SVN revision $Rev: 184 $
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.0"; #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 my @sbits = split /\s+/;
790 shift @sbits;
791 while (my $sopt = shift @sbits) {
792 if ($sopt eq '-n') {
793 $tarballdir = shift @sbits;
794 } elsif ($sopt eq '-a') {
795 # shift, next opt must be numeric (which sourcenn:)
796 $sopt = shift @sbits;
797 die "Argument for \%setup -a must be numeric at $specfile line $.\n" if $sopt !~ /^\d+$/;
798 $safter = $sopt;
799 $snum = $sopt;
800 $altsource = 1;
801 } elsif ($sopt eq '-b') {
802 # shift, next opt must be numeric (which sourcenn:)
803 $sopt = shift @sbits;
804 die "Argument for \%setup -b must be numeric at $specfile line $.\n" if $sopt !~ /^\d+$/;
805 $sbefore = $sopt;
806 $snum = $sopt;
807 $altsource = 2;
808 } elsif ($sopt eq '-c') {
809 # flag, create and change directory before unpack
810 $changedir = 1;
811 } elsif ($sopt eq '-D') {
812 # flag, do not delete directory before unpack
813 $deldir = 0;
814 } elsif ($sopt eq '-T') {
815 # flag, do not unpack first source
816 $deftarball = 0;
817 } elsif ($sopt eq '-q') {
818 # SSSH! Unpack quietly
819 $quietunpack = 1;
820 }
821 } # $sopt = shift @sbits
822
823# Note that this is an incomplete match to rpmbuild's full %setup expression.
824# Still known to do:
825# - Match implementation of -a and -b if both are specified.
826# Looks to be "expand -a into $aftersource and expand -b into $beforesource inside the while()"
827# instead of treating them as flags like the other arguments.
828# - -q appears to be somewhat positional
829
830 $prepscript .= "cd '$topdir/BUILD'\n";
831 $tarballdir = expandmacros($tarballdir,'gp');
832 $prepscript .= "rm -rf '$tarballdir'\n" if $deldir;
833 $prepscript .= "/bin/mkdir -p $tarballdir\n" if $changedir;
834
835 if ($deftarball) {
836 $prepscript .=
837 ( $pkgdata{main}{source} =~ /\.tar\.gz$/ ? "/bin/gzip" : "" ).
838 ( $pkgdata{main}{source} =~ /\.tar\.bz2$/ ? "/bin/bzip2" : "" ).
839 ( $pkgdata{main}{source} =~ /\.tar\.xz$/ ? "/usr/bin/xz" : "" ).
840 " -dc '$topdir/SOURCES/$pkgdata{main}{source}' | /bin/tar -x".
841 ( $quietunpack ? '' : 'vv' )."f -\n".
842 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n);
843 }
844
845 if ($altsource) {
846 if ($altsource == 1) { # -a
847 $prepscript .= "cd '$tarballdir'\n";
848 $prepscript .=
849 ( $pkgdata{sources}{$snum} =~ /\.tar\.gz$/ ? "/bin/gzip" : "" ).
850 ( $pkgdata{sources}{$snum} =~ /\.tar\.bz2$/ ? "/bin/bzip2" : "" ).
851 ( $pkgdata{sources}{$snum} =~ /\.tar\.xz$/ ? "/usr/bin/xz" : "" ).
852 " -dc '$topdir/SOURCES/$pkgdata{sources}{$snum}' | /bin/tar -x".
853 ( $quietunpack ? '' : 'vv' )."f -\n".
854 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n);
855 } # $altsource == 1
856
857 if ($altsource == 2) { # -b
858 $prepscript .=
859 ( $pkgdata{sources}{$snum} =~ /\.tar\.gz$/ ? "/bin/gzip" : "" ).
860 ( $pkgdata{sources}{$snum} =~ /\.tar\.bz2$/ ? "/bin/bzip2" : "" ).
861 ( $pkgdata{sources}{$snum} =~ /\.tar\.xz$/ ? "/usr/bin/xz" : "" ).
862 " -dc '$topdir/SOURCES/$pkgdata{sources}{$snum}' | /bin/tar -x".
863 ( $quietunpack ? '' : 'vv' )."f -\n".
864 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n);
865 $prepscript .= "cd '$tarballdir'\n";
866 } # $altsource == 2
867 } else {
868 $prepscript .= "cd '$tarballdir'\n" unless $changedir;
869 }
870
871# rpm doesn't seem to do the chowns any more
872# qq([ `/usr/bin/id -u` = '0' ] && /bin/chown -Rhf root .\n).
873# qq([ `/usr/bin/id -u` = '0' ] && /bin/chgrp -Rhf root .\n).
874 $prepscript .=
875 qq(/bin/chmod -Rf a+rX,u+w,g-w,o-w .\n);
876
877 } elsif ( my ($patchnum,$patchopts) = (/^\%patch([^\s]+)(\s+.+)?$/) ) {
878 chomp $patchnum;
879 $prepscript .= qq(echo "Patch #$patchnum ($pkgdata{main}{"patch$patchnum"}):"\n);
880 # If there are options passed, use'em.
881 # Otherwise, catch a bare %patch and ASS-U-ME it's '-p0'-able.
882 # Will break on options that don't provide -pnn, but what the hell.
883 # Need to decompress .bz2 and .gz patches on the fly. Not sure why one
884 # would ever have such things, but there they are... and rpmbuild supports 'em
885 # patch originally suggested by Lucian Cristian for .bz2
886 # reworked and expanded to support .Z/.gz/.xz as well
887 if ( $pkgdata{main}{"patch$patchnum"} =~ /\.(?:Z|gz|bz2|xz)$/ ) {
888 my $decompressor;
889 $decompressor = 'gzip' if $pkgdata{main}{"patch$patchnum"} =~ /\.(?:Z|gz)$/;
890 $decompressor = 'bzip2' if $pkgdata{main}{"patch$patchnum"} =~ /\.bz2$/;
891 $decompressor = 'xz' if $pkgdata{main}{"patch$patchnum"} =~ /\.xz$/;
892 $prepscript .= qq(/bin/$decompressor -d < $topdir/SOURCES/$pkgdata{main}{"patch$patchnum"} | patch );
893 $prepscript .= $patchopts if $patchopts;
894 $prepscript .= "-p0" if !$patchopts;
895 $prepscript .= qq( -s\nSTATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n);
896 } else {
897 $prepscript .= "patch ";
898 $prepscript .= $patchopts if $patchopts;
899 $prepscript .= "-p0" if !$patchopts;
900 $prepscript .= " -s <$topdir/SOURCES/".$pkgdata{main}{"patch$patchnum"}."\n";
901 }
902 } else {
903 $prepscript .= expandmacros($_,'gp');
904 }
905 next LINE;
906 } # prep
907
908 if ($stage eq 'build') {
909 # %build. This is pretty much just a shell script. There
910 # aren't many local macros to deal with.
911 if (/^\%configure/) {
912 $buildscript .= expandmacros($_,'cgbp');
913 } elsif (/^\%\{__make\}/) {
914 $buildscript .= expandmacros($_,'mgbp');
915 } else {
916 $buildscript .= expandmacros($_,'gp');
917 }
918 next LINE;
919 } # build
920
921 if ($stage eq 'install') {
922 if (/^\%makeinstall/) {
923 $installscript .= expandmacros($_,'igbp');
924 } else {
925 $installscript .= expandmacros($_,'gp');
926 }
927 next LINE;
928 } # install
929
930 if ($stage eq 'clean') {
931 $cleanscript .= expandmacros($_,'gp');
932 next LINE;
933 } # clean
934
935 if ($stage eq 'prepost') {
936 $pkgdata{$subname}{$scriptlet} .= expandmacros($_,'gp');
937 next LINE;
938 } # prepost
939
940 if ($stage eq 'files') {
941 # need to deal with these someday
942 next LINE if /^\%dir/;
943 next LINE if /^\%defattr/;
944 next LINE if /^\%verify/;
945 # dunno what to do with this; not sure if there's space in Debian's package structure for it.
946 next LINE if /^\%ghost/;
947 # Debian dpkg doesn't speak "%docdir". Meh.
948 next LINE if /^\%docdir/;
949# my $singleton = 0; # don't recall what this was for
950 next LINE if /^\s*#/;
951 next LINE if /^\s*$/;
952
953# create and initialize flags
954 my ($perms, $owner, $group, $conf, $filesline);
955 $perms = $owner = $group = $conf = '-';
956
957 $filesline = $_;
958
959 # strip and flag %attr constructs
960 if ($filesline =~ /\%attr\b/) {
961 # Extract %attr...
962 my ($args) = (/(\%attr\s*\(\s*[\d-]+\s*,\s*["a-zA-Z0-9-]+\s*,\s*["a-zA-Z0-9-]+\s*\))/);
963 $args =~ s/\s+//g;
964 $args =~ s/"//g; # don't think quotes are ever necessary, but they're *allowed*
965 # ... and parse it ...
966 ($perms,$owner,$group) = ($args =~ /\(([\d-]+),([a-zA-Z0-9-]+),([a-zA-Z0-9-]+)/);
967 # ... and wipe it when we're done.
968 $filesline =~ s/\%attr\s*\(\s*[\d-]+\s*,\s*["a-zA-Z0-9-]+\s*,\s*["a-zA-Z0-9-]+\s*\)//;
969 }
970
971 # Conffiles. Note that Debian and RH have similar, but not
972 # *quite* identical ideas of what constitutes a conffile. Nrgh.
973 # Note that dpkg will always ask if you want to replace the file - noreplace
974 # is more or less permanently enabled.
975##fixme
976# also need to handle missingok (file that doesn't exist, but should be removed on uninstall)
977# hmm. not sure if such is **POSSIBLE** with Debian... maybe an addition to %post?
978 if ($filesline =~ /\%config\b(?:\s*\(\s*noreplace\s*\)\s*)?/) {
979 $pkgdata{$subname}{conffiles} = 1; # Flag it for later
980 $conf = 'y';
981 $filesline =~ s/\%config\b(?:\s*\(\s*noreplace\s*\)\s*)?//;
982 }
983
984 # %doc needs extra processing, because it can be a space-separated list, and may
985 # include both full and partial pathnames. The partial pathnames must be fiddled
986 # into place in the %install script, because Debian doesn't really have the concept
987 # of "documentation file" that rpm does. (Debian "documentation files" are files
988 # in /usr/share/doc/<packagename>.)
989##fixme: unhandled case: %doc %defattr. Eeep.
990# don't really know what to do with %defattr, generally. :(
991 if ($filesline =~ /\%doc\b/) {
992 $filesline =~ s/\s*\%doc\s+//;
993
994# this could probably go elsewhere.
995 my $pkgname = $pkgdata{$subname}{name};
996 $pkgname =~ tr/_/-/;
997
998 # have to extract the partial pathnames that %doc installs automagically
999 foreach (split /\s+/, $filesline) {
1000 if (! (/^\%/ or m|^/|) ) {
1001 $doclist{$subname} .= " $_";
1002 my ($element) = m|([^/\s]+/?)$|;
1003 $filesline =~ s|$_|\%{_docdir}/$pkgname/$element|;
1004 }
1005 }
1006 } # $filesline =~ /\%doc\b/
1007
1008 $filesline =~ s/^\s*//; # Just In Case. For, uh, neatness.
1009
1010# due to Debian's total lack of real permissions-processing in its actual package
1011# handling component (dpkg-deb), this can't really be done "properly". We'll have
1012# to add chown/chmod commands to the postinst instead. Feh.
1013 $pkgdata{$subname}{'post'} .= "chown $owner $filesline\n" if $owner ne '-';
1014 $pkgdata{$subname}{'post'} .= "chgrp $group $filesline\n" if $group ne '-';
1015 $pkgdata{$subname}{'post'} .= "chmod $perms $filesline\n" if $perms ne '-';
1016
1017##fixme
1018# need hackery to assure only one filespec per %config. NB: "*" is one filespec. <g>
1019 push @{$pkgdata{$subname}{conflist}}, $filesline if $conf ne '-';
1020
1021 # now that we've got the specials out of the way, we can add things to the appropriate list of files.
1022 # ... and finally everything else
1023 $filelist{$subname} .= " $filesline";
1024
1025 next LINE;
1026 } # files
1027
1028 if ($stage eq 'changelog') {
1029 # this is one of the few places we do NOT generally want to replace macros...
1030 $pkgdata{main}{changelog} .= $_;
1031 }
1032
1033 if ($stage eq 'preamble') {
1034 if (/^summary:\s*(.+)/i) {
1035 $pkgdata{main}{summary} = $1;
1036 } elsif (/^name:\s*(.+)/i) {
1037 $pkgdata{main}{name} = expandmacros($1,'g');
1038 } elsif (/^epoch:\s*(.+)/i) {
1039 $pkgdata{main}{epoch} = expandmacros($1,'g');
1040 } elsif (/^version:\s*(.+)/i) {
1041 $pkgdata{main}{version} = expandmacros($1,'g');
1042 } elsif (/^release:\s*(.+)/i) {
1043 $pkgdata{main}{release} = expandmacros($1,'g');
1044 } elsif (/^group:\s*(.+)/i) {
1045 $pkgdata{main}{group} = $1;
1046 } elsif (/^copyright:\s*(.+)/i) {
1047 $pkgdata{main}{copyright} = $1;
1048 } elsif (/^url:\s*(.+)/i) {
1049 $pkgdata{main}{url} = $1;
1050 } elsif (/^packager:\s*(.+)/i) {
1051 $pkgdata{main}{packager} = $1;
1052 } elsif (/^buildroot:\s*(.+)/i) {
1053 $specglobals{buildroot} = $1;
1054 } elsif (my ($sval) = /^source0?:\s*(.+)/i) {
1055 $sval =~ s/\s*$//;
1056 $pkgdata{main}{source} = $sval;
1057 # Do this here since we don't use any URL forms elsewhere (maybe some other time)
1058 $pkgdata{main}{source} =~ s|^.+/([^/]+)$|$1|;
1059 $pkgdata{sources}{0} = $pkgdata{main}{source};
1060 # .tar, .tar.gz, .tar.bz2, .tgz, .xz
1061 die "Unknown Source: header format $sval\n" if $sval !~ /\.(?:tar(?:\.(?:gz|bz2|xz))?|tgz)$/;
1062 } elsif (my ($srcnum, $src) = (/^source([0-9]+):\s*(.+)/i)) {
1063 $src =~ s/\s*$//;
1064 $pkgdata{sources}{$srcnum} = $src;
1065 # Do this here since we don't use any URL forms elsewhere (maybe some other time)
1066 $pkgdata{sources}{$srcnum} =~ s|^.+/([^/]+)$|$1|;
1067 } elsif (/^patch([^:]+):\s*(.+)$/i) {
1068 my $patchname = "patch$1";
1069 $pkgdata{main}{$patchname} = $2;
1070 if ($pkgdata{main}{$patchname} =~ /\//) {
1071 # URL-style patch. Rare but not unheard-of.
1072 my @patchbits = split '/', $pkgdata{main}{$patchname};
1073 $pkgdata{main}{$patchname} = $patchbits[$#patchbits];
1074 }
1075 chomp $pkgdata{main}{$patchname};
1076 } elsif (/^buildarch(?:itecture)?:\s*(.+)/i) {
1077 $pkgdata{main}{arch} = $1;
1078 $pkgdata{main}{arch} =~ s/^noarch$/all/;
1079 $buildarch = $pkgdata{main}{arch};
1080 } elsif (/^buildrequires:\s*(.+)/i) {
1081 $buildreq .= ", $1";
1082 } elsif (/^requires:\s*(.+)/i) {
1083 $pkgdata{main}{requires} .= ", ".expandmacros("$1", 'gp');
1084 } elsif (/^provides:\s*(.+)/i) {
1085 $pkgdata{main}{provides} .= ", $1";
1086 } elsif (/^conflicts:\s*(.+)/i) {
1087 $pkgdata{main}{conflicts} .= ", $1";
1088 } elsif (/^recommends:\s*(.+)/i) {
1089 $pkgdata{main}{recommends} .= ", $1";
1090 warn "Warning: Debian-specific 'Recommends:' outside \%if wrapper\n" if $iflevel == 0;
1091 } elsif (/^suggests:\s*(.+)/i) {
1092 $pkgdata{main}{suggests} .= ", $1";
1093 warn "Warning: Debian-specific 'Suggests:' outside \%if wrapper\n" if $iflevel == 0;
1094 } elsif (/^enhances:\s*(.+)/i) {
1095 $pkgdata{main}{enhances} .= ", $1";
1096 warn "Warning: Debian-specific 'Enhances:' outside \%if wrapper\n" if $iflevel == 0;
1097 } elsif (/^replaces:\s*(.+)/i) {
1098 $pkgdata{main}{replaces} .= ", $1";
1099 warn "Warning: Debian-specific 'Replaces:' outside \%if wrapper\n" if $iflevel == 0;
1100 } elsif (/^autoreq(?:prov)?:\s*(.+)/i) {
1101 # we don't handle auto-provides (yet)
1102 $NoAutoReq = 1 if $1 =~ /(?:no|0)/i;
1103 }
1104 next LINE;
1105 } # preamble
1106
1107 } # while <SPEC>
1108
1109 # Parse and replace some more macros. More will be replaced even later.
1110
1111 # Expand macros as necessary.
1112 $scriptletbase = expandmacros($scriptletbase,'gp');
1113
1114 $cleanscript = expandmacros($cleanscript,'gp');
1115
1116 $specglobals{buildroot} = $cmdbuildroot if $cmdbuildroot;
1117 $specglobals{buildroot} = expandmacros($specglobals{buildroot},'gp');
1118
1119 close SPECFILE;
1120} # end parse_spec()
1121
1122
1123## prep()
1124# Writes and executes the %prep script (mostly) built while reading the spec file.
1125sub prep {
1126 # Replace some things here just to make sure.
1127 $prepscript = expandmacros($prepscript,'gp');
1128
1129 # create script filename
1130 my $prepscriptfile = "$tmpdir/deb-tmp.prep.".int(rand(99998)+1);
1131 sysopen(PREPSCRIPT, $prepscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
1132 or die "Can't open/create prep script file $prepscriptfile: $!\n";
1133 print PREPSCRIPT $scriptletbase;
1134 print PREPSCRIPT $prepscript;
1135 close PREPSCRIPT;
1136
1137 # execute
1138 print "Calling \%prep script $prepscriptfile...\n";
1139 system("/bin/sh -e $prepscriptfile") == 0
1140 or die "Can't exec: $!\n";
1141
1142 # and clean up
1143 unlink $prepscriptfile;
1144} # end prep()
1145
1146
1147## build()
1148# Writes and executes the %build script (mostly) built while reading the spec file.
1149sub build {
1150 # Expand the macros
1151 $buildscript = expandmacros($buildscript,'cgbp');
1152
1153 # create script filename
1154 my $buildscriptfile = "$tmpdir/deb-tmp.build.".int(rand(99998)+1);
1155 sysopen(BUILDSCRIPT, $buildscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
1156 or die "Can't open/create build script file $buildscriptfile: $!\n";
1157 print BUILDSCRIPT $scriptletbase;
1158 print BUILDSCRIPT $buildscript;
1159 close BUILDSCRIPT;
1160
1161 # execute
1162 print "Calling \%build script $buildscriptfile...\n";
1163 system("/bin/sh -e $buildscriptfile") == 0
1164 or die "Can't exec: $!\n";
1165
1166 # and clean up
1167 unlink $buildscriptfile;
1168} # end build()
1169
1170
1171## install()
1172# Writes and executes the %install script (mostly) built while reading the spec file.
1173sub install {
1174
1175 # munge %doc entries into place
1176 # rpm handles this with a separate executed %doc script, we're not going to bother.
1177 foreach my $docpkg (keys %doclist) {
1178 my $pkgname = $pkgdata{$docpkg}{name};
1179 $pkgname =~ s/_/-/g;
1180
1181 $installscript .= "DOCDIR=\$RPM_BUILD_ROOT\%{_docdir}/$pkgname\nexport DOCDIR\n";
1182 $installscript .= "mkdir -p \$DOCDIR\n";
1183 $doclist{$docpkg} =~ s/^\s*//;
1184 foreach (split(' ',$doclist{$docpkg})) {
1185 $installscript .= "cp -pr $_ \$DOCDIR/\n";
1186 }
1187 }
1188
1189 # Expand the macros
1190 $installscript = expandmacros($installscript,'igbp');
1191
1192 # create script filename
1193 my $installscriptfile = "$tmpdir/deb-tmp.inst.".int(rand(99998)+1);
1194 sysopen(INSTSCRIPT, $installscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
1195 or die "Can't open/create install script file $installscriptfile: $!\n";
1196 print INSTSCRIPT $scriptletbase;
1197 print INSTSCRIPT $installscript;
1198 close INSTSCRIPT;
1199
1200 # execute
1201 print "Calling \%install script $installscriptfile...\n";
1202 system("/bin/sh -e $installscriptfile") == 0
1203 or die "Can't exec: $!\n";
1204
1205 # and clean up
1206 unlink $installscriptfile;
1207
1208 # final bit: compress manpages if present
1209 # done here cuz I don't grok shell well
1210 # should probably error-check all kinds of things. <g>
1211 if (opendir MANROOT, "$specglobals{buildroot}/usr/share/man") {
1212 my @mansects = readdir MANROOT;
1213 closedir MANROOT;
1214 foreach my $mandir (@mansects) {
1215 next if $mandir !~ /^man/;
1216 opendir MANPAGES, "$specglobals{buildroot}/usr/share/man/$mandir";
1217 my @pagelist = readdir MANPAGES;
1218 closedir MANPAGES; # Slightly safer to to this; no accidental recursion. O_o
1219 foreach my $manpage (@pagelist) {
1220 $manpage = "$specglobals{buildroot}/usr/share/man/$mandir/$manpage";
1221 if ( -f $manpage) {
1222 if ($manpage =~ /^(.+)\.(?:Z|gz|bz2)\n?$/) {
1223 my $newpage = $1;
1224 `gunzip $manpage` if $manpage =~ /\.(?:Z|gz)$/;
1225 `bunzip2 $manpage` if $manpage =~ /\.bz2$/;
1226 $manpage = $newpage;
1227 }
1228 `gzip -9 -n $manpage`;
1229 } elsif ( -l $manpage) {
1230 my $linkdest = readlink $manpage;
1231 $linkdest =~ s/\.(?:Z|gz|bz2)//;
1232 unlink $manpage;
1233 $manpage =~ s/\.(?:Z|gz|bz2)//;
1234 symlink "$linkdest.gz", "$manpage.gz" or print "DEBUG: wibble: symlinking manpage failed: $!\n";
1235 }
1236 }
1237 }
1238 } # if opendir MANROOT
1239} # end install()
1240
1241
1242## binpackage()
1243# Creates the binary .deb package from the installed tree in $specglobals{buildroot}.
1244# Writes and executes a shell script to do so.
1245# Creates miscellaneous files required by dpkg-deb to actually build the package file.
1246# Should handle simple subpackages
1247sub binpackage {
1248
1249 foreach my $pkg (@pkglist) {
1250
1251 $pkgdata{$pkg}{arch} = $hostarch if !$pkgdata{$pkg}{arch}; # Just In Case.
1252
1253 # Make sure we have somewhere to write the .deb file
1254 if (!-e "$topdir/DEBS/$pkgdata{$pkg}{arch}") {
1255 mkdir "$topdir/DEBS/$pkgdata{$pkg}{arch}";
1256 }
1257
1258 # Skip building a package if it doesn't actually have any files. NB: This
1259 # differs slightly from rpm's behaviour where a package *will* be built -
1260 # even without any files - if %files is specified anywhere. I can think
1261 # of odd corner cases where that *may* be desireable.
1262 next if (!$filelist{$pkg} or $filelist{$pkg} =~ /^\s*$/);
1263
1264 # Gotta do this first, otherwise we don't have a place to move files from %files
1265 mkdir "$specglobals{buildroot}/$pkg";
1266
1267 # Eliminate any lingering % macros
1268 $filelist{$pkg} = expandmacros $filelist{$pkg}, 'g';
1269
1270 my @pkgfilelist = split ' ', $filelist{$pkg};
1271 foreach my $pkgfile (@pkgfilelist) {
1272 $pkgfile = expandmacros($pkgfile, 'gp');
1273
1274 # Feh. Manpages don't **NEED** to be gzipped, but rpmbuild does, and so shall we.
1275 # ... and your little info page too!
1276 if ($pkgfile =~ m{/usr/share/(?:man/man|info)}) {
1277 # need to check to see if manpage is gzipped
1278 if (-e "$specglobals{buildroot}$pkgfile") {
1279 # if we've just been pointed to a manpage section with "many" pages,
1280 # we need to gzip them all.
1281 # fortunately, we do NOT need to explicitly track each file for the
1282 # purpose of stuffing them in the package... the original %files
1283 # entry will do just fine.
1284 if ( -d "$specglobals{buildroot}$pkgfile") {
1285 foreach my $globfile (glob("$specglobals{buildroot}$pkgfile/*")) {
1286 gzip $globfile if $globfile !~ m|\.gz$|;
1287 }
1288 } else {
1289 if ($pkgfile !~ m|\.gz$|) {
1290 qx { gzip $specglobals{buildroot}$pkgfile };
1291 $pkgfile .= ".gz";
1292 }
1293 }
1294 } else {
1295 if ($pkgfile !~ m|\.gz$|) {
1296 $pkgfile .= ".gz";
1297 } else {
1298 $pkgfile =~ s/\.gz$//;
1299 qx { gzip $specglobals{buildroot}$pkgfile };
1300 $pkgfile .= ".gz";
1301 }
1302 }
1303 }
1304
1305 my ($fpath,$fname) = ($pkgfile =~ m|(.+?/?)?([^/]+/?)$|); # We don't need $fname now, but we might.
1306 qx { mkdir -p $specglobals{buildroot}/$pkg$fpath }
1307 if $fpath && $fpath ne '';
1308 qx { mv $specglobals{buildroot}$pkgfile $specglobals{buildroot}/$pkg$fpath };
1309 }
1310
1311 # Get the "Depends" (Requires) a la RPM. Ish. We strip the leading
1312 # comma and space here (if needed) in case there were "Requires" specified
1313 # in the spec file - those would precede these.
1314 $pkgdata{$pkg}{requires} .= getreqs("$specglobals{buildroot}/$pkg") if ! $NoAutoReq;
1315
1316 # magic needed to properly version dependencies...
1317 # only provided deps will really be included
1318 $pkgdata{$pkg}{requires} =~ s/^, //; # Still have to do this here.
1319 $pkgdata{$pkg}{requires} =~ s/\s+//g;
1320 my @deps = split /,/, $pkgdata{$pkg}{requires};
1321 my $tmp = '';
1322 foreach my $dep (@deps) {
1323 # Hack up the perl(Class::SubClass) deps into something dpkg can understand.
1324 # May or may not be versioned.
1325 # We do this first so the version rewriter can do its magic next.
1326 if (my ($mod,$ver) = ($dep =~ /^perl\(([A-Za-z0-9\:\-]+)\)([><=]+.+)?/) ) {
1327 $mod =~ s/^perl\(//;
1328 $mod =~ s/\)$//;
1329 $mod =~ s/::/-/g;
1330 $mod =~ tr/A-Z/a-z/;
1331 $mod = "lib$mod-perl";
1332 $mod .= $ver if $ver;
1333 $dep = $mod;
1334 }
1335 if (my ($name,$rel,$value) = ($dep =~ /^([a-zA-Z0-9._-]+)([><=]+)([a-zA-Z0-9._-]+)$/)) {
1336 $tmp .= ", $name ($rel $value)";
1337 } else {
1338 $tmp .= ", $dep";
1339 }
1340 }
1341 ($pkgdata{$pkg}{requires} = $tmp) =~ s/^, //;
1342
1343 # Do this here since we're doing {depends}...
1344 if (defined($pkgdata{$pkg}{provides})) {
1345 $pkgdata{$pkg}{provides} =~ s/^, //;
1346 $pkgdata{$pkg}{provides} = expandmacros($pkgdata{$pkg}{provides},'gp');
1347 }
1348 if (defined($pkgdata{$pkg}{conflicts})) {
1349 $pkgdata{$pkg}{conflicts} =~ s/^, //;
1350 $pkgdata{$pkg}{conflicts} = expandmacros($pkgdata{$pkg}{conflicts},'gp');
1351 }
1352
1353# These are Debian-specific!
1354 if (defined($pkgdata{$pkg}{recommends})) {
1355 $pkgdata{$pkg}{recommends} =~ s/^, //;
1356 $pkgdata{$pkg}{recommends} = expandmacros($pkgdata{$pkg}{recommends},'gp');
1357 }
1358 if (defined($pkgdata{$pkg}{suggests})) {
1359 $pkgdata{$pkg}{suggests} =~ s/^, //;
1360 $pkgdata{$pkg}{suggests} = expandmacros($pkgdata{$pkg}{suggests},'gp');
1361 }
1362 if (defined($pkgdata{$pkg}{enhances})) {
1363 $pkgdata{$pkg}{enhances} =~ s/^, //;
1364 $pkgdata{$pkg}{enhances} = expandmacros($pkgdata{$pkg}{enhances},'gp');
1365 }
1366 if (defined($pkgdata{$pkg}{replaces})) {
1367 $pkgdata{$pkg}{replaces} =~ s/^, //;
1368 $pkgdata{$pkg}{replaces} = expandmacros($pkgdata{$pkg}{replaces},'gp');
1369 }
1370
1371 # Gotta do this next, otherwise the control file has nowhere to go. >:(
1372 mkdir "$specglobals{buildroot}/$pkg/DEBIAN";
1373
1374 # Hack the filename for the package into a Debian-tool-compatible format. GRRRRRR!!!!!
1375 # Have I mentioned I hate Debian Policy?
1376 $pkgdata{$pkg}{name} =~ tr/_/-/;
1377
1378 # create script filename
1379 my $debscriptfile = "$tmpdir/deb-tmp.pkg.".int(rand(99998)+1);
1380 sysopen(DEBSCRIPT, $debscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
1381 or die "Can't open/create package-creation script file $debscriptfile: $!\n";
1382 print DEBSCRIPT $scriptletbase;
1383 print DEBSCRIPT "fakeroot -- dpkg-deb -b $specglobals{buildroot}/$pkg $topdir/DEBS/$pkgdata{$pkg}{arch}/".
1384 "$pkgdata{$pkg}{name}_".
1385 (defined($pkgdata{main}{epoch}) ? "$pkgdata{main}{epoch}:" : '').
1386 "$pkgdata{$pkg}{version}-$pkgdata{main}{release}_$pkgdata{$pkg}{arch}.deb\n";
1387 # %$&$%@#@@#%@@@ Debian and their horrible ugly package names. >:(
1388 close DEBSCRIPT;
1389
1390 $pkgdata{$pkg}{summary} = expandmacros($pkgdata{$pkg}{summary}, 'gp');
1391 my $control = "Package: $pkgdata{$pkg}{name}\n".
1392 "Version: ".(defined($pkgdata{main}{epoch}) ? "$pkgdata{main}{epoch}:" : '').
1393 "$pkgdata{$pkg}{version}-$pkgdata{main}{release}\n".
1394 "Section: ".($pkgdata{$pkg}{group} ? $pkgdata{$pkg}{group} : $pkgdata{main}{group})."\n".
1395 "Priority: optional\n".
1396 "Architecture: $pkgdata{$pkg}{arch}\n".
1397 "Maintainer: $pkgdata{main}{packager}\n".
1398 ( $pkgdata{$pkg}{requires} ne '' ? "Depends: $pkgdata{$pkg}{requires}\n" : '' ).
1399 ( defined($pkgdata{$pkg}{provides}) ? "Provides: $pkgdata{$pkg}{provides}\n" : '' ).
1400 ( defined($pkgdata{$pkg}{conflicts}) ? "Conflicts: $pkgdata{$pkg}{conflicts}\n" : '' ).
1401 ( defined($pkgdata{$pkg}{recommends}) ? "Recommends: $pkgdata{$pkg}{recommends}\n" : '' ).
1402 ( defined($pkgdata{$pkg}{suggests}) ? "Suggests: $pkgdata{$pkg}{suggests}\n" : '' ).
1403 ( defined($pkgdata{$pkg}{enhances}) ? "Enhances: $pkgdata{$pkg}{enhances}\n" : '' ).
1404 ( defined($pkgdata{$pkg}{replaces}) ? "Replaces: $pkgdata{$pkg}{replaces}\n" : '' ).
1405 "Description: $pkgdata{$pkg}{summary}\n";
1406 $pkgdata{$pkg}{desc} = expandmacros($pkgdata{$pkg}{desc}, 'gp');
1407 # Munge things so that Debian tools don't choke on errant blank lines
1408 $pkgdata{$pkg}{desc} =~ s/\s+$//g; # Trim trailing blanks
1409 $pkgdata{$pkg}{desc} =~ s/^ $/ ./mg; # Replace lines consisting of " \n" with " .\n"
1410 $control .= "$pkgdata{$pkg}{desc}\n";
1411
1412 open CONTROL, ">$specglobals{buildroot}/$pkg/DEBIAN/control";
1413 print CONTROL $control;
1414 close CONTROL;
1415
1416 # Iff there are conffiles (as specified in the %files list(s), add'em
1417 # in so dpkg-deb can tag them.
1418 if ($pkgdata{$pkg}{conffiles}) {
1419 open CONFLIST, ">$specglobals{buildroot}/$pkg/DEBIAN/conffiles";
1420 foreach my $conffile (@{$pkgdata{$pkg}{conflist}}) {
1421 $conffile = expandmacros($conffile, 'g');
1422 my @tmp = glob "$specglobals{buildroot}/$pkg/$conffile";
1423 foreach (@tmp) {
1424 s|$specglobals{buildroot}/$pkg/||g; # nrgl. gotta be a better way to do this...
1425 s/\s+/\n/g; # Not gonna support spaces in filenames. Ewww.
1426 print CONFLIST "$_\n";
1427 }
1428 }
1429 close CONFLIST;
1430 }
1431
1432 # found the point of scripts on subpackages.
1433 if ($pkgdata{$pkg}{'pre'}) {
1434 $pkgdata{$pkg}{'pre'} = expandmacros($pkgdata{$pkg}{'pre'},'gp');
1435 open PREINST, ">$specglobals{buildroot}/$pkg/DEBIAN/preinst";
1436 print PREINST "#!/bin/sh\nset -e\n\n";
1437 print PREINST $pkgdata{$pkg}{'pre'};
1438 close PREINST;
1439 chmod 0755, "$specglobals{buildroot}/$pkg/DEBIAN/preinst";
1440 }
1441 if ($pkgdata{$pkg}{'post'}) {
1442 $pkgdata{$pkg}{'post'} = expandmacros($pkgdata{$pkg}{'post'},'gp');
1443 open POSTINST, ">$specglobals{buildroot}/$pkg/DEBIAN/postinst";
1444 print POSTINST "#!/bin/sh\nset -e\n\n";
1445 print POSTINST $pkgdata{$pkg}{'post'};
1446 close POSTINST;
1447 chmod 0755, "$specglobals{buildroot}/$pkg/DEBIAN/postinst";
1448 }
1449 if ($pkgdata{$pkg}{'preun'}) {
1450 $pkgdata{$pkg}{'pre'} = expandmacros($pkgdata{$pkg}{'preun'},'gp');
1451 open PREUNINST, ">$specglobals{buildroot}/$pkg/DEBIAN/prerm";
1452 print PREUNINST "#!/bin/sh\nset -e\n\n";
1453 print PREUNINST $pkgdata{$pkg}{'preun'};
1454 close PREUNINST;
1455 chmod 0755, "$specglobals{buildroot}/$pkg/DEBIAN/prerm";
1456 }
1457 if ($pkgdata{$pkg}{'postun'}) {
1458 $pkgdata{$pkg}{'postun'} = expandmacros($pkgdata{$pkg}{'postun'},'gp');
1459 open POSTUNINST, ">$specglobals{buildroot}/$pkg/DEBIAN/postrm";
1460 print POSTUNINST "#!/bin/sh\nset -e\n\n";
1461 print POSTUNINST $pkgdata{$pkg}{'postun'};
1462 close POSTUNINST;
1463 chmod 0755, "$specglobals{buildroot}/$pkg/DEBIAN/postrm";
1464 }
1465
1466 # execute
1467 print "Calling package creation script $debscriptfile for $pkgdata{$pkg}{name}...\n";
1468 system("/bin/sh -e $debscriptfile") == 0
1469 or die "Can't exec: $!\n";
1470
1471 $finalmessages .= "Wrote binary package ".
1472 "$pkgdata{$pkg}{name}_".
1473 (defined($pkgdata{main}{epoch}) ? "$pkgdata{main}{epoch}:" : '').
1474 "$pkgdata{$pkg}{version}-$pkgdata{main}{release}_$pkgdata{$pkg}{arch}.deb".
1475 " in $topdir/DEBS/$pkgdata{$pkg}{arch}\n";
1476 # and clean up
1477 unlink $debscriptfile;
1478
1479 } # subpackage loop
1480
1481} # end binpackage()
1482
1483
1484## srcpackage()
1485# Builds a .src.deb source package. Note that Debian's idea of
1486# a "source package" is seriously flawed IMO, because you can't
1487# easily copy it as-is.
1488# Not quite identical to RPM, but Good Enough (TM).
1489sub srcpackage {
1490 # In case we were called with -bs.
1491 $pkgdata{main}{name} =~ tr/_/-/;
1492 my $pkgsrcname = "$pkgdata{main}{name}-".
1493 (defined($pkgdata{main}{epoch}) ? "$pkgdata{main}{epoch}:" : '').
1494 "$pkgdata{main}{version}-$pkgdata{main}{release}.sdeb";
1495
1496 my $paxcmd;
1497
1498 # We'll definitely need this later, and *may* need it sooner.
1499 (my $barespec = $specfile) =~ s|.+/([^/]+)$|$1|;
1500
1501 # Copy the specfile to the build tree, but only if it's not there already.
1502##buglet: need to deal with silly case where silly user has put the spec
1503# file in a subdir of %{_topdir}/SPECS. Ewww. Silly user!
1504 if (abs_path($specfile) !~ /^$topdir\/SPECS/) {
1505 $paxcmd .= "cp $specfile %{_topdir}/SPECS/; \n"
1506 }
1507
1508 # use pax -w [file] [file] ... >outfile.sdeb
1509 $paxcmd = "cd $topdir; pax -w ";
1510
1511 # some packages may not have a "main" source.
1512 $paxcmd .= "SOURCES/$pkgdata{main}{source} " if $pkgdata{main}{source};
1513
1514 # create file list: Source[nn], Patch[nn]
1515 foreach my $specbit (keys %{$pkgdata{main}} ) {
1516 next if $specbit eq 'source';
1517 $paxcmd .= "SOURCES/$pkgdata{main}{$specbit} " if $specbit =~ /^patch/;
1518##buglet: need to deal with case where patches are listed as URLs?
1519# or other extended pathnames? Silly !@$%^&!%%!%!! user!
1520 }
1521
1522 foreach my $source (keys %{$pkgdata{sources}}) {
1523 # Skip Source0, since it's also $pkgdata{main}{source}. Could arguably
1524 # just remove that instead, but I think that might backfire.
1525 next if $source eq '0';
1526 $paxcmd .= "SOURCES/$pkgdata{sources}{$source} ";
1527 }
1528
1529 # add the spec file, source package destination, and cd back where we came from.
1530 $paxcmd .= "SPECS/$barespec > $topdir/SDEBS/$pkgsrcname; cd -";
1531
1532 # In case of %-macros...
1533 $paxcmd = expandmacros($paxcmd,'gp');
1534
1535 system "$paxcmd";
1536 $finalmessages .= "Wrote source package $pkgsrcname in $topdir/SDEBS.\n";
1537} # end srcpackage()
1538
1539
1540## clean()
1541# Writes and executes the %clean script (mostly) built while reading the spec file.
1542sub clean {
1543 # Replace some things here just to make sure.
1544 $cleanscript = expandmacros($cleanscript,'gp');
1545
1546 # create script filename
1547 my $cleanscriptfile = "$tmpdir/deb-tmp.clean.".int(rand(99998)+1);
1548 sysopen(CLEANSCRIPT, $cleanscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
1549 or die $!;
1550 print CLEANSCRIPT $scriptletbase;
1551 print CLEANSCRIPT $cleanscript;
1552 close CLEANSCRIPT;
1553
1554 # execute
1555 print "Calling \%clean script $cleanscriptfile...\n";
1556 system("/bin/sh -e $cleanscriptfile") == 0
1557 or die "Can't exec: $!\n";
1558
1559 # and clean up
1560 unlink $cleanscriptfile;
1561} # end clean()
1562
1563
1564## checkbuildreq()
1565# Checks the build requirements (if any)
1566# Spits out a rude warning and returns a true-false error if any
1567# requirements are not met.
1568sub checkbuildreq {
1569 return 1 if $buildreq eq ''; # No use doing extra work.
1570
1571 # expand macros
1572 $buildreq = expandmacros($buildreq,'gp');
1573
1574 if ( ! -e "/usr/bin/dpkg-query" ) {
1575 print "**WARNING** dpkg-query not found. Can't check build-deps.\n".
1576 " Required for sucessful build:\n".$buildreq."\n".
1577 " Continuing anyway.\n";
1578 return 1;
1579 }
1580
1581 my $reqflag = 1; # unset iff a buildreq is missing
1582
1583 $buildreq =~ s/^, //; # Strip the leading comma and space
1584 my @reqlist = split /,\s+/, $buildreq;
1585
1586 my @missinglist;
1587 foreach my $req (@reqlist) {
1588 # from rpmbuild error message
1589 # Dependency tokens must begin with alpha-numeric, '_' or '/'
1590##fixme: check for suitable whitespace around $rel
1591 my ($pkg,$rel,$ver);
1592
1593 # We have two classes of requirements - versioned and unversioned.
1594 if ($req =~ /[><=]/) {
1595 # Pick up the details of versioned buildreqs
1596 ($pkg,$rel,$ver) = ($req =~ /([a-z0-9._-]+)\s+([><=]+)\s+([a-z0-9._-]+)/);
1597 } else {
1598 # And the unversioned ones.
1599 $pkg = $req;
1600 $rel = '>=';
1601 $ver = 0;
1602 }
1603
1604## Apparently a package that has been installed, then uninstalled, has a "known" dpkg status of
1605## "unknown ok not-installed" vs a package that has never been installed which returns nothing. O_o
1606## Virtual packages, of course, *also* show as "not installed" like this (WTF?)
1607## This causes real packages to be misdetected as installed "possible virtual packages" instead of "missing
1608## packages". I don't think there's really a solution until/unless Debian's tools properly register virtual
1609## packages as installed.
1610 my @pkglist = qx { dpkg-query --showformat '\${status}\t\${version}\n' -W $pkg };
1611 if (!$pkglist[0]) {
1612 print " * Missing build-dependency $pkg!\n $pkglist[0]";
1613 $reqflag = 0;
1614 push @missinglist, $pkg;
1615 } else {
1616# real package, installed
1617#kdeugau:~$ dpkg-query --showformat '${status}\t${version}\n' -W libc-client2007e-dev 2>&1
1618#install ok installed 8:2007f~dfsg-1
1619# virtual package, provided by ???
1620#kdeugau:~$ dpkg-query --showformat '${status}\t${version}\n' -W libc-client-dev 2>&1
1621#unknown ok not-installed
1622# real package or virtual package not installed or provided
1623#kdeugau:~$ dpkg-query --showformat '${status}\t${version}\n' -W libdb4.8-dbg 2>&1
1624#dpkg-query: no packages found matching libdb4.8-dbg
1625
1626 my ($reqstat,undef,undef,$reqver) = split /\s+/, $pkglist[0];
1627 if ($reqstat =~ /^unknown/) {
1628 # this seems to be a virtual package.
1629 print " * Warning: $pkg is probably installed but seems to be a virtual package.\n";
1630 } elsif ($reqstat =~ /^install/) {
1631 my ($resp) = qx { dpkg --compare-versions $reqver '$rel' $ver && echo "ok" };
1632 if ($resp !~ /^ok/) {
1633 $reqflag = 0;
1634 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n"
1635 }
1636 } else {
1637 # whatever state it's in, it's not completely installed, therefore it's missing.
1638 print " * Missing build-dependency $pkg!\n $pkglist[0]";
1639 $reqflag = 0;
1640 push @missinglist, $pkg;
1641 } # end not installed/installed check
1642 }
1643 } # end req loop
1644
1645 print "To install all missing dependencies, run 'apt-get install ".join(' ', @missinglist)."'.\n" unless $reqflag;
1646
1647 return $reqflag;
1648} # end checkbuildreq()
1649
1650
1651## getreqs()
1652# Find out which libraries/packages are required for any
1653# executables and libs in a given file tree.
1654# (Debian doesn't have soname-level deps; just package-level)
1655# Returns an empty string if the tree contains no binaries.
1656# Doesn't work well on shell scripts. but those *should* be
1657# fine anyway. (Yeah, right...)
1658sub getreqs() {
1659 my $pkgtree = $_[0];
1660
1661 print "Checking library requirements...\n";
1662 my @binlist = qx { find $pkgtree -type f -perm 755 };
1663
1664 if (scalar(@binlist) == 0) {
1665 return '';
1666 }
1667
1668 my @reqlist;
1669 foreach (@binlist) {
1670 push @reqlist, qx { LANG=C ldd $_ };
1671 }
1672
1673 # Get the list of libs provided by this package. Still doesn't
1674 # handle the case where the lib gets stuffed into a subpackage. :/
1675 my @intprovlist = qx { find $pkgtree -type f -name "*.so*" };
1676 my $provlist = '';
1677 foreach (@intprovlist) {
1678 s/$pkgtree//;
1679 $provlist .= "$_";
1680 }
1681
1682 my %reqs;
1683 my $reqlibs = '';
1684 my %reqliblist;
1685
1686 foreach (@reqlist) {
1687 next if /^$pkgtree/;
1688 next if /not a dynamic executable/;
1689 next if m|/lib(?:64)?/ld-linux|; # Hack! Hack! PTHBTT! (libc suxx0rz)
1690 next if /linux-gate.so/; # Kernel hackery for teh W1n!!1!1eleventy-one!1 (Don't ask. Feh.)
1691 next if /linux-vdso.so/; # More kernel hackery. Whee!
1692
1693 # Whee! more hackery to detect provided-here libs. Some apparently return from ldd as "not found".
1694 my ($a,$b) = split / => /;
1695 $a =~ s/\s*//g;
1696 if ($b =~ /not found/) {
1697 next if qx { find $specglobals{buildroot} -name "*$a" };
1698 }
1699
1700 my ($req) = (m|=\>\s+([a-zA-Z0-9._/+-]+)|); # dig out the actual library (so)name.
1701 # And feh, we need the *path*, since I've discovered a new edge case where
1702 # the same libnnn.1.2.3 *file*name is found across *several* lib dirs. >:(
1703
1704 # Ignore libs provided by this package. Note that we don't match
1705 # on word-boundary at the *end* of the lib we're looking for, as the
1706 # looked-for lib may not have the full soname version. (ie, it may
1707 # "just" point to one of the symlinks that get created somewhere.)
1708 next if $provlist =~ /\b$req/;
1709
1710 $reqlibs .= " $req";
1711 $reqliblist{$req} = 1;
1712 }
1713
1714 if (%reqliblist) {
1715 foreach my $rlib (keys %reqliblist) {
1716 my $libpkg = qx { dpkg -S $rlib };
1717 ($libpkg,undef) = split /:/, $libpkg;
1718 $reqs{$libpkg} = 1;
1719 }
1720 }
1721
1722 my $deplist = '';
1723 foreach (keys %reqs) {
1724 $deplist .= ", $_";
1725 }
1726
1727# For now, we're done. We're not going to meddle with versions yet.
1728# Among other things, it's messier than handling "simple" yes/no "do
1729# we have this lib?" deps. >:(
1730
1731 return $deplist;
1732} # end getreqs()
1733
1734
1735## install_sdeb()
1736# Extracts .sdeb contents to %_topdir as appropriate
1737sub install_sdeb {
1738 $srcpkg = abs_path($srcpkg);
1739
1740 my $paxcmd = "cd $topdir; pax -r <$srcpkg; cd -";
1741
1742 # In case of %-macros...
1743 $paxcmd = expandmacros($paxcmd,'gp');
1744
1745 system "$paxcmd";
1746 print "Extracted source package $srcpkg to $topdir.\n";
1747} # end install_sdeb()
1748
1749
1750## expandmacros()
1751# Expands all %{blah} macros in the passed string
1752# Split up a bit with some sections so we don't spend time trying to
1753# expand macros that are only used in a few specific places.
1754sub expandmacros {
1755 my $macrostring = shift;
1756 my $section = shift;
1757
1758 # To allow the FHS-ish %configure and %makeinstall to work The Right Way.
1759 # (Without clobbering the global $specglobals{buildroot}.)
1760 my $prefix = '';
1761
1762 if ($section =~ /c/) {
1763 # %configure macro
1764# Don't know what it's for, don't have a useful default replacement
1765# --program-prefix=%{_program_prefix} \
1766 $macrostring =~ s'%configure'./configure --host=$DEB_HOST_GNU_TYPE \
1767 --build=$DEB_BUILD_GNU_TYPE \
1768 --prefix=%{_prefix} \
1769 --exec-prefix=%{_exec_prefix} \
1770 --bindir=%{_bindir} \
1771 --sbindir=%{_sbindir} \
1772 --sysconfdir=%{_sysconfdir} \
1773 --datadir=%{_datadir} \
1774 --includedir=%{_includedir} \
1775 --libdir=%{_libdir} \
1776 --libexecdir=%{_libexecdir} \
1777 --localstatedir=%{_localstatedir} \
1778 --sharedstatedir=%{_sharedstatedir} \
1779 --mandir=%{_mandir} \
1780 --infodir=%{_infodir} ';
1781 } # done %configure
1782
1783 if ($section =~ /m/) {
1784 $macrostring =~ s'%{__make}'make ';
1785 } # done make
1786
1787# %makeinstall expands with recursive macros in rpm. whee.
1788 if ($section =~ /i/) {
1789 # This is where we need to mangle $prefix.
1790 $macrostring =~ s'%makeinstall'make \
1791 prefix=%{?buildroot:%{buildroot}}%{_prefix} \
1792 exec_prefix=%{?buildroot:%{buildroot}}%{_exec_prefix} \
1793 bindir=%{?buildroot:%{buildroot}}%{_bindir} \
1794 sbindir=%{?buildroot:%{buildroot}}%{_sbindir} \
1795 sysconfdir=%{?buildroot:%{buildroot}}%{_sysconfdir} \
1796 datadir=%{?buildroot:%{buildroot}}%{_datadir} \
1797 includedir=%{?buildroot:%{buildroot}}%{_includedir} \
1798 libdir=%{?buildroot:%{buildroot}}%{_libdir} \
1799 libexecdir=%{?buildroot:%{buildroot}}%{_libexecdir} \
1800 localstatedir=%{?buildroot:%{buildroot}}%{_localstatedir} \
1801 sharedstatedir=%{?buildroot:%{buildroot}}%{_sharedstatedir} \
1802 mandir=%{?buildroot:%{buildroot}}%{_mandir} \
1803 infodir=%{?buildroot:%{buildroot}}%{_infodir} \
1804 install ';
1805 $prefix = $specglobals{buildroot};
1806 } # done %install and/or %makeinstall
1807
1808 # Build data
1809 # Note that these are processed in reverse order to get the substitution order right
1810 if ($section =~ /b/) {
1811# $macrostring =~ s'%{fhs}'host=$DEB_HOST_GNU_TYPE \
1812# build=$DEB_BUILD_GNU_TYPE \
1813 $macrostring =~ s'%{fhs}'prefix=%{_prefix} \
1814 exec-prefix=%{_exec_prefix} \
1815 bindir=%{_bindir} \
1816 sbindir=%{_sbindir} \
1817 sysconfdir=%{_sysconfdir} \
1818 datadir=%{_datadir} \
1819 includedir=%{_includedir} \
1820 libdir=%{_libdir} \
1821 libexecdir=%{_libexecdir} \
1822 localstatedir=%{_localstatedir} \
1823 sharedstatedir=%{_sharedstatedir} \
1824 mandir=%{_mandir} \
1825 infodir=%{_infodir} \
1826';
1827
1828 # Note that the above regex terminates with the extra space
1829 # "Just In Case" of user additions, which will then get neatly
1830 # tagged on the end where they take precedence (supposedly)
1831 # over the "default" ones.
1832
1833 # Now we cascade the macros introduced above. >_<
1834 # Wot ot to go theah:
1835 $macrostring =~ s|%{_mandir}|%{_datadir}/man|g; #/usr/share/man
1836 $macrostring =~ s|%{_infodir}|%{_datadir}/info|g; #/usr/share/info
1837 $macrostring =~ s|%{_oldincludedir}|/usr/include|g; #/usr/include
1838 $macrostring =~ s|%{_includedir}|%{_prefix}/include|g; #/usr/include
1839 $macrostring =~ s|%{_libdir}|%{_exec_prefix}/%{_lib}|g; #/usr/lib
1840 $macrostring =~ s|%{_lib}|lib|g; #?
1841 $macrostring =~ s|%{_localstatedir}|/var|g; #/var
1842 $macrostring =~ s|%{_sharedstatedir}|%{_prefix}/com|g; #/usr/com WTF?
1843 $macrostring =~ s|%{_sysconfdir}|/etc|g; #/etc
1844 $macrostring =~ s|%{_datadir}|%{_prefix}/share|g; #/usr/share
1845 $macrostring =~ s|%{_libexecdir}|%{_exec_prefix}/libexec|g; #/usr/libexec
1846 $macrostring =~ s|%{_sbindir}|%{_exec_prefix}/sbin|g; #/usr/sbin
1847 $macrostring =~ s|%{_bindir}|%{_exec_prefix}/bin|g; #/usr/bin
1848 $macrostring =~ s|%{_exec_prefix}|%{_prefix}|g; #/usr
1849 $macrostring =~ s|%{_prefix}|/usr|g; #/usr
1850 } # done with config section
1851
1852 # Package data
1853 if ($section =~ /p/) {
1854 $macrostring =~ s/\%\{buildroot\}/$specglobals{buildroot}/gi;
1855 $macrostring =~ s/\%\{source0?}/$topdir\/SOURCES\/$pkgdata{main}{source}/gi;
1856 foreach my $source (keys %{$pkgdata{sources}}) {
1857 $macrostring =~ s/\%\{source$source\}/$topdir\/SOURCES\/$pkgdata{sources}{$source}/gi;
1858 }
1859 $macrostring =~ s/\%\{name\}/$pkgdata{main}{name}/gi;
1860 $macrostring =~ s/\%\{version\}/$pkgdata{main}{version}/gi;
1861 $macrostring =~ s/\%\{release\}/$pkgdata{main}{release}/gi;
1862 }
1863
1864 # Globals, and not-so-globals
1865 if ($section =~ /g/) {
1866
1867 $macrostring =~ s|%{_builddir}|%{_topdir}/BUILD|g;
1868# no longer a valid macro/tag
1869# $macrostring =~ s|%{buildsubdir}|$tarballdir|g;
1870 $macrostring =~ s|%{_topdir}|$topdir|g;
1871 $macrostring =~ s|%{_tmppath}|$tmpdir|g;
1872 $macrostring =~ s'%{_docdir}'%{_datadir}/doc'g;
1873
1874 # Standard FHS locations. More or less.
1875 $macrostring =~ s'%{_bindir}'/usr/bin'g;
1876 $macrostring =~ s'%{_sbindir}'/usr/sbin'g;
1877 $macrostring =~ s'%{_mandir}'%{_datadir}/man'g;
1878 $macrostring =~ s'%{_infodir}'%{_datadir}/info'g;
1879 $macrostring =~ s'%{_includedir}'/usr/include'g;
1880 $macrostring =~ s'%{_libdir}'/usr/lib'g;
1881 $macrostring =~ s'%{_sysconfdir}'/etc'g;
1882 $macrostring =~ s'%{_localstatedir}'/var'g;
1883 $macrostring =~ s'%{_prefix}'/usr'g;
1884
1885 # FHS-ish locations that aren't quite actually FHS-specified.
1886 $macrostring =~ s'%{_datadir}'/usr/share'g;
1887
1888 # special %define's. Handle the general case where we eval anything.
1889 # Even more general: %(...) is a spec-parse-time shell code wrapper.
1890 # Prime example:
1891 #%define perl_vendorlib %(eval "`perl -V:installvendorlib`"; echo $installvendorlib)
1892 if ($macrostring =~ /\%\((.+)\)/) {
1893 my $shellstr = $1;
1894 # Oy vey this gets silly for the perl bits. Executing a shell to
1895 # call Perl to get the vendorlib/sitelib/whatever "core" globals.
1896 # This can do more, but... eww.
1897 $shellstr = qx { /bin/sh -c '$shellstr' }; # Yay! ' characters apparently get properly exscapededed.
1898 $macrostring =~ s/\%\(.+\)/$shellstr/;
1899 }
1900
1901 # support for **some** %if constructs. Note that this breaks somewhat if
1902 # there's no value specified... but so does rpm.
1903
1904 my $tmpcount = 0; # runaway shutdown. Not really sure how else to avoid getting stuck.
1905 while ($macrostring =~ m/\%\{([?!]+)([a-z0-9_.-]+)(?:\:([a-z0-9_.\/\-\\]+)?)?\}/g) { #Whew....
1906 my $qex = $1;
1907 my $macro = $2;
1908 my $value = (defined($3) ? $3 : '');
1909
1910 my $neg = '1' if $qex =~ /\!/;
1911 if ($specglobals{$macro}) {
1912 $value = '' if $neg;
1913 } else {
1914 $value = '' if !$neg;
1915 }
1916 $macrostring =~ s/\%\{[?!]+[a-z0-9_.-]+(?:\:([a-z0-9_.\/\-\\]+)?)?\}/$value/;
1917
1918# not certain about this, but I don't want to run away. It *can* happen if planned carefully. :/
1919$tmpcount++;
1920# %makeinstall has 13 occurrences. D'Oh!
1921die "excessive recursive macro replacement; dying.\n" if $tmpcount > 14;
1922
1923 } # while()
1924
1925 # Misc expansions
1926 $macrostring =~ s|%{_arch}|$hostarch|g;
1927 $macrostring =~ s|%{optflags}|$optflags{$hostarch}|g;
1928 $macrostring =~ s|%{_vendor}|$specglobals{'_vendor'}|g;
1929
1930 # system programs. RPM uses a global config file for these; we'll just
1931 # ASS-U-ME and make life a little simpler.
1932 if ($macrostring =~ /\%\{\_\_([a-z0-9_-]+)\}/) {
1933 $macrostring =~ s|%{__([a-z0-9_-]+)}|$1|g;
1934 }
1935
1936# should probably stick a "no runaway" flag in here... Just In Case...
1937 # %define's
1938# note to self: find out valid chars for %define name/label so we can parse them
1939 while (my ($key) = ($macrostring =~ /%{([a-z0-9_]+)}/i) ) {
1940# hrm. This needs thinking.
1941#die "A horrible death! \%{$key}, '$macrostring'\n" if !$specglobals{$key};
1942 $macrostring =~ s|%{$key}|$specglobals{$key}|g;
1943 # wanna limit this to "... if $specglobals{$key}", but need more magic
1944 }
1945
1946 # Perl @INC/...lib locations, and other related bits.
1947 $macrostring =~ s|%{perl_archlib}|$Config{installarchlib}|g;
1948 $macrostring =~ s|%{perl_sitelib}|$Config{installsitelib}|g;
1949 $macrostring =~ s|%{perl_sitearch}|$Config{installsitearch}|g;
1950 $macrostring =~ s|%{perl_vendorlib}|$Config{installvendorlib}|g;
1951 $macrostring =~ s|%{perl_vendorarch}|$Config{installvendorarch}|g;
1952
1953 } # done with globals section
1954
1955 return $macrostring;
1956} # end expandmacros()
1957
1958
1959
1960__END__
1961
1962
1963
1964=head1 NAME
1965
1966debbuild - Build Debian-compatible .deb packages from RPM .spec files
1967
1968=head1 SYNOPSIS
1969
1970 debbuild {-ba|-bb|-bp|-bc|-bi|-bl|-bs} [build-options] file.spec
1971
1972 debbuild {-ta|-tb|-tp|-tc|-ti|-tl|-ts} [build-options] file.tar.{gz|bz2|xz}
1973
1974 debbuild --rebuild file.{src.rpm|sdeb}
1975
1976 debbuild --showpkgs
1977
1978=head1 DESCRIPTION
1979
1980debbuild attempts to build Debian-friendly semi-native packages from RPM spec files,
1981RPM-friendly tarballs, and RPM source packages (.src.rpm files). It accepts I<most> of the
1982options rpmbuild does, and should be able to interpret most spec files usefully. Perl
1983modules should be handled via CPAN+dh-make-perl instead as it's simpler than even tweaking
1984a .spec template.
1985
1986As far as possible, the command-line options are identical to those from rpmbuild, although
1987several rpmbuild options are not supported:
1988
1989 --recompile
1990 --showrc
1991 --buildroot
1992 --clean
1993 --nobuild
1994 --rmsource
1995 --rmspec
1996 --sign
1997 --target
1998
1999Some of these could probably be trivially added. Feel free to send me a patch. ;)
2000
2001Complex spec files will most likely not work well, if at all. Rewrite them from scratch -
2002you'll have to make heavy modifications anyway.
2003
2004If you see something you don't like, mail me. Send a patch if you feel inspired. I don't
2005promise I'll do anything other than say "Yup, that's broken" or "Got your message".
2006
2007=head1 ASSUMPTIONS
2008
2009As with rpmbuild, debbuild makes some assumptions about your system.
2010
2011=over 4
2012
2013=item *
2014
2015Either you have rights to do as you please under /usr/src/debian, or you have created a file
2016~/.debmacros containing a suitable %_topdir definition.
2017
2018Both rpmbuild and debbuild require the directories %_topdir/{BUILD,SOURCES,SPECS}. However,
2019where rpmbuild requires the %_topdir/{RPMS,SRPMS} directories, debbuild
2020requires %_topdir/{DEBS,SDEBS} instead. Create them in advance;
2021some subdirectories are created automatically as needed, but most are not.
2022
2023=item *
2024
2025/var/tmp must allow script execution - rpmbuild and debbuild both rely on creating and
2026executing shell scripts for much of their functionality. By default, debbuild also creates
2027install trees under /var/tmp - however this is (almost) entirely under the control of the
2028package's .spec file.
2029
2030=item *
2031
2032If you wish to --rebuild a .src.rpm, your %_topdir for both debbuild and rpmbuild must either
2033match, or be suitably symlinked one direction or another so that both programs are effectively
2034working in the same tree. (Or you could just manually wrestle files around your system.)
2035
2036You could symlink ~/.rpmmacros to ~/.debmacros (or vice versa) and save yourself some hassle
2037if you need to rebuild .src.rpm packages on a regular basis. Currently debbuild only uses the
2038%_topdir macro definition, although there are many more things that rpmbuild can use from
2039~/.rpmmacros.
2040
2041=back
2042
2043=head1 ERRATA
2044
2045debbuild deliberately does a few things differently from rpm.
2046
2047=head2 BuildArch or BuildArchitecture
2048
2049rpm takes the last BuildArch entry it finds in the .spec file, whatever it is, and runs with
2050that for all packages. Debian's repository system is fairly heavily designed around the
2051assumption that a single source package may generate small binary (executable) packages
2052for each arch, and large binary arch-all packages containing shared data.
2053
2054debbuild allows this by using the architecture specified by (in order of preference):
2055
2056=over 4
2057
2058=item * Host architecture
2059
2060=item * BuildArch specified in .spec file preamble
2061
2062=item * "Last specified" BuildArch for packages with several subpackages
2063
2064=item * BuildArch specified in the %package section for that subpackage
2065
2066=back
2067
2068=head2 Finding out what packages should be built (--showpkgs)
2069
2070rpmbuild does not include any convenient method I know of to list the packages a spec file
2071will produce. Since I needed this ability for another tool, I added it.
2072
2073It requires the .spec file for the package, and produces a list of full package filenames
2074(without path data) that would be generated by one of --rebuild, -ta, -tb, -ba, or -bb.
2075This includes the .sdeb source package.
2076
2077=head1 AUTHOR
2078
2079debbuild was written by Kris Deugau <kdeugau@deepnet.cx>. A version that approximates
2080current is available at http://www.deepnet.cx/debbuild/.
2081
2082=head1 BUGS
2083
2084Funky Things Happen if you forget a command-line option or two. I've been too lazy to bother
2085fixing this.
2086
2087Many macro expansions are unsupported or incompletely supported.
2088
2089The generated scriptlets don't quite match those from rpmbuild exactly. There are extra
2090environment variables and preprocessing that I haven't needed (yet).
2091
2092Dcumentation, such as it is, will likely remain perpetually out of date.
2093
2094%_topdir and the five "working" directories under %_topdir could arguably be created by
2095debbuild. However, rpmbuild doesn't create these directories either.
2096
2097=head1 SEE ALSO
2098
2099rpm(8), rpmbuild(8), and pretty much any document describing how to write a .spec file.
2100
2101=cut
Note: See TracBrowser for help on using the repository browser.