source: trunk/debbuild@ 50

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

/trunk

Add a few bits to mostly handle debbuild -i

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 34.4 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-03-07 22:56:45 +0000 (Tue, 07 Mar 2006) $
9# SVN revision $Rev: 50 $
10# Last update by $Author: kdeugau $
11###
12
13use strict;
14use warnings;
15use Fcntl; # for sysopen flags
16use Cwd 'abs_path'; # for finding where files really are
17
18# regex debugger
19#use re "debug";
20
21# Program flow:
22# -> Parse/execute "system" config/macros (if any - should be rare)
23# -> Parse/execute "user" config/macros (if any - *my* requirement is %_topdir)
24# -> Parse command line for options, spec file/tarball/.src.deb (NB - also accept .src.rpm)
25
26sub expandmacros;
27
28# User's prefs for dirs, environment, etc,etc,etc.
29# config file ~/.debmacros
30# Default ordered search paths for config/macros:
31# /usr/lib/rpm/rpmrc /usr/lib/rpm/redhat/rpmrc /etc/rpmrc ~/.rpmrc
32# /usr/lib/rpm/macros /usr/lib/rpm/redhat/macros /etc/rpm/macros ~/.rpmmacros
33# **NOTE: May be possible to (ab)use bits of debhelper
34
35# Build tree
36# default is /usr/src/debian/{BUILD,SOURCES,SPECS,DEBS,SDEBS}
37
38# Globals
39my $specfile;
40my $tarball;
41my $srcpkg;
42my $cmdbuildroot;
43my $tarballdir; # This should really be initialized, but the coding makes it, um, ugly.
44my %specglobals; # For %define's in specfile, among other things.
45
46# Initialized globals
47my $verbosity = 0;
48my %cmdopts = (type => '',
49 stage => 'a',
50 short => 'n'
51 );
52my $topdir = "/usr/src/debian";
53my $buildroot = "%{_tmppath}/%{name}-%{version}-%{release}.root".int(rand(99998)+1);
54
55# "Constants"
56my %targets = ('p' => 'Prep',
57 'c' => 'Compile',
58 'i' => 'Install',
59 'l' => 'Verify %files',
60 'a' => 'Build binary and source',
61 'b' => 'Build binary',
62 's' => 'Build source'
63 );
64my $scriptletbase =
65q(#!/bin/sh
66
67 RPM_SOURCE_DIR="%{_topdir}/SOURCES"
68 RPM_BUILD_DIR="%{_topdir}/BUILD"
69 RPM_OPT_FLAGS="-O2 -g -march=i386 -mcpu=i686"
70 RPM_ARCH="i386"
71 RPM_OS="linux"
72 export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS
73 RPM_DOC_DIR="/usr/share/doc"
74 export RPM_DOC_DIR
75 RPM_PACKAGE_NAME="%{name}"
76 RPM_PACKAGE_VERSION="%{version}"
77 RPM_PACKAGE_RELEASE="%{release}"
78 export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE
79 RPM_BUILD_ROOT="%{buildroot}"
80 export RPM_BUILD_ROOT
81);
82foreach (`dpkg-architecture`) {
83 s/=(.+)/="$1"/;
84 $scriptletbase .= " $_";
85}
86$scriptletbase .=
87q(
88 set -x
89 umask 022
90 cd %{_topdir}/BUILD
91);
92
93# Package data
94# This is the form of $pkgdata{pkgname}{meta}
95# meta includes Summary, Name, Version, Release, Group, Copyright,
96# Source, URL, Packager, BuildRoot, Description, BuildReq(uires),
97# Requires, Provides
98# 10/31/2005 Maybe this should be flatter? -kgd
99my %pkgdata;
100my @pkglist = ('main'); #sigh
101# Files listing. Embedding this in %pkgdata would be, um, messy.
102my %filelist;
103my $buildreq = '';
104
105# Scriptlets
106my $prepscript = '';
107my $buildscript = '';
108# %install doesn't need the full treatment from %clean; just an empty place to install to.
109# NB - rpm doesn't do this; is it really necessary?
110my $installscript = '[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT'."\n";
111my $cleanscript = '';
112# pre/post (un)install scripts. Note that these will likely barf as-is. :/
113my $preinstscript = '';
114my $postinstscript = '';
115my $preuninstscript = '';
116my $postuninstscript = '';
117
118die "Not enough arguments\n" if #$argv == 0;
119
120# Snag some environment data
121my $tmpdir;
122if (defined $ENV{TMP} && $ENV{TMP} =~ /^(\/var)?\/tmp$/) {
123 $tmpdir = $ENV{TMP};
124} else {
125 $tmpdir = "/var/tmp";
126}
127
128##main
129
130load_userconfig();
131parse_cmd();
132
133if ($cmdopts{install}) {
134 install_sdeb();
135 exit 0;
136}
137
138# Stick --rebuild handling in here - basically install_sdeb()
139# followed by tweaking options to run with -ba
140
141if ($cmdopts{type} eq 'b') {
142 # Need to read the spec file to find the tarball. Note that
143 # this also generates most of the shell script required.
144 parse_spec();
145 die "Can't build $pkgdata{main}{name}: build requirements not met.\n"
146 if !checkbuildreq();
147}
148
149# -> srcpkg if -.s
150if ($cmdopts{stage} eq 's') {
151 srcpackage();
152 exit 0;
153}
154
155# Hokay. Need to:
156# -> prep if -.p OR (-.[cilabs] AND !--short-circuit)
157if ($cmdopts{stage} eq 'p' || ($cmdopts{stage} =~ /[cilabs]/ && $cmdopts{short} ne 'y')) {
158 prep();
159}
160# -> build if -.c OR (-.[ilabs] AND !--short-circuit)
161if ($cmdopts{stage} eq 'c' || ($cmdopts{stage} =~ /[ilabs]/ && $cmdopts{short} ne 'y')) {
162 build();
163}
164# -> install if -.[ilabs]
165#if ($cmdopts{stage} eq 'i' || ($cmdopts{stage} =~ /[labs]/ && $cmdopts{short} ne 'y')) {
166if ($cmdopts{stage} =~ /[ilabs]/) {
167 install();
168#foreach my $pkg (@pkglist) {
169# print "files in $pkg:\n ".$filelist{$pkg}."\n";
170#}
171
172}
173# -> binpkg and srcpkg if -.a
174if ($cmdopts{stage} eq 'a') {
175 binpackage();
176 srcpackage();
177}
178# -> binpkg if -.b
179if ($cmdopts{stage} eq 'b') {
180 binpackage();
181}
182
183# Just in case.
184exit 0;
185
186
187## load_userconfig()
188# Loads user configuration (if any)
189# Currently only handles .debmacros
190# Needs to handle "other files"
191sub load_userconfig {
192 my (undef,undef,undef,undef,undef,undef,undef,$homedir,undef) = getpwuid($<);
193 if (-e "$homedir/.debmacros") {
194 open USERMACROS,"<$homedir/.debmacros";
195 while (<USERMACROS>) {
196 # And we also only handle a few macros at the moment.
197 if (/^\%_topdir/) {
198 my (undef,$tmp) = split /\s+/, $_;
199 $topdir = $tmp;
200 }
201 }
202 }
203} # end load_userconfig()
204
205
206## parse_cmd()
207# Parses command line into global hash %cmdopts, other globals
208# Options based on rpmbuild's options
209sub parse_cmd {
210 # Don't feel like coding my own option parser...
211 #use Getopt::Long;
212 # ... but I may have to: (OTOH, rpm uses popt, so maybe we can too.)
213 #use Getopt::Popt qw(:all);
214 # Or not. >:( Stupid Debian lack of findable Perl module names in packages.
215
216 # Stuff it.
217 my $prevopt = '';
218 foreach (@ARGV) {
219 chomp;
220
221 # Is it an option?
222 if (/^-/) {
223
224 # Is it a long option?
225 if (/^--/) {
226 if (/^--short-circuit/) {
227 $cmdopts{short} = 'y';
228 } elsif (/^--rebuild/) {
229 $cmdopts{type} = 's';
230 } else {
231 print "Long opt $_\n";
232 }
233 } else {
234 # Not a long option
235 if (/^-[bt]/) {
236 if ($cmdopts{stage} eq 's') {
237 # Mutually exclusive options.
238 die "Can't use $_ with --rebuild\n";
239 } else {
240 # Capture the type (from "bare" files or tarball) and the stage (prep, build, etc)
241 ($cmdopts{stage}) = (/^-[bt]([pcilabs])/);
242 ($cmdopts{type}) = (/^-([bt])[pcilabs]/);
243 }
244 } elsif (/^-v/) {
245 # bump verbosity. Not sure what I'll actually do here...
246 } elsif (/^-i/) {
247 $cmdopts{install} = 1;
248 $prevopt = '-i';
249 } else {
250 die "Bad option $_\n";
251 }
252 }
253
254 } else { # Not an option argument
255
256 # --buildroot is the only option that takes an argument
257 # Therefore, any *other* bare arguments are the spec file,
258 # tarball, or source package we're operating on - depending
259 # on which one we meet.
260 if ($prevopt eq '--buildroot') {
261 $cmdbuildroot = $_;
262 } elsif ($prevopt eq '-i') {
263 $srcpkg = $_;
264 } else {
265 if ($cmdopts{type} eq 's') {
266 # Source package
267 if (!/(sdeb|\.src\.rpm)$/) {
268 die "Can't --rebuild with $_\n";
269 }
270 $srcpkg = $_;
271 } elsif ($cmdopts{type} eq 'b') {
272 $specfile = $_;
273 # Spec file
274 } else {
275 # Tarball
276 }
277 }
278 }
279 $prevopt = $_;
280 } # foreach @ARGV
281
282 # Some cross-checks. rpmbuild limits --short-circuit to just
283 # the "compile" and "install" targets - with good reason IMO.
284 # Note that --short-circuit with -.p is not really an error, just redundant.
285 # NB - this is NOT fatal, just ignored!
286 if ($cmdopts{short} eq 'y' && $cmdopts{stage} =~ /[labs]/) {
287 warn "Can't use --short-circuit for $targets{$cmdopts{stage}} stage. Ignoring.\n";
288 $cmdopts{short} = 'n';
289 }
290
291 # Valid options, with example arguments (if any):
292# Build from .spec file; mutually exclusive:
293 # -bp
294 # -bc
295 # -bi
296 # -bl
297 # -ba
298 # -bb
299 # -bs
300# Build from tarball; mutually exclusive:
301 # -tp
302 # -tc
303 # -ti
304 # -ta
305 # -tb
306 # -ts
307# Build from .src.(deb|rpm)
308 # --rebuild
309 # --recompile
310
311# General options
312 # --buildroot=DIRECTORY
313 # --clean
314 # --nobuild
315 # --nodeps
316 # --nodirtokens
317 # --rmsource
318 # --rmspec
319 # --short-circuit
320 # --target=CPU-VENDOR-OS
321
322 #my $popt = new Getopt::Popt(argv => \@ARGV, options => \@optionsTable);
323
324} # end parse_cmd()
325
326
327## parse_spec()
328# Parse the .spec file.
329sub parse_spec {
330 open SPECFILE,"<$specfile";
331
332LINE: while (<SPECFILE>) {
333 next if /^#/; # Ignore comments...
334 next if /^\s+$/; # ... and blank lines.
335
336 if (/^\%/) {
337 # A macro that needs further processing.
338
339 if (/^\%define\s+([^\s]+)\s+([^\s]+)/) {
340 $specglobals{$1} = expandmacros($2,'g');
341 }
342
343 if (/^\%description(?:\s+(?:-n\s+)?([a-zA-Z0-9_.-]+))?/) {
344 my $subname = "main";
345 if ($1) {
346 my $tmp = $1;
347 if (/-n/) { $subname = $tmp; } else { $subname = "$pkgdata{main}{name}-$tmp"; }
348 }
349 while (<SPECFILE>) {
350 next if /^#/; # Messy. Should be possible to do better. :/
351 redo LINE if /^\%/;
352 $pkgdata{$subname}{desc} .= " $_";
353 }
354 }
355 if (/^\%package\s+(?:-n\s+)?([a-zA-Z0-9_.-]+)/) {
356 my $subname = $1;
357 if (! /-n/) { $subname = "$pkgdata{main}{name}-$1"; }
358 push @pkglist, $subname;
359 $pkgdata{$subname}{name} = $subname;
360 $pkgdata{$subname}{version} = $pkgdata{main}{version};
361 while (<SPECFILE>) {
362 redo LINE if /^\%/;
363 if (my ($dname,$dvalue) = (/^(Summary|Group|Version|Requires|Provides):\s+(.+)$/i)) {
364 $dname =~ tr/[A-Z]/[a-z]/;
365 $pkgdata{$subname}{$dname} = $dvalue;
366 }
367 }
368 }
369
370 if (/^\%prep/) {
371 # %prep section. May have %setup macro; may include %patch tags,
372 # may be just a bare shell script.
373
374 # This really should be local-ish, but we need just the filename for the source
375 $pkgdata{main}{source} =~ s|.+/([^/]+)$|$1|;
376
377 # Replace some core macros
378 $pkgdata{main}{source} = expandmacros($pkgdata{main}{source},'gp');
379
380PREPSCRIPT: while (<SPECFILE>) {
381 if (/^\%setup/) {
382 # Parse out the %setup macro. Note that we aren't supporting
383 # many of RPM's %setup features.
384 $prepscript .= "cd $topdir/BUILD\n";
385 if ( /\s+-n\s+([^\s]+)\s+/ ) {
386 $tarballdir = $1;
387 } else {
388 $tarballdir = "$pkgdata{main}{name}-$pkgdata{main}{version}";
389 }
390 $prepscript .= "rm -rf $tarballdir\ntar -".
391 ( $pkgdata{main}{source} =~ /\.tar\.gz$/ ? "z" : "" ).
392 ( $pkgdata{main}{source} =~ /\.tar\.bz2$/ ? "j" : "" ).
393 ( /\s+-q\s+/ ? '' : 'vv' )."xf ".
394 "$topdir/SOURCES/$pkgdata{main}{source}\n".
395 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n).
396 ( /\s+-n\s+([^\s]+)\s+/ ?
397 "cd $1\n" : "cd $pkgdata{main}{name}-$pkgdata{main}{version}\n" ).
398 qq([ `/usr/bin/id -u` = '0' ] && /bin/chown -Rhf root .\n).
399 qq([ `/usr/bin/id -u` = '0' ] && /bin/chgrp -Rhf root .\n).
400 qq(/bin/chmod -Rf a+rX,g-w,o-w .\n);
401 } elsif (/^\%patch([^:]+)\s+(.+)$/) {
402 $prepscript .= "patch $2 <$topdir/SOURCES/".$pkgdata{main}{"patch$1"}."\n";
403 } else {
404 last PREPSCRIPT if /^\%/;
405 $prepscript .= $_;
406 }
407 }
408 redo LINE;
409 }
410 if (/^\%build/) {
411 # %build. This is pretty much just a shell script. There
412 # *are* a few macros, but we're not going to deal with them yet.
413 $buildscript .= "cd $tarballdir\n";
414BUILDSCRIPT: while (<SPECFILE>) {
415 if (/^\%configure/) {
416 $buildscript .= expandmacros($_,'cgbp');
417 } elsif (/^\%\{__make\}/) {
418 $buildscript .= expandmacros($_,'mgbp');
419 } else {
420 last BUILDSCRIPT if /^\%[^{]/;
421 $buildscript .= $_;
422 }
423 }
424 redo LINE;
425 }
426 if (/^\%install/) {
427 $installscript .= "cd $tarballdir\n";
428INSTALLSCRIPT: while (<SPECFILE>) {
429 if (/^\%makeinstall/) {
430 $installscript .= expandmacros($_,'igbp');
431 } else {
432 last INSTALLSCRIPT if /^\%/;
433 $installscript .= $_;
434 }
435 }
436 redo LINE;
437 }
438 if (/^\%clean/) {
439 while (<SPECFILE>) {
440 redo LINE if /^\%/;
441 $cleanscript .= $_;
442 }
443 $cleanscript = expandmacros($cleanscript,'gp');
444 }
445
446 # pre/post (un)install scripts
447 if (/^\%pre\b/) {
448 while (<SPECFILE>) {
449 redo LINE if /^\%/;
450 $preinstscript .= $_;
451 }
452 }
453 if (/^\%post\b/) {
454 while (<SPECFILE>) {
455 redo LINE if /^\%/;
456 $postinstscript .= $_;
457 }
458 }
459 if (/^\%preun\b/) {
460 while (<SPECFILE>) {
461 redo LINE if /^\%/;
462 $preuninstscript .= $_;
463 }
464 }
465 if (/^\%postun\b/) {
466 while (<SPECFILE>) {
467 redo LINE if /^\%/;
468 $postuninstscript .= $_;
469 }
470 }
471 # done %pre/%post scripts
472
473 if (/^\%files(?:\s+(?:-n\s+)?([a-zA-z0-9]+))?/) {
474 my $pkgname = 'main';
475 if ($1) { # Magic to add entries to the right list of files
476 my $tmp = $1;
477 if (/-n/) { $pkgname = $tmp; } else { $pkgname = "$pkgdata{main}{name}-$tmp"; }
478 }
479
480 # Set this now, so it can be flipped a bit later, and used much later.
481 #$pkgdata{$pkgname}{conffiles} = 0;
482
483 while (<SPECFILE>) {
484 chomp;
485 next if /^#/;
486 # need to update this to deal (properly) with %dir, %attr, etc
487 next if /^\%dir/;
488 next if /^\%attr/;
489 next if /^\%defattr/;
490
491 # Debian dpkg doesn't speak "%docdir". Meh.
492 next if /^\%docdir/;
493
494 # Conffiles. Note that Debian and RH have similar, but not
495 # *quite* identical ideas of what constitutes a conffile. Nrgh.
496 if (/^\%config\s+(.+)$/) {
497 $pkgdata{$pkgname}{conffiles} = 1; # Flag it for later
498 my $tmp = $1; # Now we can mangleificationate it. And we probably need to. :/
499 $tmp = expandmacros($tmp, 'gp'); # Expand common macros
500 if ($tmp !~ /\s+/) {
501 # Simplest case, just a file. Whew.
502 push @{$pkgdata{$pkgname}{conflist}}, $tmp;
503 $filelist{$pkgname} .= " $tmp";
504 } else {
505 # Wot? Spaces? That means extra %-macros. Which, for the most part, can be ignored.
506 ($tmp) = ($tmp =~ /.+\s([^\s]+)/); # Strip everything before the last space
507 push @{$pkgdata{$pkgname}{conflist}}, $tmp;
508 $filelist{$pkgname} .= " $tmp";
509 }
510 next;
511 }
512
513 # and finally we can fall through %{_<FHS>}-prefixed locations...
514 if (/^\%\{_/) {
515 $filelist{$pkgname} .= " $_";
516 next;
517 }
518 # EW. Necessary to clear up %define expansions before we exit with redo.
519 $_ = expandmacros $_, 'g';
520
521 # ... unknown or "next section" % directives ...
522 redo LINE if /^\%/;
523
524 # ... and "normal" files
525 $filelist{$pkgname} .= " $_";
526 }
527 $filelist{$pkgname} = expandmacros($filelist{$pkgname}, 'gp');
528 } # done %file section
529
530 if (/^\%changelog/) {
531 $pkgdata{main}{changelog} = '';
532 while (<SPECFILE>) {
533 redo LINE if /^\%/;
534 $pkgdata{main}{changelog} .= $_;
535 }
536 }
537
538 } else { # Data from the spec file "header"
539
540 if (/^summary:\s+(.+)/i) {
541 $pkgdata{main}{summary} = $1;
542 } elsif (/^name:\s+(.+)/i) {
543 $pkgdata{main}{name} = expandmacros($1,'g');
544 } elsif (/^version:\s+(.+)/i) {
545 $pkgdata{main}{version} = expandmacros($1,'g');
546 } elsif (/^release:\s+(.+)/i) {
547 $pkgdata{main}{release} = expandmacros($1,'g');
548 } elsif (/^group:\s+(.+)/i) {
549 $pkgdata{main}{group} = $1;
550 } elsif (/^copyright:\s+(.+)/i) {
551 $pkgdata{main}{copyright} = $1;
552 } elsif (/^url:\s+(.+)/i) {
553 $pkgdata{main}{url} = $1;
554 } elsif (/^packager:\s+(.+)/i) {
555 $pkgdata{main}{packager} = $1;
556 } elsif (/^buildroot:\s+(.+)/i) {
557 $buildroot = $1;
558 } elsif (/^source:\s+(.+)/i) {
559 $pkgdata{main}{source} = $1;
560 die "Unknown tarball format $1\n" if $1 !~ /\.tar\.(?:gz|bz2)$/;
561 } elsif (/^source([0-9]+):\s+(.+)/i) {
562 $pkgdata{sources}{$1} = $2;
563 } elsif (/^patch([^:]+):\s+(.+)$/i) {
564 my $patchname = "patch$1";
565 $pkgdata{main}{$patchname} = $2;
566 if ($pkgdata{main}{$patchname} =~ /\//) {
567 # URL-style patch. Rare but not unheard-of.
568 my @patchbits = split '/', $pkgdata{main}{$patchname};
569 $pkgdata{main}{$patchname} = $patchbits[$#patchbits];
570 }
571 } elsif (/^buildreq(?:uires)?:\s+(.+)/i) {
572 $buildreq .= ", $1";
573 } elsif (/^requires:\s+(.+)/i) {
574 $pkgdata{main}{requires} .= ", $1";
575 } elsif (/^provides:\s+(.+)/i) {
576 $pkgdata{main}{provides} .= ", $1";
577 }
578#Name: suwrap
579#Version: 0.04
580#Release: 3
581#Group: Applications/System
582#Copyright: WebHart internal ONLY. :(
583#BuildArchitectures: i386
584#BuildRoot: /tmp/%{name}-%{version}
585#Url: http://virtual.webhart.net
586#Packager: Kris Deugau <kdeugau@deepnet.cx>
587#Source: ftp://virtual.webhart.net/%{name}-%{version}.tar.gz
588
589 }
590 }
591
592 # Parse and replace some more macros. More will be replaced even later.
593
594 # Expand macros as necessary.
595 $scriptletbase = expandmacros($scriptletbase,'gp');
596
597 $buildroot = $cmdbuildroot if $cmdbuildroot;
598 $buildroot = expandmacros($buildroot,'gp');
599
600 close SPECFILE;
601} # end parse_spec()
602
603
604## prep()
605# Writes and executes the %prep script (mostly) built while reading the spec file.
606sub prep {
607 # Replace some things here just to make sure.
608 $prepscript = expandmacros($prepscript,'gp');
609
610#print $prepscript; exit 0;
611
612 # create script filename
613 my $prepscriptfile = "$tmpdir/deb-tmp.prep.".int(rand(99998)+1);
614 sysopen(PREPSCRIPT, $prepscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
615 or die $!;
616 print PREPSCRIPT $scriptletbase;
617 print PREPSCRIPT $prepscript;
618 close PREPSCRIPT;
619
620 # execute
621 print "Calling \%prep script $prepscriptfile...\n";
622 system("/bin/sh -e $prepscriptfile") == 0
623 or die "Can't exec: $!\n";
624
625 # and clean up
626 unlink $prepscriptfile;
627} # end prep()
628
629
630## build()
631# Writes and executes the %build script (mostly) built while reading the spec file.
632sub build {
633 # Expand the macros
634 $buildscript = expandmacros($buildscript,'cgbp');
635
636 # create script filename
637 my $buildscriptfile = "$tmpdir/deb-tmp.build.".int(rand(99998)+1);
638 sysopen(BUILDSCRIPT, $buildscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
639 or die $!;
640 print BUILDSCRIPT $scriptletbase;
641 print BUILDSCRIPT $buildscript;
642 close BUILDSCRIPT;
643
644 # execute
645 print "Calling \%build script $buildscriptfile...\n";
646 system("/bin/sh -e $buildscriptfile") == 0
647 or die "Can't exec: $!\n";
648
649 # and clean up
650 unlink $buildscriptfile;
651} # end build()
652
653
654## install()
655# Writes and executes the %install script (mostly) built while reading the spec file.
656sub install {
657 # Expand the macros
658 $installscript = expandmacros($installscript,'igbp');
659
660 # create script filename
661 my $installscriptfile = "$tmpdir/deb-tmp.inst.".int(rand(99998)+1);
662 sysopen(INSTSCRIPT, $installscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
663 or die $!;
664 print INSTSCRIPT $scriptletbase;
665# print INSTSCRIPT $cleanscript; # Clean up our install target before installing into it.
666 print INSTSCRIPT $installscript;
667 close INSTSCRIPT;
668
669 # execute
670 print "Calling \%install script $installscriptfile...\n";
671 system("/bin/sh -e $installscriptfile") == 0
672 or die "Can't exec: $!\n";
673
674 # and clean up
675 unlink $installscriptfile;
676} # end install()
677
678
679## binpackage()
680# Creates the binary .deb package from the installed tree in $buildroot.
681# Writes and executes a shell script to do so.
682# Creates miscellaneous files required by dpkg-deb to actually build the package file.
683# Should handle simple subpackages
684sub binpackage {
685 # Make sure we have somewhere to write the .deb file
686 if (!-e "$topdir/DEBS/i386") {
687 mkdir "$topdir/DEBS/i386";
688 }
689
690 foreach my $pkg (@pkglist) {
691
692 # Gotta do this first, otherwise we don't have a place to move files from %files
693 mkdir "$buildroot/$pkg";
694
695 # Eliminate any lingering % macros
696 $filelist{$pkg} = expandmacros $filelist{$pkg}, 'g';
697
698 my @pkgfilelist = split ' ', $filelist{$pkg};
699 foreach my $pkgfile (@pkgfilelist) {
700 $pkgfile = expandmacros($pkgfile, 'gp');
701 my ($fpath,$fname) = ($pkgfile =~ m|(.+?/?)?([^/]+)$|); # We don't need $fname now, but we might.
702 qx { mkdir -p $buildroot/$pkg$fpath }
703 if $fpath && $fpath ne '';
704 qx { mv $buildroot$pkgfile $buildroot/$pkg$fpath };
705 }
706
707 # Get the "Depends" (Requires) a la RPM. Ish. We strip the leading
708 # comma and space here (if needed) in case there were "Requires" specified
709 # in the spec file - those would precede these.
710 ($pkgdata{$pkg}{requires} .= getreqs("$buildroot/$pkg")) =~ s/^, //;
711
712 # Do this here since we're doing {depends}...
713 if (defined($pkgdata{$pkg}{provides})) {
714 $pkgdata{$pkg}{provides} =~ s/^, //;
715 $pkgdata{$pkg}{provides} = expandmacros($pkgdata{$pkg}{provides},'gp');
716 }
717
718 # Gotta do this next, otherwise the control file has nowhere to go. >:(
719 mkdir "$buildroot/$pkg/DEBIAN";
720
721 # Hack the filename for the package into a Debian-tool-compatible format. GRRRRRR!!!!!
722 # Have I mentioned I hate Debian Policy?
723 $pkgdata{$pkg}{name} =~ tr/_/-/;
724
725 # create script filename
726 my $debscriptfile = "$tmpdir/deb-tmp.pkg.".int(rand(99998)+1);
727 sysopen(DEBSCRIPT, $debscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
728 or die $!;
729 print DEBSCRIPT $scriptletbase;
730 print DEBSCRIPT "fakeroot dpkg-deb -b $buildroot/$pkg $topdir/DEBS/i386/".
731 "$pkgdata{$pkg}{name}_$pkgdata{$pkg}{version}-$pkgdata{main}{release}_i386.deb\n";
732 # %$&$%@#@@#%@@@ Debian and their horrible ugly package names. >:(
733 close DEBSCRIPT;
734
735 my $control = "Package: $pkgdata{$pkg}{name}\n".
736 "Version: $pkgdata{$pkg}{version}-$pkgdata{main}{release}\n".
737 "Section: $pkgdata{$pkg}{group}\n".
738 "Priority: optional\n".
739 "Architecture: i386\n".
740 "Maintainer: $pkgdata{main}{packager}\n".
741 ( $pkgdata{$pkg}{requires} ne '' ? "Depends: $pkgdata{$pkg}{requires}\n" : '' ).
742 ( defined($pkgdata{$pkg}{provides}) ? "Provides: $pkgdata{$pkg}{provides}\n" : '' ).
743 "Description: $pkgdata{$pkg}{summary}\n";
744 $control .= "$pkgdata{$pkg}{desc}\n";
745
746 open CONTROL, ">$buildroot/$pkg/DEBIAN/control";
747 print CONTROL $control;
748 close CONTROL;
749
750 # Iff there are conffiles (as specified in the %files list(s), add'em
751 # in so dpkg-deb can tag them.
752 if ($pkgdata{$pkg}{conffiles}) {
753 open CONFLIST, ">$buildroot/$pkg/DEBIAN/conffiles";
754 foreach my $conffile (@{$pkgdata{$pkg}{conflist}}) {
755 print CONFLIST "$conffile\n";
756 }
757 close CONFLIST;
758 }
759
760 # Can't see much point in scripts on subpackages... although since
761 # it's *possible* I should support it at some point.
762 if ($pkg eq 'main') {
763 if ($preinstscript ne '') {
764 $preinstscript = expandmacros($preinstscript,'g');
765 open PREINST, ">$buildroot/$pkg/DEBIAN/preinst";
766 print PREINST "#!/bin/sh\nset -e\n\n";
767 print PREINST $preinstscript;
768 close PREINST;
769 `chmod 0755 $buildroot/$pkg/DEBIAN/preinst`;
770 }
771 if ($postinstscript ne '') {
772 $postinstscript = expandmacros($postinstscript,'g');
773 open POSTINST, ">$buildroot/$pkg/DEBIAN/postinst";
774 print POSTINST "#!/bin/sh\nset -e\n\n";
775 print POSTINST $postinstscript;
776 close POSTINST;
777 `chmod 0755 $buildroot/$pkg/DEBIAN/postinst`;
778 }
779 if ($preuninstscript ne '') {
780 $preuninstscript = expandmacros($preuninstscript,'g');
781 open PREUNINST, ">$buildroot/$pkg/DEBIAN/prerm";
782 print PREUNINST "#!/bin/sh\nset -e\n\n";
783 print PREUNINST $preuninstscript;
784 close PREUNINST;
785 `chmod 0755 $buildroot/$pkg/DEBIAN/prerm`;
786 }
787 if ($postuninstscript ne '') {
788 $postuninstscript = expandmacros($postuninstscript,'g');
789 open POSTUNINST, ">$buildroot/$pkg/DEBIAN/postrm";
790 print POSTUNINST "#!/bin/sh\nset -e\n\n";
791 print POSTUNINST $postuninstscript;
792 close POSTUNINST;
793 `chmod 0755 $buildroot/$pkg/DEBIAN/postrm`;
794 }
795 }
796
797 # execute
798 print "Calling package creation script $debscriptfile for $pkgdata{$pkg}{name}...\n";
799 system("/bin/sh -e $debscriptfile") == 0
800 or die "Can't exec: $!\n";
801
802 # and clean up
803 unlink $debscriptfile;
804
805 } # subpackage loop
806
807} # end binpackage()
808
809
810## srcpackage()
811# Builds a .src.deb source package. Note that Debian's idea of
812# a "source package" is seriously flawed IMO, because you can't
813# easily copy it as-is.
814# Not quite identical to RPM, but Good Enough (TM).
815sub srcpackage {
816 my $pkgsrcname = "$pkgdata{main}{name}-$pkgdata{main}{version}-$pkgdata{main}{release}.sdeb";
817
818 my $paxcmd;
819
820 # We'll definitely need this later, and *may* need it sooner.
821 (my $barespec = $specfile) =~ s|.+/([^/]+)$|$1|;
822
823 # Copy the specfile to the build tree, but only if it's not there already.
824##buglet: need to deal with silly case where silly user has put the spec
825# file in a subdir of %{_topdir}/SPECS. Ewww. Silly user!
826 if (abs_path($specfile) !~ /^$topdir\/SPECS/) {
827 $paxcmd .= "cp $specfile %{_topdir}/SPECS/; \n"
828 }
829
830 # use pax -w [file] [file] ... >outfile.sdeb
831 $paxcmd = "cd $topdir; pax -w ";
832
833# tweak source entry into usable form. Need it locally somewhere along the line.
834 (my $pkgsrc = $pkgdata{main}{source}) =~ s|.+/([^/]+)$|$1|;
835 $paxcmd .= "SOURCES/$pkgsrc ";
836
837 # create file list: Source[nn], Patch[nn]
838 foreach my $specbit (keys %{$pkgdata{main}} ) {
839 next if $specbit eq 'source';
840 $paxcmd .= "SOURCES/$pkgdata{main}{$specbit} " if $specbit =~ /^(source|patch)/;
841##buglet: need to deal with case where patches are listed as URLs?
842# or other extended pathnames? Silly !@$%^&!%%!%!! user!
843 }
844
845 # add the spec file, source package destination, and cd back where we came from.
846 $paxcmd .= "SPECS/$barespec > $topdir/SDEBS/$pkgsrcname; cd -";
847
848 # In case of %-macros...
849 $paxcmd = expandmacros($paxcmd,'gp');
850
851 system "$paxcmd";
852 print "Wrote source package $pkgsrcname in $topdir/SDEBS.\n";
853}
854
855
856## checkbuildreq()
857# Checks the build requirements (if any)
858# Spits out a rude warning and returns a true-false error if any
859# requirements are not met.
860sub checkbuildreq {
861 return 1 if $buildreq eq ''; # No use doing extra work.
862
863 my $reqflag = 1; # unset iff a buildreq is missing
864
865 $buildreq =~ s/^, //; # Strip the leading comma and space
866 my @reqlist = split /,\s+/, $buildreq;
867
868 foreach my $req (@reqlist) {
869 my ($pkg,$rel,$ver);
870
871 # We have two classes of requirements - versioned and unversioned.
872 if ($req =~ /[><=]/) {
873 # Pick up the details of versioned buildreqs
874 ($pkg,$rel,$ver) = ($req =~ /([a-z0-9._-]+)\s+([><=]+)\s+([a-z0-9._-]+)/);
875 } else {
876 # And the unversioned ones.
877 $pkg = $req;
878 $rel = '>=';
879 $ver = 0;
880 }
881
882 my @pkglist = qx { dpkg-query --showformat '\${status}\t\${version}\n' -W $pkg };
883# need to check if no lines returned - means a bad buildreq
884 my ($reqstat,undef,undef,$reqver) = split /\s+/, $pkglist[0];
885 if ($reqstat !~ /install/) {
886 print " * Missing build-dependency $pkg!\n";
887 $reqflag = 0;
888 } else {
889# gotta be a better way to do this... :/
890 if ($rel eq '>=' && !($reqver ge $ver)) {
891 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
892 $reqflag = 0;
893 }
894 if ($rel eq '>' && !($reqver gt $ver)) {
895 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
896 $reqflag = 0;
897 }
898 if ($rel eq '<=' && !($reqver le $ver)) {
899 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
900 $reqflag = 0;
901 }
902 if ($rel eq '<' && !($reqver lt $ver)) {
903 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
904 $reqflag = 0;
905 }
906 if ($rel eq '=' && !($reqver eq $ver)) {
907 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
908 $reqflag = 0;
909 }
910 } # end not installed/installed check
911 } # end req loop
912
913 return $reqflag;
914} # end checkbuildreq()
915
916
917## getreqs()
918# Find out which libraries/packages are required for any
919# executables and libs in a given file tree.
920# (Debian doesn't have soname-level deps; just package-level)
921# Returns an empty string if the tree contains no binaries.
922# Doesn't work well on shell scripts. but those *should* be
923# fine anyway. (Yeah, right...)
924sub getreqs() {
925 my $pkgtree = $_[0];
926
927 print "Checking library requirements...\n";
928 my @binlist = qx { find $pkgtree -type f -perm 755 };
929
930 if (scalar(@binlist) == 0) {
931 return '';
932 }
933
934 my @reqlist;
935 foreach (@binlist) {
936 push @reqlist, qx { ldd $_ };
937 }
938
939 # Get the list of libs provided by this package. Still doesn't
940 # handle the case where the lib gets stuffed into a subpackage. :/
941 my @intprovlist = qx { find $pkgtree -type f -name "*.so*" };
942 my $provlist = '';
943 foreach (@intprovlist) {
944 s/$pkgtree//;
945 $provlist .= "$_";
946 }
947
948 my %reqs;
949 my $reqlibs = '';
950
951 foreach (@reqlist) {
952 next if /^$pkgtree/;
953 next if /not a dynamic executable/;
954 next if m|/lib/ld-linux.so|; # Hack! Hack! PTHBTT! (libc suxx0rz)
955
956 my ($req) = (/^\s+([a-z0-9._-]+)/); # dig out the actual library (so)name
957
958 # Ignore libs provided by this package. Note that we don't match
959 # on word-boundary at the *end* of the lib we're looking for, as the
960 # looked-for lib may not have the full soname version. (ie, it may
961 # "just" point to one of the symlinks that get created somewhere.)
962 next if $provlist =~ /\b$req/;
963
964 $reqlibs .= " $req";
965 }
966
967 if ($reqlibs ne '') {
968 foreach (qx { dpkg -S $reqlibs }) {
969 my ($libpkg,undef) = split /:\s+/;
970 $reqs{$libpkg} = 1;
971 }
972 }
973
974 my $deplist = '';
975 foreach (keys %reqs) {
976 $deplist .= ", $_";
977 }
978
979# For now, we're done. We're not going to meddle with versions yet.
980# Among other things, it's messier than handling "simple" yes/no "do
981# we have this lib?" deps. >:(
982
983 return $deplist;
984} # end getreqs()
985
986
987## install_sdeb()
988# Extracts .sdeb contents to %_topdir as appropriate
989sub install_sdeb {
990 my $paxcmd = "cd $topdir; pax -r <$srcpkg; cd -";
991
992 # In case of %-macros...
993 $paxcmd = expandmacros($paxcmd,'gp');
994
995 system "$paxcmd";
996 print "Extracted source package $srcpkg to $topdir.\n";
997} # end install_sdeb()
998
999
1000## expandmacros()
1001# Expands all %{blah} macros in the passed string
1002# Split up a bit with some sections so we don't spend time trying to
1003# expand macros that are only used in a few specific places.
1004sub expandmacros {
1005 my $macrostring = shift;
1006 my $section = shift;
1007
1008 # To allow the FHS-ish %configure and %makeinstall to work The Right Way.
1009 # (Without clobbering the global $buildroot.)
1010 my $prefix = '';
1011
1012 if ($section =~ /c/) {
1013 # %configure macro
1014# Don't know what it's for, don't have a useful default replacement
1015# --program-prefix=%{_program_prefix} \
1016 $macrostring =~ s'%configure'./configure --host=$DEB_HOST_GNU_TYPE \
1017 --build=$DEB_BUILD_GNU_TYPE \
1018 --prefix=%{_prefix} \
1019 --exec-prefix=%{_exec_prefix} \
1020 --bindir=%{_bindir} \
1021 --sbindir=%{_sbindir} \
1022 --sysconfdir=%{_sysconfdir} \
1023 --datadir=%{_datadir} \
1024 --includedir=%{_includedir} \
1025 --libdir=%{_libdir} \
1026 --libexecdir=%{_libexecdir} \
1027 --localstatedir=%{_localstatedir} \
1028 --sharedstatedir=%{_sharedstatedir} \
1029 --mandir=%{_mandir} \
1030 --infodir=%{_infodir} ';
1031 } # done %configure
1032
1033 if ($section =~ /m/) {
1034 $macrostring =~ s'%{__make}'make ';
1035 } # done make
1036
1037 if ($section =~ /i/) {
1038 # This is where we need to mangle $prefix.
1039 $macrostring =~ s'%makeinstall'make %{fhs} install';
1040 $prefix = $buildroot;
1041 } # done %install and/or %makeinstall
1042
1043 # Build data
1044 # Note that these are processed in reverse order to get the substitution order right
1045 if ($section =~ /b/) {
1046# $macrostring =~ s'%{fhs}'host=$DEB_HOST_GNU_TYPE \
1047# build=$DEB_BUILD_GNU_TYPE \
1048 $macrostring =~ s'%{fhs}'prefix=%{_prefix} \
1049 exec-prefix=%{_exec_prefix} \
1050 bindir=%{_bindir} \
1051 sbindir=%{_sbindir} \
1052 sysconfdir=%{_sysconfdir} \
1053 datadir=%{_datadir} \
1054 includedir=%{_includedir} \
1055 libdir=%{_libdir} \
1056 libexecdir=%{_libexecdir} \
1057 localstatedir=%{_localstatedir} \
1058 sharedstatedir=%{_sharedstatedir} \
1059 mandir=%{_mandir} \
1060 infodir=%{_infodir} \
1061';
1062
1063 # Note that the above regex terminates with the extra space
1064 # "Just In Case" of user additions, which will then get neatly
1065 # tagged on the end where they take precedence (supposedly)
1066 # over the "default" ones.
1067
1068 # Now we cascade the macros introduced above. >_<
1069 # Wot ot to go theah:
1070 $macrostring =~ s|%{_mandir}|%{_datadir}/man|g; #/usr/share/man
1071 $macrostring =~ s|%{_infodir}|%{_datadir}/info|g; #/usr/share/info
1072 $macrostring =~ s|%{_oldincludedir}|/usr/include|g; #/usr/include
1073 $macrostring =~ s|%{_includedir}|%{_prefix\}/include|g; #/usr/include
1074 $macrostring =~ s|%{_libdir}|%{_exec_prefix}/%{_lib}|g; #/usr/lib
1075 $macrostring =~ s|%{_lib}|lib|g; #?
1076 $macrostring =~ s|%{_localstatedir}|/var|g; #/var
1077 $macrostring =~ s|%{_sharedstatedir}|%{_prefix}/com|g; #/usr/com WTF?
1078 $macrostring =~ s|%{_sysconfdir}|/etc|g; #/etc
1079 $macrostring =~ s|%{_datadir}|%{_prefix}/share|g; #/usr/share
1080 $macrostring =~ s|%{_libexecdir}|%{_exec_prefix}/libexec|g; #/usr/libexec
1081 $macrostring =~ s|%{_sbindir}|%{_exec_prefix}/sbin|g; #/usr/sbin
1082 $macrostring =~ s|%{_bindir}|%{_exec_prefix}/bin|g; #/usr/bin
1083 $macrostring =~ s|%{_exec_prefix}|%{_prefix}|g; #/usr
1084 $macrostring =~ s|%{_prefix}|/usr|g; #/usr
1085 } # done with config section
1086
1087 # Package data
1088 if ($section =~ /p/) {
1089 $macrostring =~ s/\%\{buildroot\}/$buildroot/gi;
1090 foreach my $source (keys %{$pkgdata{sources}}) {
1091 $macrostring =~ s/\%\{source$source\}/$topdir\/SOURCES\/$pkgdata{sources}{$source}/gi;
1092 }
1093 $macrostring =~ s/\%\{name\}/$pkgdata{main}{name}/gi;
1094 $macrostring =~ s/\%\{version\}/$pkgdata{main}{version}/gi;
1095 $macrostring =~ s/\%\{release\}/$pkgdata{main}{release}/gi;
1096 }
1097
1098 # Globals, and not-so-globals
1099 if ($section =~ /g/) {
1100 $macrostring =~ s|%{_builddir}|%{_topdir}/BUILD|g;
1101 $macrostring =~ s|%{_topdir}|$topdir|g;
1102 $macrostring =~ s|%{_tmppath}|$tmpdir|g;
1103 $macrostring =~ s'%{_docdir}'/usr/share/doc'g;
1104
1105 # Standard FHS locations. More or less.
1106 $macrostring =~ s'%{_bindir}'/usr/bin'g;
1107 $macrostring =~ s'%{_sbindir}'/usr/sbin'g;
1108 $macrostring =~ s'%{_mandir}'/usr/share/man'g;
1109 $macrostring =~ s'%{_includedir}'/usr/include'g;
1110 $macrostring =~ s'%{_libdir}'/usr/lib'g;
1111 $macrostring =~ s'%{_sysconfdir}'/etc'g;
1112 $macrostring =~ s'%{_localstatedir}'/var'g;
1113
1114 # %define's
1115 foreach my $key (keys %specglobals) {
1116 $macrostring =~ s|%{$key}|$specglobals{$key}|g;
1117 }
1118
1119 # system programs. RPM uses a global config file for these; we'll just
1120 # ASS-U-ME and make life a little simpler.
1121 if ($macrostring =~ /\%\{\_\_([a-z0-9_-]+)\}/) {
1122 $macrostring =~ s|%{__([a-z0-9_-]+)}|$1|g;
1123 }
1124 } # done with globals section
1125
1126 return $macrostring;
1127} # end expandmacros()
1128
1129
1130
1131__END__
1132
1133
1134
1135=head1 NAME
1136
1137debbuild - Build Debian-compatible packages from RPM spec files
1138
1139=head1 SYNOPSIS
1140
1141 debbuild {-ba|-bb|-bp|-bc|-bi|-bl|-bs} [build-options] file.spec
1142
1143 debbuild {-ta|-tb|-tp|-tc|-ti|-tl|-ts} [build-options] file.tar.{gz|bz2}
1144
1145 debbuild --rebuild file.src.{rpm|deb}
1146
1147=head1 DESCRIPTION
1148
1149This script attempts to build Debian-friendly semi-native packages
1150from RPM spec files, RPM-friendly tarballs, and RPM source packages
1151(.src.rpm). It accepts I<most> of the options rpmbuild does, and
1152should be able to interpret most spec files usefully. Perl modules
1153should be handled via CPAN+dh-make-perl instead; Debian's conventions
1154for such things do not lend themselves to automated conversion.
1155
1156As far as possible, the command-line options are identical to those
1157from rpmbuild, although several rpmbuild options are not supported.
1158
1159=cut
Note: See TracBrowser for help on using the repository browser.