source: trunk/debbuild@ 78

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

/trunk

*sigh* Switch order of chown/chgrp and chmod commands used to hack
%attr support into postinst. chown/chgrp apparently reset little
things like setuid. >:(

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