source: trunk/debbuild@ 38

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

/trunk

First iteration of build-deps and install reqs:

  • Build requirements are checked, including version requirements.
  • Specified "Requires" will be listed in the Depends field in the package's control file; subpackage Requires should be added the same way. Versioned Require:'s are NOT (yet) supported.
  • Automatic shlibs requirements are partially processed - package names are added to the list, but versions are NOT. (dpkg's tool dpkg-shlibdeps seems to generate versions on *some* libs, but not all, without any immediately obvious pattern.)
  • Provides are passed through as-is.

Requires:/Depends: and Provides: versioning are not supported because
of the format used for dpkg to specify versions.

Debian does not support dependencies on library sonames, just the containing package name
or a virtual package. :/ Any package providing a soname I want to depend on will have to
Provide: something suitable.

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