source: trunk/debbuild@ 149

Last change on this file since 149 was 149, checked in by kdeugau, 15 years ago

/trunk

Allow uncompressed tarballs, and .tgz tarballs
Handle %{SOURCE0} properly (well, er, better...)

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