source: trunk/debbuild@ 53

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

/trunk

Bugfix: .sdeb's didn't get extra source files properly included

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 35.0 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-04-07 21:51:50 +0000 (Fri, 07 Apr 2006) $
9# SVN revision $Rev: 53 $
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 } elsif (/^conflicts:\s+(.+)/i) {
578 $pkgdata{main}{conflicts} .= ", $1";
579 }
580#Name: suwrap
581#Version: 0.04
582#Release: 3
583#Group: Applications/System
584#Copyright: WebHart internal ONLY. :(
585#BuildArchitectures: i386
586#BuildRoot: /tmp/%{name}-%{version}
587#Url: http://virtual.webhart.net
588#Packager: Kris Deugau <kdeugau@deepnet.cx>
589#Source: ftp://virtual.webhart.net/%{name}-%{version}.tar.gz
590
591 }
592 }
593
594 # Parse and replace some more macros. More will be replaced even later.
595
596 # Expand macros as necessary.
597 $scriptletbase = expandmacros($scriptletbase,'gp');
598
599 $buildroot = $cmdbuildroot if $cmdbuildroot;
600 $buildroot = expandmacros($buildroot,'gp');
601
602 close SPECFILE;
603} # end parse_spec()
604
605
606## prep()
607# Writes and executes the %prep script (mostly) built while reading the spec file.
608sub prep {
609 # Replace some things here just to make sure.
610 $prepscript = expandmacros($prepscript,'gp');
611
612#print $prepscript; exit 0;
613
614 # create script filename
615 my $prepscriptfile = "$tmpdir/deb-tmp.prep.".int(rand(99998)+1);
616 sysopen(PREPSCRIPT, $prepscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
617 or die $!;
618 print PREPSCRIPT $scriptletbase;
619 print PREPSCRIPT $prepscript;
620 close PREPSCRIPT;
621
622 # execute
623 print "Calling \%prep script $prepscriptfile...\n";
624 system("/bin/sh -e $prepscriptfile") == 0
625 or die "Can't exec: $!\n";
626
627 # and clean up
628 unlink $prepscriptfile;
629} # end prep()
630
631
632## build()
633# Writes and executes the %build script (mostly) built while reading the spec file.
634sub build {
635 # Expand the macros
636 $buildscript = expandmacros($buildscript,'cgbp');
637
638 # create script filename
639 my $buildscriptfile = "$tmpdir/deb-tmp.build.".int(rand(99998)+1);
640 sysopen(BUILDSCRIPT, $buildscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
641 or die $!;
642 print BUILDSCRIPT $scriptletbase;
643 print BUILDSCRIPT $buildscript;
644 close BUILDSCRIPT;
645
646 # execute
647 print "Calling \%build script $buildscriptfile...\n";
648 system("/bin/sh -e $buildscriptfile") == 0
649 or die "Can't exec: $!\n";
650
651 # and clean up
652 unlink $buildscriptfile;
653} # end build()
654
655
656## install()
657# Writes and executes the %install script (mostly) built while reading the spec file.
658sub install {
659 # Expand the macros
660 $installscript = expandmacros($installscript,'igbp');
661
662 # create script filename
663 my $installscriptfile = "$tmpdir/deb-tmp.inst.".int(rand(99998)+1);
664 sysopen(INSTSCRIPT, $installscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
665 or die $!;
666 print INSTSCRIPT $scriptletbase;
667# print INSTSCRIPT $cleanscript; # Clean up our install target before installing into it.
668 print INSTSCRIPT $installscript;
669 close INSTSCRIPT;
670
671 # execute
672 print "Calling \%install script $installscriptfile...\n";
673 system("/bin/sh -e $installscriptfile") == 0
674 or die "Can't exec: $!\n";
675
676 # and clean up
677 unlink $installscriptfile;
678} # end install()
679
680
681## binpackage()
682# Creates the binary .deb package from the installed tree in $buildroot.
683# Writes and executes a shell script to do so.
684# Creates miscellaneous files required by dpkg-deb to actually build the package file.
685# Should handle simple subpackages
686sub binpackage {
687 # Make sure we have somewhere to write the .deb file
688 if (!-e "$topdir/DEBS/i386") {
689 mkdir "$topdir/DEBS/i386";
690 }
691
692 foreach my $pkg (@pkglist) {
693
694 # Gotta do this first, otherwise we don't have a place to move files from %files
695 mkdir "$buildroot/$pkg";
696
697 # Eliminate any lingering % macros
698 $filelist{$pkg} = expandmacros $filelist{$pkg}, 'g';
699
700 my @pkgfilelist = split ' ', $filelist{$pkg};
701 foreach my $pkgfile (@pkgfilelist) {
702 $pkgfile = expandmacros($pkgfile, 'gp');
703 my ($fpath,$fname) = ($pkgfile =~ m|(.+?/?)?([^/]+)$|); # We don't need $fname now, but we might.
704 qx { mkdir -p $buildroot/$pkg$fpath }
705 if $fpath && $fpath ne '';
706 qx { mv $buildroot$pkgfile $buildroot/$pkg$fpath };
707 }
708
709 # Get the "Depends" (Requires) a la RPM. Ish. We strip the leading
710 # comma and space here (if needed) in case there were "Requires" specified
711 # in the spec file - those would precede these.
712 ($pkgdata{$pkg}{requires} .= getreqs("$buildroot/$pkg")) =~ s/^, //;
713
714 # Do this here since we're doing {depends}...
715 if (defined($pkgdata{$pkg}{provides})) {
716 $pkgdata{$pkg}{provides} =~ s/^, //;
717 $pkgdata{$pkg}{provides} = expandmacros($pkgdata{$pkg}{provides},'gp');
718 }
719 if (defined($pkgdata{$pkg}{conflicts})) {
720 $pkgdata{$pkg}{conflicts} =~ s/^, //;
721 $pkgdata{$pkg}{conflicts} = expandmacros($pkgdata{$pkg}{conflicts},'gp');
722 }
723
724 # Gotta do this next, otherwise the control file has nowhere to go. >:(
725 mkdir "$buildroot/$pkg/DEBIAN";
726
727 # Hack the filename for the package into a Debian-tool-compatible format. GRRRRRR!!!!!
728 # Have I mentioned I hate Debian Policy?
729 $pkgdata{$pkg}{name} =~ tr/_/-/;
730
731 # create script filename
732 my $debscriptfile = "$tmpdir/deb-tmp.pkg.".int(rand(99998)+1);
733 sysopen(DEBSCRIPT, $debscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
734 or die $!;
735 print DEBSCRIPT $scriptletbase;
736 print DEBSCRIPT "fakeroot dpkg-deb -b $buildroot/$pkg $topdir/DEBS/i386/".
737 "$pkgdata{$pkg}{name}_$pkgdata{$pkg}{version}-$pkgdata{main}{release}_i386.deb\n";
738 # %$&$%@#@@#%@@@ Debian and their horrible ugly package names. >:(
739 close DEBSCRIPT;
740
741 my $control = "Package: $pkgdata{$pkg}{name}\n".
742 "Version: $pkgdata{$pkg}{version}-$pkgdata{main}{release}\n".
743 "Section: $pkgdata{$pkg}{group}\n".
744 "Priority: optional\n".
745 "Architecture: i386\n".
746 "Maintainer: $pkgdata{main}{packager}\n".
747 ( $pkgdata{$pkg}{requires} ne '' ? "Depends: $pkgdata{$pkg}{requires}\n" : '' ).
748 ( defined($pkgdata{$pkg}{provides}) ? "Provides: $pkgdata{$pkg}{provides}\n" : '' ).
749 ( defined($pkgdata{$pkg}{conflicts}) ? "Conflicts: $pkgdata{$pkg}{conflicts}\n" : '' ).
750 "Description: $pkgdata{$pkg}{summary}\n";
751 $control .= "$pkgdata{$pkg}{desc}\n";
752
753 open CONTROL, ">$buildroot/$pkg/DEBIAN/control";
754 print CONTROL $control;
755 close CONTROL;
756
757 # Iff there are conffiles (as specified in the %files list(s), add'em
758 # in so dpkg-deb can tag them.
759 if ($pkgdata{$pkg}{conffiles}) {
760 open CONFLIST, ">$buildroot/$pkg/DEBIAN/conffiles";
761 foreach my $conffile (@{$pkgdata{$pkg}{conflist}}) {
762 print CONFLIST "$conffile\n";
763 }
764 close CONFLIST;
765 }
766
767 # Can't see much point in scripts on subpackages... although since
768 # it's *possible* I should support it at some point.
769 if ($pkg eq 'main') {
770 if ($preinstscript ne '') {
771 $preinstscript = expandmacros($preinstscript,'g');
772 open PREINST, ">$buildroot/$pkg/DEBIAN/preinst";
773 print PREINST "#!/bin/sh\nset -e\n\n";
774 print PREINST $preinstscript;
775 close PREINST;
776 `chmod 0755 $buildroot/$pkg/DEBIAN/preinst`;
777 }
778 if ($postinstscript ne '') {
779 $postinstscript = expandmacros($postinstscript,'g');
780 open POSTINST, ">$buildroot/$pkg/DEBIAN/postinst";
781 print POSTINST "#!/bin/sh\nset -e\n\n";
782 print POSTINST $postinstscript;
783 close POSTINST;
784 `chmod 0755 $buildroot/$pkg/DEBIAN/postinst`;
785 }
786 if ($preuninstscript ne '') {
787 $preuninstscript = expandmacros($preuninstscript,'g');
788 open PREUNINST, ">$buildroot/$pkg/DEBIAN/prerm";
789 print PREUNINST "#!/bin/sh\nset -e\n\n";
790 print PREUNINST $preuninstscript;
791 close PREUNINST;
792 `chmod 0755 $buildroot/$pkg/DEBIAN/prerm`;
793 }
794 if ($postuninstscript ne '') {
795 $postuninstscript = expandmacros($postuninstscript,'g');
796 open POSTUNINST, ">$buildroot/$pkg/DEBIAN/postrm";
797 print POSTUNINST "#!/bin/sh\nset -e\n\n";
798 print POSTUNINST $postuninstscript;
799 close POSTUNINST;
800 `chmod 0755 $buildroot/$pkg/DEBIAN/postrm`;
801 }
802 }
803
804 # execute
805 print "Calling package creation script $debscriptfile for $pkgdata{$pkg}{name}...\n";
806 system("/bin/sh -e $debscriptfile") == 0
807 or die "Can't exec: $!\n";
808
809 # and clean up
810 unlink $debscriptfile;
811
812 } # subpackage loop
813
814} # end binpackage()
815
816
817## srcpackage()
818# Builds a .src.deb source package. Note that Debian's idea of
819# a "source package" is seriously flawed IMO, because you can't
820# easily copy it as-is.
821# Not quite identical to RPM, but Good Enough (TM).
822sub srcpackage {
823 my $pkgsrcname = "$pkgdata{main}{name}-$pkgdata{main}{version}-$pkgdata{main}{release}.sdeb";
824
825 my $paxcmd;
826
827 # We'll definitely need this later, and *may* need it sooner.
828 (my $barespec = $specfile) =~ s|.+/([^/]+)$|$1|;
829
830 # Copy the specfile to the build tree, but only if it's not there already.
831##buglet: need to deal with silly case where silly user has put the spec
832# file in a subdir of %{_topdir}/SPECS. Ewww. Silly user!
833 if (abs_path($specfile) !~ /^$topdir\/SPECS/) {
834 $paxcmd .= "cp $specfile %{_topdir}/SPECS/; \n"
835 }
836
837 # use pax -w [file] [file] ... >outfile.sdeb
838 $paxcmd = "cd $topdir; pax -w ";
839
840# tweak source entry into usable form. Need it locally somewhere along the line.
841 (my $pkgsrc = $pkgdata{main}{source}) =~ s|.+/([^/]+)$|$1|;
842 $paxcmd .= "SOURCES/$pkgsrc ";
843
844 # create file list: Source[nn], Patch[nn]
845 foreach my $specbit (keys %{$pkgdata{main}} ) {
846 next if $specbit eq 'source';
847 $paxcmd .= "SOURCES/$pkgdata{main}{$specbit} " if $specbit =~ /^patch/;
848##buglet: need to deal with case where patches are listed as URLs?
849# or other extended pathnames? Silly !@$%^&!%%!%!! user!
850 }
851
852 foreach my $source (keys %{$pkgdata{sources}}) {
853 $paxcmd .= "SOURCES/$pkgdata{sources}{$source} ";
854 }
855
856 # add the spec file, source package destination, and cd back where we came from.
857 $paxcmd .= "SPECS/$barespec > $topdir/SDEBS/$pkgsrcname; cd -";
858
859 # In case of %-macros...
860 $paxcmd = expandmacros($paxcmd,'gp');
861
862 system "$paxcmd";
863 print "Wrote source package $pkgsrcname in $topdir/SDEBS.\n";
864}
865
866
867## checkbuildreq()
868# Checks the build requirements (if any)
869# Spits out a rude warning and returns a true-false error if any
870# requirements are not met.
871sub checkbuildreq {
872 return 1 if $buildreq eq ''; # No use doing extra work.
873
874 if ( ! -e "/usr/bin/dpkg-query" ) {
875 print "**WARNING** dpkg-query not found. Can't check build-deps.\n".
876 " Required for sucessful build:\n".$buildreq."\n".
877 " Continuing anyway.\n";
878 return 1;
879 }
880
881 my $reqflag = 1; # unset iff a buildreq is missing
882
883 $buildreq =~ s/^, //; # Strip the leading comma and space
884 my @reqlist = split /,\s+/, $buildreq;
885
886 foreach my $req (@reqlist) {
887 my ($pkg,$rel,$ver);
888
889 # We have two classes of requirements - versioned and unversioned.
890 if ($req =~ /[><=]/) {
891 # Pick up the details of versioned buildreqs
892 ($pkg,$rel,$ver) = ($req =~ /([a-z0-9._-]+)\s+([><=]+)\s+([a-z0-9._-]+)/);
893 } else {
894 # And the unversioned ones.
895 $pkg = $req;
896 $rel = '>=';
897 $ver = 0;
898 }
899
900 my @pkglist = qx { dpkg-query --showformat '\${status}\t\${version}\n' -W $pkg };
901# need to check if no lines returned - means a bad buildreq
902 my ($reqstat,undef,undef,$reqver) = split /\s+/, $pkglist[0];
903 if ($reqstat !~ /install/) {
904 print " * Missing build-dependency $pkg!\n";
905 $reqflag = 0;
906 } else {
907# gotta be a better way to do this... :/
908 if ($rel eq '>=' && !($reqver ge $ver)) {
909 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
910 $reqflag = 0;
911 }
912 if ($rel eq '>' && !($reqver gt $ver)) {
913 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
914 $reqflag = 0;
915 }
916 if ($rel eq '<=' && !($reqver le $ver)) {
917 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
918 $reqflag = 0;
919 }
920 if ($rel eq '<' && !($reqver lt $ver)) {
921 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
922 $reqflag = 0;
923 }
924 if ($rel eq '=' && !($reqver eq $ver)) {
925 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
926 $reqflag = 0;
927 }
928 } # end not installed/installed check
929 } # end req loop
930
931 return $reqflag;
932} # end checkbuildreq()
933
934
935## getreqs()
936# Find out which libraries/packages are required for any
937# executables and libs in a given file tree.
938# (Debian doesn't have soname-level deps; just package-level)
939# Returns an empty string if the tree contains no binaries.
940# Doesn't work well on shell scripts. but those *should* be
941# fine anyway. (Yeah, right...)
942sub getreqs() {
943 my $pkgtree = $_[0];
944
945 print "Checking library requirements...\n";
946 my @binlist = qx { find $pkgtree -type f -perm 755 };
947
948 if (scalar(@binlist) == 0) {
949 return '';
950 }
951
952 my @reqlist;
953 foreach (@binlist) {
954 push @reqlist, qx { ldd $_ };
955 }
956
957 # Get the list of libs provided by this package. Still doesn't
958 # handle the case where the lib gets stuffed into a subpackage. :/
959 my @intprovlist = qx { find $pkgtree -type f -name "*.so*" };
960 my $provlist = '';
961 foreach (@intprovlist) {
962 s/$pkgtree//;
963 $provlist .= "$_";
964 }
965
966 my %reqs;
967 my $reqlibs = '';
968
969 foreach (@reqlist) {
970 next if /^$pkgtree/;
971 next if /not a dynamic executable/;
972 next if m|/lib/ld-linux.so|; # Hack! Hack! PTHBTT! (libc suxx0rz)
973
974 my ($req) = (/^\s+([a-z0-9._-]+)/); # dig out the actual library (so)name
975
976 # Ignore libs provided by this package. Note that we don't match
977 # on word-boundary at the *end* of the lib we're looking for, as the
978 # looked-for lib may not have the full soname version. (ie, it may
979 # "just" point to one of the symlinks that get created somewhere.)
980 next if $provlist =~ /\b$req/;
981
982 $reqlibs .= " $req";
983 }
984
985 if ($reqlibs ne '') {
986 foreach (qx { dpkg -S $reqlibs }) {
987 my ($libpkg,undef) = split /:\s+/;
988 $reqs{$libpkg} = 1;
989 }
990 }
991
992 my $deplist = '';
993 foreach (keys %reqs) {
994 $deplist .= ", $_";
995 }
996
997# For now, we're done. We're not going to meddle with versions yet.
998# Among other things, it's messier than handling "simple" yes/no "do
999# we have this lib?" deps. >:(
1000
1001 return $deplist;
1002} # end getreqs()
1003
1004
1005## install_sdeb()
1006# Extracts .sdeb contents to %_topdir as appropriate
1007sub install_sdeb {
1008 my $paxcmd = "cd $topdir; pax -r <$srcpkg; cd -";
1009
1010 # In case of %-macros...
1011 $paxcmd = expandmacros($paxcmd,'gp');
1012
1013 system "$paxcmd";
1014 print "Extracted source package $srcpkg to $topdir.\n";
1015} # end install_sdeb()
1016
1017
1018## expandmacros()
1019# Expands all %{blah} macros in the passed string
1020# Split up a bit with some sections so we don't spend time trying to
1021# expand macros that are only used in a few specific places.
1022sub expandmacros {
1023 my $macrostring = shift;
1024 my $section = shift;
1025
1026 # To allow the FHS-ish %configure and %makeinstall to work The Right Way.
1027 # (Without clobbering the global $buildroot.)
1028 my $prefix = '';
1029
1030 if ($section =~ /c/) {
1031 # %configure macro
1032# Don't know what it's for, don't have a useful default replacement
1033# --program-prefix=%{_program_prefix} \
1034 $macrostring =~ s'%configure'./configure --host=$DEB_HOST_GNU_TYPE \
1035 --build=$DEB_BUILD_GNU_TYPE \
1036 --prefix=%{_prefix} \
1037 --exec-prefix=%{_exec_prefix} \
1038 --bindir=%{_bindir} \
1039 --sbindir=%{_sbindir} \
1040 --sysconfdir=%{_sysconfdir} \
1041 --datadir=%{_datadir} \
1042 --includedir=%{_includedir} \
1043 --libdir=%{_libdir} \
1044 --libexecdir=%{_libexecdir} \
1045 --localstatedir=%{_localstatedir} \
1046 --sharedstatedir=%{_sharedstatedir} \
1047 --mandir=%{_mandir} \
1048 --infodir=%{_infodir} ';
1049 } # done %configure
1050
1051 if ($section =~ /m/) {
1052 $macrostring =~ s'%{__make}'make ';
1053 } # done make
1054
1055 if ($section =~ /i/) {
1056 # This is where we need to mangle $prefix.
1057 $macrostring =~ s'%makeinstall'make %{fhs} install';
1058 $prefix = $buildroot;
1059 } # done %install and/or %makeinstall
1060
1061 # Build data
1062 # Note that these are processed in reverse order to get the substitution order right
1063 if ($section =~ /b/) {
1064# $macrostring =~ s'%{fhs}'host=$DEB_HOST_GNU_TYPE \
1065# build=$DEB_BUILD_GNU_TYPE \
1066 $macrostring =~ s'%{fhs}'prefix=%{_prefix} \
1067 exec-prefix=%{_exec_prefix} \
1068 bindir=%{_bindir} \
1069 sbindir=%{_sbindir} \
1070 sysconfdir=%{_sysconfdir} \
1071 datadir=%{_datadir} \
1072 includedir=%{_includedir} \
1073 libdir=%{_libdir} \
1074 libexecdir=%{_libexecdir} \
1075 localstatedir=%{_localstatedir} \
1076 sharedstatedir=%{_sharedstatedir} \
1077 mandir=%{_mandir} \
1078 infodir=%{_infodir} \
1079';
1080
1081 # Note that the above regex terminates with the extra space
1082 # "Just In Case" of user additions, which will then get neatly
1083 # tagged on the end where they take precedence (supposedly)
1084 # over the "default" ones.
1085
1086 # Now we cascade the macros introduced above. >_<
1087 # Wot ot to go theah:
1088 $macrostring =~ s|%{_mandir}|%{_datadir}/man|g; #/usr/share/man
1089 $macrostring =~ s|%{_infodir}|%{_datadir}/info|g; #/usr/share/info
1090 $macrostring =~ s|%{_oldincludedir}|/usr/include|g; #/usr/include
1091 $macrostring =~ s|%{_includedir}|%{_prefix\}/include|g; #/usr/include
1092 $macrostring =~ s|%{_libdir}|%{_exec_prefix}/%{_lib}|g; #/usr/lib
1093 $macrostring =~ s|%{_lib}|lib|g; #?
1094 $macrostring =~ s|%{_localstatedir}|/var|g; #/var
1095 $macrostring =~ s|%{_sharedstatedir}|%{_prefix}/com|g; #/usr/com WTF?
1096 $macrostring =~ s|%{_sysconfdir}|/etc|g; #/etc
1097 $macrostring =~ s|%{_datadir}|%{_prefix}/share|g; #/usr/share
1098 $macrostring =~ s|%{_libexecdir}|%{_exec_prefix}/libexec|g; #/usr/libexec
1099 $macrostring =~ s|%{_sbindir}|%{_exec_prefix}/sbin|g; #/usr/sbin
1100 $macrostring =~ s|%{_bindir}|%{_exec_prefix}/bin|g; #/usr/bin
1101 $macrostring =~ s|%{_exec_prefix}|%{_prefix}|g; #/usr
1102 $macrostring =~ s|%{_prefix}|/usr|g; #/usr
1103 } # done with config section
1104
1105 # Package data
1106 if ($section =~ /p/) {
1107 $macrostring =~ s/\%\{buildroot\}/$buildroot/gi;
1108 foreach my $source (keys %{$pkgdata{sources}}) {
1109 $macrostring =~ s/\%\{source$source\}/$topdir\/SOURCES\/$pkgdata{sources}{$source}/gi;
1110 }
1111 $macrostring =~ s/\%\{name\}/$pkgdata{main}{name}/gi;
1112 $macrostring =~ s/\%\{version\}/$pkgdata{main}{version}/gi;
1113 $macrostring =~ s/\%\{release\}/$pkgdata{main}{release}/gi;
1114 }
1115
1116 # Globals, and not-so-globals
1117 if ($section =~ /g/) {
1118 $macrostring =~ s|%{_builddir}|%{_topdir}/BUILD|g;
1119 $macrostring =~ s|%{_topdir}|$topdir|g;
1120 $macrostring =~ s|%{_tmppath}|$tmpdir|g;
1121 $macrostring =~ s'%{_docdir}'/usr/share/doc'g;
1122
1123 # Standard FHS locations. More or less.
1124 $macrostring =~ s'%{_bindir}'/usr/bin'g;
1125 $macrostring =~ s'%{_sbindir}'/usr/sbin'g;
1126 $macrostring =~ s'%{_mandir}'/usr/share/man'g;
1127 $macrostring =~ s'%{_includedir}'/usr/include'g;
1128 $macrostring =~ s'%{_libdir}'/usr/lib'g;
1129 $macrostring =~ s'%{_sysconfdir}'/etc'g;
1130 $macrostring =~ s'%{_localstatedir}'/var'g;
1131
1132 # %define's
1133 foreach my $key (keys %specglobals) {
1134 $macrostring =~ s|%{$key}|$specglobals{$key}|g;
1135 }
1136
1137 # system programs. RPM uses a global config file for these; we'll just
1138 # ASS-U-ME and make life a little simpler.
1139 if ($macrostring =~ /\%\{\_\_([a-z0-9_-]+)\}/) {
1140 $macrostring =~ s|%{__([a-z0-9_-]+)}|$1|g;
1141 }
1142 } # done with globals section
1143
1144 return $macrostring;
1145} # end expandmacros()
1146
1147
1148
1149__END__
1150
1151
1152
1153=head1 NAME
1154
1155debbuild - Build Debian-compatible packages from RPM spec files
1156
1157=head1 SYNOPSIS
1158
1159 debbuild {-ba|-bb|-bp|-bc|-bi|-bl|-bs} [build-options] file.spec
1160
1161 debbuild {-ta|-tb|-tp|-tc|-ti|-tl|-ts} [build-options] file.tar.{gz|bz2}
1162
1163 debbuild --rebuild file.src.{rpm|deb}
1164
1165=head1 DESCRIPTION
1166
1167This script attempts to build Debian-friendly semi-native packages
1168from RPM spec files, RPM-friendly tarballs, and RPM source packages
1169(.src.rpm). It accepts I<most> of the options rpmbuild does, and
1170should be able to interpret most spec files usefully. Perl modules
1171should be handled via CPAN+dh-make-perl instead; Debian's conventions
1172for such things do not lend themselves to automated conversion.
1173
1174As far as possible, the command-line options are identical to those
1175from rpmbuild, although several rpmbuild options are not supported.
1176
1177=cut
Note: See TracBrowser for help on using the repository browser.