source: trunk/debbuild@ 74

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

/trunk

Minor bugfix on "empty %files list" handling

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