source: trunk/debbuild@ 36

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

/trunk

Source package creation functional. May need tweaking and/or changes.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 27.8 KB
Line 
1#!/usr/bin/perl
2# debbuild script
3# Shamlessly steals intreface from rpm's "rpmbuild" to create
4# Debian packages. Please note that such packages are highly
5# unlikely to conform to "Debian Policy".
6###
7# SVN revision info
8# $Date: 2006-02-16 17:13:40 +0000 (Thu, 16 Feb 2006) $
9# SVN revision $Rev: 36 $
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
97# 10/31/2005 Maybe this should be flatter? -kgd
98my %pkgdata;
99my @pkglist = ('main'); #sigh
100# Files listing. Embedding this in %pkgdata would be, um, messy.
101my %filelist;
102
103# Scriptlets
104my $prepscript;
105my $buildscript;
106# %install doesn't need the full treatment from %clean; just an empty place to install to.
107# NB - rpm doesn't do this; is it really necessary?
108my $installscript = '[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT'."\n";
109my $cleanscript;
110# pre/post (un)install scripts. Note that these will likely barf as-is. :/
111my $preinstscript = '';
112my $postinstscript = '';
113my $preuninstscript = '';
114my $postuninstscript = '';
115
116die "Not enough arguments\n" if #$argv == 0;
117
118# Snag some environment data
119my $tmpdir;
120if (defined $ENV{TMP} && $ENV{TMP} =~ /^(\/var)?\/tmp$/) {
121 $tmpdir = $ENV{TMP};
122} else {
123 $tmpdir = "/var/tmp";
124}
125
126##main
127
128load_userconfig();
129parse_cmd();
130
131if ($cmdopts{type} eq 'b') {
132 # Need to read the spec file to find the tarball. Note that
133 # this also generates most of the shell script required.
134 parse_spec();
135}
136
137# -> srcpkg if -.s
138if ($cmdopts{stage} eq 's') {
139 srcpackage();
140 exit 0;
141}
142
143# Hokay. Need to:
144# -> prep if -.p OR (-.[cilabs] AND !--short-circuit)
145if ($cmdopts{stage} eq 'p' || ($cmdopts{stage} =~ /[cilabs]/ && $cmdopts{short} ne 'y')) {
146 prep();
147}
148# -> build if -.c OR (-.[ilabs] AND !--short-circuit)
149if ($cmdopts{stage} eq 'c' || ($cmdopts{stage} =~ /[ilabs]/ && $cmdopts{short} ne 'y')) {
150 build();
151}
152# -> install if -.[ilabs]
153#if ($cmdopts{stage} eq 'i' || ($cmdopts{stage} =~ /[labs]/ && $cmdopts{short} ne 'y')) {
154if ($cmdopts{stage} =~ /[ilabs]/) {
155 install();
156#foreach my $pkg (@pkglist) {
157# print "files in $pkg:\n ".$filelist{$pkg}."\n";
158#}
159
160}
161# -> binpkg and srcpkg if -.a
162if ($cmdopts{stage} eq 'a') {
163 binpackage();
164 srcpackage();
165}
166# -> binpkg if -.b
167if ($cmdopts{stage} eq 'b') {
168 binpackage();
169}
170
171# Just in case.
172exit 0;
173
174
175## load_userconfig()
176# Loads user configuration (if any)
177# Currently only handles .debmacros
178# Needs to handle "other files"
179sub load_userconfig {
180 my (undef,undef,undef,undef,undef,undef,undef,$homedir,undef) = getpwuid($<);
181 if (-e "$homedir/.debmacros") {
182 open USERMACROS,"<$homedir/.debmacros";
183 while (<USERMACROS>) {
184 # And we also only handle a few macros at the moment.
185 if (/^\%_topdir/) {
186 my (undef,$tmp) = split /\s+/, $_;
187 $topdir = $tmp;
188 }
189 }
190 }
191} # end load_userconfig()
192
193
194## parse_cmd()
195# Parses command line into global hash %cmdopts, other globals
196# Options based on rpmbuild's options
197sub parse_cmd {
198 # Don't feel like coding my own option parser...
199 #use Getopt::Long;
200 # ... but I may have to: (OTOH, rpm uses popt, so maybe we can too.)
201 #use Getopt::Popt qw(:all);
202 # Or not. >:( Stupid Debian lack of findable Perl module names in packages.
203
204 # Stuff it.
205 my $prevopt = '';
206 foreach (@ARGV) {
207 chomp;
208
209 # Is it an option?
210 if (/^-/) {
211
212 # Is it a long option?
213 if (/^--/) {
214 if (/^--short-circuit/) {
215 $cmdopts{short} = 'y';
216 } elsif (/^--rebuild/) {
217 $cmdopts{type} = 's';
218 } else {
219 print "Long opt $_\n";
220 }
221 } else {
222 # Not a long option
223 if (/^-[bt]/) {
224 if ($cmdopts{stage} eq 's') {
225 # Mutually exclusive options.
226 die "Can't use $_ with --rebuild\n";
227 } else {
228 # Capture the type (from "bare" files or tarball) and the stage (prep, build, etc)
229 ($cmdopts{stage}) = (/^-[bt]([pcilabs])/);
230 ($cmdopts{type}) = (/^-([bt])[pcilabs]/);
231 }
232 } elsif (/^-v/) {
233 # bump verbosity. Not sure what I'll actually do here...
234 } else {
235 die "Bad option $_\n";
236 }
237 }
238
239 } else { # Not an option argument
240
241 # --buildroot is the only option that takes an argument
242 # Therefore, any *other* bare arguments are the spec file,
243 # tarball, or source package we're operating on - depending
244 # on which one we meet.
245 if ($prevopt eq '--buildroot') {
246 $cmdbuildroot = $_;
247 } else {
248 if ($cmdopts{type} eq 's') {
249 # Source package
250 if (!/\.src\.(deb|rpm)$/) {
251 die "Can't --rebuild with $_\n";
252 }
253 } elsif ($cmdopts{type} eq 'b') {
254 $specfile = $_;
255 # Spec file
256 } else {
257 # Tarball
258 }
259 }
260 }
261 $prevopt = $_;
262 } # foreach @ARGV
263
264 # Some cross-checks. rpmbuild limits --short-circuit to just
265 # the "compile" and "install" targets - with good reason IMO.
266 # Note that --short-circuit with -.p is not really an error, just redundant.
267 # NB - this is NOT fatal, just ignored!
268 if ($cmdopts{short} eq 'y' && $cmdopts{stage} =~ /[labs]/) {
269 warn "Can't use --short-circuit for $targets{$cmdopts{stage}} stage. Ignoring.\n";
270 $cmdopts{short} = 'n';
271 }
272
273 # Valid options, with example arguments (if any):
274# Build from .spec file; mutually exclusive:
275 # -bp
276 # -bc
277 # -bi
278 # -bl
279 # -ba
280 # -bb
281 # -bs
282# Build from tarball; mutually exclusive:
283 # -tp
284 # -tc
285 # -ti
286 # -ta
287 # -tb
288 # -ts
289# Build from .src.(deb|rpm)
290 # --rebuild
291 # --recompile
292
293# General options
294 # --buildroot=DIRECTORY
295 # --clean
296 # --nobuild
297 # --nodeps
298 # --nodirtokens
299 # --rmsource
300 # --rmspec
301 # --short-circuit
302 # --target=CPU-VENDOR-OS
303
304 #my $popt = new Getopt::Popt(argv => \@ARGV, options => \@optionsTable);
305
306} # end parse_cmd()
307
308
309## parse_spec()
310# Parse the .spec file.
311sub parse_spec {
312 open SPECFILE,"<$specfile";
313
314LINE: while (<SPECFILE>) {
315 next if /^#/; # Ignore comments...
316 next if /^\s+$/; # ... and blank lines.
317
318 if (/^\%/) {
319 # A macro that needs further processing.
320
321 if (/^\%define\s+([^\s]+)\s+([^\s]+)/) {
322 $specglobals{$1} = expandmacros($2,'g');
323 }
324
325 if (/^\%description(?:\s+(?:-n\s+)?([a-zA-Z0-9_.-]+))?/) {
326 my $subname = "main";
327 if ($1) {
328 if (/-n/) { $subname = $1; } else { $subname = "$pkgdata{main}{name}-$1"; }
329 }
330 while (<SPECFILE>) {
331 redo LINE if /^\%/;
332 $pkgdata{$subname}{desc} .= " $_";
333 }
334 }
335 if (/^\%package\s+(?:-n\s+)?([a-zA-Z0-9_.-]+)/) {
336 my $subname;
337 if (/-n/) { $subname = $1; } else { $subname = "$pkgdata{main}{name}-$1"; }
338 push @pkglist, $subname;
339 $pkgdata{$subname}{name} = $subname;
340 $pkgdata{$subname}{version} = $pkgdata{main}{version};
341 while (<SPECFILE>) {
342 redo LINE if /^\%/;
343 if (my ($dname,$dvalue) = (/^(Summary|Group|Version):\s+(.+)$/i)) {
344 $dname =~ tr/[A-Z]/[a-z]/;
345 $pkgdata{$subname}{$dname} = $dvalue;
346 }
347 }
348 }
349
350 if (/^\%prep/) {
351 # %prep section. May have %setup macro; may include %patch tags,
352 # may be just a bare shell script.
353
354 # This really should be local-ish, but we need just the filename for the source
355 $pkgdata{main}{source} =~ s|.+/([^/]+)$|$1|;
356
357 # Replace some core macros
358 $pkgdata{main}{source} = expandmacros($pkgdata{main}{source},'gp');
359
360PREPSCRIPT: while (<SPECFILE>) {
361 if (/^\%setup/) {
362 # Parse out the %setup macro. Note that we aren't supporting
363 # many of RPM's %setup features.
364 $prepscript .= "cd $topdir/BUILD\n";
365 if ( /\s+-n\s+([^\s]+)\s+/ ) {
366 $tarballdir = $1;
367 } else {
368 $tarballdir = "$pkgdata{main}{name}-$pkgdata{main}{version}";
369 }
370 $prepscript .= "rm -rf $tarballdir\ntar -".
371 ( $pkgdata{main}{source} =~ /\.tar\.gz$/ ? "z" : "" ).
372 ( $pkgdata{main}{source} =~ /\.tar\.bz2$/ ? "j" : "" ).
373 ( /\s+-q\s+/ ? '' : 'vv' )."xf ".
374 "$topdir/SOURCES/$pkgdata{main}{source}\n".
375 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n).
376 ( /\s+-n\s+([^\s]+)\s+/ ?
377 "cd $1\n" : "cd $pkgdata{main}{name}-$pkgdata{main}{version}\n" ).
378 qq([ `/usr/bin/id -u` = '0' ] && /bin/chown -Rhf root .\n).
379 qq([ `/usr/bin/id -u` = '0' ] && /bin/chgrp -Rhf root .\n).
380 qq(/bin/chmod -Rf a+rX,g-w,o-w .\n);
381 } elsif (/^\%patch([^:]+)\s+(.+)$/) {
382 $prepscript .= "patch $2 <$topdir/SOURCES/".$pkgdata{main}{"patch$1"}."\n";
383 } else {
384 last PREPSCRIPT if /^\%/;
385 $prepscript .= $_;
386 }
387 }
388 redo LINE;
389 }
390 if (/^\%build/) {
391 # %build. This is pretty much just a shell script. There
392 # *are* a few macros, but we're not going to deal with them yet.
393 $buildscript .= "cd $tarballdir\n";
394BUILDSCRIPT: while (<SPECFILE>) {
395 if (/^\%configure/) {
396 $buildscript .= expandmacros($_,'cgbp');
397 } elsif (/^\%\{__make\}/) {
398 $buildscript .= expandmacros($_,'mgbp');
399 } else {
400 last BUILDSCRIPT if /^\%[^{]/;
401 $buildscript .= $_;
402 }
403 }
404 redo LINE;
405 }
406 if (/^\%install/) {
407 $installscript .= "cd $tarballdir\n";
408INSTALLSCRIPT: while (<SPECFILE>) {
409 if (/^\%makeinstall/) {
410 $installscript .= expandmacros($_,'igbp');
411 } else {
412 last INSTALLSCRIPT if /^\%/;
413 $installscript .= $_;
414 }
415 }
416 redo LINE;
417 }
418 if (/^\%clean/) {
419 while (<SPECFILE>) {
420 redo LINE if /^\%/;
421 $cleanscript .= $_;
422 }
423 $cleanscript = expandmacros($cleanscript,'gp');
424 }
425
426 # pre/post (un)install scripts
427 if (/^\%pre\b/) {
428 while (<SPECFILE>) {
429 redo LINE if /^\%/;
430 $preinstscript .= $_;
431 }
432 }
433 if (/^\%post\b/) {
434 while (<SPECFILE>) {
435 redo LINE if /^\%/;
436 $postinstscript .= $_;
437 }
438 }
439 if (/^\%preun\b/) {
440 while (<SPECFILE>) {
441 redo LINE if /^\%/;
442 $preuninstscript .= $_;
443 }
444 }
445 if (/^\%postun\b/) {
446 while (<SPECFILE>) {
447 redo LINE if /^\%/;
448 $postuninstscript .= $_;
449 }
450 }
451 # done %pre/%post scripts
452
453 if (/^\%files(?:\s+(?:-n\s+)?([a-zA-z0-9]+))?/) {
454 my $pkgname = 'main';
455 if ($1) { # Magic to add entries to the right list of files
456 if (/-n/) { $pkgname = $1; } else { $pkgname = "$pkgdata{main}{name}-$1"; }
457 }
458 while (<SPECFILE>) {
459 chomp;
460 # need to update this to deal (properly) with %dir, %attr, etc
461 next if /^\%dir/;
462 next if /^\%attr/;
463 next if /^\%defattr/;
464
465 # and finally we can fall through %{_<FHS>}-prefixed locations...
466 if (/^\%\{_/) {
467 $filelist{$pkgname} .= " $_";
468 next;
469 }
470 # EW. Necessary to clear up %define expansions before we exit with redo.
471 $_ = expandmacros $_, 'g';
472
473 # ... unknown or "next section" % directives ...
474 redo LINE if /^\%/;
475
476 # ... and "normal" files
477 $filelist{$pkgname} .= " $_";
478 }
479 $filelist{$pkgname} = expandmacros($filelist{$pkgname}, 'g');
480 } # done %file section
481
482 if (/^\%changelog/) {
483 $pkgdata{main}{changelog} = '';
484 while (<SPECFILE>) {
485 redo LINE if /^\%/;
486 $pkgdata{main}{changelog} .= $_;
487 }
488 }
489
490 } else { # Data from the spec file "header"
491
492 if (/^summary:\s+(.+)/i) {
493 $pkgdata{main}{summary} = $1;
494 } elsif (/^name:\s+(.+)/i) {
495 $pkgdata{main}{name} = expandmacros($1,'g');
496 } elsif (/^version:\s+(.+)/i) {
497 $pkgdata{main}{version} = expandmacros($1,'g');
498 } elsif (/^release:\s+(.+)/i) {
499 $pkgdata{main}{release} = expandmacros($1,'g');
500 } elsif (/^group:\s+(.+)/i) {
501 $pkgdata{main}{group} = $1;
502 } elsif (/^copyright:\s+(.+)/i) {
503 $pkgdata{main}{copyright} = $1;
504 } elsif (/^url:\s+(.+)/i) {
505 $pkgdata{main}{url} = $1;
506 } elsif (/^packager:\s+(.+)/i) {
507 $pkgdata{main}{packager} = $1;
508 } elsif (/^buildroot:\s+(.+)/i) {
509 $buildroot = $1;
510 } elsif (/^source:\s+(.+)/i) {
511 $pkgdata{main}{source} = $1;
512 die "Unknown tarball format $1\n" if $1 !~ /\.tar\.(?:gz|bz2)$/;
513 } elsif (/^source([0-9]+):\s+(.+)/i) {
514 $pkgdata{sources}{$1} = $2;
515 } elsif (/^patch([^:]+):\s+(.+)$/i) {
516 my $patchname = "patch$1";
517 $pkgdata{main}{$patchname} = $2;
518 if ($pkgdata{main}{$patchname} =~ /\//) {
519 # URL-style patch. Rare but not unheard-of.
520 my @patchbits = split '/', $pkgdata{main}{$patchname};
521 $pkgdata{main}{$patchname} = $patchbits[$#patchbits];
522 }
523 }
524#Name: suwrap
525#Version: 0.04
526#Release: 3
527#Group: Applications/System
528#Copyright: WebHart internal ONLY. :(
529#BuildArchitectures: i386
530#BuildRoot: /tmp/%{name}-%{version}
531#Url: http://virtual.webhart.net
532#Packager: Kris Deugau <kdeugau@deepnet.cx>
533#Source: ftp://virtual.webhart.net/%{name}-%{version}.tar.gz
534
535 }
536 }
537
538 # Parse and replace some more macros. More will be replaced even later.
539
540 # Expand macros as necessary.
541 $scriptletbase = expandmacros($scriptletbase,'gp');
542
543 $buildroot = $cmdbuildroot if $cmdbuildroot;
544 $buildroot = expandmacros($buildroot,'gp');
545
546 close SPECFILE;
547} # end parse_spec()
548
549
550## prep()
551# Writes and executes the %prep script (mostly) built while reading the spec file.
552sub prep {
553 # Replace some things here just to make sure.
554 $prepscript = expandmacros($prepscript,'gp');
555
556#print $prepscript; exit 0;
557
558 # create script filename
559 my $prepscriptfile = "$tmpdir/deb-tmp.prep.".int(rand(99998)+1);
560 sysopen(PREPSCRIPT, $prepscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
561 or die $!;
562 print PREPSCRIPT $scriptletbase;
563 print PREPSCRIPT $prepscript;
564 close PREPSCRIPT;
565
566 # execute
567 print "Calling \%prep script $prepscriptfile...\n";
568 system("/bin/sh -e $prepscriptfile") == 0
569 or die "Can't exec: $!\n";
570
571 # and clean up
572 unlink $prepscriptfile;
573} # end prep()
574
575
576## build()
577# Writes and executes the %build script (mostly) built while reading the spec file.
578sub build {
579 # Expand the macros
580 $buildscript = expandmacros($buildscript,'cgbp');
581
582 # create script filename
583 my $buildscriptfile = "$tmpdir/deb-tmp.build.".int(rand(99998)+1);
584 sysopen(BUILDSCRIPT, $buildscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
585 or die $!;
586 print BUILDSCRIPT $scriptletbase;
587 print BUILDSCRIPT $buildscript;
588 close BUILDSCRIPT;
589
590 # execute
591 print "Calling \%build script $buildscriptfile...\n";
592 system("/bin/sh -e $buildscriptfile") == 0
593 or die "Can't exec: $!\n";
594
595 # and clean up
596 unlink $buildscriptfile;
597} # end build()
598
599
600## install()
601# Writes and executes the %install script (mostly) built while reading the spec file.
602sub install {
603 # Expand the macros
604 $installscript = expandmacros($installscript,'igbp');
605
606 # create script filename
607 my $installscriptfile = "$tmpdir/deb-tmp.inst.".int(rand(99998)+1);
608 sysopen(INSTSCRIPT, $installscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
609 or die $!;
610 print INSTSCRIPT $scriptletbase;
611# print INSTSCRIPT $cleanscript; # Clean up our install target before installing into it.
612 print INSTSCRIPT $installscript;
613 close INSTSCRIPT;
614
615 # execute
616 print "Calling \%install script $installscriptfile...\n";
617 system("/bin/sh -e $installscriptfile") == 0
618 or die "Can't exec: $!\n";
619
620 # and clean up
621 unlink $installscriptfile;
622} # end install()
623
624
625## binpackage()
626# Creates the binary .deb package from the installed tree in $buildroot.
627# Writes and executes a shell script to do so.
628# Creates miscellaneous files required by dpkg-deb to actually build the package file.
629# Should handle simple subpackages
630sub binpackage {
631 # Make sure we have somewhere to write the .deb file
632 if (!-e "$topdir/DEBS/i386") {
633 mkdir "$topdir/DEBS/i386";
634 }
635
636##work
637 foreach my $pkg (@pkglist) {
638
639 # Gotta do this first, otherwise we don't have a place to move files from %files
640 mkdir "$buildroot/$pkg";
641
642 # Eliminate any lingering % macros
643 $filelist{$pkg} = expandmacros $filelist{$pkg}, 'g';
644
645 my @pkgfilelist = split ' ', $filelist{$pkg};
646 foreach my $pkgfile (@pkgfilelist) {
647 my @filepath = ($pkgfile =~ m|(.+)/([^/]+)$|);
648 qx { mkdir -p $buildroot/$pkg$filepath[0] }
649 if $filepath[0] ne '';
650 qx { mv $buildroot$pkgfile $buildroot/$pkg$filepath[0] };
651 }
652
653 # Gotta do this next, otherwise the control file has nowhere to go. >:(
654 mkdir "$buildroot/$pkg/DEBIAN";
655
656 # Hack the filename for the package into a Debian-tool-compatible format. GRRRRRR!!!!!
657 # Have I mentioned I hate Debian Policy?
658 $pkgdata{$pkg}{name} =~ tr/_/-/;
659
660 # create script filename
661 my $debscriptfile = "$tmpdir/deb-tmp.pkg.".int(rand(99998)+1);
662 sysopen(DEBSCRIPT, $debscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
663 or die $!;
664 print DEBSCRIPT $scriptletbase;
665 print DEBSCRIPT "fakeroot dpkg-deb -b $buildroot/$pkg $topdir/DEBS/i386/".
666 "$pkgdata{$pkg}{name}_$pkgdata{$pkg}{version}-$pkgdata{main}{release}_i386.deb\n";
667 # %$&$%@#@@#%@@@ Debian and their horrible ugly package names. >:(
668 close DEBSCRIPT;
669
670 my $control = "Package: $pkgdata{$pkg}{name}\n".
671 "Version: $pkgdata{$pkg}{version}-$pkgdata{main}{release}\n".
672 "Section: $pkgdata{$pkg}{group}\n".
673 "Priority: optional\n".
674 "Architecture: i386\n".
675 "Maintainer: $pkgdata{main}{packager}\n".
676 "Description: $pkgdata{$pkg}{summary}\n";
677 $control .= "$pkgdata{$pkg}{desc}\n";
678
679 open CONTROL, ">$buildroot/$pkg/DEBIAN/control";
680 print CONTROL $control;
681 close CONTROL;
682
683 # Can't see much point in scripts on subpackages... although since
684 # it's *possible* I should support it at some point.
685 if ($pkg eq 'main') {
686 if ($preinstscript ne '') {
687 $preinstscript = expandmacros($preinstscript,'g');
688 open PREINST, ">$buildroot/$pkg/DEBIAN/preinst";
689 print PREINST "#!/bin/sh\nset -e\n\n";
690 print PREINST $preinstscript;
691 close PREINST;
692 `chmod 0755 $buildroot/$pkg/DEBIAN/preinst`;
693 }
694 if ($postinstscript ne '') {
695 $postinstscript = expandmacros($postinstscript,'g');
696 open POSTINST, ">$buildroot/$pkg/DEBIAN/postinst";
697 print POSTINST "#!/bin/sh\nset -e\n\n";
698 print POSTINST $postinstscript;
699 close POSTINST;
700 `chmod 0755 $buildroot/$pkg/DEBIAN/postinst`;
701 }
702 if ($preuninstscript ne '') {
703 $preuninstscript = expandmacros($preuninstscript,'g');
704 open PREUNINST, ">$buildroot/$pkg/DEBIAN/prerm";
705 print PREUNINST "#!/bin/sh\nset -e\n\n";
706 print PREUNINST $preuninstscript;
707 close PREUNINST;
708 `chmod 0755 $buildroot/$pkg/DEBIAN/prerm`;
709 }
710 if ($postuninstscript ne '') {
711 $postuninstscript = expandmacros($postuninstscript,'g');
712 open POSTUNINST, ">$buildroot/$pkg/DEBIAN/postrm";
713 print POSTUNINST "#!/bin/sh\nset -e\n\n";
714 print POSTUNINST $postuninstscript;
715 close POSTUNINST;
716 `chmod 0755 $buildroot/$pkg/DEBIAN/postrm`;
717 }
718 }
719
720#print `ls -l $buildroot/DEBIAN`;
721
722#Package: httpd
723#Version: 2.0.54-7via
724#Section: unknown
725#Priority: optional
726#Architecture: i386
727#Depends: libc6 (>= 2.3.2.ds1-21), libdb4.2, libexpat1 (>= 1.95.8), libssl0.9.7, libapr0
728#Replaces: apache2
729#Installed-Size: 3076
730#Maintainer: Kris Deugau <kdeugau@vianet.ca>
731#Description: apache2 for ViaNet
732# apache2 for ViaNet. Includes per-vhost setuid patches from
733# http://home.samfundet.no/~sesse/mpm-itk/.
734
735 # execute
736 print "Calling package creation script $debscriptfile for $pkgdata{$pkg}{name}...\n";
737 system("/bin/sh -e $debscriptfile") == 0
738 or die "Can't exec: $!\n";
739
740 # and clean up
741 unlink $debscriptfile;
742
743 } # subpackage loop
744
745} # end binpackage()
746
747
748## srcpackage()
749# Builds a .src.deb source package. Note that Debian's idea of
750# a "source package" is seriously flawed IMO, because you can't
751# easily copy it as-is.
752# Not quite identical to RPM, but Good Enough (TM).
753sub srcpackage {
754 my $pkgsrcname = "$pkgdata{main}{name}-$pkgdata{main}{version}-$pkgdata{main}{release}.sdeb";
755
756 my $paxcmd;
757
758 # We'll definitely need this later, and *may* need it sooner.
759 (my $barespec = $specfile) =~ s|.+/([^/]+)$|$1|;
760
761 # Copy the specfile to the build tree, but only if it's not there already.
762##buglet: need to deal with silly case where silly user has put the spec
763# file in a subdir of %{_topdir}/SPECS. Ewww. Silly user!
764 if (abs_path($specfile) !~ /^$topdir\/SPECS/) {
765 $paxcmd .= "cp $specfile %{_topdir}/SPECS/; \n"
766 }
767
768 # use pax -w [file] [file] ... >outfile.sdeb
769 $paxcmd = "cd $topdir; pax -w ";
770
771# tweak source entry into usable form. Need it locally somewhere along the line.
772 (my $pkgsrc = $pkgdata{main}{source}) =~ s|.+/([^/]+)$|$1|;
773 $paxcmd .= "SOURCES/$pkgsrc ";
774
775 # create file list: Source[nn], Patch[nn]
776 foreach my $specbit (keys %{$pkgdata{main}} ) {
777 next if $specbit eq 'source';
778 $paxcmd .= "SOURCES/$pkgdata{main}{$specbit} " if $specbit =~ /^(source|patch)/;
779##buglet: need to deal with case where patches are listed as URLs?
780# or other extended pathnames? Silly !@$%^&!%%!%!! user!
781 }
782
783 # add the spec file, source package destination, and cd back where we came from.
784 $paxcmd .= "SPECS/$barespec > $topdir/SDEBS/$pkgsrcname; cd -";
785
786 # In case of %-macros...
787 $paxcmd = expandmacros($paxcmd,'gp');
788
789 system "$paxcmd";
790 print "Wrote source package $pkgsrcname in $topdir/SDEBS.\n";
791}
792
793
794## expandmacros()
795# Expands all %{blah} macros in the passed string
796# Split up a bit with some sections so we don't spend time trying to
797# expand macros that are only used in a few specific places.
798sub expandmacros {
799 my $macrostring = shift;
800 my $section = shift;
801
802 # To allow the FHS-ish %configure and %makeinstall to work The Right Way.
803 # (Without clobbering the global $buildroot.)
804 my $prefix = '';
805
806 if ($section =~ /c/) {
807 # %configure macro
808# Don't know what it's for, don't have a useful default replacement
809# --program-prefix=%{_program_prefix} \
810 $macrostring =~ s'%configure'./configure --host=$DEB_HOST_GNU_TYPE \
811 --build=$DEB_BUILD_GNU_TYPE \
812 --prefix=%{_prefix} \
813 --exec-prefix=%{_exec_prefix} \
814 --bindir=%{_bindir} \
815 --sbindir=%{_sbindir} \
816 --sysconfdir=%{_sysconfdir} \
817 --datadir=%{_datadir} \
818 --includedir=%{_includedir} \
819 --libdir=%{_libdir} \
820 --libexecdir=%{_libexecdir} \
821 --localstatedir=%{_localstatedir} \
822 --sharedstatedir=%{_sharedstatedir} \
823 --mandir=%{_mandir} \
824 --infodir=%{_infodir} ';
825 } # done %configure
826
827 if ($section =~ /m/) {
828 $macrostring =~ s'%{__make}'make ';
829 } # done make
830
831 if ($section =~ /i/) {
832 # This is where we need to mangle $prefix.
833 $macrostring =~ s'%makeinstall'make %{fhs} install';
834 $prefix = $buildroot;
835 } # done %install and/or %makeinstall
836
837 # Build data
838 # Note that these are processed in reverse order to get the substitution order right
839 if ($section =~ /b/) {
840# $macrostring =~ s'%{fhs}'host=$DEB_HOST_GNU_TYPE \
841# build=$DEB_BUILD_GNU_TYPE \
842 $macrostring =~ s'%{fhs}'prefix=%{_prefix} \
843 exec-prefix=%{_exec_prefix} \
844 bindir=%{_bindir} \
845 sbindir=%{_sbindir} \
846 sysconfdir=%{_sysconfdir} \
847 datadir=%{_datadir} \
848 includedir=%{_includedir} \
849 libdir=%{_libdir} \
850 libexecdir=%{_libexecdir} \
851 localstatedir=%{_localstatedir} \
852 sharedstatedir=%{_sharedstatedir} \
853 mandir=%{_mandir} \
854 infodir=%{_infodir} \
855';
856
857 # Note that the above regex terminates with the extra space
858 # "Just In Case" of user additions, which will then get neatly
859 # tagged on the end where they take precedence (supposedly)
860 # over the "default" ones.
861
862 # Now we cascade the macros introduced above. >_<
863 # Wot ot to go theah:
864 $macrostring =~ s|%{_mandir}|%{_datadir}/man|g; #/usr/share/man
865 $macrostring =~ s|%{_infodir}|%{_datadir}/info|g; #/usr/share/info
866 $macrostring =~ s|%{_oldincludedir}|/usr/include|g; #/usr/include
867 $macrostring =~ s|%{_includedir}|%{_prefix\}/include|g; #/usr/include
868 $macrostring =~ s|%{_libdir}|%{_exec_prefix}/%{_lib}|g; #/usr/lib
869 $macrostring =~ s|%{_lib}|lib|g; #?
870 $macrostring =~ s|%{_localstatedir}|/var|g; #/var
871 $macrostring =~ s|%{_sharedstatedir}|%{_prefix}/com|g; #/usr/com WTF?
872 $macrostring =~ s|%{_sysconfdir}|/etc|g; #/etc
873 $macrostring =~ s|%{_datadir}|%{_prefix}/share|g; #/usr/share
874 $macrostring =~ s|%{_libexecdir}|%{_exec_prefix}/libexec|g; #/usr/libexec
875 $macrostring =~ s|%{_sbindir}|%{_exec_prefix}/sbin|g; #/usr/sbin
876 $macrostring =~ s|%{_bindir}|%{_exec_prefix}/bin|g; #/usr/bin
877 $macrostring =~ s|%{_exec_prefix}|%{_prefix}|g; #/usr
878 $macrostring =~ s|%{_prefix}|/usr|g; #/usr
879 } # done with config section
880
881 # Package data
882 if ($section =~ /p/) {
883 $macrostring =~ s/\%\{buildroot\}/$buildroot/gi;
884 foreach my $source (keys %{$pkgdata{sources}}) {
885 $macrostring =~ s/\%\{source$source\}/$topdir\/SOURCES\/$pkgdata{sources}{$source}/gi;
886 }
887 $macrostring =~ s/\%\{name\}/$pkgdata{main}{name}/gi;
888 $macrostring =~ s/\%\{version\}/$pkgdata{main}{version}/gi;
889 $macrostring =~ s/\%\{release\}/$pkgdata{main}{release}/gi;
890 }
891
892 # Globals, and not-so-globals
893 if ($section =~ /g/) {
894 $macrostring =~ s|%{_builddir}|%{_topdir}/BUILD|g;
895 $macrostring =~ s|%{_topdir}|$topdir|g;
896 $macrostring =~ s|%{_tmppath}|$tmpdir|g;
897 $macrostring =~ s'%{_docdir}'/usr/share/doc'g;
898
899 # Standard FHS locations. More or less.
900 $macrostring =~ s'%{_bindir}'/usr/bin'g;
901 $macrostring =~ s'%{_sbindir}'/usr/sbin'g;
902 $macrostring =~ s'%{_mandir}'/usr/share/man'g;
903 $macrostring =~ s'%{_includedir}'/usr/include'g;
904 $macrostring =~ s'%{_libdir}'/usr/lib'g;
905 $macrostring =~ s'%{_sysconfdir}'/etc'g;
906 $macrostring =~ s'%{_localstatedir}'/var'g;
907
908 # %define's
909 foreach my $key (keys %specglobals) {
910 $macrostring =~ s|%{$key}|$specglobals{$key}|g;
911 }
912
913 # system programs. RPM uses a global config file for these; we'll just
914 # ASS-U-ME and make life a little simpler.
915 if ($macrostring =~ /\%\{\_\_([a-z0-9_-]+)\}/) {
916 $macrostring =~ s|%{__([a-z0-9_-]+)}|$1|g;
917 }
918 } # done with globals section
919
920 return $macrostring;
921} # end expandmacros()
922
923
924
925=head1 NAME
926
927debbuild - Build Debian-compatible packages from RPM spec files
928
929=head1 SYNOPSIS
930
931 debbuild {-ba|-bb|-bp|-bc|-bi|-bl|-bs} [build-options] file.spec
932
933 debbuild {-ta|-tb|-tp|-tc|-ti|-tl|-ts} [build-options] file.tar.{gz|bz2}
934
935 debbuild --rebuild file.src.{rpm|deb}
936
937=head1 DESCRIPTION
938
939This script attempts to build Debian-friendly semi-native packages
940from RPM spec files, RPM-friendly tarballs, and RPM source packages
941(.src.rpm). It accepts I<most> of the options rpmbuild does, and
942should be able to interpret most spec files usefully. Perl modules
943should be handled via CPAN+dh-make-perl instead; Debian's conventions
944for such things do not lend themselves to automated conversion.
945
946As far as possible, the command-line options are identical to those
947from rpmbuild, although several rpmbuild options are not supported.
948
949=cut
Note: See TracBrowser for help on using the repository browser.