source: trunk/debbuild@ 76

Last change on this file since 76 was 76, checked in by kdeugau, 17 years ago

/trunk

Add basic support for %attr
Bugfix --showpkgs to not show "empty" packages

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