source: trunk/debbuild@ 71

Last change on this file since 71 was 71, checked in by kdeugau, 18 years ago

/trunk

Add --showpkgs option to dump package list

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 42.6 KB
RevLine 
[2]1#!/usr/bin/perl
2# debbuild script
3# Shamlessly steals intreface from rpm's "rpmbuild" to create
4# Debian packages. Please note that such packages are highly
5# unlikely to conform to "Debian Policy".
6###
7# SVN revision info
8# $Date: 2006-11-15 18:39:51 +0000 (Wed, 15 Nov 2006) $
9# SVN revision $Rev: 71 $
10# Last update by $Author: kdeugau $
11###
[55]12# Copyright 2005,2006 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
[5]32
[10]33# regex debugger
34#use re "debug";
35
[3]36# Program flow:
37# -> Parse/execute "system" config/macros (if any - should be rare)
38# -> Parse/execute "user" config/macros (if any - *my* requirement is %_topdir)
39# -> Parse command line for options, spec file/tarball/.src.deb (NB - also accept .src.rpm)
40
[30]41sub expandmacros;
42
[2]43# User's prefs for dirs, environment, etc,etc,etc.
44# config file ~/.debmacros
45# Default ordered search paths for config/macros:
46# /usr/lib/rpm/rpmrc /usr/lib/rpm/redhat/rpmrc /etc/rpmrc ~/.rpmrc
47# /usr/lib/rpm/macros /usr/lib/rpm/redhat/macros /etc/rpm/macros ~/.rpmmacros
48# **NOTE: May be possible to (ab)use bits of debhelper
49
50# Build tree
51# default is /usr/src/debian/{BUILD,SOURCES,SPECS,DEBS,SDEBS}
52
[3]53# Globals
54my $specfile;
55my $tarball;
56my $srcpkg;
[7]57my $cmdbuildroot;
58my $tarballdir; # This should really be initialized, but the coding makes it, um, ugly.
[23]59my %specglobals; # For %define's in specfile, among other things.
[6]60
61# Initialized globals
[3]62my $verbosity = 0;
[4]63my %cmdopts = (type => '',
64 stage => 'a',
65 short => 'n'
66 );
[6]67my $topdir = "/usr/src/debian";
[13]68my $buildroot = "%{_tmppath}/%{name}-%{version}-%{release}.root".int(rand(99998)+1);
[6]69
70# "Constants"
[4]71my %targets = ('p' => 'Prep',
72 'c' => 'Compile',
73 'i' => 'Install',
74 'l' => 'Verify %files',
75 'a' => 'Build binary and source',
76 'b' => 'Build binary',
77 's' => 'Build source'
78 );
[6]79my $scriptletbase =
80q(#!/bin/sh
[3]81
[6]82 RPM_SOURCE_DIR="%{_topdir}/SOURCES"
83 RPM_BUILD_DIR="%{_topdir}/BUILD"
84 RPM_OPT_FLAGS="-O2 -g -march=i386 -mcpu=i686"
85 RPM_ARCH="i386"
86 RPM_OS="linux"
87 export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS
88 RPM_DOC_DIR="/usr/share/doc"
89 export RPM_DOC_DIR
90 RPM_PACKAGE_NAME="%{name}"
91 RPM_PACKAGE_VERSION="%{version}"
92 RPM_PACKAGE_RELEASE="%{release}"
93 export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE
94 RPM_BUILD_ROOT="%{buildroot}"
95 export RPM_BUILD_ROOT
[10]96);
97foreach (`dpkg-architecture`) {
98 s/=(.+)/="$1"/;
99 $scriptletbase .= " $_";
100}
101$scriptletbase .=
102q(
[6]103 set -x
104 umask 022
105 cd %{_topdir}/BUILD
106);
107
[5]108# Package data
109# This is the form of $pkgdata{pkgname}{meta}
110# meta includes Summary, Name, Version, Release, Group, Copyright,
[38]111# Source, URL, Packager, BuildRoot, Description, BuildReq(uires),
112# Requires, Provides
[6]113# 10/31/2005 Maybe this should be flatter? -kgd
[5]114my %pkgdata;
[27]115my @pkglist = ('main'); #sigh
[23]116# Files listing. Embedding this in %pkgdata would be, um, messy.
117my %filelist;
[40]118my $buildreq = '';
[5]119
[6]120# Scriptlets
[48]121my $prepscript = '';
122my $buildscript = '';
[23]123# %install doesn't need the full treatment from %clean; just an empty place to install to.
124# NB - rpm doesn't do this; is it really necessary?
125my $installscript = '[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT'."\n";
[48]126my $cleanscript = '';
[16]127# pre/post (un)install scripts. Note that these will likely barf as-is. :/
128my $preinstscript = '';
129my $postinstscript = '';
130my $preuninstscript = '';
131my $postuninstscript = '';
[6]132
[5]133die "Not enough arguments\n" if #$argv == 0;
134
[13]135# Snag some environment data
136my $tmpdir;
137if (defined $ENV{TMP} && $ENV{TMP} =~ /^(\/var)?\/tmp$/) {
138 $tmpdir = $ENV{TMP};
139} else {
140 $tmpdir = "/var/tmp";
141}
142
[7]143##main
144
[4]145load_userconfig();
[3]146parse_cmd();
147
[50]148if ($cmdopts{install}) {
149 install_sdeb();
150 exit 0;
151}
152
[71]153if ($cmdopts{type} eq 'd') {
154 parse_spec();
155 foreach my $pkg (@pkglist) {
156 $pkgdata{$pkg}{name} =~ tr/_/-/;
157
158 my $pkgfullname = "$pkgdata{$pkg}{name}_$pkgdata{$pkg}{version}-$pkgdata{main}{release}_i386.deb";
159
160 print "$pkgfullname\n";
161 }
162 exit 0;
163}
164
[50]165# Stick --rebuild handling in here - basically install_sdeb()
166# followed by tweaking options to run with -ba
[54]167if ($cmdopts{type} eq 's') {
[55]168 if ($srcpkg =~ /\.src\.rpm$/) {
[58]169 my @srclist = qx { rpm -qlp $srcpkg };
[55]170 foreach (@srclist) {
[54]171 chomp;
[55]172 $specfile = "$topdir/SPECS/$_" if /\.spec$/;
[54]173 }
[55]174 qx { rpm -i $srcpkg };
175 } else {
176 install_sdeb();
177 my @srclist = qx { pax < $srcpkg };
178 foreach (@srclist) {
179 chomp;
180 $specfile = "$topdir/$_" if /SPECS/;
181 }
[54]182 }
[55]183 $cmdopts{type} = 'b';
184 $cmdopts{stage} = 'a';
[54]185}
[50]186
[7]187if ($cmdopts{type} eq 'b') {
188 # Need to read the spec file to find the tarball. Note that
189 # this also generates most of the shell script required.
190 parse_spec();
[38]191 die "Can't build $pkgdata{main}{name}: build requirements not met.\n"
192 if !checkbuildreq();
[7]193}
194
[65]195if ($cmdopts{type} eq 't') {
196 # Need to unpack the tarball to find the spec file. Sort of the inverse of -b above.
197 # zcat $tarball |tar -t |grep .spec
198 # collect some info about the tarball
199 $specfile = "$topdir/BUILD/". qx { zcat $tarball |tar -t |grep .spec\$ };
200 chomp $specfile;
201 my ($fileonly, $dirname) = ($tarball =~ /(([a-zA-Z0-9._-]+)\.tar\.(?:gz|bz2))$/);
202
203 $tarball = abs_path($tarball);
204 my $unpackcmd = "cd $topdir/BUILD; tar -".
205 ( $tarball =~ /\.tar\.gz$/ ? "z" : "" ).
206 ( $tarball =~ /\.tar\.bz2$/ ? "j" : "" ). "xf $tarball";
207 system "$unpackcmd";
208 system "cp $tarball $topdir/SOURCES/$fileonly";
209 system "cp $specfile $topdir/SPECS/";
210 parse_spec();
211 die "Can't build $pkgdata{main}{name}: build requirements not met.\n"
212 if !checkbuildreq();
213}
214
[35]215# -> srcpkg if -.s
216if ($cmdopts{stage} eq 's') {
217 srcpackage();
218 exit 0;
219}
220
[4]221# Hokay. Need to:
[14]222# -> prep if -.p OR (-.[cilabs] AND !--short-circuit)
[7]223if ($cmdopts{stage} eq 'p' || ($cmdopts{stage} =~ /[cilabs]/ && $cmdopts{short} ne 'y')) {
224 prep();
225}
[14]226# -> build if -.c OR (-.[ilabs] AND !--short-circuit)
[7]227if ($cmdopts{stage} eq 'c' || ($cmdopts{stage} =~ /[ilabs]/ && $cmdopts{short} ne 'y')) {
228 build();
229}
[14]230# -> install if -.[ilabs]
[28]231#if ($cmdopts{stage} eq 'i' || ($cmdopts{stage} =~ /[labs]/ && $cmdopts{short} ne 'y')) {
232if ($cmdopts{stage} =~ /[ilabs]/) {
[7]233 install();
[30]234#foreach my $pkg (@pkglist) {
235# print "files in $pkg:\n ".$filelist{$pkg}."\n";
236#}
237
[7]238}
[14]239# -> binpkg and srcpkg if -.a
[7]240if ($cmdopts{stage} eq 'a') {
[4]241 binpackage();
[7]242 srcpackage();
[4]243}
[14]244# -> binpkg if -.b
[7]245if ($cmdopts{stage} eq 'b') {
246 binpackage();
247}
[4]248
249# Just in case.
250exit 0;
251
252
253## load_userconfig()
254# Loads user configuration (if any)
255# Currently only handles .debmacros
256# Needs to handle "other files"
257sub load_userconfig {
[5]258 my (undef,undef,undef,undef,undef,undef,undef,$homedir,undef) = getpwuid($<);
[4]259 if (-e "$homedir/.debmacros") {
260 open USERMACROS,"<$homedir/.debmacros";
261 while (<USERMACROS>) {
[7]262 # And we also only handle a few macros at the moment.
[4]263 if (/^\%_topdir/) {
[5]264 my (undef,$tmp) = split /\s+/, $_;
265 $topdir = $tmp;
[4]266 }
267 }
268 }
269} # end load_userconfig()
270
271
[3]272## parse_cmd()
[4]273# Parses command line into global hash %cmdopts, other globals
[3]274# Options based on rpmbuild's options
275sub parse_cmd {
276 # Don't feel like coding my own option parser...
277 #use Getopt::Long;
278 # ... but I may have to: (OTOH, rpm uses popt, so maybe we can too.)
279 #use Getopt::Popt qw(:all);
[4]280 # Or not. >:( Stupid Debian lack of findable Perl module names in packages.
[3]281
282 # Stuff it.
[4]283 my $prevopt = '';
[3]284 foreach (@ARGV) {
285 chomp;
[7]286
[3]287 # Is it an option?
288 if (/^-/) {
[7]289
[3]290 # Is it a long option?
291 if (/^--/) {
[4]292 if (/^--short-circuit/) {
293 $cmdopts{short} = 'y';
[5]294 } elsif (/^--rebuild/) {
295 $cmdopts{type} = 's';
[71]296 } elsif (/^--showpkgs/) {
297 $cmdopts{type} = 'd'; # d for 'diagnostic' or 'debug' or 'dump'
[4]298 } else {
299 print "Long opt $_\n";
300 }
[3]301 } else {
[14]302 # Not a long option
[5]303 if (/^-[bt]/) {
304 if ($cmdopts{stage} eq 's') {
305 # Mutually exclusive options.
306 die "Can't use $_ with --rebuild\n";
307 } else {
[14]308 # Capture the type (from "bare" files or tarball) and the stage (prep, build, etc)
[5]309 ($cmdopts{stage}) = (/^-[bt]([pcilabs])/);
310 ($cmdopts{type}) = (/^-([bt])[pcilabs]/);
311 }
[4]312 } elsif (/^-v/) {
313 # bump verbosity. Not sure what I'll actually do here...
[50]314 } elsif (/^-i/) {
315 $cmdopts{install} = 1;
316 $prevopt = '-i';
[4]317 } else {
318 die "Bad option $_\n";
319 }
[3]320 }
[14]321
322 } else { # Not an option argument
323
[5]324 # --buildroot is the only option that takes an argument
325 # Therefore, any *other* bare arguments are the spec file,
326 # tarball, or source package we're operating on - depending
327 # on which one we meet.
328 if ($prevopt eq '--buildroot') {
[7]329 $cmdbuildroot = $_;
[50]330 } elsif ($prevopt eq '-i') {
331 $srcpkg = $_;
[5]332 } else {
333 if ($cmdopts{type} eq 's') {
334 # Source package
[50]335 if (!/(sdeb|\.src\.rpm)$/) {
[5]336 die "Can't --rebuild with $_\n";
337 }
[50]338 $srcpkg = $_;
[71]339 } elsif ($cmdopts{type} eq 'b' || $cmdopts{type} eq 'd') {
[65]340 # Spec file
[5]341 $specfile = $_;
342 } else {
[65]343 # Tarball build. Need to extract tarball to find spec file. Whee.
344 $tarball = $_;
[5]345 }
346 }
[3]347 }
[7]348 $prevopt = $_;
349 } # foreach @ARGV
[4]350
351 # Some cross-checks. rpmbuild limits --short-circuit to just
352 # the "compile" and "install" targets - with good reason IMO.
[14]353 # Note that --short-circuit with -.p is not really an error, just redundant.
[4]354 # NB - this is NOT fatal, just ignored!
[10]355 if ($cmdopts{short} eq 'y' && $cmdopts{stage} =~ /[labs]/) {
[4]356 warn "Can't use --short-circuit for $targets{$cmdopts{stage}} stage. Ignoring.\n";
[28]357 $cmdopts{short} = 'n';
[4]358 }
[14]359
[3]360 # Valid options, with example arguments (if any):
361# Build from .spec file; mutually exclusive:
362 # -bp
363 # -bc
364 # -bi
365 # -bl
366 # -ba
367 # -bb
368 # -bs
369# Build from tarball; mutually exclusive:
370 # -tp
371 # -tc
372 # -ti
373 # -ta
374 # -tb
375 # -ts
376# Build from .src.(deb|rpm)
377 # --rebuild
378 # --recompile
379
380# General options
381 # --buildroot=DIRECTORY
382 # --clean
383 # --nobuild
384 # --nodeps
385 # --nodirtokens
386 # --rmsource
387 # --rmspec
388 # --short-circuit
389 # --target=CPU-VENDOR-OS
390
391 #my $popt = new Getopt::Popt(argv => \@ARGV, options => \@optionsTable);
392
393} # end parse_cmd()
394
[4]395
[5]396## parse_spec()
[14]397# Parse the .spec file.
[5]398sub parse_spec {
[65]399 open SPECFILE,"<$specfile" or die "specfile ($specfile) barfed: $!";
[5]400
[6]401LINE: while (<SPECFILE>) {
[14]402 next if /^#/; # Ignore comments...
403 next if /^\s+$/; # ... and blank lines.
404
[5]405 if (/^\%/) {
406 # A macro that needs further processing.
[14]407
[60]408 if (my ($key, $def) = (/^\%define\s+([^\s]+)\s+(.+)$/) ) {
409 $specglobals{$key} = expandmacros($def,'g');
[23]410 }
411
[62]412 if (/^\%description(?:\s+(?:-n\s+)?(.+))?/) {
[27]413 my $subname = "main";
414 if ($1) {
[62]415 my $tmp = expandmacros("$1", 'g');
[49]416 if (/-n/) { $subname = $tmp; } else { $subname = "$pkgdata{main}{name}-$tmp"; }
[27]417 }
[8]418 while (<SPECFILE>) {
[39]419 next if /^#/; # Messy. Should be possible to do better. :/
[8]420 redo LINE if /^\%/;
[27]421 $pkgdata{$subname}{desc} .= " $_";
[8]422 }
[18]423 }
[62]424 if (/^\%package\s+(?:-n\s+)?(.+)/) {
425 # gotta expand %defines here. Whee.
426 my $subname = expandmacros("$1", 'g');
[49]427 if (! /-n/) { $subname = "$pkgdata{main}{name}-$1"; }
[27]428 push @pkglist, $subname;
429 $pkgdata{$subname}{name} = $subname;
430 $pkgdata{$subname}{version} = $pkgdata{main}{version};
431 while (<SPECFILE>) {
432 redo LINE if /^\%/;
[38]433 if (my ($dname,$dvalue) = (/^(Summary|Group|Version|Requires|Provides):\s+(.+)$/i)) {
[27]434 $dname =~ tr/[A-Z]/[a-z]/;
[63]435 $pkgdata{$subname}{$dname} = expandmacros($dvalue, 'gp');
[27]436 }
437 }
438 }
[8]439
[6]440 if (/^\%prep/) {
441 # %prep section. May have %setup macro; may include %patch tags,
442 # may be just a bare shell script.
443
444 # This really should be local-ish, but we need just the filename for the source
445 $pkgdata{main}{source} =~ s|.+/([^/]+)$|$1|;
446
447 # Replace some core macros
[14]448 $pkgdata{main}{source} = expandmacros($pkgdata{main}{source},'gp');
[6]449
450PREPSCRIPT: while (<SPECFILE>) {
451 if (/^\%setup/) {
452 # Parse out the %setup macro. Note that we aren't supporting
[14]453 # many of RPM's %setup features.
[7]454 $prepscript .= "cd $topdir/BUILD\n";
455 if ( /\s+-n\s+([^\s]+)\s+/ ) {
456 $tarballdir = $1;
457 } else {
458 $tarballdir = "$pkgdata{main}{name}-$pkgdata{main}{version}";
459 }
[69]460 $tarballdir = expandmacros($tarballdir,'gp');
461 $prepscript .= "rm -rf $tarballdir\n";
462 if (/\s+-c\s+/) {
463 $prepscript .= "mkdir $tarballdir\ncd $tarballdir\n";
464 }
465 $prepscript .= "tar -".
[6]466 ( $pkgdata{main}{source} =~ /\.tar\.gz$/ ? "z" : "" ).
467 ( $pkgdata{main}{source} =~ /\.tar\.bz2$/ ? "j" : "" ).
[21]468 ( /\s+-q\s+/ ? '' : 'vv' )."xf ".
469 "$topdir/SOURCES/$pkgdata{main}{source}\n".
[6]470 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n).
[69]471 "cd $topdir/BUILD/$tarballdir\n".
[6]472 qq([ `/usr/bin/id -u` = '0' ] && /bin/chown -Rhf root .\n).
473 qq([ `/usr/bin/id -u` = '0' ] && /bin/chgrp -Rhf root .\n).
474 qq(/bin/chmod -Rf a+rX,g-w,o-w .\n);
[67]475 } elsif ( my ($patchnum,$patchopts) = (/^\%patch([^\s]+)(\s+.+)?$/) ) {
476 chomp $patchnum;
[70]477 $prepscript .= qq(echo "Patch #$patchnum ($pkgdata{main}{"patch$patchnum"}):"\n).
478 "patch ";
479 # If there are options passed, use'em.
480 # Otherwise, catch a bare %patch and ASS-U-ME it's '-p0'-able.
481 # Will break on options that don't provide -pnn, but what the hell.
[66]482 $prepscript .= $patchopts if $patchopts;
[70]483 $prepscript .= "-p0" if !$patchopts;
484 $prepscript .= " -s <$topdir/SOURCES/".$pkgdata{main}{"patch$patchnum"}."\n";
[6]485 } else {
486 last PREPSCRIPT if /^\%/;
487 $prepscript .= $_;
488 }
489 }
490 redo LINE;
491 }
[12]492 if (/^\%build/) {
[6]493 # %build. This is pretty much just a shell script. There
494 # *are* a few macros, but we're not going to deal with them yet.
[7]495 $buildscript .= "cd $tarballdir\n";
[10]496BUILDSCRIPT: while (<SPECFILE>) {
497 if (/^\%configure/) {
[12]498 $buildscript .= expandmacros($_,'cgbp');
[25]499 } elsif (/^\%\{__make\}/) {
[12]500 $buildscript .= expandmacros($_,'mgbp');
[10]501 } else {
[24]502 last BUILDSCRIPT if /^\%[^{]/;
[10]503 $buildscript .= $_;
504 }
[6]505 }
[10]506 redo LINE;
[5]507 }
[6]508 if (/^\%install/) {
[7]509 $installscript .= "cd $tarballdir\n";
[12]510INSTALLSCRIPT: while (<SPECFILE>) {
511 if (/^\%makeinstall/) {
512 $installscript .= expandmacros($_,'igbp');
513 } else {
514 last INSTALLSCRIPT if /^\%/;
515 $installscript .= $_;
516 }
517 }
518 redo LINE;
[6]519 }
520 if (/^\%clean/) {
[7]521 while (<SPECFILE>) {
522 redo LINE if /^\%/;
523 $cleanscript .= $_;
524 }
[12]525 $cleanscript = expandmacros($cleanscript,'gp');
[6]526 }
[29]527
[16]528 # pre/post (un)install scripts
529 if (/^\%pre\b/) {
530 while (<SPECFILE>) {
531 redo LINE if /^\%/;
532 $preinstscript .= $_;
533 }
[6]534 }
[16]535 if (/^\%post\b/) {
536 while (<SPECFILE>) {
537 redo LINE if /^\%/;
538 $postinstscript .= $_;
539 }
[6]540 }
[16]541 if (/^\%preun\b/) {
542 while (<SPECFILE>) {
543 redo LINE if /^\%/;
544 $preuninstscript .= $_;
545 }
546 }
547 if (/^\%postun\b/) {
548 while (<SPECFILE>) {
549 redo LINE if /^\%/;
550 $postuninstscript .= $_;
551 }
552 }
[29]553 # done %pre/%post scripts
554
[62]555 if (/^\%files(?:\s+(?:-n\s+)?(.+))?/) {
[23]556 my $pkgname = 'main';
[29]557 if ($1) { # Magic to add entries to the right list of files
[62]558 my $tmp = expandmacros("$1", 'g');
[49]559 if (/-n/) { $pkgname = $tmp; } else { $pkgname = "$pkgdata{main}{name}-$tmp"; }
[23]560 }
[37]561
562 # Set this now, so it can be flipped a bit later, and used much later.
563 #$pkgdata{$pkgname}{conffiles} = 0;
564
[27]565 while (<SPECFILE>) {
[29]566 chomp;
[45]567 next if /^#/;
[29]568 # need to update this to deal (properly) with %dir, %attr, etc
569 next if /^\%dir/;
570 next if /^\%attr/;
571 next if /^\%defattr/;
572
[37]573 # Debian dpkg doesn't speak "%docdir". Meh.
574 next if /^\%docdir/;
575
576 # Conffiles. Note that Debian and RH have similar, but not
577 # *quite* identical ideas of what constitutes a conffile. Nrgh.
578 if (/^\%config\s+(.+)$/) {
579 $pkgdata{$pkgname}{conffiles} = 1; # Flag it for later
580 my $tmp = $1; # Now we can mangleificationate it. And we probably need to. :/
[46]581 $tmp = expandmacros($tmp, 'gp'); # Expand common macros
[37]582 if ($tmp !~ /\s+/) {
583 # Simplest case, just a file. Whew.
584 push @{$pkgdata{$pkgname}{conflist}}, $tmp;
585 $filelist{$pkgname} .= " $tmp";
586 } else {
587 # Wot? Spaces? That means extra %-macros. Which, for the most part, can be ignored.
588 ($tmp) = ($tmp =~ /.+\s([^\s]+)/); # Strip everything before the last space
589 push @{$pkgdata{$pkgname}{conflist}}, $tmp;
590 $filelist{$pkgname} .= " $tmp";
591 }
592 next;
593 }
594
[29]595 # and finally we can fall through %{_<FHS>}-prefixed locations...
[30]596 if (/^\%\{_/) {
597 $filelist{$pkgname} .= " $_";
598 next;
599 }
600 # EW. Necessary to clear up %define expansions before we exit with redo.
601 $_ = expandmacros $_, 'g';
[29]602
603 # ... unknown or "next section" % directives ...
[27]604 redo LINE if /^\%/;
[29]605
606 # ... and "normal" files
[27]607 $filelist{$pkgname} .= " $_";
608 }
[46]609 $filelist{$pkgname} = expandmacros($filelist{$pkgname}, 'gp');
[29]610 } # done %file section
611
[18]612 if (/^\%changelog/) {
613 $pkgdata{main}{changelog} = '';
614 while (<SPECFILE>) {
615 redo LINE if /^\%/;
616 $pkgdata{main}{changelog} .= $_;
617 }
618 }
[14]619
620 } else { # Data from the spec file "header"
621
[5]622 if (/^summary:\s+(.+)/i) {
623 $pkgdata{main}{summary} = $1;
624 } elsif (/^name:\s+(.+)/i) {
[24]625 $pkgdata{main}{name} = expandmacros($1,'g');
[5]626 } elsif (/^version:\s+(.+)/i) {
[24]627 $pkgdata{main}{version} = expandmacros($1,'g');
[5]628 } elsif (/^release:\s+(.+)/i) {
[24]629 $pkgdata{main}{release} = expandmacros($1,'g');
[5]630 } elsif (/^group:\s+(.+)/i) {
631 $pkgdata{main}{group} = $1;
632 } elsif (/^copyright:\s+(.+)/i) {
633 $pkgdata{main}{copyright} = $1;
634 } elsif (/^url:\s+(.+)/i) {
635 $pkgdata{main}{url} = $1;
636 } elsif (/^packager:\s+(.+)/i) {
637 $pkgdata{main}{packager} = $1;
[7]638 } elsif (/^buildroot:\s+(.+)/i) {
639 $buildroot = $1;
[59]640 } elsif (/^source0?:\s+(.+)/i) {
[5]641 $pkgdata{main}{source} = $1;
[6]642 die "Unknown tarball format $1\n" if $1 !~ /\.tar\.(?:gz|bz2)$/;
[15]643 } elsif (/^source([0-9]+):\s+(.+)/i) {
644 $pkgdata{sources}{$1} = $2;
[10]645 } elsif (/^patch([^:]+):\s+(.+)$/i) {
[26]646 my $patchname = "patch$1";
647 $pkgdata{main}{$patchname} = $2;
648 if ($pkgdata{main}{$patchname} =~ /\//) {
649 # URL-style patch. Rare but not unheard-of.
650 my @patchbits = split '/', $pkgdata{main}{$patchname};
651 $pkgdata{main}{$patchname} = $patchbits[$#patchbits];
652 }
[66]653 chomp $pkgdata{main}{$patchname};
[38]654 } elsif (/^buildreq(?:uires)?:\s+(.+)/i) {
655 $buildreq .= ", $1";
656 } elsif (/^requires:\s+(.+)/i) {
[63]657 $pkgdata{main}{requires} .= ", ".expandmacros("$1", 'gp');
[38]658 } elsif (/^provides:\s+(.+)/i) {
659 $pkgdata{main}{provides} .= ", $1";
[51]660 } elsif (/^conflicts:\s+(.+)/i) {
661 $pkgdata{main}{conflicts} .= ", $1";
[5]662 }
663#Name: suwrap
664#Version: 0.04
665#Release: 3
666#Group: Applications/System
667#Copyright: WebHart internal ONLY. :(
668#BuildArchitectures: i386
669#BuildRoot: /tmp/%{name}-%{version}
670#Url: http://virtual.webhart.net
671#Packager: Kris Deugau <kdeugau@deepnet.cx>
672#Source: ftp://virtual.webhart.net/%{name}-%{version}.tar.gz
673
674 }
675 }
[7]676
[14]677 # Parse and replace some more macros. More will be replaced even later.
678
[10]679 # Expand macros as necessary.
[12]680 $scriptletbase = expandmacros($scriptletbase,'gp');
[7]681
682 $buildroot = $cmdbuildroot if $cmdbuildroot;
[12]683 $buildroot = expandmacros($buildroot,'gp');
[7]684
[16]685 close SPECFILE;
[5]686} # end parse_spec()
687
688
[7]689## prep()
[14]690# Writes and executes the %prep script (mostly) built while reading the spec file.
[7]691sub prep {
[10]692 # Replace some things here just to make sure.
[12]693 $prepscript = expandmacros($prepscript,'gp');
[10]694
[24]695#print $prepscript; exit 0;
[23]696
[10]697 # create script filename
[13]698 my $prepscriptfile = "$tmpdir/deb-tmp.prep.".int(rand(99998)+1);
[10]699 sysopen(PREPSCRIPT, $prepscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
[7]700 or die $!;
[10]701 print PREPSCRIPT $scriptletbase;
702 print PREPSCRIPT $prepscript;
703 close PREPSCRIPT;
[7]704
[10]705 # execute
706 print "Calling \%prep script $prepscriptfile...\n";
707 system("/bin/sh -e $prepscriptfile") == 0
[14]708 or die "Can't exec: $!\n";
[7]709
[10]710 # and clean up
711 unlink $prepscriptfile;
[7]712} # end prep()
713
714
[5]715## build()
[14]716# Writes and executes the %build script (mostly) built while reading the spec file.
[5]717sub build {
[12]718 # Expand the macros
719 $buildscript = expandmacros($buildscript,'cgbp');
720
[7]721 # create script filename
[13]722 my $buildscriptfile = "$tmpdir/deb-tmp.build.".int(rand(99998)+1);
[7]723 sysopen(BUILDSCRIPT, $buildscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
724 or die $!;
725 print BUILDSCRIPT $scriptletbase;
726 print BUILDSCRIPT $buildscript;
727 close BUILDSCRIPT;
728
729 # execute
730 print "Calling \%build script $buildscriptfile...\n";
731 system("/bin/sh -e $buildscriptfile") == 0
[14]732 or die "Can't exec: $!\n";
[7]733
734 # and clean up
735 unlink $buildscriptfile;
[5]736} # end build()
737
738
[7]739## install()
[14]740# Writes and executes the %install script (mostly) built while reading the spec file.
[7]741sub install {
[12]742 # Expand the macros
743 $installscript = expandmacros($installscript,'igbp');
744
[7]745 # create script filename
[13]746 my $installscriptfile = "$tmpdir/deb-tmp.inst.".int(rand(99998)+1);
[7]747 sysopen(INSTSCRIPT, $installscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
748 or die $!;
749 print INSTSCRIPT $scriptletbase;
[23]750# print INSTSCRIPT $cleanscript; # Clean up our install target before installing into it.
[7]751 print INSTSCRIPT $installscript;
752 close INSTSCRIPT;
753
754 # execute
755 print "Calling \%install script $installscriptfile...\n";
756 system("/bin/sh -e $installscriptfile") == 0
[14]757 or die "Can't exec: $!\n";
[7]758
759 # and clean up
760 unlink $installscriptfile;
761} # end install()
762
763
[8]764## binpackage()
765# Creates the binary .deb package from the installed tree in $buildroot.
[14]766# Writes and executes a shell script to do so.
767# Creates miscellaneous files required by dpkg-deb to actually build the package file.
[27]768# Should handle simple subpackages
[8]769sub binpackage {
[14]770 # Make sure we have somewhere to write the .deb file
[9]771 if (!-e "$topdir/DEBS/i386") {
772 mkdir "$topdir/DEBS/i386";
773 }
[14]774
[27]775 foreach my $pkg (@pkglist) {
[8]776
[27]777 # Gotta do this first, otherwise we don't have a place to move files from %files
778 mkdir "$buildroot/$pkg";
[28]779
[30]780 # Eliminate any lingering % macros
781 $filelist{$pkg} = expandmacros $filelist{$pkg}, 'g';
782
[27]783 my @pkgfilelist = split ' ', $filelist{$pkg};
784 foreach my $pkgfile (@pkgfilelist) {
[37]785 $pkgfile = expandmacros($pkgfile, 'gp');
[44]786 my ($fpath,$fname) = ($pkgfile =~ m|(.+?/?)?([^/]+)$|); # We don't need $fname now, but we might.
787 qx { mkdir -p $buildroot/$pkg$fpath }
788 if $fpath && $fpath ne '';
789 qx { mv $buildroot$pkgfile $buildroot/$pkg$fpath };
[27]790 }
[28]791
[38]792 # Get the "Depends" (Requires) a la RPM. Ish. We strip the leading
793 # comma and space here (if needed) in case there were "Requires" specified
794 # in the spec file - those would precede these.
[64]795 $pkgdata{$pkg}{requires} .= getreqs("$buildroot/$pkg");
[38]796
[64]797 # magic needed to properly version dependencies...
798 # only provided deps will really be included
[65]799 $pkgdata{$pkg}{requires} =~ s/^, //; # Still have to do this here.
[64]800 $pkgdata{$pkg}{requires} =~ s/\s+//g;
801 my @deps = split /,/, $pkgdata{$pkg}{requires};
802 my $tmp = '';
803 foreach my $dep (@deps) {
[68]804 # Hack up the perl(Class::SubClass) deps into something dpkg can understand.
805 # May or may not be versioned.
806 # We do this first so the version rewriter can do its magic next.
807 if (my ($mod,$ver) = ($dep =~ /^perl\(([A-Za-z0-9\:\-]+)\)([><=]+.+)?/) ) {
808 $mod =~ s/^perl\(//;
809 $mod =~ s/\)$//;
810 $mod =~ s/::/-/g;
811 $mod =~ tr/A-Z/a-z/;
812 $mod = "lib$mod-perl";
813 $mod .= $ver if $ver;
814 $dep = $mod;
815 }
[64]816 if (my ($name,$rel,$value) = ($dep =~ /^([a-zA-Z0-9._-]+)([><=]+)([a-zA-Z0-9._-]+)$/)) {
817 $tmp .= ", $name ($rel $value)";
818 } else {
819 $tmp .= ", $dep";
820 }
821 }
822 ($pkgdata{$pkg}{requires} = $tmp) =~ s/^, //;
823
[38]824 # Do this here since we're doing {depends}...
[41]825 if (defined($pkgdata{$pkg}{provides})) {
826 $pkgdata{$pkg}{provides} =~ s/^, //;
827 $pkgdata{$pkg}{provides} = expandmacros($pkgdata{$pkg}{provides},'gp');
828 }
[51]829 if (defined($pkgdata{$pkg}{conflicts})) {
830 $pkgdata{$pkg}{conflicts} =~ s/^, //;
831 $pkgdata{$pkg}{conflicts} = expandmacros($pkgdata{$pkg}{conflicts},'gp');
832 }
[38]833
[27]834 # Gotta do this next, otherwise the control file has nowhere to go. >:(
835 mkdir "$buildroot/$pkg/DEBIAN";
836
[33]837 # Hack the filename for the package into a Debian-tool-compatible format. GRRRRRR!!!!!
838 # Have I mentioned I hate Debian Policy?
839 $pkgdata{$pkg}{name} =~ tr/_/-/;
840
[27]841 # create script filename
842 my $debscriptfile = "$tmpdir/deb-tmp.pkg.".int(rand(99998)+1);
843 sysopen(DEBSCRIPT, $debscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
[8]844 or die $!;
[27]845 print DEBSCRIPT $scriptletbase;
846 print DEBSCRIPT "fakeroot dpkg-deb -b $buildroot/$pkg $topdir/DEBS/i386/".
847 "$pkgdata{$pkg}{name}_$pkgdata{$pkg}{version}-$pkgdata{main}{release}_i386.deb\n";
848 # %$&$%@#@@#%@@@ Debian and their horrible ugly package names. >:(
849 close DEBSCRIPT;
[8]850
[27]851 my $control = "Package: $pkgdata{$pkg}{name}\n".
852 "Version: $pkgdata{$pkg}{version}-$pkgdata{main}{release}\n".
853 "Section: $pkgdata{$pkg}{group}\n".
[8]854 "Priority: optional\n".
855 "Architecture: i386\n".
856 "Maintainer: $pkgdata{main}{packager}\n".
[47]857 ( $pkgdata{$pkg}{requires} ne '' ? "Depends: $pkgdata{$pkg}{requires}\n" : '' ).
[39]858 ( defined($pkgdata{$pkg}{provides}) ? "Provides: $pkgdata{$pkg}{provides}\n" : '' ).
[51]859 ( defined($pkgdata{$pkg}{conflicts}) ? "Conflicts: $pkgdata{$pkg}{conflicts}\n" : '' ).
[27]860 "Description: $pkgdata{$pkg}{summary}\n";
861 $control .= "$pkgdata{$pkg}{desc}\n";
[8]862
[27]863 open CONTROL, ">$buildroot/$pkg/DEBIAN/control";
864 print CONTROL $control;
865 close CONTROL;
[8]866
[37]867 # Iff there are conffiles (as specified in the %files list(s), add'em
868 # in so dpkg-deb can tag them.
869 if ($pkgdata{$pkg}{conffiles}) {
870 open CONFLIST, ">$buildroot/$pkg/DEBIAN/conffiles";
871 foreach my $conffile (@{$pkgdata{$pkg}{conflist}}) {
872 print CONFLIST "$conffile\n";
873 }
874 close CONFLIST;
875 }
876
[32]877 # Can't see much point in scripts on subpackages... although since
878 # it's *possible* I should support it at some point.
879 if ($pkg eq 'main') {
880 if ($preinstscript ne '') {
[34]881 $preinstscript = expandmacros($preinstscript,'g');
[32]882 open PREINST, ">$buildroot/$pkg/DEBIAN/preinst";
883 print PREINST "#!/bin/sh\nset -e\n\n";
884 print PREINST $preinstscript;
885 close PREINST;
886 `chmod 0755 $buildroot/$pkg/DEBIAN/preinst`;
887 }
888 if ($postinstscript ne '') {
[34]889 $postinstscript = expandmacros($postinstscript,'g');
[32]890 open POSTINST, ">$buildroot/$pkg/DEBIAN/postinst";
891 print POSTINST "#!/bin/sh\nset -e\n\n";
892 print POSTINST $postinstscript;
893 close POSTINST;
894 `chmod 0755 $buildroot/$pkg/DEBIAN/postinst`;
895 }
896 if ($preuninstscript ne '') {
[34]897 $preuninstscript = expandmacros($preuninstscript,'g');
[32]898 open PREUNINST, ">$buildroot/$pkg/DEBIAN/prerm";
899 print PREUNINST "#!/bin/sh\nset -e\n\n";
900 print PREUNINST $preuninstscript;
901 close PREUNINST;
902 `chmod 0755 $buildroot/$pkg/DEBIAN/prerm`;
903 }
904 if ($postuninstscript ne '') {
[34]905 $postuninstscript = expandmacros($postuninstscript,'g');
[32]906 open POSTUNINST, ">$buildroot/$pkg/DEBIAN/postrm";
907 print POSTUNINST "#!/bin/sh\nset -e\n\n";
908 print POSTUNINST $postuninstscript;
909 close POSTUNINST;
910 `chmod 0755 $buildroot/$pkg/DEBIAN/postrm`;
911 }
[27]912 }
[16]913
[27]914 # execute
915 print "Calling package creation script $debscriptfile for $pkgdata{$pkg}{name}...\n";
916 system("/bin/sh -e $debscriptfile") == 0
[14]917 or die "Can't exec: $!\n";
[8]918
[27]919 # and clean up
920 unlink $debscriptfile;
921
922 } # subpackage loop
923
[8]924} # end binpackage()
925
926
[35]927## srcpackage()
928# Builds a .src.deb source package. Note that Debian's idea of
929# a "source package" is seriously flawed IMO, because you can't
930# easily copy it as-is.
[36]931# Not quite identical to RPM, but Good Enough (TM).
[35]932sub srcpackage {
[54]933 # In case we were called with -bs.
934 $pkgdata{main}{name} =~ tr/_/-/;
[36]935 my $pkgsrcname = "$pkgdata{main}{name}-$pkgdata{main}{version}-$pkgdata{main}{release}.sdeb";
936
937 my $paxcmd;
938
939 # We'll definitely need this later, and *may* need it sooner.
940 (my $barespec = $specfile) =~ s|.+/([^/]+)$|$1|;
941
942 # Copy the specfile to the build tree, but only if it's not there already.
943##buglet: need to deal with silly case where silly user has put the spec
944# file in a subdir of %{_topdir}/SPECS. Ewww. Silly user!
945 if (abs_path($specfile) !~ /^$topdir\/SPECS/) {
946 $paxcmd .= "cp $specfile %{_topdir}/SPECS/; \n"
947 }
948
[35]949 # use pax -w [file] [file] ... >outfile.sdeb
[36]950 $paxcmd = "cd $topdir; pax -w ";
[10]951
[35]952# tweak source entry into usable form. Need it locally somewhere along the line.
953 (my $pkgsrc = $pkgdata{main}{source}) =~ s|.+/([^/]+)$|$1|;
[36]954 $paxcmd .= "SOURCES/$pkgsrc ";
[10]955
[35]956 # create file list: Source[nn], Patch[nn]
957 foreach my $specbit (keys %{$pkgdata{main}} ) {
958 next if $specbit eq 'source';
[53]959 $paxcmd .= "SOURCES/$pkgdata{main}{$specbit} " if $specbit =~ /^patch/;
[36]960##buglet: need to deal with case where patches are listed as URLs?
961# or other extended pathnames? Silly !@$%^&!%%!%!! user!
[35]962 }
963
[53]964 foreach my $source (keys %{$pkgdata{sources}}) {
965 $paxcmd .= "SOURCES/$pkgdata{sources}{$source} ";
966 }
967
[36]968 # add the spec file, source package destination, and cd back where we came from.
969 $paxcmd .= "SPECS/$barespec > $topdir/SDEBS/$pkgsrcname; cd -";
[35]970
[36]971 # In case of %-macros...
[35]972 $paxcmd = expandmacros($paxcmd,'gp');
973
[36]974 system "$paxcmd";
975 print "Wrote source package $pkgsrcname in $topdir/SDEBS.\n";
[35]976}
977
978
[38]979## checkbuildreq()
980# Checks the build requirements (if any)
981# Spits out a rude warning and returns a true-false error if any
982# requirements are not met.
983sub checkbuildreq {
[40]984 return 1 if $buildreq eq ''; # No use doing extra work.
[38]985
[52]986 if ( ! -e "/usr/bin/dpkg-query" ) {
987 print "**WARNING** dpkg-query not found. Can't check build-deps.\n".
988 " Required for sucessful build:\n".$buildreq."\n".
989 " Continuing anyway.\n";
990 return 1;
991 }
992
[38]993 my $reqflag = 1; # unset iff a buildreq is missing
994
995 $buildreq =~ s/^, //; # Strip the leading comma and space
996 my @reqlist = split /,\s+/, $buildreq;
997
998 foreach my $req (@reqlist) {
999 my ($pkg,$rel,$ver);
1000
1001 # We have two classes of requirements - versioned and unversioned.
1002 if ($req =~ /[><=]/) {
1003 # Pick up the details of versioned buildreqs
1004 ($pkg,$rel,$ver) = ($req =~ /([a-z0-9._-]+)\s+([><=]+)\s+([a-z0-9._-]+)/);
1005 } else {
1006 # And the unversioned ones.
1007 $pkg = $req;
1008 $rel = '>=';
1009 $ver = 0;
1010 }
1011
1012 my @pkglist = qx { dpkg-query --showformat '\${status}\t\${version}\n' -W $pkg };
1013# need to check if no lines returned - means a bad buildreq
1014 my ($reqstat,undef,undef,$reqver) = split /\s+/, $pkglist[0];
1015 if ($reqstat !~ /install/) {
1016 print " * Missing build-dependency $pkg!\n";
1017 $reqflag = 0;
1018 } else {
1019# gotta be a better way to do this... :/
1020 if ($rel eq '>=' && !($reqver ge $ver)) {
1021 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1022 $reqflag = 0;
1023 }
1024 if ($rel eq '>' && !($reqver gt $ver)) {
1025 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1026 $reqflag = 0;
1027 }
1028 if ($rel eq '<=' && !($reqver le $ver)) {
1029 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1030 $reqflag = 0;
1031 }
1032 if ($rel eq '<' && !($reqver lt $ver)) {
1033 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1034 $reqflag = 0;
1035 }
1036 if ($rel eq '=' && !($reqver eq $ver)) {
1037 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1038 $reqflag = 0;
1039 }
1040 } # end not installed/installed check
1041 } # end req loop
1042
1043 return $reqflag;
1044} # end checkbuildreq()
1045
1046
1047## getreqs()
1048# Find out which libraries/packages are required for any
1049# executables and libs in a given file tree.
1050# (Debian doesn't have soname-level deps; just package-level)
[41]1051# Returns an empty string if the tree contains no binaries.
1052# Doesn't work well on shell scripts. but those *should* be
1053# fine anyway. (Yeah, right...)
[38]1054sub getreqs() {
1055 my $pkgtree = $_[0];
1056
[39]1057 print "Checking library requirements...\n";
[43]1058 my @binlist = qx { find $pkgtree -type f -perm 755 };
[38]1059
[41]1060 if (scalar(@binlist) == 0) {
1061 return '';
1062 }
1063
1064 my @reqlist;
1065 foreach (@binlist) {
1066 push @reqlist, qx { ldd $_ };
1067 }
1068
1069 # Get the list of libs provided by this package. Still doesn't
1070 # handle the case where the lib gets stuffed into a subpackage. :/
1071 my @intprovlist = qx { find $pkgtree -type f -name "*.so*" };
1072 my $provlist = '';
1073 foreach (@intprovlist) {
1074 s/$pkgtree//;
1075 $provlist .= "$_";
1076 }
1077
[38]1078 my %reqs;
[43]1079 my $reqlibs = '';
[38]1080
1081 foreach (@reqlist) {
1082 next if /^$pkgtree/;
[42]1083 next if /not a dynamic executable/;
[41]1084 next if m|/lib/ld-linux.so|; # Hack! Hack! PTHBTT! (libc suxx0rz)
[38]1085
[41]1086 my ($req) = (/^\s+([a-z0-9._-]+)/); # dig out the actual library (so)name
1087
1088 # Ignore libs provided by this package. Note that we don't match
1089 # on word-boundary at the *end* of the lib we're looking for, as the
1090 # looked-for lib may not have the full soname version. (ie, it may
1091 # "just" point to one of the symlinks that get created somewhere.)
1092 next if $provlist =~ /\b$req/;
1093
[38]1094 $reqlibs .= " $req";
1095 }
1096
[43]1097 if ($reqlibs ne '') {
1098 foreach (qx { dpkg -S $reqlibs }) {
1099 my ($libpkg,undef) = split /:\s+/;
1100 $reqs{$libpkg} = 1;
1101 }
[38]1102 }
1103
[43]1104 my $deplist = '';
[38]1105 foreach (keys %reqs) {
1106 $deplist .= ", $_";
1107 }
1108
1109# For now, we're done. We're not going to meddle with versions yet.
1110# Among other things, it's messier than handling "simple" yes/no "do
1111# we have this lib?" deps. >:(
1112
1113 return $deplist;
1114} # end getreqs()
1115
1116
[50]1117## install_sdeb()
1118# Extracts .sdeb contents to %_topdir as appropriate
1119sub install_sdeb {
1120 my $paxcmd = "cd $topdir; pax -r <$srcpkg; cd -";
1121
1122 # In case of %-macros...
1123 $paxcmd = expandmacros($paxcmd,'gp');
1124
1125 system "$paxcmd";
1126 print "Extracted source package $srcpkg to $topdir.\n";
1127} # end install_sdeb()
1128
1129
[10]1130## expandmacros()
1131# Expands all %{blah} macros in the passed string
[14]1132# Split up a bit with some sections so we don't spend time trying to
1133# expand macros that are only used in a few specific places.
[10]1134sub expandmacros {
1135 my $macrostring = shift;
[14]1136 my $section = shift;
[10]1137
[25]1138 # To allow the FHS-ish %configure and %makeinstall to work The Right Way.
[12]1139 # (Without clobbering the global $buildroot.)
1140 my $prefix = '';
1141
[11]1142 if ($section =~ /c/) {
[12]1143 # %configure macro
[17]1144# Don't know what it's for, don't have a useful default replacement
1145# --program-prefix=%{_program_prefix} \
1146 $macrostring =~ s'%configure'./configure --host=$DEB_HOST_GNU_TYPE \
1147 --build=$DEB_BUILD_GNU_TYPE \
1148 --prefix=%{_prefix} \
1149 --exec-prefix=%{_exec_prefix} \
1150 --bindir=%{_bindir} \
1151 --sbindir=%{_sbindir} \
1152 --sysconfdir=%{_sysconfdir} \
1153 --datadir=%{_datadir} \
1154 --includedir=%{_includedir} \
1155 --libdir=%{_libdir} \
1156 --libexecdir=%{_libexecdir} \
1157 --localstatedir=%{_localstatedir} \
1158 --sharedstatedir=%{_sharedstatedir} \
1159 --mandir=%{_mandir} \
1160 --infodir=%{_infodir} ';
[12]1161 } # done %configure
1162
1163 if ($section =~ /m/) {
1164 $macrostring =~ s'%{__make}'make ';
[25]1165 } # done make
[12]1166
1167 if ($section =~ /i/) {
1168 # This is where we need to mangle $prefix.
[24]1169 $macrostring =~ s'%makeinstall'make %{fhs} install';
[12]1170 $prefix = $buildroot;
1171 } # done %install and/or %makeinstall
1172
1173 # Build data
1174 # Note that these are processed in reverse order to get the substitution order right
1175 if ($section =~ /b/) {
[24]1176# $macrostring =~ s'%{fhs}'host=$DEB_HOST_GNU_TYPE \
1177# build=$DEB_BUILD_GNU_TYPE \
1178 $macrostring =~ s'%{fhs}'prefix=%{_prefix} \
[17]1179 exec-prefix=%{_exec_prefix} \
1180 bindir=%{_bindir} \
1181 sbindir=%{_sbindir} \
1182 sysconfdir=%{_sysconfdir} \
1183 datadir=%{_datadir} \
1184 includedir=%{_includedir} \
1185 libdir=%{_libdir} \
1186 libexecdir=%{_libexecdir} \
1187 localstatedir=%{_localstatedir} \
1188 sharedstatedir=%{_sharedstatedir} \
1189 mandir=%{_mandir} \
1190 infodir=%{_infodir} \
1191';
[10]1192
1193 # Note that the above regex terminates with the extra space
[12]1194 # "Just In Case" of user additions, which will then get neatly
1195 # tagged on the end where they take precedence (supposedly)
1196 # over the "default" ones.
[10]1197
[14]1198 # Now we cascade the macros introduced above. >_<
1199 # Wot ot to go theah:
1200 $macrostring =~ s|%{_mandir}|%{_datadir}/man|g; #/usr/share/man
1201 $macrostring =~ s|%{_infodir}|%{_datadir}/info|g; #/usr/share/info
[25]1202 $macrostring =~ s|%{_oldincludedir}|/usr/include|g; #/usr/include
1203 $macrostring =~ s|%{_includedir}|%{_prefix\}/include|g; #/usr/include
1204 $macrostring =~ s|%{_libdir}|%{_exec_prefix}/%{_lib}|g; #/usr/lib
[14]1205 $macrostring =~ s|%{_lib}|lib|g; #?
[25]1206 $macrostring =~ s|%{_localstatedir}|/var|g; #/var
1207 $macrostring =~ s|%{_sharedstatedir}|%{_prefix}/com|g; #/usr/com WTF?
1208 $macrostring =~ s|%{_sysconfdir}|/etc|g; #/etc
1209 $macrostring =~ s|%{_datadir}|%{_prefix}/share|g; #/usr/share
1210 $macrostring =~ s|%{_libexecdir}|%{_exec_prefix}/libexec|g; #/usr/libexec
1211 $macrostring =~ s|%{_sbindir}|%{_exec_prefix}/sbin|g; #/usr/sbin
1212 $macrostring =~ s|%{_bindir}|%{_exec_prefix}/bin|g; #/usr/bin
1213 $macrostring =~ s|%{_exec_prefix}|%{_prefix}|g; #/usr
[14]1214 $macrostring =~ s|%{_prefix}|/usr|g; #/usr
[10]1215 } # done with config section
1216
[12]1217 # Package data
1218 if ($section =~ /p/) {
[15]1219 $macrostring =~ s/\%\{buildroot\}/$buildroot/gi;
1220 foreach my $source (keys %{$pkgdata{sources}}) {
1221 $macrostring =~ s/\%\{source$source\}/$topdir\/SOURCES\/$pkgdata{sources}{$source}/gi;
1222 }
1223 $macrostring =~ s/\%\{name\}/$pkgdata{main}{name}/gi;
1224 $macrostring =~ s/\%\{version\}/$pkgdata{main}{version}/gi;
1225 $macrostring =~ s/\%\{release\}/$pkgdata{main}{release}/gi;
[12]1226 }
1227
1228 # Globals, and not-so-globals
1229 if ($section =~ /g/) {
[60]1230
1231 # special %define's. Handle the general case where we eval anything.
1232 # Prime example:
1233 #%define perl_vendorlib %(eval "`perl -V:installvendorlib`"; echo $installvendorlib)
1234 if ($macrostring =~ /^\%\(eval.+\)$/) {
1235 $macrostring =~ s/^\%\(//;
1236 $macrostring =~ s/\)$//;
1237 # Oy vey this gets silly for the perl bits. Executing a shell to
1238 # call Perl to get the vendorlib/sitelib/whatever "core" globals.
1239 # This can do more, but... eww.
1240 # Next line is non-optimal - what if $macrostring contains ' characters?
1241 $macrostring = qx { /bin/sh -c '$macrostring' };
1242 }
1243
[29]1244 $macrostring =~ s|%{_builddir}|%{_topdir}/BUILD|g;
1245 $macrostring =~ s|%{_topdir}|$topdir|g;
[13]1246 $macrostring =~ s|%{_tmppath}|$tmpdir|g;
[12]1247 $macrostring =~ s'%{_docdir}'/usr/share/doc'g;
[23]1248
[31]1249 # Standard FHS locations. More or less.
[30]1250 $macrostring =~ s'%{_bindir}'/usr/bin'g;
[31]1251 $macrostring =~ s'%{_sbindir}'/usr/sbin'g;
[30]1252 $macrostring =~ s'%{_mandir}'/usr/share/man'g;
1253 $macrostring =~ s'%{_includedir}'/usr/include'g;
[31]1254 $macrostring =~ s'%{_libdir}'/usr/lib'g;
1255 $macrostring =~ s'%{_sysconfdir}'/etc'g;
1256 $macrostring =~ s'%{_localstatedir}'/var'g;
[30]1257
[23]1258 # %define's
1259 foreach my $key (keys %specglobals) {
1260 $macrostring =~ s|%{$key}|$specglobals{$key}|g;
1261 }
1262
1263 # system programs. RPM uses a global config file for these; we'll just
1264 # ASS-U-ME and make life a little simpler.
1265 if ($macrostring =~ /\%\{\_\_([a-z0-9_-]+)\}/) {
1266 $macrostring =~ s|%{__([a-z0-9_-]+)}|$1|g;
1267 }
[10]1268 } # done with globals section
1269
1270 return $macrostring;
1271} # end expandmacros()
[22]1272
1273
1274
[38]1275__END__
1276
1277
1278
[22]1279=head1 NAME
1280
1281debbuild - Build Debian-compatible packages from RPM spec files
1282
1283=head1 SYNOPSIS
1284
1285 debbuild {-ba|-bb|-bp|-bc|-bi|-bl|-bs} [build-options] file.spec
1286
1287 debbuild {-ta|-tb|-tp|-tc|-ti|-tl|-ts} [build-options] file.tar.{gz|bz2}
1288
[55]1289 debbuild --rebuild file.{src.rpm|sdeb}
[22]1290
1291=head1 DESCRIPTION
1292
[55]1293This script attempts to build Debian-friendly semi-native packages from RPM spec files,
1294RPM-friendly tarballs, and RPM source packages (.src.rpm files). It accepts I<most> of the
1295options rpmbuild does, and should be able to interpret most spec files usefully. Perl
1296modules should be handled via CPAN+dh-make-perl instead; Debian's conventions for such
1297things do not lend themselves to automated conversion.
[22]1298
[55]1299As far as possible, the command-line options are identical to those from rpmbuild, although
[57]1300several rpmbuild options are not supported:
[22]1301
[57]1302 --recompile
1303 --showrc
1304 --buildroot
1305 --clean
1306 --nobuild
1307 --rmsource
1308 --rmspec
1309 --sign
1310 --target
1311
1312Some of these could probably be trivially added. Feel free to send me a patch. ;)
1313
1314Complex spec files will most likely not work well, if at all. Rewrite them from scratch -
1315you'll have to make heavy modifications anyway.
1316
1317If you see something you don't like, mail me. Send a patch if you feel inspired. I don't
1318promise I'll do anything other than say "Yup, that's broken" or "Got your message".
1319
[55]1320=head1 ASSUMPTIONS
1321
1322As with rpmbuild, debbuild makes some assumptions about your system.
1323
1324=over 4
1325
1326=item *
1327
1328Either you have rights to do as you please under /usr/src/debian, or you have created a file
1329~/.debmacros containing a suitable %_topdir definition.
1330
[57]1331Both rpmbuild and debbuild require the directories %_topdir/{BUILD,SOURCES,SPECS}. However,
1332where rpmbuild requires the %_topdir/{RPMS,SRPMS} directories, debbuild
1333requires %_topdir/{DEBS,SDEBS} instead. Create them in advance;
[55]1334some subdirectories are created automatically as needed, but most are not.
1335
1336=item *
1337
1338/var/tmp must allow script execution - rpmbuild and debbuild both rely on creating and
1339executing shell scripts for much of their functionality. By default, debbuild also creates
1340install trees under /var/tmp - however this is (almost) entirely under the control of the
1341package's .spec file.
1342
1343=item *
1344
1345If you wish to --rebuild a .src.rpm, your %_topdir for both debbuild and rpmbuild must either
1346match, or be suitably symlinked one direction or another so that both programs are effectively
1347working in the same tree. (Or you could just manually wrestle files around your system.)
1348
1349You could symlink ~/.rpmmacros to ~/.debmacros (or vice versa) and save yourself some hassle
1350if you need to rebuild .src.rpm packages on a regular basis. Currently debbuild only uses the
1351%_topdir macro definition, although there are many more things that rpmbuild can use from
1352~/.rpmmacros.
1353
1354=back
1355
[57]1356=head1 AUTHOR
[55]1357
[57]1358debbuild was written by Kris Deugau <kdeugau@deepnet.cx>. A version that approximates
1359current is available at http://www.deepnet.cx/debbuild/.
[55]1360
1361=head1 BUGS
1362
[57]1363Funky Things Happen if you forget a command-line option or two. I've been too lazy to bother
1364fixing this.
[55]1365
[57]1366Many macro expansions are unsupported or incompletely supported.
1367
1368The generated scriptlets don't quite match those from rpmbuild exactly. There are extra
1369environment variables and preprocessing that I haven't needed (yet).
1370
1371Dcumentation, such as it is, will likely remain perpetually out of date.
1372
1373%_topdir and the five "working" directories under %_topdir could arguably be created by
1374debbuild. However, rpmbuild doesn't create these directories either.
1375
1376=head1 SEE ALSO
1377
1378rpm(8), rpmbuild(8), and pretty much any document describing how to write a .spec file.
1379
[22]1380=cut
Note: See TracBrowser for help on using the repository browser.