source: trunk/debbuild@ 182

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

/trunk

Major update worth a bump in the major version.

Rewrite handling for the %setup macro/tag after a bit of prodding from
Neal Gompa from the Enano CMS Project. The main addition is support
for the -a and -b flags, along with their related baggage.

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