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