source: trunk/debbuild@ 40

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

/trunk

Fugbix: 1 is true, and $buildreq must be initialized.

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