source: trunk/debbuild@ 187

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

/trunk

Add some missing documentation in the POD about debbuild -i

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