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