Release: fix release script
[exim.git] / release-process / scripts / mk_exim_release.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use Carp;
6 use File::Copy;
7 use File::Spec;
8 use File::Path;
9 use File::Temp;
10 use FindBin;
11 use Getopt::Long;
12 use IO::File;
13 use Pod::Usage;
14
15 my $debug   = 0;
16 my $verbose = 0;
17
18 # ------------------------------------------------------------------
19
20 sub get_and_check_version {
21     my $release = shift;
22     my $context = shift;
23
24     # make sure this looks like a real release version
25     # which should (currently) be 4.xx[.y] or 4.xx[.y]_RCx
26     unless ( $release =~ /^(?<release>(?<major>4\.\d\d)(?:\.(?<minor>\d+))?(?:_RC\d+)?)$/ ) {
27         croak "The given version number does not look right - $release";
28     }
29     $context->{release}  = $+{release};
30     $context->{major} = $+{major};
31     $context->{minor} = $+{minor};
32
33     ($context->{trelease} = $+{release}) =~ s/_RC\d+//;
34 }
35
36 # ------------------------------------------------------------------
37
38 sub build_tag {
39     my $context = shift;
40
41     # The CVS tag consists of exim-$version where $version
42     # is the version number with . replaced with _
43     my $modversion = $context->{release};
44     $modversion =~ tr/0-9RC/_/cs;
45
46     return sprintf( 'exim-%s', $modversion );
47 }
48
49 # ------------------------------------------------------------------
50
51 sub deal_with_working_directory {
52     my $context = shift;
53     my $delete  = shift;
54
55     # Set default directory
56     $context->{directory} ||= File::Spec->rel2abs( sprintf( 'exim-packaging-%s', $context->{release} ) );
57     my $directory = $context->{directory};
58
59     # ensure the working directory is not in place
60     if ( -d $directory ) {
61         if ($delete) {
62             print "Deleting existing $directory\n" if ($verbose);
63             rmtree( $directory, { verbose => $debug } );
64         }
65         if ( -d $directory ) {
66             croak "Working directory $directory exists";
67         }
68     }
69
70     # create base directory
71     mkpath( $context->{directory}, { verbose => ( $verbose || $debug ) } );
72
73     # set and create subdirectories
74     foreach (qw(release_tree pkgs pkgdirs docbook tmp)) {
75         $context->{$_} = File::Spec->catdir( $context->{directory}, $_ );
76         mkpath( $context->{$_}, { verbose => ( $verbose || $debug ) } );
77     }
78 }
79
80 # ------------------------------------------------------------------
81
82 sub export_git_tree {
83     my $context = shift;
84
85     # build git command
86     my $archive_file = sprintf( '%s/%s-%s.tar', $context->{tmp}, $context->{pkgname}, $context->{release} );
87     $context->{tmp_archive_file} = $archive_file;
88     my @cmd = ( 'git', 'archive', '--format=tar', "--output=$archive_file", $context->{tag} );
89     # run git command
90     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
91     system(@cmd) == 0 || croak "Export failed";
92 }
93
94 # ------------------------------------------------------------------
95
96 sub unpack_tree {
97     my $context = shift;
98
99     die "Cannot see archive file\n" unless ( -f $context->{tmp_archive_file} );
100     my @cmd = ( 'tar', 'xf', $context->{tmp_archive_file}, '-C', $context->{release_tree} );
101
102     # run  command
103     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
104     system(@cmd) == 0 || croak "Unpack failed";
105 }
106
107 # ------------------------------------------------------------------
108
109 sub make_version_script {
110     my $context = shift;
111
112     my $variant = substr( $context->{release}, length($context->{trelease}) );
113     if ( $context->{release} ne $context->{trelease} . $variant ) {
114         die "Broken version numbering, I'm buggy";
115     }
116
117     my $srcdir    = File::Spec->catdir( $context->{release_tree}, 'src', 'src' );
118     chdir $srcdir or die "chdir $srcdir: $!\n";
119
120     if ( -f "version.sh" ) {
121         print( "WARNING: version.sh already exists - leaving it in place\n" );
122         return;
123     }
124
125     # Currently (25. Feb. 2016) the mk_exim_release.pl up to now can't
126     # deal with security releases.!? So we need a current
127     # mk_exim_release.pl. But if we use a current (master), the
128     # reversion script returns wrong version info (it's running inside
129     # the Git tree and uses git --describe, which always returns the
130     # current version of master.) I do not want to change the old
131     # reversion scripts (in 4.86.1, 4.85.1).
132     #
133     # Thus we've to provide the version.sh, based on the info we have
134     # about the release. If reversion finds this, it doesn't try to find
135     # it's own way to get a valid version number from the git.
136     open(my $v, '>', 'version.sh') or die "Can't open '>version.sh' $!\n";
137     print {$v} <<__;
138 # initial version automatically generated from $0
139 EXIM_RELEASE_VERSION=$context->{major}
140 EXIM_VARIANT_VERSION=@{[$context->{minor}?'_'.$context->{minor}:'']}
141 EXIM_COMPILE_NUMBER=0
142 __
143     close($v);
144     unlink 'version.h';
145     return;
146
147     # Later, if we get the reversion script fixed, we can call it again.
148     # For now (25. Feb. 2016) we'll leave it unused.
149     my @cmd = ("../scripts/reversion", "release", $context->{tag});
150     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
151     system(@cmd) == 0 || croak "reversion failed";
152
153     unlink "version.h";
154
155     -f "version.sh" or die "failed to create version.sh";
156 }
157
158 # ------------------------------------------------------------------
159
160 sub build_html_documentation {
161     my $context = shift;
162
163     my $genpath   = $context->{webgen_base} . '/script/gen.pl';
164     my $templates = $context->{webgen_base} . '/templates';
165     my $dir       = File::Spec->catdir( $context->{release_tree}, 'html' );
166     my $spec      = File::Spec->catfile( $context->{docbook}, 'spec.xml' );
167     my $filter    = File::Spec->catfile( $context->{docbook}, 'filter.xml' );
168
169     mkdir($dir);
170
171     my @cmd = (
172         $genpath,   '--spec',    $spec,                '--filter',
173         $filter,    '--latest',  $context->{trelease}, '--tmpl',
174         $templates, '--docroot', $dir,                 '--localstatic'
175     );
176     push @cmd, '--verbose' if $verbose or $debug;
177
178     print "Executing ", join( ' ', @cmd ), "\n";
179     system(@cmd);
180
181     # move directory into right place
182     my $sourcedir = File::Spec->catdir( $context->{docbook}, 'filter.xml' );
183
184     rename(
185         File::Spec->catdir( $dir,                sprintf( 'exim-html-%s', $context->{trelease} ) ),
186         File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-html-%s', $context->{release} ) )
187     );
188 }
189
190 # ------------------------------------------------------------------
191
192 sub copy_docbook_files {
193     my $context = shift;
194
195     # where the generated docbook files can be found
196     my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
197
198     # where the website docbook source dir is - push files to here
199     my $webdir = File::Spec->catdir( $context->{webgen_base}, 'docbook', $context->{trelease} );
200     mkpath( $webdir, { verbose => ( $verbose || $debug ) } );
201
202     foreach my $file ( 'spec.xml', 'filter.xml' ) {
203         my $from  = File::Spec->catfile( $docdir,             $file );
204         my $to    = File::Spec->catfile( $context->{docbook}, $file );
205         my $webto = File::Spec->catfile( $webdir,             $file );
206         copy( $from, $to );
207         copy( $from, $webto );
208     }
209 }
210
211 # ------------------------------------------------------------------
212
213 sub build_documentation {
214     my $context = shift;
215
216     my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
217     # documentation building gets the truncated release, without RC
218     system("cd '$docdir' && ./OS-Fixups && $context->{make_cmd} EXIM_VER=$context->{trelease} everything") == 0
219       || croak "Doc build failed";
220
221     copy_docbook_files($context);
222     build_html_documentation($context);
223 }
224
225 # ------------------------------------------------------------------
226
227 sub move_text_docs_into_pkg {
228     my $context = shift;
229
230     my $old_docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
231     my $old_txtdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-txt' );
232     my $new_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc' );
233     mkpath( $new_docdir, { verbose => ( $verbose || $debug ) } );
234
235     # move generated documents from docbook stuff
236     foreach my $file (qw/exim.8 spec.txt filter.txt/) {
237         move( File::Spec->catfile( $old_docdir, $file ), File::Spec->catfile( $new_docdir, $file ) );
238     }
239
240     # move text documents across
241     foreach my $file ( glob( File::Spec->catfile( $old_txtdir, '*' ) ) ) {
242
243         # skip a few we dont want
244         my $fn = ( File::Spec->splitpath($file) )[2];
245         next
246           if ( ( $fn eq 'ABOUT' )
247             || ( $fn eq 'ChangeLog.0' )
248             || ( $fn eq 'test-harness.txt' )
249             # Debian issue re licensing of RFCs
250             || ( $fn =~ /^draft-ietf-.*/ )
251             || ( $fn =~ /^rfc.*/ )
252              );
253         move( $file, File::Spec->catfile( $new_docdir, $fn ) );
254     }
255 }
256
257 # ------------------------------------------------------------------
258
259 sub build_pspdfinfo_directory {
260     my $context = shift;
261
262     ##foreach my $format (qw/pdf postscript texinfo info/) {
263     foreach my $format (qw/pdf postscript/) {
264         my $target = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s-%s', $format, $context->{release} ), 'doc' );
265         mkpath( $target, { verbose => ( $verbose || $debug ) } );
266
267         # move documents across
268         foreach my $file (
269             glob(
270                 File::Spec->catfile(
271                     $context->{release_tree},
272                     'doc',
273                     'doc-docbook',
274                     (
275                         ( $format eq 'postscript' )
276                         ? '*.ps'
277                         : ( '*.' . $format )
278                     )
279                 )
280             )
281           )
282         {
283             move( $file, File::Spec->catfile( $target, ( File::Spec->splitpath($file) )[2] ) );
284         }
285     }
286 }
287
288 # ------------------------------------------------------------------
289
290 sub build_main_package_directory {
291     my $context = shift;
292
293     # build the exim package directory path
294     $context->{eximpkgdir} = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s', $context->{release} ) );
295
296     # initially we move the exim-src directory to the new directory name
297     rename( File::Spec->catdir( $context->{release_tree}, 'src' ), $context->{eximpkgdir} )
298       || croak "Rename of src dir failed - $!";
299
300     # add Local subdirectory
301     mkpath( File::Spec->catdir( $context->{eximpkgdir}, 'Local' ), { verbose => ( $verbose || $debug ) } );
302
303     # now add the text docs
304     move_text_docs_into_pkg($context);
305 }
306
307 # ------------------------------------------------------------------
308
309 sub build_package_directories {
310     my $context = shift;
311
312     build_main_package_directory($context);
313     build_pspdfinfo_directory($context) if $context->{build_docs};
314 }
315
316 # ------------------------------------------------------------------
317
318 sub do_cleanup {
319     my $context = shift;
320
321     print "Cleaning up\n" if ($verbose);
322     chdir( $context->{directory} ) || die;
323     rmtree( $context->{release_tree}, { verbose => $debug } );
324     rmtree( $context->{docbook},      { verbose => $debug } );
325     rmtree( $context->{pkgdirs},      { verbose => $debug } );
326 }
327
328 # ------------------------------------------------------------------
329
330 # We prefer gtar to tar if gtar exists in $PATH
331
332 sub fix_paths_tar {
333     my $context = shift;
334     my $tar = $context->{tar_cmd};
335
336     return unless $tar eq 'tar';
337
338     foreach my $d (File::Spec->path()) {
339         my $p = File::Spec->catfile($d, 'gtar');
340         if (-x $p) {
341             $context->{tar_cmd} = $p;
342             print "Switched tar command to: $p\n" if ($verbose);
343             return;
344         }
345     }
346 }
347
348 # ------------------------------------------------------------------
349
350 sub create_tar_files {
351     my $context = shift;
352
353     my $pkgs    = $context->{pkgs};
354     my $pkgdirs = $context->{pkgdirs};
355     my $tar     = $context->{tar_cmd};
356     if ($verbose) {
357         foreach my $c (keys %{ $context->{compressors} }) {
358             print "Compression: $c\t$context->{compressors}{$c}\n";
359         }
360     }
361
362     foreach my $dir ( glob( File::Spec->catdir( $pkgdirs, ( 'exim*-' . $context->{release} ) ) ) ) {
363         my $dirname = ( File::Spec->splitdir($dir) )[-1];
364         if ($context->{compressors}{gzip}) {
365             print "Creating: ${pkgs}/${dirname}.tar.gz\n" if ($verbose || $debug);
366             system("$tar cf  ${pkgs}/${dirname}.tar.gz  --gzip  -C ${pkgdirs} ${dirname}")
367         }
368         if ($context->{compressors}{bzip2}) {
369             print "Creating: ${pkgs}/${dirname}.tar.bz2\n" if ($verbose || $debug);
370             system("$tar cf  ${pkgs}/${dirname}.tar.bz2 --bzip2 -C ${pkgdirs} ${dirname}")
371         }
372         if ($context->{compressors}{lzip}) {
373             print "Creating: ${pkgs}/${dirname}.tar.lz\n" if ($verbose || $debug);
374             system("$tar cf  ${pkgs}/${dirname}.tar.lz  --lzip  -C ${pkgdirs} ${dirname}")
375         }
376     }
377 }
378
379 # ------------------------------------------------------------------
380 {
381     my $man;
382     my $help;
383     my $context = {
384         pkgname     => 'exim',
385         orig_dir    => File::Spec->curdir(),
386         tmp_dir     => File::Temp->newdir(),
387         webgen_base => "$FindBin::Bin/../../../exim-website",
388         tar_cmd     => 'tar',
389         make_cmd    => 'make',
390         compressors => {
391                 gzip    => 1,
392                 bzip2   => 1,
393                 lzip    => 0,
394         },
395         build_docs   => 1,
396     };
397     my $delete;
398     my $cleanup = 1;
399     ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'};
400
401     unless (
402         GetOptions(
403             'directory=s'   => \$context->{directory},
404             'webgen_base=s' => \$context->{webgen_base},
405             'tar=s'         => \$context->{tar_cmd},
406             'make=s'        => \$context->{make_cmd},
407             'lzip!'         => \$context->{compressors}{lzip},
408             'verbose!'      => \$verbose,
409             'debug!'        => \$debug,
410             'help|?'        => \$help,
411             'man!'          => \$man,
412             'delete!'       => \$delete,
413             'cleanup!'      => \$cleanup,
414             'build-docs!'   => \$context->{build_docs},
415         )
416       )
417     {
418         pod2usage( -exitval => 1, -verbose => 0 );
419     }
420     pod2usage(0) if $help;
421     pod2usage( -verbose => 2 ) if $man;
422
423     get_and_check_version( shift, $context );
424     fix_paths_tar($context);
425     $context->{tag} = build_tag($context);
426     deal_with_working_directory( $context, $delete );
427     export_git_tree($context);
428     chdir( $context->{directory} ) || die;
429     unpack_tree($context);
430     make_version_script($context);
431     build_documentation($context) if $context->{build_docs};
432     build_package_directories($context);
433     create_tar_files($context);
434     do_cleanup($context) if ($cleanup);
435 }
436
437 1;
438
439 __END__
440
441 =head1 NAME
442
443 mk_exim_release.pl - Build an exim release
444
445 =head1 SYNOPSIS
446
447 mk_exim_release.pl [options] version
448
449  Options:
450    --debug             force debug mode
451    --verbose           force verbose mode
452    --help              display this help and exits
453    --man               displays man page
454    --tar=cmd           command to use for tar
455    --make=cmd          command to use for make
456    --directory=dir     dir to package
457    --no-lzip           do not create .tar.lz files
458    --delete            Delete packaging directory at start
459    --noweb             skip the website generation
460
461 =head1 OPTIONS
462
463 =over 4
464
465 =item B<--debug>
466
467 Forces debug mode.
468
469 =item B<--tar>
470
471 Use to override the path to the tar command; without this, will search for
472 gtar, and if not found use tar.  Need GNU tar for lzip, unless --no-lzip is
473 used.
474
475 =item B<--make>
476
477 Use to override the path/name of the make command.
478 Useful sometimes to force gmake.
479
480 =item B<--lzip>
481
482 Build the lzip tarballs.
483
484 =item B<--verbose>
485
486 Force verbose mode
487
488 =item B<--help>
489
490 Display help and exits
491
492 =item B<--man>
493
494 Display man page
495
496 =back
497
498 =head1 DESCRIPTION
499
500 Builds an exim release.
501
502 Starting in a populated git repo that has already been tagged for
503 release, build docs, build packages etc.
504
505 Parameter is the version number to build as - ie 4.72 4.72RC1, 4.86.1, etc
506
507 =head1 AUTHOR
508
509 Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>
510
511 =head1 COPYRIGHT
512
513 Copyright 2010 Exim Maintainers. All rights reserved.
514
515 =cut
516 # vim: set sw=4 et :