source: trunk/debbuild@ 189

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

/trunk

Fix handling of %{vendor} vs %{_vendor} as promted by Neal Gompa

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