source: trunk/debbuild@ 7

Last change on this file since 7 was 7, checked in by kdeugau, 19 years ago

/trunk

Checkpoint.
%prep, %build, and %install functionality is (should be) complete.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 12.3 KB
Line 
1#!/usr/bin/perl
2# debbuild script
3# Shamlessly steals intreface from rpm's "rpmbuild" to create
4# Debian packages. Please note that such packages are highly
5# unlikely to conform to "Debian Policy".
6###
7# SVN revision info
8# $Date: 2005-11-01 21:09:06 +0000 (Tue, 01 Nov 2005) $
9# SVN revision $Rev: 7 $
10# Last update by $Author: kdeugau $
11###
12
13use strict;
14use warnings;
15use Fcntl; # for sysopen flags
16
17# Program flow:
18# -> Parse/execute "system" config/macros (if any - should be rare)
19# -> Parse/execute "user" config/macros (if any - *my* requirement is %_topdir)
20# -> Parse command line for options, spec file/tarball/.src.deb (NB - also accept .src.rpm)
21
22# User's prefs for dirs, environment, etc,etc,etc.
23# config file ~/.debmacros
24# Default ordered search paths for config/macros:
25# /usr/lib/rpm/rpmrc /usr/lib/rpm/redhat/rpmrc /etc/rpmrc ~/.rpmrc
26# /usr/lib/rpm/macros /usr/lib/rpm/redhat/macros /etc/rpm/macros ~/.rpmmacros
27# **NOTE: May be possible to (ab)use bits of debhelper
28
29# Build tree
30# default is /usr/src/debian/{BUILD,SOURCES,SPECS,DEBS,SDEBS}
31
32# Globals
33my $specfile;
34my $tarball;
35my $srcpkg;
36my $cmdbuildroot;
37my $tarballdir; # This should really be initialized, but the coding makes it, um, ugly.
38
39# Initialized globals
40my $verbosity = 0;
41my %cmdopts = (type => '',
42 stage => 'a',
43 short => 'n'
44 );
45my $topdir = "/usr/src/debian";
46my $buildroot = "/var/tmp/%{name}-%{version}-%{release}.root".int(rand(99998)+1);
47
48# "Constants"
49my %targets = ('p' => 'Prep',
50 'c' => 'Compile',
51 'i' => 'Install',
52 'l' => 'Verify %files',
53 'a' => 'Build binary and source',
54 'b' => 'Build binary',
55 's' => 'Build source'
56 );
57my $scriptletbase =
58q(#!/bin/sh
59
60 RPM_SOURCE_DIR="%{_topdir}/SOURCES"
61 RPM_BUILD_DIR="%{_topdir}/BUILD"
62 RPM_OPT_FLAGS="-O2 -g -march=i386 -mcpu=i686"
63 RPM_ARCH="i386"
64 RPM_OS="linux"
65 export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS
66 RPM_DOC_DIR="/usr/share/doc"
67 export RPM_DOC_DIR
68 RPM_PACKAGE_NAME="%{name}"
69 RPM_PACKAGE_VERSION="%{version}"
70 RPM_PACKAGE_RELEASE="%{release}"
71 export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE
72 RPM_BUILD_ROOT="%{buildroot}"
73 export RPM_BUILD_ROOT
74
75 set -x
76 umask 022
77 cd %{_topdir}/BUILD
78);
79
80# Package data
81# This is the form of $pkgdata{pkgname}{meta}
82# meta includes Summary, Name, Version, Release, Group, Copyright,
83# Source, URL, Packager, BuildRoot, Description
84# 10/31/2005 Maybe this should be flatter? -kgd
85my %pkgdata;
86
87# Scriptlets
88my $prepscript;
89my $buildscript;
90my $installscript;
91my $cleanscript;
92
93# Functions
94sub prep;
95sub parse_spec;
96
97die "Not enough arguments\n" if #$argv == 0;
98
99##main
100
101load_userconfig();
102parse_cmd();
103
104if ($cmdopts{type} eq 'b') {
105 # Need to read the spec file to find the tarball. Note that
106 # this also generates most of the shell script required.
107 parse_spec();
108}
109
110# Hokay. Need to:
111# Execute prep if *not* --short-circuit
112if ($cmdopts{stage} eq 'p' || ($cmdopts{stage} =~ /[cilabs]/ && $cmdopts{short} ne 'y')) {
113 prep();
114}
115if ($cmdopts{stage} eq 'c' || ($cmdopts{stage} =~ /[ilabs]/ && $cmdopts{short} ne 'y')) {
116 build();
117}
118if ($cmdopts{stage} =~ /[ilabs]/) {
119 install();
120}
121if ($cmdopts{stage} eq 'a') {
122 binpackage();
123 srcpackage();
124}
125if ($cmdopts{stage} eq 'b') {
126 binpackage();
127}
128if ($cmdopts{stage} eq 's') {
129 srcpackage();
130}
131
132# Just in case.
133exit 0;
134
135
136## load_userconfig()
137# Loads user configuration (if any)
138# Currently only handles .debmacros
139# Needs to handle "other files"
140sub load_userconfig {
141 my (undef,undef,undef,undef,undef,undef,undef,$homedir,undef) = getpwuid($<);
142 if (-e "$homedir/.debmacros") {
143 open USERMACROS,"<$homedir/.debmacros";
144 while (<USERMACROS>) {
145 # And we also only handle a few macros at the moment.
146 if (/^\%_topdir/) {
147 my (undef,$tmp) = split /\s+/, $_;
148 $topdir = $tmp;
149 }
150 }
151 }
152} # end load_userconfig()
153
154
155## parse_cmd()
156# Parses command line into global hash %cmdopts, other globals
157# Options based on rpmbuild's options
158sub parse_cmd {
159 # Don't feel like coding my own option parser...
160 #use Getopt::Long;
161 # ... but I may have to: (OTOH, rpm uses popt, so maybe we can too.)
162 #use Getopt::Popt qw(:all);
163 # Or not. >:( Stupid Debian lack of findable Perl module names in packages.
164
165 # Stuff it.
166 my $prevopt = '';
167 foreach (@ARGV) {
168 chomp;
169
170 # Is it an option?
171 if (/^-/) {
172
173 # Is it a long option?
174 if (/^--/) {
175 if (/^--short-circuit/) {
176 $cmdopts{short} = 'y';
177 } elsif (/^--rebuild/) {
178 $cmdopts{type} = 's';
179 } else {
180 print "Long opt $_\n";
181 }
182 } else {
183 if (/^-[bt]/) {
184 if ($cmdopts{stage} eq 's') {
185 # Mutually exclusive options.
186 die "Can't use $_ with --rebuild\n";
187 } else {
188 ($cmdopts{stage}) = (/^-[bt]([pcilabs])/);
189 ($cmdopts{type}) = (/^-([bt])[pcilabs]/);
190 }
191 } elsif (/^-v/) {
192 # bump verbosity. Not sure what I'll actually do here...
193 } else {
194 die "Bad option $_\n";
195 }
196 }
197 } else {
198 # --buildroot is the only option that takes an argument
199 # Therefore, any *other* bare arguments are the spec file,
200 # tarball, or source package we're operating on - depending
201 # on which one we meet.
202 if ($prevopt eq '--buildroot') {
203 $cmdbuildroot = $_;
204 } else {
205 if ($cmdopts{type} eq 's') {
206 # Source package
207 if (!/\.src\.(deb|rpm)$/) {
208 die "Can't --rebuild with $_\n";
209 }
210 } elsif ($cmdopts{type} eq 'b') {
211 $specfile = $_;
212 # Spec file
213 } else {
214 # Tarball
215 }
216 }
217 }
218 $prevopt = $_;
219 } # foreach @ARGV
220
221 # Some cross-checks. rpmbuild limits --short-circuit to just
222 # the "compile" and "install" targets - with good reason IMO.
223 # NB - this is NOT fatal, just ignored!
224 if ($cmdopts{short} eq 'y') {
225 warn "Can't use --short-circuit for $targets{$cmdopts{stage}} stage. Ignoring.\n";
226 $cmdopts{short} = 'n';
227 }
228 # Valid options, with example arguments (if any):
229# Build from .spec file; mutually exclusive:
230 # -bp
231 # -bc
232 # -bi
233 # -bl
234 # -ba
235 # -bb
236 # -bs
237# Build from tarball; mutually exclusive:
238 # -tp
239 # -tc
240 # -ti
241 # -ta
242 # -tb
243 # -ts
244# Build from .src.(deb|rpm)
245 # --rebuild
246 # --recompile
247
248# General options
249 # --buildroot=DIRECTORY
250 # --clean
251 # --nobuild
252 # --nodeps
253 # --nodirtokens
254 # --rmsource
255 # --rmspec
256 # --short-circuit
257 # --target=CPU-VENDOR-OS
258
259 #my $popt = new Getopt::Popt(argv => \@ARGV, options => \@optionsTable);
260
261} # end parse_cmd()
262
263
264## parse_spec()
265# Parse the .spec file once we've.... uh....
266sub parse_spec {
267 open SPECFILE,"<$specfile";
268
269LINE: while (<SPECFILE>) {
270 next if /^#/;
271 next if /^\s+$/;
272 if (/^\%/) {
273 # A macro that needs further processing.
274 if (/^\%prep/) {
275 # %prep section. May have %setup macro; may include %patch tags,
276 # may be just a bare shell script.
277
278 # This really should be local-ish, but we need just the filename for the source
279 $pkgdata{main}{source} =~ s|.+/([^/]+)$|$1|;
280
281 # Replace some core macros
282 $pkgdata{main}{source} =~ s/\%\{name\}/$pkgdata{main}{name}/;
283 $pkgdata{main}{source} =~ s/\%\{version\}/$pkgdata{main}{version}/;
284
285PREPSCRIPT: while (<SPECFILE>) {
286 if (/^\%setup/) {
287 # Parse out the %setup macro. Note that we aren't supporting
288 # most of RPM's %setup features.
289 $prepscript .= "cd $topdir/BUILD\n";
290 if ( /\s+-n\s+([^\s]+)\s+/ ) {
291 $tarballdir = $1;
292 } else {
293 $tarballdir = "$pkgdata{main}{name}-$pkgdata{main}{version}";
294 }
295 $prepscript .= "rm -rf $tarballdir\ntar -".
296 ( $pkgdata{main}{source} =~ /\.tar\.gz$/ ? "z" : "" ).
297 ( $pkgdata{main}{source} =~ /\.tar\.bz2$/ ? "j" : "" ).
298 "x".( /\s+-q\s+/ ? '' : 'vv' )."f ".
299 "$topdir/SOURCES/$pkgdata{main}{source}\n".
300 qq(STATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n exit \$STATUS\nfi\n).
301 ( /\s+-n\s+([^\s]+)\s+/ ?
302 "cd $1\n" : "cd $pkgdata{main}{name}-$pkgdata{main}{version}\n" ).
303 qq([ `/usr/bin/id -u` = '0' ] && /bin/chown -Rhf root .\n).
304 qq([ `/usr/bin/id -u` = '0' ] && /bin/chgrp -Rhf root .\n).
305 qq(/bin/chmod -Rf a+rX,g-w,o-w .\n);
306 } elsif (/^\%patch/) {
307 print "patch macro\n";
308 } else {
309 last PREPSCRIPT if /^\%/;
310 $prepscript .= $_;
311 }
312 }
313 redo LINE;
314 }
315 if (/^\%build/) {
316 # %build. This is pretty much just a shell script. There
317 # *are* a few macros, but we're not going to deal with them yet.
318 $buildscript .= "cd $tarballdir\n";
319 while (<SPECFILE>) {
320 redo LINE if /^\%/;
321 $buildscript .= $_;
322 }
323 }
324 if (/^\%install/) {
325 $installscript .= "cd $tarballdir\n";
326 while (<SPECFILE>) {
327 redo LINE if /^\%/;
328 $installscript .= $_;
329 }
330 }
331 if (/^\%clean/) {
332 while (<SPECFILE>) {
333 redo LINE if /^\%/;
334 $cleanscript .= $_;
335 }
336 }
337 if (/^\%post/) {
338 }
339 if (/^\%preun/) {
340 }
341 if (/^\%files/) {
342 # Danger! Danger!
343 }
344 } else {
345 if (/^summary:\s+(.+)/i) {
346 $pkgdata{main}{summary} = $1;
347 } elsif (/^name:\s+(.+)/i) {
348 $pkgdata{main}{name} = $1;
349 } elsif (/^version:\s+(.+)/i) {
350 $pkgdata{main}{version} = $1;
351 } elsif (/^release:\s+(.+)/i) {
352 $pkgdata{main}{release} = $1;
353 } elsif (/^group:\s+(.+)/i) {
354 $pkgdata{main}{group} = $1;
355 } elsif (/^copyright:\s+(.+)/i) {
356 $pkgdata{main}{copyright} = $1;
357 } elsif (/^url:\s+(.+)/i) {
358 $pkgdata{main}{url} = $1;
359 } elsif (/^packager:\s+(.+)/i) {
360 $pkgdata{main}{packager} = $1;
361 } elsif (/^buildroot:\s+(.+)/i) {
362 $buildroot = $1;
363 } elsif (/^source:\s+(.+)/i) {
364 $pkgdata{main}{source} = $1;
365 die "Unknown tarball format $1\n" if $1 !~ /\.tar\.(?:gz|bz2)$/;
366 }
367#Name: suwrap
368#Version: 0.04
369#Release: 3
370#Group: Applications/System
371#Copyright: WebHart internal ONLY. :(
372#BuildArchitectures: i386
373#BuildRoot: /tmp/%{name}-%{version}
374#Url: http://virtual.webhart.net
375#Packager: Kris Deugau <kdeugau@deepnet.cx>
376#Source: ftp://virtual.webhart.net/%{name}-%{version}.tar.gz
377
378 }
379 }
380 # Parse and replace macros
381 # Start with the ones I use
382# $pkgdata{main}{source} =~ s/\%\{name\}/$pkgdata{main}{name}/;
383# $pkgdata{main}{source} =~ s/\%\{version\}/$pkgdata{main}{version}/;
384
385# Scriptlet core. Use /g to make sure all occurrences get tweaked.
386 # "real" globals (apply to all packages the same way)
387 $scriptletbase =~ s/\%\{_topdir\}/$topdir/g;
388
389 # "fake" globals (may be tweaked by package)
390 $buildroot = $cmdbuildroot if $cmdbuildroot;
391 $scriptletbase =~ s/\%\{buildroot\}/$buildroot/g;
392
393 # sub-package(compatible) stuff
394 $scriptletbase =~ s/\%\{name\}/$pkgdata{main}{name}/g;
395 $scriptletbase =~ s/\%\{version\}/$pkgdata{main}{version}/g;
396 $scriptletbase =~ s/\%\{release\}/$pkgdata{main}{release}/g;
397
398} # end parse_spec()
399
400
401## prep()
402# Unpacks the tarball from the SOURCES directory to the BUILD directory.
403# Speaks gzip and bzip2.
404# Finishes by applying patches in %prep section of spec file
405sub prep {
406 {
407 # create script filename
408 my $prepscriptfile = "/var/tmp/deb-tmp.prep.".int(rand(99998)+1);
409 sysopen(PREPSCRIPT, $prepscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
410 or die $!;
411 print PREPSCRIPT $scriptletbase;
412 print PREPSCRIPT $prepscript;
413 close PREPSCRIPT;
414
415 # execute
416 print "Calling \%prep script $prepscriptfile...\n";
417 system("/bin/sh -e $prepscriptfile") == 0
418 or die "Can't exec: $!\nalso: $?";
419
420 # and clean up
421 unlink $prepscriptfile;
422 }
423} # end prep()
424
425
426## build()
427# Execute commands provided as a shell script. It may prove simpler
428# to do as rpm does and actually create a little shell script.
429sub build {
430 # create script filename
431 my $buildscriptfile = "/var/tmp/deb-tmp.build.".int(rand(99998)+1);
432 sysopen(BUILDSCRIPT, $buildscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
433 or die $!;
434 print BUILDSCRIPT $scriptletbase;
435 print BUILDSCRIPT $buildscript;
436 close BUILDSCRIPT;
437
438 # execute
439 print "Calling \%build script $buildscriptfile...\n";
440 system("/bin/sh -e $buildscriptfile") == 0
441 or die "Can't exec: $!\nalso: $?";
442
443 # and clean up
444 unlink $buildscriptfile;
445} # end build()
446
447
448## install()
449# Creates and executes %install script(let)
450sub install {
451 # create script filename
452 my $installscriptfile = "/var/tmp/deb-tmp.inst.".int(rand(99998)+1);
453 sysopen(INSTSCRIPT, $installscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
454 or die $!;
455 print INSTSCRIPT $scriptletbase;
456 print INSTSCRIPT $cleanscript; # Clean up our install target before installing into it.
457 print INSTSCRIPT $installscript;
458 close INSTSCRIPT;
459
460 # execute
461 print "Calling \%install script $installscriptfile...\n";
462 system("/bin/sh -e $installscriptfile") == 0
463 or die "Can't exec: $!\nalso: $?";
464
465 # and clean up
466 unlink $installscriptfile;
467} # end install()
468
469
470sub binpackage {}
471sub srcpackage {}
Note: See TracBrowser for help on using the repository browser.