source: trunk/debbuild@ 171

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

/trunk

Improve handling for Source: entries

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