e3267fd3e80efb5f5ecf8e68252681a1dbac557e
[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 or 4.xx_RCx
26     unless ( $release =~ /^(4\.\d\d(?:_RC\d+)?)$/ ) {
27         croak "The given version number does not look right - $release";
28     }
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;
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
91     # run git command
92     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
93     system(@cmd) == 0 || croak "Export failed";
94 }
95
96 # ------------------------------------------------------------------
97
98 sub unpack_tree {
99     my $context = shift;
100
101     die "Cannot see archive file\n" unless ( -f $context->{tmp_archive_file} );
102     my @cmd = ( 'tar', 'xf', $context->{tmp_archive_file}, '-C', $context->{release_tree} );
103
104     # run  command
105     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
106     system(@cmd) == 0 || croak "Unpack failed";
107 }
108
109 # ------------------------------------------------------------------
110
111 sub make_version_script {
112     my $context = shift;
113
114     my $variant = substr( $context->{release}, length($context->{trelease}) );
115     if ( $context->{release} ne $context->{trelease} . $variant ) {
116         die "Broken version numbering, I'm buggy";
117     }
118
119     my $srcdir    = File::Spec->catdir( $context->{release_tree}, 'src', 'src' );
120     chdir $srcdir or die "chdir $srcdir: $\n";
121
122     if ( -f "version.sh" ) {
123         print( "WARNING: version.sh already exists - leaving it in place\n" );
124         return;
125     }
126
127     my @cmd = ("../scripts/reversion", "release");
128     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
129     system(@cmd) == 0 || croak "reversion failed";
130
131     unlink "version.h";
132
133     -f "version.sh" or die "failed to create version.h";
134 }
135
136 # ------------------------------------------------------------------
137
138 sub build_html_documentation {
139     my $context = shift;
140
141     my $genpath   = $context->{webgen_base} . '/script/gen.pl';
142     my $templates = $context->{webgen_base} . '/templates';
143     my $dir       = File::Spec->catdir( $context->{release_tree}, 'html' );
144     my $spec      = File::Spec->catfile( $context->{docbook}, 'spec.xml' );
145     my $filter    = File::Spec->catfile( $context->{docbook}, 'filter.xml' );
146
147     mkdir($dir);
148
149     my @cmd = (
150         $genpath,   '--spec',    $spec,                '--filter',
151         $filter,    '--latest',  $context->{trelease}, '--tmpl',
152         $templates, '--docroot', $dir,                 '--localstatic',
153         (($verbose||$debug) ? '--verbose' : '')
154     );
155
156     print "Executing ", join( ' ', @cmd ), "\n";
157     system(@cmd);
158
159     # move directory into right place
160     my $sourcedir = File::Spec->catdir( $context->{docbook}, 'filter.xml' );
161
162     rename(
163         File::Spec->catdir( $dir,                sprintf( 'exim-html-%s', $context->{trelease} ) ),
164         File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-html-%s', $context->{release} ) )
165     );
166 }
167
168 # ------------------------------------------------------------------
169
170 sub copy_docbook_files {
171     my $context = shift;
172
173     # where the generated docbook files can be found
174     my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
175
176     # where the website docbook source dir is - push files to here
177     my $webdir = File::Spec->catdir( $context->{webgen_base}, 'docbook', $context->{trelease} );
178     mkpath( $webdir, { verbose => ( $verbose || $debug ) } );
179
180     foreach my $file ( 'spec.xml', 'filter.xml' ) {
181         my $from  = File::Spec->catfile( $docdir,             $file );
182         my $to    = File::Spec->catfile( $context->{docbook}, $file );
183         my $webto = File::Spec->catfile( $webdir,             $file );
184         copy( $from, $to );
185         copy( $from, $webto );
186     }
187 }
188
189 # ------------------------------------------------------------------
190
191 sub build_documentation {
192     my $context = shift;
193
194     my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
195     # documentation building gets the truncated release, without RC
196     system("cd '$docdir' && ./OS-Fixups && make EXIM_VER=$context->{trelease} everything") == 0
197       || croak "Doc build failed";
198
199     copy_docbook_files($context);
200     build_html_documentation($context);
201 }
202
203 # ------------------------------------------------------------------
204
205 sub move_text_docs_into_pkg {
206     my $context = shift;
207
208     my $old_docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
209     my $old_txtdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-txt' );
210     my $new_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc' );
211     mkpath( $new_docdir, { verbose => ( $verbose || $debug ) } );
212
213     # move generated documents from docbook stuff
214     foreach my $file (qw/exim.8 spec.txt filter.txt/) {
215         move( File::Spec->catfile( $old_docdir, $file ), File::Spec->catfile( $new_docdir, $file ) );
216     }
217
218     # move text documents across
219     foreach my $file ( glob( File::Spec->catfile( $old_txtdir, '*' ) ) ) {
220
221         # skip a few we dont want
222         my $fn = ( File::Spec->splitpath($file) )[2];
223         next
224           if ( ( $fn eq 'ABOUT' )
225             || ( $fn eq 'ChangeLog.0' )
226             || ( $fn eq 'test-harness.txt' ) );
227         move( $file, File::Spec->catfile( $new_docdir, $fn ) );
228     }
229 }
230
231 # ------------------------------------------------------------------
232
233 sub build_pspdfinfo_directory {
234     my $context = shift;
235
236     ##foreach my $format (qw/pdf postscript texinfo info/) {
237     foreach my $format (qw/pdf postscript/) {
238         my $target = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s-%s', $format, $context->{release} ), 'doc' );
239         mkpath( $target, { verbose => ( $verbose || $debug ) } );
240
241         # move documents across
242         foreach my $file (
243             glob(
244                 File::Spec->catfile(
245                     $context->{release_tree},
246                     'doc',
247                     'doc-docbook',
248                     (
249                         ( $format eq 'postscript' )
250                         ? '*.ps'
251                         : ( '*.' . $format )
252                     )
253                 )
254             )
255           )
256         {
257             move( $file, File::Spec->catfile( $target, ( File::Spec->splitpath($file) )[2] ) );
258         }
259     }
260 }
261
262 # ------------------------------------------------------------------
263
264 sub build_main_package_directory {
265     my $context = shift;
266
267     # build the exim package directory path
268     $context->{eximpkgdir} = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s', $context->{release} ) );
269
270     # initially we move the exim-src directory to the new directory name
271     rename( File::Spec->catdir( $context->{release_tree}, 'src' ), $context->{eximpkgdir} )
272       || croak "Rename of src dir failed - $!";
273
274     # add Local subdirectory
275     mkpath( File::Spec->catdir( $context->{eximpkgdir}, 'Local' ), { verbose => ( $verbose || $debug ) } );
276
277     # now add the text docs
278     move_text_docs_into_pkg($context);
279 }
280
281 # ------------------------------------------------------------------
282
283 sub build_package_directories {
284     my $context = shift;
285
286     build_main_package_directory($context);
287     build_pspdfinfo_directory($context);
288 }
289
290 # ------------------------------------------------------------------
291
292 sub do_cleanup {
293     my $context = shift;
294
295     print "Cleaning up\n" if ($verbose);
296     rmtree( $context->{release_tree}, { verbose => $debug } );
297     rmtree( $context->{docbook},      { verbose => $debug } );
298     rmtree( $context->{pkgdirs},      { verbose => $debug } );
299 }
300
301 # ------------------------------------------------------------------
302
303 # We prefer gtar to tar if gtar exists in $PATH
304
305 sub fix_paths_tar {
306     my $context = shift;
307     my $tar = $context->{tar_cmd};
308
309     return unless $tar eq 'tar';
310
311     foreach my $d (File::Spec->path()) {
312         my $p = File::Spec->catfile($d, 'gtar');
313         if (-x $p) {
314             $context->{tar_cmd} = $p;
315             print "Switched tar command to: $p\n" if ($verbose);
316             return;
317         }
318     }
319 }
320
321 # ------------------------------------------------------------------
322
323 sub create_tar_files {
324     my $context = shift;
325
326     my $pkgs    = $context->{pkgs};
327     my $pkgdirs = $context->{pkgdirs};
328     my $tar     = $context->{tar_cmd};
329     if ($verbose) {
330         foreach my $c (keys %{ $context->{compressors} }) {
331             print "Compression: $c\t$context->{compressors}{$c}\n";
332         }
333     }
334
335     foreach my $dir ( glob( File::Spec->catdir( $pkgdirs, ( 'exim*-' . $context->{release} ) ) ) ) {
336         my $dirname = ( File::Spec->splitdir($dir) )[-1];
337         if ($context->{compressors}{gzip}) {
338             print "Creating: ${pkgs}/${dirname}.tar.gz\n" if ($verbose || $debug);
339             system("$tar cf  ${pkgs}/${dirname}.tar.gz  --gzip  -C ${pkgdirs} ${dirname}")
340         }
341         if ($context->{compressors}{bzip2}) {
342             print "Creating: ${pkgs}/${dirname}.tar.bz2\n" if ($verbose || $debug);
343             system("$tar cf  ${pkgs}/${dirname}.tar.bz2 --bzip2 -C ${pkgdirs} ${dirname}")
344         }
345         if ($context->{compressors}{lzip}) {
346             print "Creating: ${pkgs}/${dirname}.tar.lz\n" if ($verbose || $debug);
347             system("$tar cf  ${pkgs}/${dirname}.tar.lz  --lzip  -C ${pkgdirs} ${dirname}")
348         }
349     }
350 }
351
352 # ------------------------------------------------------------------
353 {
354     my $man;
355     my $help;
356     my $context = {
357         pkgname     => 'exim',
358         orig_dir    => File::Spec->curdir(),
359         tmp_dir     => File::Temp->newdir(),
360         webgen_base => "$FindBin::Bin/../../../exim-website",
361         tar_cmd     => 'tar',
362         compressors => {
363                 gzip    => 1,
364                 bzip2   => 1,
365                 lzip    => 0,
366         },
367     };
368     my $delete;
369     my $cleanup = 1;
370     ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'};
371
372     unless (
373         GetOptions(
374             'directory=s'   => \$context->{directory},
375             'webgen_base=s' => \$context->{webgen_base},
376             'tar'           => \$context->{tar_cmd},
377             'lzip!'         => \$context->{compressors}{lzip},
378             'verbose!'      => \$verbose,
379             'debug!'        => \$debug,
380             'help|?'        => \$help,
381             'man!'          => \$man,
382             'delete!'       => \$delete,
383             'cleanup!'      => \$cleanup,
384         )
385       )
386     {
387         pod2usage( -exitval => 1, -verbose => 0 );
388     }
389     pod2usage(0) if $help;
390     pod2usage( -verbose => 2 ) if $man;
391
392     get_and_check_version( shift, $context );
393     fix_paths_tar($context);
394     $context->{tag} = build_tag($context);
395     deal_with_working_directory( $context, $delete );
396     export_git_tree($context);
397     chdir( $context->{directory} ) || die;
398     unpack_tree($context);
399     make_version_script($context);
400     build_documentation($context);
401     build_package_directories($context);
402     create_tar_files($context);
403     do_cleanup($context) if ($cleanup);
404 }
405
406 1;
407
408 __END__
409
410 =head1 NAME
411
412 mk_exim_release.pl - Build an exim release
413
414 =head1 SYNOPSIS
415
416 mk_exim_release.pl [options] version
417
418  Options:
419    --debug             force debug mode (SQL Trace)
420    --verbose           force verbose mode
421    --help              display this help and exits
422    --man               displays man page
423    --tar=cmd           command to use for tar
424    --directory=dir     dir to package
425    --no-lzip           do not create .tar.lz files
426    --delete            Delete packaging directory at start
427
428 =head1 OPTIONS
429
430 =over 4
431
432 =item B<--debug>
433
434 Forces debug mode cause all SQL statements generated by L<DBIx::Class>
435 to be output.
436
437 =item B<--tar>
438
439 Use to override the path to the tar command; without this, will search for
440 gtar, and if not found use tar.  Need GNU tar for lzip, unless --no-lzip is
441 used.
442
443 =item B<--lzip>
444
445 Build the lzip tarballs.
446
447 =item B<--verbose>
448
449 Force verbose mode
450
451 =item B<--help>
452
453 Display help and exits
454
455 =item B<--man>
456
457 Display man page
458
459 =back
460
461 =head1 DESCRIPTION
462
463 Builds an exim release.
464
465 Starting in a populated git repo that has already been tagged for
466 release, build docs, build packages etc.
467
468 Parameter is the version number to build as - ie 4.72 4.72RC1 etc
469
470 =head1 AUTHOR
471
472 Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>
473
474 =head1 COPYRIGHT
475
476 Copyright 2010 Exim Maintainers. All rights reserved.
477
478 =cut
479 # vim: set sw=4 et :