source: trunk/debbuild@ 71

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

/trunk

Add --showpkgs option to dump package list

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