source: trunk/debbuild@ 65

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

/trunk

Fixed the bug from the bugfix of the bug.... in Depends processing
Added basic -t support; functional but probably has a few bugs.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 41.2 KB
RevLine 
[2]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-06-06 21:46:56 +0000 (Tue, 06 Jun 2006) $
9# SVN revision $Rev: 65 $
10# Last update by $Author: kdeugau $
11###
[55]12# Copyright 2005,2006 Kris Deugau <kdeugau@deepnet.cx>
[56]13#
14# This program is free software; you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation; either version 2 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
[2]27
[5]28use strict;
[6]29use warnings;
[7]30use Fcntl; # for sysopen flags
[36]31use Cwd 'abs_path'; # for finding where files really are
[5]32
[10]33# regex debugger
34#use re "debug";
35
[3]36# Program flow:
37# -> Parse/execute "system" config/macros (if any - should be rare)
38# -> Parse/execute "user" config/macros (if any - *my* requirement is %_topdir)
39# -> Parse command line for options, spec file/tarball/.src.deb (NB - also accept .src.rpm)
40
[30]41sub expandmacros;
42
[2]43# User's prefs for dirs, environment, etc,etc,etc.
44# config file ~/.debmacros
45# Default ordered search paths for config/macros:
46# /usr/lib/rpm/rpmrc /usr/lib/rpm/redhat/rpmrc /etc/rpmrc ~/.rpmrc
47# /usr/lib/rpm/macros /usr/lib/rpm/redhat/macros /etc/rpm/macros ~/.rpmmacros
48# **NOTE: May be possible to (ab)use bits of debhelper
49
50# Build tree
51# default is /usr/src/debian/{BUILD,SOURCES,SPECS,DEBS,SDEBS}
52
[3]53# Globals
54my $specfile;
55my $tarball;
56my $srcpkg;
[7]57my $cmdbuildroot;
58my $tarballdir; # This should really be initialized, but the coding makes it, um, ugly.
[23]59my %specglobals; # For %define's in specfile, among other things.
[6]60
61# Initialized globals
[3]62my $verbosity = 0;
[4]63my %cmdopts = (type => '',
64 stage => 'a',
65 short => 'n'
66 );
[6]67my $topdir = "/usr/src/debian";
[13]68my $buildroot = "%{_tmppath}/%{name}-%{version}-%{release}.root".int(rand(99998)+1);
[6]69
70# "Constants"
[4]71my %targets = ('p' => 'Prep',
72 'c' => 'Compile',
73 'i' => 'Install',
74 'l' => 'Verify %files',
75 'a' => 'Build binary and source',
76 'b' => 'Build binary',
77 's' => 'Build source'
78 );
[6]79my $scriptletbase =
80q(#!/bin/sh
[3]81
[6]82 RPM_SOURCE_DIR="%{_topdir}/SOURCES"
83 RPM_BUILD_DIR="%{_topdir}/BUILD"
84 RPM_OPT_FLAGS="-O2 -g -march=i386 -mcpu=i686"
85 RPM_ARCH="i386"
86 RPM_OS="linux"
87 export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS
88 RPM_DOC_DIR="/usr/share/doc"
89 export RPM_DOC_DIR
90 RPM_PACKAGE_NAME="%{name}"
91 RPM_PACKAGE_VERSION="%{version}"
92 RPM_PACKAGE_RELEASE="%{release}"
93 export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE
94 RPM_BUILD_ROOT="%{buildroot}"
95 export RPM_BUILD_ROOT
[10]96);
97foreach (`dpkg-architecture`) {
98 s/=(.+)/="$1"/;
99 $scriptletbase .= " $_";
100}
101$scriptletbase .=
102q(
[6]103 set -x
104 umask 022
105 cd %{_topdir}/BUILD
106);
107
[5]108# Package data
109# This is the form of $pkgdata{pkgname}{meta}
110# meta includes Summary, Name, Version, Release, Group, Copyright,
[38]111# Source, URL, Packager, BuildRoot, Description, BuildReq(uires),
112# Requires, Provides
[6]113# 10/31/2005 Maybe this should be flatter? -kgd
[5]114my %pkgdata;
[27]115my @pkglist = ('main'); #sigh
[23]116# Files listing. Embedding this in %pkgdata would be, um, messy.
117my %filelist;
[40]118my $buildreq = '';
[5]119
[6]120# Scriptlets
[48]121my $prepscript = '';
122my $buildscript = '';
[23]123# %install doesn't need the full treatment from %clean; just an empty place to install to.
124# NB - rpm doesn't do this; is it really necessary?
125my $installscript = '[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT'."\n";
[48]126my $cleanscript = '';
[16]127# pre/post (un)install scripts. Note that these will likely barf as-is. :/
128my $preinstscript = '';
129my $postinstscript = '';
130my $preuninstscript = '';
131my $postuninstscript = '';
[6]132
[5]133die "Not enough arguments\n" if #$argv == 0;
134
[13]135# Snag some environment data
136my $tmpdir;
137if (defined $ENV{TMP} && $ENV{TMP} =~ /^(\/var)?\/tmp$/) {
138 $tmpdir = $ENV{TMP};
139} else {
140 $tmpdir = "/var/tmp";
141}
142
[7]143##main
144
[4]145load_userconfig();
[3]146parse_cmd();
147
[50]148if ($cmdopts{install}) {
149 install_sdeb();
150 exit 0;
151}
152
153# Stick --rebuild handling in here - basically install_sdeb()
154# followed by tweaking options to run with -ba
[54]155if ($cmdopts{type} eq 's') {
[55]156 if ($srcpkg =~ /\.src\.rpm$/) {
[58]157 my @srclist = qx { rpm -qlp $srcpkg };
[55]158 foreach (@srclist) {
[54]159 chomp;
[55]160 $specfile = "$topdir/SPECS/$_" if /\.spec$/;
[54]161 }
[55]162 qx { rpm -i $srcpkg };
163 } else {
164 install_sdeb();
165 my @srclist = qx { pax < $srcpkg };
166 foreach (@srclist) {
167 chomp;
168 $specfile = "$topdir/$_" if /SPECS/;
169 }
[54]170 }
[55]171 $cmdopts{type} = 'b';
172 $cmdopts{stage} = 'a';
[54]173}
[50]174
[7]175if ($cmdopts{type} eq 'b') {
176 # Need to read the spec file to find the tarball. Note that
177 # this also generates most of the shell script required.
178 parse_spec();
[38]179 die "Can't build $pkgdata{main}{name}: build requirements not met.\n"
180 if !checkbuildreq();
[7]181}
182
[65]183if ($cmdopts{type} eq 't') {
184 # Need to unpack the tarball to find the spec file. Sort of the inverse of -b above.
185 # zcat $tarball |tar -t |grep .spec
186 # collect some info about the tarball
187 $specfile = "$topdir/BUILD/". qx { zcat $tarball |tar -t |grep .spec\$ };
188 chomp $specfile;
189 my ($fileonly, $dirname) = ($tarball =~ /(([a-zA-Z0-9._-]+)\.tar\.(?:gz|bz2))$/);
190
191 $tarball = abs_path($tarball);
192 my $unpackcmd = "cd $topdir/BUILD; tar -".
193 ( $tarball =~ /\.tar\.gz$/ ? "z" : "" ).
194 ( $tarball =~ /\.tar\.bz2$/ ? "j" : "" ). "xf $tarball";
195 system "$unpackcmd";
196 system "cp $tarball $topdir/SOURCES/$fileonly";
197 system "cp $specfile $topdir/SPECS/";
198 parse_spec();
199 die "Can't build $pkgdata{main}{name}: build requirements not met.\n"
200 if !checkbuildreq();
201}
202
[35]203# -> srcpkg if -.s
204if ($cmdopts{stage} eq 's') {
205 srcpackage();
206 exit 0;
207}
208
[4]209# Hokay. Need to:
[14]210# -> prep if -.p OR (-.[cilabs] AND !--short-circuit)
[7]211if ($cmdopts{stage} eq 'p' || ($cmdopts{stage} =~ /[cilabs]/ && $cmdopts{short} ne 'y')) {
212 prep();
213}
[14]214# -> build if -.c OR (-.[ilabs] AND !--short-circuit)
[7]215if ($cmdopts{stage} eq 'c' || ($cmdopts{stage} =~ /[ilabs]/ && $cmdopts{short} ne 'y')) {
216 build();
217}
[14]218# -> install if -.[ilabs]
[28]219#if ($cmdopts{stage} eq 'i' || ($cmdopts{stage} =~ /[labs]/ && $cmdopts{short} ne 'y')) {
220if ($cmdopts{stage} =~ /[ilabs]/) {
[7]221 install();
[30]222#foreach my $pkg (@pkglist) {
223# print "files in $pkg:\n ".$filelist{$pkg}."\n";
224#}
225
[7]226}
[14]227# -> binpkg and srcpkg if -.a
[7]228if ($cmdopts{stage} eq 'a') {
[4]229 binpackage();
[7]230 srcpackage();
[4]231}
[14]232# -> binpkg if -.b
[7]233if ($cmdopts{stage} eq 'b') {
234 binpackage();
235}
[4]236
237# Just in case.
238exit 0;
239
240
241## load_userconfig()
242# Loads user configuration (if any)
243# Currently only handles .debmacros
244# Needs to handle "other files"
245sub load_userconfig {
[5]246 my (undef,undef,undef,undef,undef,undef,undef,$homedir,undef) = getpwuid($<);
[4]247 if (-e "$homedir/.debmacros") {
248 open USERMACROS,"<$homedir/.debmacros";
249 while (<USERMACROS>) {
[7]250 # And we also only handle a few macros at the moment.
[4]251 if (/^\%_topdir/) {
[5]252 my (undef,$tmp) = split /\s+/, $_;
253 $topdir = $tmp;
[4]254 }
255 }
256 }
257} # end load_userconfig()
258
259
[3]260## parse_cmd()
[4]261# Parses command line into global hash %cmdopts, other globals
[3]262# Options based on rpmbuild's options
263sub parse_cmd {
264 # Don't feel like coding my own option parser...
265 #use Getopt::Long;
266 # ... but I may have to: (OTOH, rpm uses popt, so maybe we can too.)
267 #use Getopt::Popt qw(:all);
[4]268 # Or not. >:( Stupid Debian lack of findable Perl module names in packages.
[3]269
270 # Stuff it.
[4]271 my $prevopt = '';
[3]272 foreach (@ARGV) {
273 chomp;
[7]274
[3]275 # Is it an option?
276 if (/^-/) {
[7]277
[3]278 # Is it a long option?
279 if (/^--/) {
[4]280 if (/^--short-circuit/) {
281 $cmdopts{short} = 'y';
[5]282 } elsif (/^--rebuild/) {
283 $cmdopts{type} = 's';
[4]284 } else {
285 print "Long opt $_\n";
286 }
[3]287 } else {
[14]288 # Not a long option
[5]289 if (/^-[bt]/) {
290 if ($cmdopts{stage} eq 's') {
291 # Mutually exclusive options.
292 die "Can't use $_ with --rebuild\n";
293 } else {
[14]294 # Capture the type (from "bare" files or tarball) and the stage (prep, build, etc)
[5]295 ($cmdopts{stage}) = (/^-[bt]([pcilabs])/);
296 ($cmdopts{type}) = (/^-([bt])[pcilabs]/);
297 }
[4]298 } elsif (/^-v/) {
299 # bump verbosity. Not sure what I'll actually do here...
[50]300 } elsif (/^-i/) {
301 $cmdopts{install} = 1;
302 $prevopt = '-i';
[4]303 } else {
304 die "Bad option $_\n";
305 }
[3]306 }
[14]307
308 } else { # Not an option argument
309
[5]310 # --buildroot is the only option that takes an argument
311 # Therefore, any *other* bare arguments are the spec file,
312 # tarball, or source package we're operating on - depending
313 # on which one we meet.
314 if ($prevopt eq '--buildroot') {
[7]315 $cmdbuildroot = $_;
[50]316 } elsif ($prevopt eq '-i') {
317 $srcpkg = $_;
[5]318 } else {
319 if ($cmdopts{type} eq 's') {
320 # Source package
[50]321 if (!/(sdeb|\.src\.rpm)$/) {
[5]322 die "Can't --rebuild with $_\n";
323 }
[50]324 $srcpkg = $_;
[5]325 } elsif ($cmdopts{type} eq 'b') {
[65]326 # Spec file
[5]327 $specfile = $_;
328 } else {
[65]329 # Tarball build. Need to extract tarball to find spec file. Whee.
330 $tarball = $_;
[5]331 }
332 }
[3]333 }
[7]334 $prevopt = $_;
335 } # foreach @ARGV
[4]336
337 # Some cross-checks. rpmbuild limits --short-circuit to just
338 # the "compile" and "install" targets - with good reason IMO.
[14]339 # Note that --short-circuit with -.p is not really an error, just redundant.
[4]340 # NB - this is NOT fatal, just ignored!
[10]341 if ($cmdopts{short} eq 'y' && $cmdopts{stage} =~ /[labs]/) {
[4]342 warn "Can't use --short-circuit for $targets{$cmdopts{stage}} stage. Ignoring.\n";
[28]343 $cmdopts{short} = 'n';
[4]344 }
[14]345
[3]346 # Valid options, with example arguments (if any):
347# Build from .spec file; mutually exclusive:
348 # -bp
349 # -bc
350 # -bi
351 # -bl
352 # -ba
353 # -bb
354 # -bs
355# Build from tarball; mutually exclusive:
356 # -tp
357 # -tc
358 # -ti
359 # -ta
360 # -tb
361 # -ts
362# Build from .src.(deb|rpm)
363 # --rebuild
364 # --recompile
365
366# General options
367 # --buildroot=DIRECTORY
368 # --clean
369 # --nobuild
370 # --nodeps
371 # --nodirtokens
372 # --rmsource
373 # --rmspec
374 # --short-circuit
375 # --target=CPU-VENDOR-OS
376
377 #my $popt = new Getopt::Popt(argv => \@ARGV, options => \@optionsTable);
378
379} # end parse_cmd()
380
[4]381
[5]382## parse_spec()
[14]383# Parse the .spec file.
[5]384sub parse_spec {
[65]385 open SPECFILE,"<$specfile" or die "specfile ($specfile) barfed: $!";
[5]386
[6]387LINE: while (<SPECFILE>) {
[14]388 next if /^#/; # Ignore comments...
389 next if /^\s+$/; # ... and blank lines.
390
[5]391 if (/^\%/) {
392 # A macro that needs further processing.
[14]393
[60]394 if (my ($key, $def) = (/^\%define\s+([^\s]+)\s+(.+)$/) ) {
395 $specglobals{$key} = expandmacros($def,'g');
[23]396 }
397
[62]398 if (/^\%description(?:\s+(?:-n\s+)?(.+))?/) {
[27]399 my $subname = "main";
400 if ($1) {
[62]401 my $tmp = expandmacros("$1", 'g');
[49]402 if (/-n/) { $subname = $tmp; } else { $subname = "$pkgdata{main}{name}-$tmp"; }
[27]403 }
[8]404 while (<SPECFILE>) {
[39]405 next if /^#/; # Messy. Should be possible to do better. :/
[8]406 redo LINE if /^\%/;
[27]407 $pkgdata{$subname}{desc} .= " $_";
[8]408 }
[18]409 }
[62]410 if (/^\%package\s+(?:-n\s+)?(.+)/) {
411 # gotta expand %defines here. Whee.
412 my $subname = expandmacros("$1", 'g');
[49]413 if (! /-n/) { $subname = "$pkgdata{main}{name}-$1"; }
[27]414 push @pkglist, $subname;
415 $pkgdata{$subname}{name} = $subname;
416 $pkgdata{$subname}{version} = $pkgdata{main}{version};
417 while (<SPECFILE>) {
418 redo LINE if /^\%/;
[38]419 if (my ($dname,$dvalue) = (/^(Summary|Group|Version|Requires|Provides):\s+(.+)$/i)) {
[27]420 $dname =~ tr/[A-Z]/[a-z]/;
[63]421 $pkgdata{$subname}{$dname} = expandmacros($dvalue, 'gp');
[27]422 }
423 }
424 }
[8]425
[6]426 if (/^\%prep/) {
427 # %prep section. May have %setup macro; may include %patch tags,
428 # may be just a bare shell script.
429
430 # This really should be local-ish, but we need just the filename for the source
431 $pkgdata{main}{source} =~ s|.+/([^/]+)$|$1|;
432
433 # Replace some core macros
[14]434 $pkgdata{main}{source} = expandmacros($pkgdata{main}{source},'gp');
[6]435
436PREPSCRIPT: while (<SPECFILE>) {
437 if (/^\%setup/) {
438 # Parse out the %setup macro. Note that we aren't supporting
[14]439 # many of RPM's %setup features.
[7]440 $prepscript .= "cd $topdir/BUILD\n";
441 if ( /\s+-n\s+([^\s]+)\s+/ ) {
442 $tarballdir = $1;
443 } else {
444 $tarballdir = "$pkgdata{main}{name}-$pkgdata{main}{version}";
445 }
[21]446 $prepscript .= "rm -rf $tarballdir\ntar -".
[6]447 ( $pkgdata{main}{source} =~ /\.tar\.gz$/ ? "z" : "" ).
448 ( $pkgdata{main}{source} =~ /\.tar\.bz2$/ ? "j" : "" ).
[21]449 ( /\s+-q\s+/ ? '' : 'vv' )."xf ".
450 "$topdir/SOURCES/$pkgdata{main}{source}\n".
[6]451 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n).
452 ( /\s+-n\s+([^\s]+)\s+/ ?
453 "cd $1\n" : "cd $pkgdata{main}{name}-$pkgdata{main}{version}\n" ).
454 qq([ `/usr/bin/id -u` = '0' ] && /bin/chown -Rhf root .\n).
455 qq([ `/usr/bin/id -u` = '0' ] && /bin/chgrp -Rhf root .\n).
456 qq(/bin/chmod -Rf a+rX,g-w,o-w .\n);
[10]457 } elsif (/^\%patch([^:]+)\s+(.+)$/) {
458 $prepscript .= "patch $2 <$topdir/SOURCES/".$pkgdata{main}{"patch$1"}."\n";
[6]459 } else {
460 last PREPSCRIPT if /^\%/;
461 $prepscript .= $_;
462 }
463 }
464 redo LINE;
465 }
[12]466 if (/^\%build/) {
[6]467 # %build. This is pretty much just a shell script. There
468 # *are* a few macros, but we're not going to deal with them yet.
[7]469 $buildscript .= "cd $tarballdir\n";
[10]470BUILDSCRIPT: while (<SPECFILE>) {
471 if (/^\%configure/) {
[12]472 $buildscript .= expandmacros($_,'cgbp');
[25]473 } elsif (/^\%\{__make\}/) {
[12]474 $buildscript .= expandmacros($_,'mgbp');
[10]475 } else {
[24]476 last BUILDSCRIPT if /^\%[^{]/;
[10]477 $buildscript .= $_;
478 }
[6]479 }
[10]480 redo LINE;
[5]481 }
[6]482 if (/^\%install/) {
[7]483 $installscript .= "cd $tarballdir\n";
[12]484INSTALLSCRIPT: while (<SPECFILE>) {
485 if (/^\%makeinstall/) {
486 $installscript .= expandmacros($_,'igbp');
487 } else {
488 last INSTALLSCRIPT if /^\%/;
489 $installscript .= $_;
490 }
491 }
492 redo LINE;
[6]493 }
494 if (/^\%clean/) {
[7]495 while (<SPECFILE>) {
496 redo LINE if /^\%/;
497 $cleanscript .= $_;
498 }
[12]499 $cleanscript = expandmacros($cleanscript,'gp');
[6]500 }
[29]501
[16]502 # pre/post (un)install scripts
503 if (/^\%pre\b/) {
504 while (<SPECFILE>) {
505 redo LINE if /^\%/;
506 $preinstscript .= $_;
507 }
[6]508 }
[16]509 if (/^\%post\b/) {
510 while (<SPECFILE>) {
511 redo LINE if /^\%/;
512 $postinstscript .= $_;
513 }
[6]514 }
[16]515 if (/^\%preun\b/) {
516 while (<SPECFILE>) {
517 redo LINE if /^\%/;
518 $preuninstscript .= $_;
519 }
520 }
521 if (/^\%postun\b/) {
522 while (<SPECFILE>) {
523 redo LINE if /^\%/;
524 $postuninstscript .= $_;
525 }
526 }
[29]527 # done %pre/%post scripts
528
[62]529 if (/^\%files(?:\s+(?:-n\s+)?(.+))?/) {
[23]530 my $pkgname = 'main';
[29]531 if ($1) { # Magic to add entries to the right list of files
[62]532 my $tmp = expandmacros("$1", 'g');
[49]533 if (/-n/) { $pkgname = $tmp; } else { $pkgname = "$pkgdata{main}{name}-$tmp"; }
[23]534 }
[37]535
536 # Set this now, so it can be flipped a bit later, and used much later.
537 #$pkgdata{$pkgname}{conffiles} = 0;
538
[27]539 while (<SPECFILE>) {
[29]540 chomp;
[45]541 next if /^#/;
[29]542 # need to update this to deal (properly) with %dir, %attr, etc
543 next if /^\%dir/;
544 next if /^\%attr/;
545 next if /^\%defattr/;
546
[37]547 # Debian dpkg doesn't speak "%docdir". Meh.
548 next if /^\%docdir/;
549
550 # Conffiles. Note that Debian and RH have similar, but not
551 # *quite* identical ideas of what constitutes a conffile. Nrgh.
552 if (/^\%config\s+(.+)$/) {
553 $pkgdata{$pkgname}{conffiles} = 1; # Flag it for later
554 my $tmp = $1; # Now we can mangleificationate it. And we probably need to. :/
[46]555 $tmp = expandmacros($tmp, 'gp'); # Expand common macros
[37]556 if ($tmp !~ /\s+/) {
557 # Simplest case, just a file. Whew.
558 push @{$pkgdata{$pkgname}{conflist}}, $tmp;
559 $filelist{$pkgname} .= " $tmp";
560 } else {
561 # Wot? Spaces? That means extra %-macros. Which, for the most part, can be ignored.
562 ($tmp) = ($tmp =~ /.+\s([^\s]+)/); # Strip everything before the last space
563 push @{$pkgdata{$pkgname}{conflist}}, $tmp;
564 $filelist{$pkgname} .= " $tmp";
565 }
566 next;
567 }
568
[29]569 # and finally we can fall through %{_<FHS>}-prefixed locations...
[30]570 if (/^\%\{_/) {
571 $filelist{$pkgname} .= " $_";
572 next;
573 }
574 # EW. Necessary to clear up %define expansions before we exit with redo.
575 $_ = expandmacros $_, 'g';
[29]576
577 # ... unknown or "next section" % directives ...
[27]578 redo LINE if /^\%/;
[29]579
580 # ... and "normal" files
[27]581 $filelist{$pkgname} .= " $_";
582 }
[46]583 $filelist{$pkgname} = expandmacros($filelist{$pkgname}, 'gp');
[29]584 } # done %file section
585
[18]586 if (/^\%changelog/) {
587 $pkgdata{main}{changelog} = '';
588 while (<SPECFILE>) {
589 redo LINE if /^\%/;
590 $pkgdata{main}{changelog} .= $_;
591 }
592 }
[14]593
594 } else { # Data from the spec file "header"
595
[5]596 if (/^summary:\s+(.+)/i) {
597 $pkgdata{main}{summary} = $1;
598 } elsif (/^name:\s+(.+)/i) {
[24]599 $pkgdata{main}{name} = expandmacros($1,'g');
[5]600 } elsif (/^version:\s+(.+)/i) {
[24]601 $pkgdata{main}{version} = expandmacros($1,'g');
[5]602 } elsif (/^release:\s+(.+)/i) {
[24]603 $pkgdata{main}{release} = expandmacros($1,'g');
[5]604 } elsif (/^group:\s+(.+)/i) {
605 $pkgdata{main}{group} = $1;
606 } elsif (/^copyright:\s+(.+)/i) {
607 $pkgdata{main}{copyright} = $1;
608 } elsif (/^url:\s+(.+)/i) {
609 $pkgdata{main}{url} = $1;
610 } elsif (/^packager:\s+(.+)/i) {
611 $pkgdata{main}{packager} = $1;
[7]612 } elsif (/^buildroot:\s+(.+)/i) {
613 $buildroot = $1;
[59]614 } elsif (/^source0?:\s+(.+)/i) {
[5]615 $pkgdata{main}{source} = $1;
[6]616 die "Unknown tarball format $1\n" if $1 !~ /\.tar\.(?:gz|bz2)$/;
[15]617 } elsif (/^source([0-9]+):\s+(.+)/i) {
618 $pkgdata{sources}{$1} = $2;
[10]619 } elsif (/^patch([^:]+):\s+(.+)$/i) {
[26]620 my $patchname = "patch$1";
621 $pkgdata{main}{$patchname} = $2;
622 if ($pkgdata{main}{$patchname} =~ /\//) {
623 # URL-style patch. Rare but not unheard-of.
624 my @patchbits = split '/', $pkgdata{main}{$patchname};
625 $pkgdata{main}{$patchname} = $patchbits[$#patchbits];
626 }
[38]627 } elsif (/^buildreq(?:uires)?:\s+(.+)/i) {
628 $buildreq .= ", $1";
629 } elsif (/^requires:\s+(.+)/i) {
[63]630 $pkgdata{main}{requires} .= ", ".expandmacros("$1", 'gp');
[38]631 } elsif (/^provides:\s+(.+)/i) {
632 $pkgdata{main}{provides} .= ", $1";
[51]633 } elsif (/^conflicts:\s+(.+)/i) {
634 $pkgdata{main}{conflicts} .= ", $1";
[5]635 }
636#Name: suwrap
637#Version: 0.04
638#Release: 3
639#Group: Applications/System
640#Copyright: WebHart internal ONLY. :(
641#BuildArchitectures: i386
642#BuildRoot: /tmp/%{name}-%{version}
643#Url: http://virtual.webhart.net
644#Packager: Kris Deugau <kdeugau@deepnet.cx>
645#Source: ftp://virtual.webhart.net/%{name}-%{version}.tar.gz
646
647 }
648 }
[7]649
[14]650 # Parse and replace some more macros. More will be replaced even later.
651
[10]652 # Expand macros as necessary.
[12]653 $scriptletbase = expandmacros($scriptletbase,'gp');
[7]654
655 $buildroot = $cmdbuildroot if $cmdbuildroot;
[12]656 $buildroot = expandmacros($buildroot,'gp');
[7]657
[16]658 close SPECFILE;
[5]659} # end parse_spec()
660
661
[7]662## prep()
[14]663# Writes and executes the %prep script (mostly) built while reading the spec file.
[7]664sub prep {
[10]665 # Replace some things here just to make sure.
[12]666 $prepscript = expandmacros($prepscript,'gp');
[10]667
[24]668#print $prepscript; exit 0;
[23]669
[10]670 # create script filename
[13]671 my $prepscriptfile = "$tmpdir/deb-tmp.prep.".int(rand(99998)+1);
[10]672 sysopen(PREPSCRIPT, $prepscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
[7]673 or die $!;
[10]674 print PREPSCRIPT $scriptletbase;
675 print PREPSCRIPT $prepscript;
676 close PREPSCRIPT;
[7]677
[10]678 # execute
679 print "Calling \%prep script $prepscriptfile...\n";
680 system("/bin/sh -e $prepscriptfile") == 0
[14]681 or die "Can't exec: $!\n";
[7]682
[10]683 # and clean up
684 unlink $prepscriptfile;
[7]685} # end prep()
686
687
[5]688## build()
[14]689# Writes and executes the %build script (mostly) built while reading the spec file.
[5]690sub build {
[12]691 # Expand the macros
692 $buildscript = expandmacros($buildscript,'cgbp');
693
[7]694 # create script filename
[13]695 my $buildscriptfile = "$tmpdir/deb-tmp.build.".int(rand(99998)+1);
[7]696 sysopen(BUILDSCRIPT, $buildscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
697 or die $!;
698 print BUILDSCRIPT $scriptletbase;
699 print BUILDSCRIPT $buildscript;
700 close BUILDSCRIPT;
701
702 # execute
703 print "Calling \%build script $buildscriptfile...\n";
704 system("/bin/sh -e $buildscriptfile") == 0
[14]705 or die "Can't exec: $!\n";
[7]706
707 # and clean up
708 unlink $buildscriptfile;
[5]709} # end build()
710
711
[7]712## install()
[14]713# Writes and executes the %install script (mostly) built while reading the spec file.
[7]714sub install {
[12]715 # Expand the macros
716 $installscript = expandmacros($installscript,'igbp');
717
[7]718 # create script filename
[13]719 my $installscriptfile = "$tmpdir/deb-tmp.inst.".int(rand(99998)+1);
[7]720 sysopen(INSTSCRIPT, $installscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
721 or die $!;
722 print INSTSCRIPT $scriptletbase;
[23]723# print INSTSCRIPT $cleanscript; # Clean up our install target before installing into it.
[7]724 print INSTSCRIPT $installscript;
725 close INSTSCRIPT;
726
727 # execute
728 print "Calling \%install script $installscriptfile...\n";
729 system("/bin/sh -e $installscriptfile") == 0
[14]730 or die "Can't exec: $!\n";
[7]731
732 # and clean up
733 unlink $installscriptfile;
734} # end install()
735
736
[8]737## binpackage()
738# Creates the binary .deb package from the installed tree in $buildroot.
[14]739# Writes and executes a shell script to do so.
740# Creates miscellaneous files required by dpkg-deb to actually build the package file.
[27]741# Should handle simple subpackages
[8]742sub binpackage {
[14]743 # Make sure we have somewhere to write the .deb file
[9]744 if (!-e "$topdir/DEBS/i386") {
745 mkdir "$topdir/DEBS/i386";
746 }
[14]747
[27]748 foreach my $pkg (@pkglist) {
[8]749
[27]750 # Gotta do this first, otherwise we don't have a place to move files from %files
751 mkdir "$buildroot/$pkg";
[28]752
[30]753 # Eliminate any lingering % macros
754 $filelist{$pkg} = expandmacros $filelist{$pkg}, 'g';
755
[27]756 my @pkgfilelist = split ' ', $filelist{$pkg};
757 foreach my $pkgfile (@pkgfilelist) {
[37]758 $pkgfile = expandmacros($pkgfile, 'gp');
[44]759 my ($fpath,$fname) = ($pkgfile =~ m|(.+?/?)?([^/]+)$|); # We don't need $fname now, but we might.
760 qx { mkdir -p $buildroot/$pkg$fpath }
761 if $fpath && $fpath ne '';
762 qx { mv $buildroot$pkgfile $buildroot/$pkg$fpath };
[27]763 }
[28]764
[38]765 # Get the "Depends" (Requires) a la RPM. Ish. We strip the leading
766 # comma and space here (if needed) in case there were "Requires" specified
767 # in the spec file - those would precede these.
[64]768 $pkgdata{$pkg}{requires} .= getreqs("$buildroot/$pkg");
[38]769
[64]770 # magic needed to properly version dependencies...
771 # only provided deps will really be included
[65]772 $pkgdata{$pkg}{requires} =~ s/^, //; # Still have to do this here.
[64]773 $pkgdata{$pkg}{requires} =~ s/\s+//g;
774 my @deps = split /,/, $pkgdata{$pkg}{requires};
775 my $tmp = '';
776 foreach my $dep (@deps) {
777 if (my ($name,$rel,$value) = ($dep =~ /^([a-zA-Z0-9._-]+)([><=]+)([a-zA-Z0-9._-]+)$/)) {
778 $tmp .= ", $name ($rel $value)";
779 } else {
780 $tmp .= ", $dep";
781 }
782 }
783 ($pkgdata{$pkg}{requires} = $tmp) =~ s/^, //;
784
[38]785 # Do this here since we're doing {depends}...
[41]786 if (defined($pkgdata{$pkg}{provides})) {
787 $pkgdata{$pkg}{provides} =~ s/^, //;
788 $pkgdata{$pkg}{provides} = expandmacros($pkgdata{$pkg}{provides},'gp');
789 }
[51]790 if (defined($pkgdata{$pkg}{conflicts})) {
791 $pkgdata{$pkg}{conflicts} =~ s/^, //;
792 $pkgdata{$pkg}{conflicts} = expandmacros($pkgdata{$pkg}{conflicts},'gp');
793 }
[38]794
[27]795 # Gotta do this next, otherwise the control file has nowhere to go. >:(
796 mkdir "$buildroot/$pkg/DEBIAN";
797
[33]798 # Hack the filename for the package into a Debian-tool-compatible format. GRRRRRR!!!!!
799 # Have I mentioned I hate Debian Policy?
800 $pkgdata{$pkg}{name} =~ tr/_/-/;
801
[27]802 # create script filename
803 my $debscriptfile = "$tmpdir/deb-tmp.pkg.".int(rand(99998)+1);
804 sysopen(DEBSCRIPT, $debscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
[8]805 or die $!;
[27]806 print DEBSCRIPT $scriptletbase;
807 print DEBSCRIPT "fakeroot dpkg-deb -b $buildroot/$pkg $topdir/DEBS/i386/".
808 "$pkgdata{$pkg}{name}_$pkgdata{$pkg}{version}-$pkgdata{main}{release}_i386.deb\n";
809 # %$&$%@#@@#%@@@ Debian and their horrible ugly package names. >:(
810 close DEBSCRIPT;
[8]811
[27]812 my $control = "Package: $pkgdata{$pkg}{name}\n".
813 "Version: $pkgdata{$pkg}{version}-$pkgdata{main}{release}\n".
814 "Section: $pkgdata{$pkg}{group}\n".
[8]815 "Priority: optional\n".
816 "Architecture: i386\n".
817 "Maintainer: $pkgdata{main}{packager}\n".
[47]818 ( $pkgdata{$pkg}{requires} ne '' ? "Depends: $pkgdata{$pkg}{requires}\n" : '' ).
[39]819 ( defined($pkgdata{$pkg}{provides}) ? "Provides: $pkgdata{$pkg}{provides}\n" : '' ).
[51]820 ( defined($pkgdata{$pkg}{conflicts}) ? "Conflicts: $pkgdata{$pkg}{conflicts}\n" : '' ).
[27]821 "Description: $pkgdata{$pkg}{summary}\n";
822 $control .= "$pkgdata{$pkg}{desc}\n";
[8]823
[27]824 open CONTROL, ">$buildroot/$pkg/DEBIAN/control";
825 print CONTROL $control;
826 close CONTROL;
[8]827
[37]828 # Iff there are conffiles (as specified in the %files list(s), add'em
829 # in so dpkg-deb can tag them.
830 if ($pkgdata{$pkg}{conffiles}) {
831 open CONFLIST, ">$buildroot/$pkg/DEBIAN/conffiles";
832 foreach my $conffile (@{$pkgdata{$pkg}{conflist}}) {
833 print CONFLIST "$conffile\n";
834 }
835 close CONFLIST;
836 }
837
[32]838 # Can't see much point in scripts on subpackages... although since
839 # it's *possible* I should support it at some point.
840 if ($pkg eq 'main') {
841 if ($preinstscript ne '') {
[34]842 $preinstscript = expandmacros($preinstscript,'g');
[32]843 open PREINST, ">$buildroot/$pkg/DEBIAN/preinst";
844 print PREINST "#!/bin/sh\nset -e\n\n";
845 print PREINST $preinstscript;
846 close PREINST;
847 `chmod 0755 $buildroot/$pkg/DEBIAN/preinst`;
848 }
849 if ($postinstscript ne '') {
[34]850 $postinstscript = expandmacros($postinstscript,'g');
[32]851 open POSTINST, ">$buildroot/$pkg/DEBIAN/postinst";
852 print POSTINST "#!/bin/sh\nset -e\n\n";
853 print POSTINST $postinstscript;
854 close POSTINST;
855 `chmod 0755 $buildroot/$pkg/DEBIAN/postinst`;
856 }
857 if ($preuninstscript ne '') {
[34]858 $preuninstscript = expandmacros($preuninstscript,'g');
[32]859 open PREUNINST, ">$buildroot/$pkg/DEBIAN/prerm";
860 print PREUNINST "#!/bin/sh\nset -e\n\n";
861 print PREUNINST $preuninstscript;
862 close PREUNINST;
863 `chmod 0755 $buildroot/$pkg/DEBIAN/prerm`;
864 }
865 if ($postuninstscript ne '') {
[34]866 $postuninstscript = expandmacros($postuninstscript,'g');
[32]867 open POSTUNINST, ">$buildroot/$pkg/DEBIAN/postrm";
868 print POSTUNINST "#!/bin/sh\nset -e\n\n";
869 print POSTUNINST $postuninstscript;
870 close POSTUNINST;
871 `chmod 0755 $buildroot/$pkg/DEBIAN/postrm`;
872 }
[27]873 }
[16]874
[27]875 # execute
876 print "Calling package creation script $debscriptfile for $pkgdata{$pkg}{name}...\n";
877 system("/bin/sh -e $debscriptfile") == 0
[14]878 or die "Can't exec: $!\n";
[8]879
[27]880 # and clean up
881 unlink $debscriptfile;
882
883 } # subpackage loop
884
[8]885} # end binpackage()
886
887
[35]888## srcpackage()
889# Builds a .src.deb source package. Note that Debian's idea of
890# a "source package" is seriously flawed IMO, because you can't
891# easily copy it as-is.
[36]892# Not quite identical to RPM, but Good Enough (TM).
[35]893sub srcpackage {
[54]894 # In case we were called with -bs.
895 $pkgdata{main}{name} =~ tr/_/-/;
[36]896 my $pkgsrcname = "$pkgdata{main}{name}-$pkgdata{main}{version}-$pkgdata{main}{release}.sdeb";
897
898 my $paxcmd;
899
900 # We'll definitely need this later, and *may* need it sooner.
901 (my $barespec = $specfile) =~ s|.+/([^/]+)$|$1|;
902
903 # Copy the specfile to the build tree, but only if it's not there already.
904##buglet: need to deal with silly case where silly user has put the spec
905# file in a subdir of %{_topdir}/SPECS. Ewww. Silly user!
906 if (abs_path($specfile) !~ /^$topdir\/SPECS/) {
907 $paxcmd .= "cp $specfile %{_topdir}/SPECS/; \n"
908 }
909
[35]910 # use pax -w [file] [file] ... >outfile.sdeb
[36]911 $paxcmd = "cd $topdir; pax -w ";
[10]912
[35]913# tweak source entry into usable form. Need it locally somewhere along the line.
914 (my $pkgsrc = $pkgdata{main}{source}) =~ s|.+/([^/]+)$|$1|;
[36]915 $paxcmd .= "SOURCES/$pkgsrc ";
[10]916
[35]917 # create file list: Source[nn], Patch[nn]
918 foreach my $specbit (keys %{$pkgdata{main}} ) {
919 next if $specbit eq 'source';
[53]920 $paxcmd .= "SOURCES/$pkgdata{main}{$specbit} " if $specbit =~ /^patch/;
[36]921##buglet: need to deal with case where patches are listed as URLs?
922# or other extended pathnames? Silly !@$%^&!%%!%!! user!
[35]923 }
924
[53]925 foreach my $source (keys %{$pkgdata{sources}}) {
926 $paxcmd .= "SOURCES/$pkgdata{sources}{$source} ";
927 }
928
[36]929 # add the spec file, source package destination, and cd back where we came from.
930 $paxcmd .= "SPECS/$barespec > $topdir/SDEBS/$pkgsrcname; cd -";
[35]931
[36]932 # In case of %-macros...
[35]933 $paxcmd = expandmacros($paxcmd,'gp');
934
[36]935 system "$paxcmd";
936 print "Wrote source package $pkgsrcname in $topdir/SDEBS.\n";
[35]937}
938
939
[38]940## checkbuildreq()
941# Checks the build requirements (if any)
942# Spits out a rude warning and returns a true-false error if any
943# requirements are not met.
944sub checkbuildreq {
[40]945 return 1 if $buildreq eq ''; # No use doing extra work.
[38]946
[52]947 if ( ! -e "/usr/bin/dpkg-query" ) {
948 print "**WARNING** dpkg-query not found. Can't check build-deps.\n".
949 " Required for sucessful build:\n".$buildreq."\n".
950 " Continuing anyway.\n";
951 return 1;
952 }
953
[38]954 my $reqflag = 1; # unset iff a buildreq is missing
955
956 $buildreq =~ s/^, //; # Strip the leading comma and space
957 my @reqlist = split /,\s+/, $buildreq;
958
959 foreach my $req (@reqlist) {
960 my ($pkg,$rel,$ver);
961
962 # We have two classes of requirements - versioned and unversioned.
963 if ($req =~ /[><=]/) {
964 # Pick up the details of versioned buildreqs
965 ($pkg,$rel,$ver) = ($req =~ /([a-z0-9._-]+)\s+([><=]+)\s+([a-z0-9._-]+)/);
966 } else {
967 # And the unversioned ones.
968 $pkg = $req;
969 $rel = '>=';
970 $ver = 0;
971 }
972
973 my @pkglist = qx { dpkg-query --showformat '\${status}\t\${version}\n' -W $pkg };
974# need to check if no lines returned - means a bad buildreq
975 my ($reqstat,undef,undef,$reqver) = split /\s+/, $pkglist[0];
976 if ($reqstat !~ /install/) {
977 print " * Missing build-dependency $pkg!\n";
978 $reqflag = 0;
979 } else {
980# gotta be a better way to do this... :/
981 if ($rel eq '>=' && !($reqver ge $ver)) {
982 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
983 $reqflag = 0;
984 }
985 if ($rel eq '>' && !($reqver gt $ver)) {
986 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
987 $reqflag = 0;
988 }
989 if ($rel eq '<=' && !($reqver le $ver)) {
990 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
991 $reqflag = 0;
992 }
993 if ($rel eq '<' && !($reqver lt $ver)) {
994 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
995 $reqflag = 0;
996 }
997 if ($rel eq '=' && !($reqver eq $ver)) {
998 print " * Buildreq $pkg is installed, but wrong version ($reqver): Need $ver\n";
999 $reqflag = 0;
1000 }
1001 } # end not installed/installed check
1002 } # end req loop
1003
1004 return $reqflag;
1005} # end checkbuildreq()
1006
1007
1008## getreqs()
1009# Find out which libraries/packages are required for any
1010# executables and libs in a given file tree.
1011# (Debian doesn't have soname-level deps; just package-level)
[41]1012# Returns an empty string if the tree contains no binaries.
1013# Doesn't work well on shell scripts. but those *should* be
1014# fine anyway. (Yeah, right...)
[38]1015sub getreqs() {
1016 my $pkgtree = $_[0];
1017
[39]1018 print "Checking library requirements...\n";
[43]1019 my @binlist = qx { find $pkgtree -type f -perm 755 };
[38]1020
[41]1021 if (scalar(@binlist) == 0) {
1022 return '';
1023 }
1024
1025 my @reqlist;
1026 foreach (@binlist) {
1027 push @reqlist, qx { ldd $_ };
1028 }
1029
1030 # Get the list of libs provided by this package. Still doesn't
1031 # handle the case where the lib gets stuffed into a subpackage. :/
1032 my @intprovlist = qx { find $pkgtree -type f -name "*.so*" };
1033 my $provlist = '';
1034 foreach (@intprovlist) {
1035 s/$pkgtree//;
1036 $provlist .= "$_";
1037 }
1038
[38]1039 my %reqs;
[43]1040 my $reqlibs = '';
[38]1041
1042 foreach (@reqlist) {
1043 next if /^$pkgtree/;
[42]1044 next if /not a dynamic executable/;
[41]1045 next if m|/lib/ld-linux.so|; # Hack! Hack! PTHBTT! (libc suxx0rz)
[38]1046
[41]1047 my ($req) = (/^\s+([a-z0-9._-]+)/); # dig out the actual library (so)name
1048
1049 # Ignore libs provided by this package. Note that we don't match
1050 # on word-boundary at the *end* of the lib we're looking for, as the
1051 # looked-for lib may not have the full soname version. (ie, it may
1052 # "just" point to one of the symlinks that get created somewhere.)
1053 next if $provlist =~ /\b$req/;
1054
[38]1055 $reqlibs .= " $req";
1056 }
1057
[43]1058 if ($reqlibs ne '') {
1059 foreach (qx { dpkg -S $reqlibs }) {
1060 my ($libpkg,undef) = split /:\s+/;
1061 $reqs{$libpkg} = 1;
1062 }
[38]1063 }
1064
[43]1065 my $deplist = '';
[38]1066 foreach (keys %reqs) {
1067 $deplist .= ", $_";
1068 }
1069
1070# For now, we're done. We're not going to meddle with versions yet.
1071# Among other things, it's messier than handling "simple" yes/no "do
1072# we have this lib?" deps. >:(
1073
1074 return $deplist;
1075} # end getreqs()
1076
1077
[50]1078## install_sdeb()
1079# Extracts .sdeb contents to %_topdir as appropriate
1080sub install_sdeb {
1081 my $paxcmd = "cd $topdir; pax -r <$srcpkg; cd -";
1082
1083 # In case of %-macros...
1084 $paxcmd = expandmacros($paxcmd,'gp');
1085
1086 system "$paxcmd";
1087 print "Extracted source package $srcpkg to $topdir.\n";
1088} # end install_sdeb()
1089
1090
[10]1091## expandmacros()
1092# Expands all %{blah} macros in the passed string
[14]1093# Split up a bit with some sections so we don't spend time trying to
1094# expand macros that are only used in a few specific places.
[10]1095sub expandmacros {
1096 my $macrostring = shift;
[14]1097 my $section = shift;
[10]1098
[25]1099 # To allow the FHS-ish %configure and %makeinstall to work The Right Way.
[12]1100 # (Without clobbering the global $buildroot.)
1101 my $prefix = '';
1102
[11]1103 if ($section =~ /c/) {
[12]1104 # %configure macro
[17]1105# Don't know what it's for, don't have a useful default replacement
1106# --program-prefix=%{_program_prefix} \
1107 $macrostring =~ s'%configure'./configure --host=$DEB_HOST_GNU_TYPE \
1108 --build=$DEB_BUILD_GNU_TYPE \
1109 --prefix=%{_prefix} \
1110 --exec-prefix=%{_exec_prefix} \
1111 --bindir=%{_bindir} \
1112 --sbindir=%{_sbindir} \
1113 --sysconfdir=%{_sysconfdir} \
1114 --datadir=%{_datadir} \
1115 --includedir=%{_includedir} \
1116 --libdir=%{_libdir} \
1117 --libexecdir=%{_libexecdir} \
1118 --localstatedir=%{_localstatedir} \
1119 --sharedstatedir=%{_sharedstatedir} \
1120 --mandir=%{_mandir} \
1121 --infodir=%{_infodir} ';
[12]1122 } # done %configure
1123
1124 if ($section =~ /m/) {
1125 $macrostring =~ s'%{__make}'make ';
[25]1126 } # done make
[12]1127
1128 if ($section =~ /i/) {
1129 # This is where we need to mangle $prefix.
[24]1130 $macrostring =~ s'%makeinstall'make %{fhs} install';
[12]1131 $prefix = $buildroot;
1132 } # done %install and/or %makeinstall
1133
1134 # Build data
1135 # Note that these are processed in reverse order to get the substitution order right
1136 if ($section =~ /b/) {
[24]1137# $macrostring =~ s'%{fhs}'host=$DEB_HOST_GNU_TYPE \
1138# build=$DEB_BUILD_GNU_TYPE \
1139 $macrostring =~ s'%{fhs}'prefix=%{_prefix} \
[17]1140 exec-prefix=%{_exec_prefix} \
1141 bindir=%{_bindir} \
1142 sbindir=%{_sbindir} \
1143 sysconfdir=%{_sysconfdir} \
1144 datadir=%{_datadir} \
1145 includedir=%{_includedir} \
1146 libdir=%{_libdir} \
1147 libexecdir=%{_libexecdir} \
1148 localstatedir=%{_localstatedir} \
1149 sharedstatedir=%{_sharedstatedir} \
1150 mandir=%{_mandir} \
1151 infodir=%{_infodir} \
1152';
[10]1153
1154 # Note that the above regex terminates with the extra space
[12]1155 # "Just In Case" of user additions, which will then get neatly
1156 # tagged on the end where they take precedence (supposedly)
1157 # over the "default" ones.
[10]1158
[14]1159 # Now we cascade the macros introduced above. >_<
1160 # Wot ot to go theah:
1161 $macrostring =~ s|%{_mandir}|%{_datadir}/man|g; #/usr/share/man
1162 $macrostring =~ s|%{_infodir}|%{_datadir}/info|g; #/usr/share/info
[25]1163 $macrostring =~ s|%{_oldincludedir}|/usr/include|g; #/usr/include
1164 $macrostring =~ s|%{_includedir}|%{_prefix\}/include|g; #/usr/include
1165 $macrostring =~ s|%{_libdir}|%{_exec_prefix}/%{_lib}|g; #/usr/lib
[14]1166 $macrostring =~ s|%{_lib}|lib|g; #?
[25]1167 $macrostring =~ s|%{_localstatedir}|/var|g; #/var
1168 $macrostring =~ s|%{_sharedstatedir}|%{_prefix}/com|g; #/usr/com WTF?
1169 $macrostring =~ s|%{_sysconfdir}|/etc|g; #/etc
1170 $macrostring =~ s|%{_datadir}|%{_prefix}/share|g; #/usr/share
1171 $macrostring =~ s|%{_libexecdir}|%{_exec_prefix}/libexec|g; #/usr/libexec
1172 $macrostring =~ s|%{_sbindir}|%{_exec_prefix}/sbin|g; #/usr/sbin
1173 $macrostring =~ s|%{_bindir}|%{_exec_prefix}/bin|g; #/usr/bin
1174 $macrostring =~ s|%{_exec_prefix}|%{_prefix}|g; #/usr
[14]1175 $macrostring =~ s|%{_prefix}|/usr|g; #/usr
[10]1176 } # done with config section
1177
[12]1178 # Package data
1179 if ($section =~ /p/) {
[15]1180 $macrostring =~ s/\%\{buildroot\}/$buildroot/gi;
1181 foreach my $source (keys %{$pkgdata{sources}}) {
1182 $macrostring =~ s/\%\{source$source\}/$topdir\/SOURCES\/$pkgdata{sources}{$source}/gi;
1183 }
1184 $macrostring =~ s/\%\{name\}/$pkgdata{main}{name}/gi;
1185 $macrostring =~ s/\%\{version\}/$pkgdata{main}{version}/gi;
1186 $macrostring =~ s/\%\{release\}/$pkgdata{main}{release}/gi;
[12]1187 }
1188
1189 # Globals, and not-so-globals
1190 if ($section =~ /g/) {
[60]1191
1192 # special %define's. Handle the general case where we eval anything.
1193 # Prime example:
1194 #%define perl_vendorlib %(eval "`perl -V:installvendorlib`"; echo $installvendorlib)
1195 if ($macrostring =~ /^\%\(eval.+\)$/) {
1196 $macrostring =~ s/^\%\(//;
1197 $macrostring =~ s/\)$//;
1198 # Oy vey this gets silly for the perl bits. Executing a shell to
1199 # call Perl to get the vendorlib/sitelib/whatever "core" globals.
1200 # This can do more, but... eww.
1201 # Next line is non-optimal - what if $macrostring contains ' characters?
1202 $macrostring = qx { /bin/sh -c '$macrostring' };
1203 }
1204
[29]1205 $macrostring =~ s|%{_builddir}|%{_topdir}/BUILD|g;
1206 $macrostring =~ s|%{_topdir}|$topdir|g;
[13]1207 $macrostring =~ s|%{_tmppath}|$tmpdir|g;
[12]1208 $macrostring =~ s'%{_docdir}'/usr/share/doc'g;
[23]1209
[31]1210 # Standard FHS locations. More or less.
[30]1211 $macrostring =~ s'%{_bindir}'/usr/bin'g;
[31]1212 $macrostring =~ s'%{_sbindir}'/usr/sbin'g;
[30]1213 $macrostring =~ s'%{_mandir}'/usr/share/man'g;
1214 $macrostring =~ s'%{_includedir}'/usr/include'g;
[31]1215 $macrostring =~ s'%{_libdir}'/usr/lib'g;
1216 $macrostring =~ s'%{_sysconfdir}'/etc'g;
1217 $macrostring =~ s'%{_localstatedir}'/var'g;
[30]1218
[23]1219 # %define's
1220 foreach my $key (keys %specglobals) {
1221 $macrostring =~ s|%{$key}|$specglobals{$key}|g;
1222 }
1223
1224 # system programs. RPM uses a global config file for these; we'll just
1225 # ASS-U-ME and make life a little simpler.
1226 if ($macrostring =~ /\%\{\_\_([a-z0-9_-]+)\}/) {
1227 $macrostring =~ s|%{__([a-z0-9_-]+)}|$1|g;
1228 }
[10]1229 } # done with globals section
1230
1231 return $macrostring;
1232} # end expandmacros()
[22]1233
1234
1235
[38]1236__END__
1237
1238
1239
[22]1240=head1 NAME
1241
1242debbuild - Build Debian-compatible packages from RPM spec files
1243
1244=head1 SYNOPSIS
1245
1246 debbuild {-ba|-bb|-bp|-bc|-bi|-bl|-bs} [build-options] file.spec
1247
1248 debbuild {-ta|-tb|-tp|-tc|-ti|-tl|-ts} [build-options] file.tar.{gz|bz2}
1249
[55]1250 debbuild --rebuild file.{src.rpm|sdeb}
[22]1251
1252=head1 DESCRIPTION
1253
[55]1254This script attempts to build Debian-friendly semi-native packages from RPM spec files,
1255RPM-friendly tarballs, and RPM source packages (.src.rpm files). It accepts I<most> of the
1256options rpmbuild does, and should be able to interpret most spec files usefully. Perl
1257modules should be handled via CPAN+dh-make-perl instead; Debian's conventions for such
1258things do not lend themselves to automated conversion.
[22]1259
[55]1260As far as possible, the command-line options are identical to those from rpmbuild, although
[57]1261several rpmbuild options are not supported:
[22]1262
[57]1263 --recompile
1264 --showrc
1265 --buildroot
1266 --clean
1267 --nobuild
1268 --rmsource
1269 --rmspec
1270 --sign
1271 --target
1272
1273Some of these could probably be trivially added. Feel free to send me a patch. ;)
1274
1275Complex spec files will most likely not work well, if at all. Rewrite them from scratch -
1276you'll have to make heavy modifications anyway.
1277
1278If you see something you don't like, mail me. Send a patch if you feel inspired. I don't
1279promise I'll do anything other than say "Yup, that's broken" or "Got your message".
1280
[55]1281=head1 ASSUMPTIONS
1282
1283As with rpmbuild, debbuild makes some assumptions about your system.
1284
1285=over 4
1286
1287=item *
1288
1289Either you have rights to do as you please under /usr/src/debian, or you have created a file
1290~/.debmacros containing a suitable %_topdir definition.
1291
[57]1292Both rpmbuild and debbuild require the directories %_topdir/{BUILD,SOURCES,SPECS}. However,
1293where rpmbuild requires the %_topdir/{RPMS,SRPMS} directories, debbuild
1294requires %_topdir/{DEBS,SDEBS} instead. Create them in advance;
[55]1295some subdirectories are created automatically as needed, but most are not.
1296
1297=item *
1298
1299/var/tmp must allow script execution - rpmbuild and debbuild both rely on creating and
1300executing shell scripts for much of their functionality. By default, debbuild also creates
1301install trees under /var/tmp - however this is (almost) entirely under the control of the
1302package's .spec file.
1303
1304=item *
1305
1306If you wish to --rebuild a .src.rpm, your %_topdir for both debbuild and rpmbuild must either
1307match, or be suitably symlinked one direction or another so that both programs are effectively
1308working in the same tree. (Or you could just manually wrestle files around your system.)
1309
1310You could symlink ~/.rpmmacros to ~/.debmacros (or vice versa) and save yourself some hassle
1311if you need to rebuild .src.rpm packages on a regular basis. Currently debbuild only uses the
1312%_topdir macro definition, although there are many more things that rpmbuild can use from
1313~/.rpmmacros.
1314
1315=back
1316
[57]1317=head1 AUTHOR
[55]1318
[57]1319debbuild was written by Kris Deugau <kdeugau@deepnet.cx>. A version that approximates
1320current is available at http://www.deepnet.cx/debbuild/.
[55]1321
1322=head1 BUGS
1323
[57]1324Funky Things Happen if you forget a command-line option or two. I've been too lazy to bother
1325fixing this.
[55]1326
[57]1327Many macro expansions are unsupported or incompletely supported.
1328
1329The generated scriptlets don't quite match those from rpmbuild exactly. There are extra
1330environment variables and preprocessing that I haven't needed (yet).
1331
1332Dcumentation, such as it is, will likely remain perpetually out of date.
1333
1334%_topdir and the five "working" directories under %_topdir could arguably be created by
1335debbuild. However, rpmbuild doesn't create these directories either.
1336
1337=head1 SEE ALSO
1338
1339rpm(8), rpmbuild(8), and pretty much any document describing how to write a .spec file.
1340
[22]1341=cut
Note: See TracBrowser for help on using the repository browser.