source: trunk/debbuild@ 48

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

/trunk

Initialize more variables - scriptlets.

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