RC releases get marked as such in version.h.
[exim.git] / release-process / scripts / mk_exim_release.pl
CommitLineData
46c573de 1#!/usr/bin/env perl
8a483da6
NM
2#
3# $Cambridge: exim/release-process/scripts/mk_exim_release.pl,v 1.1 2010/06/03 12:00:38 nm4 Exp $
4#
5use strict;
6use warnings;
7use Carp;
8use File::Copy;
9use File::Spec;
10use File::Path;
8f29c950 11use File::Temp;
be914e6c 12use FindBin;
8a483da6 13use Getopt::Long;
ba41f854 14use IO::File;
8a483da6
NM
15use Pod::Usage;
16
17my $debug = 0;
18my $verbose = 0;
19
20# ------------------------------------------------------------------
21
22sub get_and_check_version {
23 my $release = shift;
be914e6c 24 my $context = shift;
8a483da6
NM
25
26 # make sure this looks like a real release version
8f29c950 27 # which should (currently) be 4.xx or 4.xx_RCx
8a483da6
NM
28 unless ( $release =~ /^(4\.\d\d(?:_RC\d+)?)$/ ) {
29 croak "The given version number does not look right - $release";
30 }
be914e6c
NM
31 my $full_release = $1; # untainted here...
32 my $trunc_release = $full_release;
33 $trunc_release =~ s/^(4\.\d\d)(?:_RC\d+)?$/$1/;
34
35 $context->{release} = $full_release;
36 $context->{trelease} = $trunc_release;
8a483da6
NM
37}
38
39# ------------------------------------------------------------------
40
8f29c950 41sub build_tag {
8a483da6
NM
42 my $context = shift;
43
44 # The CVS tag consists of exim-$version where $version
45 # is the version number with . replaced with _
46 my $modversion = $context->{release};
47 $modversion =~ tr/0-9RC/_/cs;
48
49 return sprintf( 'exim-%s', $modversion );
50}
51
52# ------------------------------------------------------------------
53
54sub deal_with_working_directory {
55 my $context = shift;
56 my $delete = shift;
57
58 # Set default directory
59 $context->{directory} ||= File::Spec->rel2abs( sprintf( 'exim-packaging-%s', $context->{release} ) );
60 my $directory = $context->{directory};
61
62 # ensure the working directory is not in place
63 if ( -d $directory ) {
64 if ($delete) {
65 print "Deleting existing $directory\n" if ($verbose);
66 rmtree( $directory, { verbose => $debug } );
67 }
68 if ( -d $directory ) {
69 croak "Working directory $directory exists";
70 }
71 }
72
46c573de 73 # create base directory
8a483da6 74 mkpath( $context->{directory}, { verbose => ( $verbose || $debug ) } );
46c573de
NM
75
76 # set and create subdirectories
77 foreach (qw(release_tree pkgs pkgdirs docbook)) {
78 $context->{$_} = File::Spec->catdir( $context->{directory}, $_ );
79 mkpath( $context->{$_}, { verbose => ( $verbose || $debug ) } );
80 }
8a483da6
NM
81}
82
83# ------------------------------------------------------------------
84
8f29c950 85sub export_git_tree {
8a483da6
NM
86 my $context = shift;
87
8f29c950
NM
88 # build git command
89 my $archive_file = sprintf( '%s/%s-%s.tar', $context->{tmp_dir}, $context->{pkgname}, $context->{release} );
90 $context->{tmp_archive_file} = $archive_file;
91 my @cmd = ( 'git', 'archive', '--format=tar', "--output=$archive_file", $context->{tag} );
8a483da6 92
8f29c950 93 # run git command
8a483da6
NM
94 print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
95 system(@cmd) == 0 || croak "Export failed";
96}
97
98# ------------------------------------------------------------------
99
8f29c950
NM
100sub unpack_tree {
101 my $context = shift;
102
103 die "Cannot see archive file\n" unless ( -f $context->{tmp_archive_file} );
46c573de 104 my @cmd = ( 'tar', 'xf', $context->{tmp_archive_file}, '-C', $context->{release_tree} );
8f29c950
NM
105
106 # run command
107 print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
108 system(@cmd) == 0 || croak "Unpack failed";
109}
110
111# ------------------------------------------------------------------
112
ba41f854
PP
113sub adjust_version_extension {
114 my $context = shift;
115
116 return if ($context->{release} eq $context->{trelease});
117
118 my $variant = substr( $context->{release}, length($context->{trelease}) );
119 if ( $context->{release} ne $context->{trelease} . $variant ) {
120 die "Broken version numbering, I'm buggy";
121 }
122
123 my $srcdir = File::Spec->catdir( $context->{release_tree}, 'src', 'src' );
124 my $version_h = File::Spec->catfile( $srcdir, 'version.h' );
125
126 my $fh = new IO::File $version_h, 'r';
127 die "Cannot read version.h: $!\n" unless ( defined $fh );
128 my @lines = <$fh>;
129 $fh->close() or die "Failed to close-read($version_h): $!\n";
130
131 my $found = 0;
132 my $i;
133 for ( $i = 0 ; $i < @lines ; ++$i ) {
134 if ( $lines[$i] =~ /EXIM_VARIANT_VERSION/ ) {
135 $found = 1;
136 last;
137 }
138 }
139 die "Cannot find version.h EXIM_VARIANT_VERSION\n" unless $found;
140 unless ( $lines[$i] =~ m/^\s* \# \s* define \s+ EXIM_VARIANT_VERSION \s+ "(.*)" \s* $/x ) {
141 die "Broken version.h EXIM_VARIANT_VERSION line\n";
142 }
143 if ( length $1 ) {
144 print( "WARNING: version.h has a variant tag already defined: $1\n" );
145 print( " not changing that tag\n" );
146 return;
147 }
148
149 $lines[$i] = qq{#define EXIM_VARIANT_VERSION\t\t"$variant"\n};
150 # deliberately not verbose constrained:
151 print( "Adjusting version.h for $variant release.\n" );
152
153 $fh = new IO::File $version_h, "w";
154 die "Cannot write version.h: $!\n" unless ( defined $fh );
155 $fh->print( @lines );
156 $fh->close() or die "Failed to close-write($version_h): $!\n";
157}
158
159# ------------------------------------------------------------------
160
be914e6c
NM
161sub build_html_documentation {
162 my $context = shift;
163
164 my $genpath = $context->{webgen_base} . '/script/gen.pl';
165 my $templates = $context->{webgen_base} . '/templates';
46c573de
NM
166 my $dir = File::Spec->catdir( $context->{release_tree}, 'html' );
167 my $spec = File::Spec->catfile( $context->{docbook}, 'spec.xml' );
168 my $filter = File::Spec->catfile( $context->{docbook}, 'filter.xml' );
169
be914e6c
NM
170 mkdir($dir);
171
46c573de
NM
172 my @cmd =
173 ( $genpath, '--spec', $spec, '--filter', $filter, '--latest', $context->{trelease}, '--tmpl', $templates, '--docroot', $dir );
be914e6c
NM
174
175 print "Executing ", join( ' ', @cmd ), "\n";
176 system(@cmd);
177
178 # move directory into right place
46c573de
NM
179 my $sourcedir = File::Spec->catdir( $context->{docbook}, 'filter.xml' );
180
181 rename(
182 File::Spec->catdir( $dir, sprintf( 'exim-html-%s', $context->{trelease} ) ),
183 File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-html-%s', $context->{release} ) )
184 );
185}
186
187# ------------------------------------------------------------------
188
189sub copy_docbook_files {
190 my $context = shift;
191
192 # where the generated docbook files can be found
193 my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
194
195 # where the website docbook source dir is - push files to here
196 my $webdir = File::Spec->catdir( $context->{webgen_base}, 'docbook', $context->{trelease} );
197 mkpath( $webdir, { verbose => ( $verbose || $debug ) } );
198
199 foreach my $file ( 'spec.xml', 'filter.xml' ) {
200 my $from = File::Spec->catfile( $docdir, $file );
201 my $to = File::Spec->catfile( $context->{docbook}, $file );
202 my $webto = File::Spec->catfile( $webdir, $file );
203 copy( $from, $to );
204 copy( $from, $webto );
205 }
be914e6c
NM
206}
207
208# ------------------------------------------------------------------
209
8a483da6 210sub build_documentation {
be914e6c
NM
211 my $context = shift;
212
46c573de
NM
213 my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
214 system("cd '$docdir' && ./OS-Fixups && make everything") == 0
8a483da6 215 || croak "Doc build failed";
be914e6c 216
46c573de 217 copy_docbook_files($context);
be914e6c 218 build_html_documentation($context);
8a483da6
NM
219}
220
221# ------------------------------------------------------------------
222
223sub move_text_docs_into_pkg {
224 my $context = shift;
225
46c573de
NM
226 my $old_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc', 'doc-docbook' );
227 my $new_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc' );
8a483da6
NM
228 mkpath( $new_docdir, { verbose => ( $verbose || $debug ) } );
229
230 # move generated documents from docbook stuff
231 foreach my $file (qw/exim.8 spec.txt filter.txt/) {
232 move( File::Spec->catfile( $old_docdir, $file ), File::Spec->catfile( $new_docdir, $file ) );
233 }
234
235 # move text documents across
8f29c950 236 foreach my $file ( glob( File::Spec->catfile( 'doc/doc-txt', '*' ) ) ) {
8a483da6
NM
237
238 # skip a few we dont want
239 my $fn = ( File::Spec->splitpath($file) )[2];
240 next
241 if ( ( $fn eq 'ABOUT' )
242 || ( $fn eq 'ChangeLog.0' )
243 || ( $fn eq 'test-harness.txt' ) );
244 move( $file, File::Spec->catfile( $new_docdir, $fn ) );
245 }
246}
247
248# ------------------------------------------------------------------
249
250sub build_pspdfinfo_directory {
251 my $context = shift;
252
253 ##foreach my $format (qw/pdf postscript texinfo info/) {
254 foreach my $format (qw/pdf postscript/) {
46c573de 255 my $target = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s-%s', $format, $context->{release} ), 'doc' );
8a483da6
NM
256 mkpath( $target, { verbose => ( $verbose || $debug ) } );
257
258 # move documents across
259 foreach my $file (
260 glob(
261 File::Spec->catfile(
46c573de
NM
262 $context->{release_tree},
263 'doc',
264 'doc-docbook',
8a483da6
NM
265 (
266 ( $format eq 'postscript' )
267 ? '*.ps'
268 : ( '*.' . $format )
269 )
270 )
271 )
272 )
273 {
46c573de 274 move( $file, File::Spec->catfile( $target, ( File::Spec->splitpath($file) )[2] ) );
8a483da6
NM
275 }
276 }
277}
278
279# ------------------------------------------------------------------
280
8a483da6
NM
281sub build_main_package_directory {
282 my $context = shift;
283
46c573de
NM
284 # build the exim package directory path
285 $context->{eximpkgdir} = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s', $context->{release} ) );
286
8a483da6 287 # initially we move the exim-src directory to the new directory name
46c573de
NM
288 rename( File::Spec->catdir( $context->{release_tree}, 'src' ), $context->{eximpkgdir} )
289 || croak "Rename of src dir failed - $!";
8a483da6
NM
290
291 # add Local subdirectory
46c573de 292 mkpath( File::Spec->catdir( $context->{eximpkgdir}, 'Local' ), { verbose => ( $verbose || $debug ) } );
8a483da6
NM
293
294 # now add the text docs
295 move_text_docs_into_pkg($context);
296}
297
298# ------------------------------------------------------------------
299
300sub build_package_directories {
301 my $context = shift;
302
303 build_main_package_directory($context);
304 build_pspdfinfo_directory($context);
8a483da6
NM
305}
306
307# ------------------------------------------------------------------
308
46c573de
NM
309sub do_cleanup {
310 my $context = shift;
311
312 print "Cleaning up\n" if ($verbose);
313 rmtree( $context->{release_tree}, { verbose => $debug } );
314 rmtree( $context->{docbook}, { verbose => $debug } );
315 rmtree( $context->{pkgdirs}, { verbose => $debug } );
316}
317
318# ------------------------------------------------------------------
319
8a483da6
NM
320sub create_tar_files {
321 my $context = shift;
322
46c573de
NM
323 my $pkgs = $context->{pkgs};
324 my $pkgdirs = $context->{pkgdirs};
325 foreach my $dir ( glob( File::Spec->catdir( $pkgdirs, ( 'exim*-' . $context->{release} ) ) ) ) {
326 my $dirname = ( File::Spec->splitdir($dir) )[-1];
327 system("tar cfz ${pkgs}/${dirname}.tar.gz -C ${pkgdirs} ${dirname}");
328 system("tar cfj ${pkgs}/${dirname}.tar.bz2 -C ${pkgdirs} ${dirname}");
8a483da6
NM
329 }
330}
331
332# ------------------------------------------------------------------
333{
334 my $man;
335 my $help;
336 my $context = {
be914e6c
NM
337 pkgname => 'exim',
338 orig_dir => File::Spec->curdir(),
339 tmp_dir => File::Temp->newdir(),
340 webgen_base => "$FindBin::Bin/../../../exim-website",
8a483da6
NM
341 };
342 my $delete;
46c573de 343 my $cleanup = 1;
8f29c950 344 ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'};
8a483da6
NM
345
346 unless (
347 GetOptions(
be914e6c
NM
348 'directory=s' => \$context->{directory},
349 'webgen_base=s' => \$context->{webgen_base},
350 'verbose!' => \$verbose,
351 'debug!' => \$debug,
352 'help|?' => \$help,
353 'man!' => \$man,
354 'delete!' => \$delete,
46c573de 355 'cleanup!' => \$cleanup,
8a483da6
NM
356 )
357 )
358 {
359 pod2usage( -exitval => 1, -verbose => 0 );
360 }
361 pod2usage(0) if $help;
362 pod2usage( -verbose => 2 ) if $man;
363
be914e6c
NM
364 get_and_check_version( shift, $context );
365 $context->{tag} = build_tag($context);
8a483da6 366 deal_with_working_directory( $context, $delete );
8f29c950 367 export_git_tree($context);
8a483da6 368 chdir( $context->{directory} ) || die;
8f29c950 369 unpack_tree($context);
ba41f854 370 adjust_version_extension($context);
8a483da6
NM
371 build_documentation($context);
372 build_package_directories($context);
373 create_tar_files($context);
46c573de 374 do_cleanup($context) if ($cleanup);
8a483da6
NM
375}
376
3771;
378
379__END__
380
381=head1 NAME
382
383mk_exim_release.pl - Build an exim release
384
385=head1 SYNOPSIS
386
387mk_exim_release.pl [options] version
388
389 Options:
390 --debug force debug mode (SQL Trace)
391 --verbose force verbose mode
392 --help display this help and exits
393 --man displays man page
394 --directory=dir dir to package
8a483da6
NM
395 --delete Delete packaging directory at start
396
397=head1 OPTIONS
398
399=over 4
400
401=item B<--debug>
402
403Forces debug mode cause all SQL statements generated by L<DBIx::Class>
404to be output.
405
406=item B<--verbose>
407
408Force verbose mode - currently this has no effect
409
410=item B<--help>
411
412Display help and exits
413
414=item B<--man>
415
416Display man page
417
418=back
419
420=head1 DESCRIPTION
421
422Builds an exim release.
423
8f29c950
NM
424Starting in a populated git repo that has already been tagged for
425release, build docs, build packages etc.
8a483da6
NM
426
427Parameter is the version number to build as - ie 4.72 4.72RC1 etc
428
429=head1 AUTHOR
430
431Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>
432
433=head1 COPYRIGHT
434
8f29c950 435Copyright 2010 Exim Maintainers. All rights reserved.
8a483da6
NM
436
437=cut