source: trunk/debbuild@ 162

Last change on this file since 162 was 162, checked in by kdeugau, 12 years ago

/trunk

Tweak and fiddle %{debdist} generation, since a few new Debian releases
have happened

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