source: trunk/debbuild@ 139

Last change on this file since 139 was 139, checked in by kdeugau, 16 years ago

/trunk

Whoops, gotta ignore libc on x86_64 as well as x86

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