source: trunk/debbuild@ 72

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

/trunk

Add %docdir support. Sort of. Mostly.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 42.8 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 18:40:52 +0000 (Wed, 15 Nov 2006) $
9# SVN revision $Rev: 72 $
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 # Gotta do this first, otherwise we don't have a place to move files from %files
787 mkdir "$buildroot/$pkg";
788
789 # Eliminate any lingering % macros
790 $filelist{$pkg} = expandmacros $filelist{$pkg}, 'g';
791
792 my @pkgfilelist = split ' ', $filelist{$pkg};
793 foreach my $pkgfile (@pkgfilelist) {
794 $pkgfile = expandmacros($pkgfile, 'gp');
795 my ($fpath,$fname) = ($pkgfile =~ m|(.+?/?)?([^/]+)$|); # We don't need $fname now, but we might.
796 qx { mkdir -p $buildroot/$pkg$fpath }
797 if $fpath && $fpath ne '';
798 qx { mv $buildroot$pkgfile $buildroot/$pkg$fpath };
799 }
800
801 # Get the "Depends" (Requires) a la RPM. Ish. We strip the leading
802 # comma and space here (if needed) in case there were "Requires" specified
803 # in the spec file - those would precede these.
804 $pkgdata{$pkg}{requires} .= getreqs("$buildroot/$pkg");
805
806 # magic needed to properly version dependencies...
807 # only provided deps will really be included
808 $pkgdata{$pkg}{requires} =~ s/^, //; # Still have to do this here.
809 $pkgdata{$pkg}{requires} =~ s/\s+//g;
810 my @deps = split /,/, $pkgdata{$pkg}{requires};
811 my $tmp = '';
812 foreach my $dep (@deps) {
813 # Hack up the perl(Class::SubClass) deps into something dpkg can understand.
814 # May or may not be versioned.
815 # We do this first so the version rewriter can do its magic next.
816 if (my ($mod,$ver) = ($dep =~ /^perl\(([A-Za-z0-9\:\-]+)\)([><=]+.+)?/) ) {
817 $mod =~ s/^perl\(//;
818 $mod =~ s/\)$//;
819 $mod =~ s/::/-/g;
820 $mod =~ tr/A-Z/a-z/;
821 $mod = "lib$mod-perl";
822 $mod .= $ver if $ver;
823 $dep = $mod;
824 }
825 if (my ($name,$rel,$value) = ($dep =~ /^([a-zA-Z0-9._-]+)([><=]+)([a-zA-Z0-9._-]+)$/)) {
826 $tmp .= ", $name ($rel $value)";
827 } else {
828 $tmp .= ", $dep";
829 }
830 }
831 ($pkgdata{$pkg}{requires} = $tmp) =~ s/^, //;
832
833 # Do this here since we're doing {depends}...
834 if (defined($pkgdata{$pkg}{provides})) {
835 $pkgdata{$pkg}{provides} =~ s/^, //;
836 $pkgdata{$pkg}{provides} = expandmacros($pkgdata{$pkg}{provides},'gp');
837 }
838 if (defined($pkgdata{$pkg}{conflicts})) {
839 $pkgdata{$pkg}{conflicts} =~ s/^, //;
840 $pkgdata{$pkg}{conflicts} = expandmacros($pkgdata{$pkg}{conflicts},'gp');
841 }
842
843 # Gotta do this next, otherwise the control file has nowhere to go. >:(
844 mkdir "$buildroot/$pkg/DEBIAN";
845
846 # Hack the filename for the package into a Debian-tool-compatible format. GRRRRRR!!!!!
847 # Have I mentioned I hate Debian Policy?
848 $pkgdata{$pkg}{name} =~ tr/_/-/;
849
850 # create script filename
851 my $debscriptfile = "$tmpdir/deb-tmp.pkg.".int(rand(99998)+1);
852 sysopen(DEBSCRIPT, $debscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
853 or die $!;
854 print DEBSCRIPT $scriptletbase;
855 print DEBSCRIPT "fakeroot dpkg-deb -b $buildroot/$pkg $topdir/DEBS/i386/".
856 "$pkgdata{$pkg}{name}_$pkgdata{$pkg}{version}-$pkgdata{main}{release}_i386.deb\n";
857 # %$&$%@#@@#%@@@ Debian and their horrible ugly package names. >:(
858 close DEBSCRIPT;
859
860 my $control = "Package: $pkgdata{$pkg}{name}\n".
861 "Version: $pkgdata{$pkg}{version}-$pkgdata{main}{release}\n".
862 "Section: $pkgdata{$pkg}{group}\n".
863 "Priority: optional\n".
864 "Architecture: i386\n".
865 "Maintainer: $pkgdata{main}{packager}\n".
866 ( $pkgdata{$pkg}{requires} ne '' ? "Depends: $pkgdata{$pkg}{requires}\n" : '' ).
867 ( defined($pkgdata{$pkg}{provides}) ? "Provides: $pkgdata{$pkg}{provides}\n" : '' ).
868 ( defined($pkgdata{$pkg}{conflicts}) ? "Conflicts: $pkgdata{$pkg}{conflicts}\n" : '' ).
869 "Description: $pkgdata{$pkg}{summary}\n";
870 $control .= "$pkgdata{$pkg}{desc}\n";
871
872 open CONTROL, ">$buildroot/$pkg/DEBIAN/control";
873 print CONTROL $control;
874 close CONTROL;
875
876 # Iff there are conffiles (as specified in the %files list(s), add'em
877 # in so dpkg-deb can tag them.
878 if ($pkgdata{$pkg}{conffiles}) {
879 open CONFLIST, ">$buildroot/$pkg/DEBIAN/conffiles";
880 foreach my $conffile (@{$pkgdata{$pkg}{conflist}}) {
881 print CONFLIST "$conffile\n";
882 }
883 close CONFLIST;
884 }
885
886 # Can't see much point in scripts on subpackages... although since
887 # it's *possible* I should support it at some point.
888 if ($pkg eq 'main') {
889 if ($preinstscript ne '') {
890 $preinstscript = expandmacros($preinstscript,'g');
891 open PREINST, ">$buildroot/$pkg/DEBIAN/preinst";
892 print PREINST "#!/bin/sh\nset -e\n\n";
893 print PREINST $preinstscript;
894 close PREINST;
895 `chmod 0755 $buildroot/$pkg/DEBIAN/preinst`;
896 }
897 if ($postinstscript ne '') {
898 $postinstscript = expandmacros($postinstscript,'g');
899 open POSTINST, ">$buildroot/$pkg/DEBIAN/postinst";
900 print POSTINST "#!/bin/sh\nset -e\n\n";
901 print POSTINST $postinstscript;
902 close POSTINST;
903 `chmod 0755 $buildroot/$pkg/DEBIAN/postinst`;
904 }
905 if ($preuninstscript ne '') {
906 $preuninstscript = expandmacros($preuninstscript,'g');
907 open PREUNINST, ">$buildroot/$pkg/DEBIAN/prerm";
908 print PREUNINST "#!/bin/sh\nset -e\n\n";
909 print PREUNINST $preuninstscript;
910 close PREUNINST;
911 `chmod 0755 $buildroot/$pkg/DEBIAN/prerm`;
912 }
913 if ($postuninstscript ne '') {
914 $postuninstscript = expandmacros($postuninstscript,'g');
915 open POSTUNINST, ">$buildroot/$pkg/DEBIAN/postrm";
916 print POSTUNINST "#!/bin/sh\nset -e\n\n";
917 print POSTUNINST $postuninstscript;
918 close POSTUNINST;
919 `chmod 0755 $buildroot/$pkg/DEBIAN/postrm`;
920 }
921 }
922
923 # execute
924 print "Calling package creation script $debscriptfile for $pkgdata{$pkg}{name}...\n";
925 system("/bin/sh -e $debscriptfile") == 0
926 or die "Can't exec: $!\n";
927
928 # and clean up
929 unlink $debscriptfile;
930
931 } # subpackage loop
932
933} # end binpackage()
934
935
936## srcpackage()
937# Builds a .src.deb source package. Note that Debian's idea of
938# a "source package" is seriously flawed IMO, because you can't
939# easily copy it as-is.
940# Not quite identical to RPM, but Good Enough (TM).
941sub srcpackage {
942 # In case we were called with -bs.
943 $pkgdata{main}{name} =~ tr/_/-/;
944 my $pkgsrcname = "$pkgdata{main}{name}-$pkgdata{main}{version}-$pkgdata{main}{release}.sdeb";
945
946 my $paxcmd;
947
948 # We'll definitely need this later, and *may* need it sooner.
949 (my $barespec = $specfile) =~ s|.+/([^/]+)$|$1|;
950
951 # Copy the specfile to the build tree, but only if it's not there already.
952##buglet: need to deal with silly case where silly user has put the spec
953# file in a subdir of %{_topdir}/SPECS. Ewww. Silly user!
954 if (abs_path($specfile) !~ /^$topdir\/SPECS/) {
955 $paxcmd .= "cp $specfile %{_topdir}/SPECS/; \n"
956 }
957
958 # use pax -w [file] [file] ... >outfile.sdeb
959 $paxcmd = "cd $topdir; pax -w ";
960
961# tweak source entry into usable form. Need it locally somewhere along the line.
962 (my $pkgsrc = $pkgdata{main}{source}) =~ s|.+/([^/]+)$|$1|;
963 $paxcmd .= "SOURCES/$pkgsrc ";
964
965 # create file list: Source[nn], Patch[nn]
966 foreach my $specbit (keys %{$pkgdata{main}} ) {
967 next if $specbit eq 'source';
968 $paxcmd .= "SOURCES/$pkgdata{main}{$specbit} " if $specbit =~ /^patch/;
969##buglet: need to deal with case where patches are listed as URLs?
970# or other extended pathnames? Silly !@$%^&!%%!%!! user!
971 }
972
973 foreach my $source (keys %{$pkgdata{sources}}) {
974 $paxcmd .= "SOURCES/$pkgdata{sources}{$source} ";
975 }
976
977 # add the spec file, source package destination, and cd back where we came from.
978 $paxcmd .= "SPECS/$barespec > $topdir/SDEBS/$pkgsrcname; cd -";
979
980 # In case of %-macros...
981 $paxcmd = expandmacros($paxcmd,'gp');
982
983 system "$paxcmd";
984 print "Wrote source package $pkgsrcname in $topdir/SDEBS.\n";
985}
986
987
988## checkbuildreq()
989# Checks the build requirements (if any)
990# Spits out a rude warning and returns a true-false error if any
991# requirements are not met.
992sub checkbuildreq {
993 return 1 if $buildreq eq ''; # No use doing extra work.
994
995 if ( ! -e "/usr/bin/dpkg-query" ) {
996 print "**WARNING** dpkg-query not found. Can't check build-deps.\n".
997 " Required for sucessful build:\n".$buildreq."\n".
998 " Continuing anyway.\n";
999 return 1;
1000 }
1001
1002 my $reqflag = 1; # unset iff a buildreq is missing
1003
1004 $buildreq =~ s/^, //; # Strip the leading comma and space
1005 my @reqlist = split /,\s+/, $buildreq;
1006
1007 foreach my $req (@reqlist) {
1008 my ($pkg,$rel,$ver);
1009
1010 # We have two classes of requirements - versioned and unversioned.
1011 if ($req =~ /[><=]/) {
1012 # Pick up the details of versioned buildreqs
1013 ($pkg,$rel,$ver) = ($req =~ /([a-z0-9._-]+)\s+([><=]+)\s+([a-z0-9._-]+)/);
1014 } else {
1015 # And the unversioned ones.
1016 $pkg = $req;
1017 $rel = '>=';
1018 $ver = 0;
1019 }
1020
1021 my @pkglist = qx { dpkg-query --showformat '\${status}\t\${version}\n' -W $pkg };
1022# need to check if no lines returned - means a bad buildreq
1023 my ($reqstat,undef,undef,$reqver) = split /\s+/, $pkglist[0];
1024 if ($reqstat !~ /install/) {
1025 print " * Missing build-dependency $pkg!\n";
1026 $reqflag = 0;
1027 } else {
1028# gotta be a better way to do this... :/
1029 if ($rel eq '>=' && !($reqver ge $ver)) {
1030 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1031 $reqflag = 0;
1032 }
1033 if ($rel eq '>' && !($reqver gt $ver)) {
1034 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1035 $reqflag = 0;
1036 }
1037 if ($rel eq '<=' && !($reqver le $ver)) {
1038 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1039 $reqflag = 0;
1040 }
1041 if ($rel eq '<' && !($reqver lt $ver)) {
1042 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1043 $reqflag = 0;
1044 }
1045 if ($rel eq '=' && !($reqver eq $ver)) {
1046 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
1047 $reqflag = 0;
1048 }
1049 } # end not installed/installed check
1050 } # end req loop
1051
1052 return $reqflag;
1053} # end checkbuildreq()
1054
1055
1056## getreqs()
1057# Find out which libraries/packages are required for any
1058# executables and libs in a given file tree.
1059# (Debian doesn't have soname-level deps; just package-level)
1060# Returns an empty string if the tree contains no binaries.
1061# Doesn't work well on shell scripts. but those *should* be
1062# fine anyway. (Yeah, right...)
1063sub getreqs() {
1064 my $pkgtree = $_[0];
1065
1066 print "Checking library requirements...\n";
1067 my @binlist = qx { find $pkgtree -type f -perm 755 };
1068
1069 if (scalar(@binlist) == 0) {
1070 return '';
1071 }
1072
1073 my @reqlist;
1074 foreach (@binlist) {
1075 push @reqlist, qx { ldd $_ };
1076 }
1077
1078 # Get the list of libs provided by this package. Still doesn't
1079 # handle the case where the lib gets stuffed into a subpackage. :/
1080 my @intprovlist = qx { find $pkgtree -type f -name "*.so*" };
1081 my $provlist = '';
1082 foreach (@intprovlist) {
1083 s/$pkgtree//;
1084 $provlist .= "$_";
1085 }
1086
1087 my %reqs;
1088 my $reqlibs = '';
1089
1090 foreach (@reqlist) {
1091 next if /^$pkgtree/;
1092 next if /not a dynamic executable/;
1093 next if m|/lib/ld-linux.so|; # Hack! Hack! PTHBTT! (libc suxx0rz)
1094
1095 my ($req) = (/^\s+([a-z0-9._-]+)/); # dig out the actual library (so)name
1096
1097 # Ignore libs provided by this package. Note that we don't match
1098 # on word-boundary at the *end* of the lib we're looking for, as the
1099 # looked-for lib may not have the full soname version. (ie, it may
1100 # "just" point to one of the symlinks that get created somewhere.)
1101 next if $provlist =~ /\b$req/;
1102
1103 $reqlibs .= " $req";
1104 }
1105
1106 if ($reqlibs ne '') {
1107 foreach (qx { dpkg -S $reqlibs }) {
1108 my ($libpkg,undef) = split /:\s+/;
1109 $reqs{$libpkg} = 1;
1110 }
1111 }
1112
1113 my $deplist = '';
1114 foreach (keys %reqs) {
1115 $deplist .= ", $_";
1116 }
1117
1118# For now, we're done. We're not going to meddle with versions yet.
1119# Among other things, it's messier than handling "simple" yes/no "do
1120# we have this lib?" deps. >:(
1121
1122 return $deplist;
1123} # end getreqs()
1124
1125
1126## install_sdeb()
1127# Extracts .sdeb contents to %_topdir as appropriate
1128sub install_sdeb {
1129 my $paxcmd = "cd $topdir; pax -r <$srcpkg; cd -";
1130
1131 # In case of %-macros...
1132 $paxcmd = expandmacros($paxcmd,'gp');
1133
1134 system "$paxcmd";
1135 print "Extracted source package $srcpkg to $topdir.\n";
1136} # end install_sdeb()
1137
1138
1139## expandmacros()
1140# Expands all %{blah} macros in the passed string
1141# Split up a bit with some sections so we don't spend time trying to
1142# expand macros that are only used in a few specific places.
1143sub expandmacros {
1144 my $macrostring = shift;
1145 my $section = shift;
1146
1147 # To allow the FHS-ish %configure and %makeinstall to work The Right Way.
1148 # (Without clobbering the global $buildroot.)
1149 my $prefix = '';
1150
1151 if ($section =~ /c/) {
1152 # %configure macro
1153# Don't know what it's for, don't have a useful default replacement
1154# --program-prefix=%{_program_prefix} \
1155 $macrostring =~ s'%configure'./configure --host=$DEB_HOST_GNU_TYPE \
1156 --build=$DEB_BUILD_GNU_TYPE \
1157 --prefix=%{_prefix} \
1158 --exec-prefix=%{_exec_prefix} \
1159 --bindir=%{_bindir} \
1160 --sbindir=%{_sbindir} \
1161 --sysconfdir=%{_sysconfdir} \
1162 --datadir=%{_datadir} \
1163 --includedir=%{_includedir} \
1164 --libdir=%{_libdir} \
1165 --libexecdir=%{_libexecdir} \
1166 --localstatedir=%{_localstatedir} \
1167 --sharedstatedir=%{_sharedstatedir} \
1168 --mandir=%{_mandir} \
1169 --infodir=%{_infodir} ';
1170 } # done %configure
1171
1172 if ($section =~ /m/) {
1173 $macrostring =~ s'%{__make}'make ';
1174 } # done make
1175
1176 if ($section =~ /i/) {
1177 # This is where we need to mangle $prefix.
1178 $macrostring =~ s'%makeinstall'make %{fhs} install';
1179 $prefix = $buildroot;
1180 } # done %install and/or %makeinstall
1181
1182 # Build data
1183 # Note that these are processed in reverse order to get the substitution order right
1184 if ($section =~ /b/) {
1185# $macrostring =~ s'%{fhs}'host=$DEB_HOST_GNU_TYPE \
1186# build=$DEB_BUILD_GNU_TYPE \
1187 $macrostring =~ s'%{fhs}'prefix=%{_prefix} \
1188 exec-prefix=%{_exec_prefix} \
1189 bindir=%{_bindir} \
1190 sbindir=%{_sbindir} \
1191 sysconfdir=%{_sysconfdir} \
1192 datadir=%{_datadir} \
1193 includedir=%{_includedir} \
1194 libdir=%{_libdir} \
1195 libexecdir=%{_libexecdir} \
1196 localstatedir=%{_localstatedir} \
1197 sharedstatedir=%{_sharedstatedir} \
1198 mandir=%{_mandir} \
1199 infodir=%{_infodir} \
1200';
1201
1202 # Note that the above regex terminates with the extra space
1203 # "Just In Case" of user additions, which will then get neatly
1204 # tagged on the end where they take precedence (supposedly)
1205 # over the "default" ones.
1206
1207 # Now we cascade the macros introduced above. >_<
1208 # Wot ot to go theah:
1209 $macrostring =~ s|%{_mandir}|%{_datadir}/man|g; #/usr/share/man
1210 $macrostring =~ s|%{_infodir}|%{_datadir}/info|g; #/usr/share/info
1211 $macrostring =~ s|%{_oldincludedir}|/usr/include|g; #/usr/include
1212 $macrostring =~ s|%{_includedir}|%{_prefix\}/include|g; #/usr/include
1213 $macrostring =~ s|%{_libdir}|%{_exec_prefix}/%{_lib}|g; #/usr/lib
1214 $macrostring =~ s|%{_lib}|lib|g; #?
1215 $macrostring =~ s|%{_localstatedir}|/var|g; #/var
1216 $macrostring =~ s|%{_sharedstatedir}|%{_prefix}/com|g; #/usr/com WTF?
1217 $macrostring =~ s|%{_sysconfdir}|/etc|g; #/etc
1218 $macrostring =~ s|%{_datadir}|%{_prefix}/share|g; #/usr/share
1219 $macrostring =~ s|%{_libexecdir}|%{_exec_prefix}/libexec|g; #/usr/libexec
1220 $macrostring =~ s|%{_sbindir}|%{_exec_prefix}/sbin|g; #/usr/sbin
1221 $macrostring =~ s|%{_bindir}|%{_exec_prefix}/bin|g; #/usr/bin
1222 $macrostring =~ s|%{_exec_prefix}|%{_prefix}|g; #/usr
1223 $macrostring =~ s|%{_prefix}|/usr|g; #/usr
1224 } # done with config section
1225
1226 # Package data
1227 if ($section =~ /p/) {
1228 $macrostring =~ s/\%\{buildroot\}/$buildroot/gi;
1229 foreach my $source (keys %{$pkgdata{sources}}) {
1230 $macrostring =~ s/\%\{source$source\}/$topdir\/SOURCES\/$pkgdata{sources}{$source}/gi;
1231 }
1232 $macrostring =~ s/\%\{name\}/$pkgdata{main}{name}/gi;
1233 $macrostring =~ s/\%\{version\}/$pkgdata{main}{version}/gi;
1234 $macrostring =~ s/\%\{release\}/$pkgdata{main}{release}/gi;
1235 }
1236
1237 # Globals, and not-so-globals
1238 if ($section =~ /g/) {
1239
1240 # special %define's. Handle the general case where we eval anything.
1241 # Prime example:
1242 #%define perl_vendorlib %(eval "`perl -V:installvendorlib`"; echo $installvendorlib)
1243 if ($macrostring =~ /^\%\(eval.+\)$/) {
1244 $macrostring =~ s/^\%\(//;
1245 $macrostring =~ s/\)$//;
1246 # Oy vey this gets silly for the perl bits. Executing a shell to
1247 # call Perl to get the vendorlib/sitelib/whatever "core" globals.
1248 # This can do more, but... eww.
1249 # Next line is non-optimal - what if $macrostring contains ' characters?
1250 $macrostring = qx { /bin/sh -c '$macrostring' };
1251 }
1252
1253 $macrostring =~ s|%{_builddir}|%{_topdir}/BUILD|g;
1254 $macrostring =~ s|%{_topdir}|$topdir|g;
1255 $macrostring =~ s|%{_tmppath}|$tmpdir|g;
1256 $macrostring =~ s'%{_docdir}'/usr/share/doc'g;
1257
1258 # Standard FHS locations. More or less.
1259 $macrostring =~ s'%{_bindir}'/usr/bin'g;
1260 $macrostring =~ s'%{_sbindir}'/usr/sbin'g;
1261 $macrostring =~ s'%{_mandir}'/usr/share/man'g;
1262 $macrostring =~ s'%{_includedir}'/usr/include'g;
1263 $macrostring =~ s'%{_libdir}'/usr/lib'g;
1264 $macrostring =~ s'%{_sysconfdir}'/etc'g;
1265 $macrostring =~ s'%{_localstatedir}'/var'g;
1266
1267 # %define's
1268 foreach my $key (keys %specglobals) {
1269 $macrostring =~ s|%{$key}|$specglobals{$key}|g;
1270 }
1271
1272 # system programs. RPM uses a global config file for these; we'll just
1273 # ASS-U-ME and make life a little simpler.
1274 if ($macrostring =~ /\%\{\_\_([a-z0-9_-]+)\}/) {
1275 $macrostring =~ s|%{__([a-z0-9_-]+)}|$1|g;
1276 }
1277 } # done with globals section
1278
1279 return $macrostring;
1280} # end expandmacros()
1281
1282
1283
1284__END__
1285
1286
1287
1288=head1 NAME
1289
1290debbuild - Build Debian-compatible packages from RPM spec files
1291
1292=head1 SYNOPSIS
1293
1294 debbuild {-ba|-bb|-bp|-bc|-bi|-bl|-bs} [build-options] file.spec
1295
1296 debbuild {-ta|-tb|-tp|-tc|-ti|-tl|-ts} [build-options] file.tar.{gz|bz2}
1297
1298 debbuild --rebuild file.{src.rpm|sdeb}
1299
1300=head1 DESCRIPTION
1301
1302This script attempts to build Debian-friendly semi-native packages from RPM spec files,
1303RPM-friendly tarballs, and RPM source packages (.src.rpm files). It accepts I<most> of the
1304options rpmbuild does, and should be able to interpret most spec files usefully. Perl
1305modules should be handled via CPAN+dh-make-perl instead; Debian's conventions for such
1306things do not lend themselves to automated conversion.
1307
1308As far as possible, the command-line options are identical to those from rpmbuild, although
1309several rpmbuild options are not supported:
1310
1311 --recompile
1312 --showrc
1313 --buildroot
1314 --clean
1315 --nobuild
1316 --rmsource
1317 --rmspec
1318 --sign
1319 --target
1320
1321Some of these could probably be trivially added. Feel free to send me a patch. ;)
1322
1323Complex spec files will most likely not work well, if at all. Rewrite them from scratch -
1324you'll have to make heavy modifications anyway.
1325
1326If you see something you don't like, mail me. Send a patch if you feel inspired. I don't
1327promise I'll do anything other than say "Yup, that's broken" or "Got your message".
1328
1329=head1 ASSUMPTIONS
1330
1331As with rpmbuild, debbuild makes some assumptions about your system.
1332
1333=over 4
1334
1335=item *
1336
1337Either you have rights to do as you please under /usr/src/debian, or you have created a file
1338~/.debmacros containing a suitable %_topdir definition.
1339
1340Both rpmbuild and debbuild require the directories %_topdir/{BUILD,SOURCES,SPECS}. However,
1341where rpmbuild requires the %_topdir/{RPMS,SRPMS} directories, debbuild
1342requires %_topdir/{DEBS,SDEBS} instead. Create them in advance;
1343some subdirectories are created automatically as needed, but most are not.
1344
1345=item *
1346
1347/var/tmp must allow script execution - rpmbuild and debbuild both rely on creating and
1348executing shell scripts for much of their functionality. By default, debbuild also creates
1349install trees under /var/tmp - however this is (almost) entirely under the control of the
1350package's .spec file.
1351
1352=item *
1353
1354If you wish to --rebuild a .src.rpm, your %_topdir for both debbuild and rpmbuild must either
1355match, or be suitably symlinked one direction or another so that both programs are effectively
1356working in the same tree. (Or you could just manually wrestle files around your system.)
1357
1358You could symlink ~/.rpmmacros to ~/.debmacros (or vice versa) and save yourself some hassle
1359if you need to rebuild .src.rpm packages on a regular basis. Currently debbuild only uses the
1360%_topdir macro definition, although there are many more things that rpmbuild can use from
1361~/.rpmmacros.
1362
1363=back
1364
1365=head1 AUTHOR
1366
1367debbuild was written by Kris Deugau <kdeugau@deepnet.cx>. A version that approximates
1368current is available at http://www.deepnet.cx/debbuild/.
1369
1370=head1 BUGS
1371
1372Funky Things Happen if you forget a command-line option or two. I've been too lazy to bother
1373fixing this.
1374
1375Many macro expansions are unsupported or incompletely supported.
1376
1377The generated scriptlets don't quite match those from rpmbuild exactly. There are extra
1378environment variables and preprocessing that I haven't needed (yet).
1379
1380Dcumentation, such as it is, will likely remain perpetually out of date.
1381
1382%_topdir and the five "working" directories under %_topdir could arguably be created by
1383debbuild. However, rpmbuild doesn't create these directories either.
1384
1385=head1 SEE ALSO
1386
1387rpm(8), rpmbuild(8), and pretty much any document describing how to write a .spec file.
1388
1389=cut
Note: See TracBrowser for help on using the repository browser.