566f2619d8748d5531982ef001ba8150be32f565
[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->{release_tree}, 'doc', 'doc-docbook' );
227     my $old_txtdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-txt' );
228     my $new_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc' );
229     mkpath( $new_docdir, { verbose => ( $verbose || $debug ) } );
230
231     # move generated documents from docbook stuff
232     foreach my $file (qw/exim.8 spec.txt filter.txt/) {
233         move( File::Spec->catfile( $old_docdir, $file ), File::Spec->catfile( $new_docdir, $file ) );
234     }
235
236     # move text documents across
237     foreach my $file ( glob( File::Spec->catfile( $old_txtdir, '*' ) ) ) {
238
239         # skip a few we dont want
240         my $fn = ( File::Spec->splitpath($file) )[2];
241         next
242           if ( ( $fn eq 'ABOUT' )
243             || ( $fn eq 'ChangeLog.0' )
244             || ( $fn eq 'test-harness.txt' ) );
245         move( $file, File::Spec->catfile( $new_docdir, $fn ) );
246     }
247 }
248
249 # ------------------------------------------------------------------
250
251 sub build_pspdfinfo_directory {
252     my $context = shift;
253
254     ##foreach my $format (qw/pdf postscript texinfo info/) {
255     foreach my $format (qw/pdf postscript/) {
256         my $target = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s-%s', $format, $context->{release} ), 'doc' );
257         mkpath( $target, { verbose => ( $verbose || $debug ) } );
258
259         # move documents across
260         foreach my $file (
261             glob(
262                 File::Spec->catfile(
263                     $context->{release_tree},
264                     'doc',
265                     'doc-docbook',
266                     (
267                         ( $format eq 'postscript' )
268                         ? '*.ps'
269                         : ( '*.' . $format )
270                     )
271                 )
272             )
273           )
274         {
275             move( $file, File::Spec->catfile( $target, ( File::Spec->splitpath($file) )[2] ) );
276         }
277     }
278 }
279
280 # ------------------------------------------------------------------
281
282 sub build_main_package_directory {
283     my $context = shift;
284
285     # build the exim package directory path
286     $context->{eximpkgdir} = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s', $context->{release} ) );
287
288     # initially we move the exim-src directory to the new directory name
289     rename( File::Spec->catdir( $context->{release_tree}, 'src' ), $context->{eximpkgdir} )
290       || croak "Rename of src dir failed - $!";
291
292     # add Local subdirectory
293     mkpath( File::Spec->catdir( $context->{eximpkgdir}, 'Local' ), { verbose => ( $verbose || $debug ) } );
294
295     # now add the text docs
296     move_text_docs_into_pkg($context);
297 }
298
299 # ------------------------------------------------------------------
300
301 sub build_package_directories {
302     my $context = shift;
303
304     build_main_package_directory($context);
305     build_pspdfinfo_directory($context);
306 }
307
308 # ------------------------------------------------------------------
309
310 sub do_cleanup {
311     my $context = shift;
312
313     print "Cleaning up\n" if ($verbose);
314     rmtree( $context->{release_tree}, { verbose => $debug } );
315     rmtree( $context->{docbook},      { verbose => $debug } );
316     rmtree( $context->{pkgdirs},      { verbose => $debug } );
317 }
318
319 # ------------------------------------------------------------------
320
321 sub create_tar_files {
322     my $context = shift;
323
324     my $pkgs    = $context->{pkgs};
325     my $pkgdirs = $context->{pkgdirs};
326     foreach my $dir ( glob( File::Spec->catdir( $pkgdirs, ( 'exim*-' . $context->{release} ) ) ) ) {
327         my $dirname = ( File::Spec->splitdir($dir) )[-1];
328         system("tar cfz ${pkgs}/${dirname}.tar.gz  -C ${pkgdirs} ${dirname}");
329         system("tar cfj ${pkgs}/${dirname}.tar.bz2 -C ${pkgdirs} ${dirname}");
330     }
331 }
332
333 # ------------------------------------------------------------------
334 {
335     my $man;
336     my $help;
337     my $context = {
338         pkgname     => 'exim',
339         orig_dir    => File::Spec->curdir(),
340         tmp_dir     => File::Temp->newdir(),
341         webgen_base => "$FindBin::Bin/../../../exim-website",
342     };
343     my $delete;
344     my $cleanup = 1;
345     ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'};
346
347     unless (
348         GetOptions(
349             'directory=s'   => \$context->{directory},
350             'webgen_base=s' => \$context->{webgen_base},
351             'verbose!'      => \$verbose,
352             'debug!'        => \$debug,
353             'help|?'        => \$help,
354             'man!'          => \$man,
355             'delete!'       => \$delete,
356             'cleanup!'      => \$cleanup,
357         )
358       )
359     {
360         pod2usage( -exitval => 1, -verbose => 0 );
361     }
362     pod2usage(0) if $help;
363     pod2usage( -verbose => 2 ) if $man;
364
365     get_and_check_version( shift, $context );
366     $context->{tag} = build_tag($context);
367     deal_with_working_directory( $context, $delete );
368     export_git_tree($context);
369     chdir( $context->{directory} ) || die;
370     unpack_tree($context);
371     adjust_version_extension($context);
372     build_documentation($context);
373     build_package_directories($context);
374     create_tar_files($context);
375     do_cleanup($context) if ($cleanup);
376 }
377
378 1;
379
380 __END__
381
382 =head1 NAME
383
384 mk_exim_release.pl - Build an exim release
385
386 =head1 SYNOPSIS
387
388 mk_exim_release.pl [options] version
389
390  Options:
391    --debug             force debug mode (SQL Trace)
392    --verbose           force verbose mode
393    --help              display this help and exits
394    --man               displays man page
395    --directory=dir     dir to package
396    --delete            Delete packaging directory at start
397
398 =head1 OPTIONS
399
400 =over 4
401
402 =item B<--debug>
403
404 Forces debug mode cause all SQL statements generated by L<DBIx::Class>
405 to be output.
406
407 =item B<--verbose>
408
409 Force verbose mode - currently this has no effect
410
411 =item B<--help>
412
413 Display help and exits
414
415 =item B<--man>
416
417 Display man page
418
419 =back
420
421 =head1 DESCRIPTION
422
423 Builds an exim release.
424
425 Starting in a populated git repo that has already been tagged for
426 release, build docs, build packages etc.
427
428 Parameter is the version number to build as - ie 4.72 4.72RC1 etc
429
430 =head1 AUTHOR
431
432 Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>
433
434 =head1 COPYRIGHT
435
436 Copyright 2010 Exim Maintainers. All rights reserved.
437
438 =cut