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