source: trunk/debbuild@ 70

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

/trunk

Update handling of %patch macro; bare %patch will now work
more or less correctly.

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