f9dbe21a317981c1144ffde5aacf5a8927ad9fbb
[exim.git] / release-process / scripts / mk_exim_release.pl
1 #!/usr/bin/env perl
2 #
3 # $Cambridge: exim/release-process/scripts/mk_exim_release.pl,v 1.1 2010/06/03 12:00:38 nm4 Exp $
4 #
5 use strict;
6 use warnings;
7 use Carp;
8 use File::Copy;
9 use File::Spec;
10 use File::Path;
11 use File::Temp;
12 use FindBin;
13 use Getopt::Long;
14 use IO::File;
15 use Pod::Usage;
16
17 my $debug   = 0;
18 my $verbose = 0;
19
20 # ------------------------------------------------------------------
21
22 sub get_and_check_version {
23     my $release = shift;
24     my $context = shift;
25
26     # make sure this looks like a real release version
27     # which should (currently) be 4.xx or 4.xx_RCx
28     unless ( $release =~ /^(4\.\d\d(?:_RC\d+)?)$/ ) {
29         croak "The given version number does not look right - $release";
30     }
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;
37 }
38
39 # ------------------------------------------------------------------
40
41 sub build_tag {
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
54 sub 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
73     # create base directory
74     mkpath( $context->{directory}, { verbose => ( $verbose || $debug ) } );
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     }
81 }
82
83 # ------------------------------------------------------------------
84
85 sub export_git_tree {
86     my $context = shift;
87
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} );
92
93     # run git command
94     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
95     system(@cmd) == 0 || croak "Export failed";
96 }
97
98 # ------------------------------------------------------------------
99
100 sub unpack_tree {
101     my $context = shift;
102
103     die "Cannot see archive file\n" unless ( -f $context->{tmp_archive_file} );
104     my @cmd = ( 'tar', 'xf', $context->{tmp_archive_file}, '-C', $context->{release_tree} );
105
106     # run  command
107     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
108     system(@cmd) == 0 || croak "Unpack failed";
109 }
110
111 # ------------------------------------------------------------------
112
113 sub 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
161 sub build_html_documentation {
162     my $context = shift;
163
164     my $genpath   = $context->{webgen_base} . '/script/gen.pl';
165     my $templates = $context->{webgen_base} . '/templates';
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
170     mkdir($dir);
171
172     my @cmd =
173       ( $genpath, '--spec', $spec, '--filter', $filter, '--latest', $context->{trelease}, '--tmpl', $templates, '--docroot', $dir );
174
175     print "Executing ", join( ' ', @cmd ), "\n";
176     system(@cmd);
177
178     # move directory into right place
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
189 sub 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     }
206 }
207
208 # ------------------------------------------------------------------
209
210 sub build_documentation {
211     my $context = shift;
212
213     my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
214     system("cd '$docdir' && ./OS-Fixups && make everything") == 0
215       || croak "Doc build failed";
216
217     copy_docbook_files($context);
218     build_html_documentation($context);
219 }
220
221 # ------------------------------------------------------------------
222
223 sub move_text_docs_into_pkg {
224     my $context = shift;
225
226     my $old_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc', 'doc-docbook' );
227     my $new_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc' );
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
236     foreach my $file ( glob( File::Spec->catfile( 'doc/doc-txt', '*' ) ) ) {
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
250 sub build_pspdfinfo_directory {
251     my $context = shift;
252
253     ##foreach my $format (qw/pdf postscript texinfo info/) {
254     foreach my $format (qw/pdf postscript/) {
255         my $target = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s-%s', $format, $context->{release} ), 'doc' );
256         mkpath( $target, { verbose => ( $verbose || $debug ) } );
257
258         # move documents across
259         foreach my $file (
260             glob(
261                 File::Spec->catfile(
262                     $context->{release_tree},
263                     'doc',
264                     'doc-docbook',
265                     (
266                         ( $format eq 'postscript' )
267                         ? '*.ps'
268                         : ( '*.' . $format )
269                     )
270                 )
271             )
272           )
273         {
274             move( $file, File::Spec->catfile( $target, ( File::Spec->splitpath($file) )[2] ) );
275         }
276     }
277 }
278
279 # ------------------------------------------------------------------
280
281 sub build_main_package_directory {
282     my $context = shift;
283
284     # build the exim package directory path
285     $context->{eximpkgdir} = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s', $context->{release} ) );
286
287     # initially we move the exim-src directory to the new directory name
288     rename( File::Spec->catdir( $context->{release_tree}, 'src' ), $context->{eximpkgdir} )
289       || croak "Rename of src dir failed - $!";
290
291     # add Local subdirectory
292     mkpath( File::Spec->catdir( $context->{eximpkgdir}, 'Local' ), { verbose => ( $verbose || $debug ) } );
293
294     # now add the text docs
295     move_text_docs_into_pkg($context);
296 }
297
298 # ------------------------------------------------------------------
299
300 sub build_package_directories {
301     my $context = shift;
302
303     build_main_package_directory($context);
304     build_pspdfinfo_directory($context);
305 }
306
307 # ------------------------------------------------------------------
308
309 sub 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
320 sub create_tar_files {
321     my $context = shift;
322
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}");
329     }
330 }
331
332 # ------------------------------------------------------------------
333 {
334     my $man;
335     my $help;
336     my $context = {
337         pkgname     => 'exim',
338         orig_dir    => File::Spec->curdir(),
339         tmp_dir     => File::Temp->newdir(),
340         webgen_base => "$FindBin::Bin/../../../exim-website",
341     };
342     my $delete;
343     my $cleanup = 1;
344     ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'};
345
346     unless (
347         GetOptions(
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,
355             'cleanup!'      => \$cleanup,
356         )
357       )
358     {
359         pod2usage( -exitval => 1, -verbose => 0 );
360     }
361     pod2usage(0) if $help;
362     pod2usage( -verbose => 2 ) if $man;
363
364     get_and_check_version( shift, $context );
365     $context->{tag} = build_tag($context);
366     deal_with_working_directory( $context, $delete );
367     export_git_tree($context);
368     chdir( $context->{directory} ) || die;
369     unpack_tree($context);
370     adjust_version_extension($context);
371     build_documentation($context);
372     build_package_directories($context);
373     create_tar_files($context);
374     do_cleanup($context) if ($cleanup);
375 }
376
377 1;
378
379 __END__
380
381 =head1 NAME
382
383 mk_exim_release.pl - Build an exim release
384
385 =head1 SYNOPSIS
386
387 mk_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
395    --delete            Delete packaging directory at start
396
397 =head1 OPTIONS
398
399 =over 4
400
401 =item B<--debug>
402
403 Forces debug mode cause all SQL statements generated by L<DBIx::Class>
404 to be output.
405
406 =item B<--verbose>
407
408 Force verbose mode - currently this has no effect
409
410 =item B<--help>
411
412 Display help and exits
413
414 =item B<--man>
415
416 Display man page
417
418 =back
419
420 =head1 DESCRIPTION
421
422 Builds an exim release.
423
424 Starting in a populated git repo that has already been tagged for
425 release, build docs, build packages etc.
426
427 Parameter is the version number to build as - ie 4.72 4.72RC1 etc
428
429 =head1 AUTHOR
430
431 Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>
432
433 =head1 COPYRIGHT
434
435 Copyright 2010 Exim Maintainers. All rights reserved.
436
437 =cut