source: trunk/debbuild@ 49

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

/trunk

Fix some broken behaviour with subpackages and -n

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