Commit | Line | Data |
---|---|---|
46c573de | 1 | #!/usr/bin/env perl |
3634fc25 | 2 | |
8a483da6 NM |
3 | use strict; |
4 | use warnings; | |
5 | use Carp; | |
6 | use File::Copy; | |
7 | use File::Spec; | |
8 | use File::Path; | |
8f29c950 | 9 | use File::Temp; |
be914e6c | 10 | use FindBin; |
8a483da6 | 11 | use Getopt::Long; |
ba41f854 | 12 | use IO::File; |
8a483da6 NM |
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; | |
be914e6c | 22 | my $context = shift; |
8a483da6 NM |
23 | |
24 | # make sure this looks like a real release version | |
8f29c950 | 25 | # which should (currently) be 4.xx or 4.xx_RCx |
8a483da6 NM |
26 | unless ( $release =~ /^(4\.\d\d(?:_RC\d+)?)$/ ) { |
27 | croak "The given version number does not look right - $release"; | |
28 | } | |
be914e6c NM |
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; | |
8a483da6 NM |
35 | } |
36 | ||
37 | # ------------------------------------------------------------------ | |
38 | ||
8f29c950 | 39 | sub build_tag { |
8a483da6 NM |
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 | ||
46c573de | 71 | # create base directory |
8a483da6 | 72 | mkpath( $context->{directory}, { verbose => ( $verbose || $debug ) } ); |
46c573de NM |
73 | |
74 | # set and create subdirectories | |
75 | foreach (qw(release_tree pkgs pkgdirs docbook)) { | |
76 | $context->{$_} = File::Spec->catdir( $context->{directory}, $_ ); | |
77 | mkpath( $context->{$_}, { verbose => ( $verbose || $debug ) } ); | |
78 | } | |
8a483da6 NM |
79 | } |
80 | ||
81 | # ------------------------------------------------------------------ | |
82 | ||
8f29c950 | 83 | sub export_git_tree { |
8a483da6 NM |
84 | my $context = shift; |
85 | ||
8f29c950 NM |
86 | # build git command |
87 | my $archive_file = sprintf( '%s/%s-%s.tar', $context->{tmp_dir}, $context->{pkgname}, $context->{release} ); | |
88 | $context->{tmp_archive_file} = $archive_file; | |
89 | my @cmd = ( 'git', 'archive', '--format=tar', "--output=$archive_file", $context->{tag} ); | |
8a483da6 | 90 | |
8f29c950 | 91 | # run git command |
8a483da6 NM |
92 | print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose); |
93 | system(@cmd) == 0 || croak "Export failed"; | |
94 | } | |
95 | ||
96 | # ------------------------------------------------------------------ | |
97 | ||
8f29c950 NM |
98 | sub unpack_tree { |
99 | my $context = shift; | |
100 | ||
101 | die "Cannot see archive file\n" unless ( -f $context->{tmp_archive_file} ); | |
46c573de | 102 | my @cmd = ( 'tar', 'xf', $context->{tmp_archive_file}, '-C', $context->{release_tree} ); |
8f29c950 NM |
103 | |
104 | # run command | |
105 | print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose); | |
106 | system(@cmd) == 0 || croak "Unpack failed"; | |
107 | } | |
108 | ||
109 | # ------------------------------------------------------------------ | |
110 | ||
ba41f854 PP |
111 | sub adjust_version_extension { |
112 | my $context = shift; | |
113 | ||
114 | return if ($context->{release} eq $context->{trelease}); | |
115 | ||
116 | my $variant = substr( $context->{release}, length($context->{trelease}) ); | |
117 | if ( $context->{release} ne $context->{trelease} . $variant ) { | |
118 | die "Broken version numbering, I'm buggy"; | |
119 | } | |
120 | ||
121 | my $srcdir = File::Spec->catdir( $context->{release_tree}, 'src', 'src' ); | |
122 | my $version_h = File::Spec->catfile( $srcdir, 'version.h' ); | |
123 | ||
124 | my $fh = new IO::File $version_h, 'r'; | |
125 | die "Cannot read version.h: $!\n" unless ( defined $fh ); | |
126 | my @lines = <$fh>; | |
127 | $fh->close() or die "Failed to close-read($version_h): $!\n"; | |
128 | ||
129 | my $found = 0; | |
130 | my $i; | |
131 | for ( $i = 0 ; $i < @lines ; ++$i ) { | |
132 | if ( $lines[$i] =~ /EXIM_VARIANT_VERSION/ ) { | |
133 | $found = 1; | |
134 | last; | |
135 | } | |
136 | } | |
137 | die "Cannot find version.h EXIM_VARIANT_VERSION\n" unless $found; | |
138 | unless ( $lines[$i] =~ m/^\s* \# \s* define \s+ EXIM_VARIANT_VERSION \s+ "(.*)" \s* $/x ) { | |
139 | die "Broken version.h EXIM_VARIANT_VERSION line\n"; | |
140 | } | |
141 | if ( length $1 ) { | |
142 | print( "WARNING: version.h has a variant tag already defined: $1\n" ); | |
143 | print( " not changing that tag\n" ); | |
144 | return; | |
145 | } | |
146 | ||
147 | $lines[$i] = qq{#define EXIM_VARIANT_VERSION\t\t"$variant"\n}; | |
148 | # deliberately not verbose constrained: | |
149 | print( "Adjusting version.h for $variant release.\n" ); | |
150 | ||
151 | $fh = new IO::File $version_h, "w"; | |
152 | die "Cannot write version.h: $!\n" unless ( defined $fh ); | |
153 | $fh->print( @lines ); | |
154 | $fh->close() or die "Failed to close-write($version_h): $!\n"; | |
155 | } | |
156 | ||
157 | # ------------------------------------------------------------------ | |
158 | ||
be914e6c NM |
159 | sub build_html_documentation { |
160 | my $context = shift; | |
161 | ||
162 | my $genpath = $context->{webgen_base} . '/script/gen.pl'; | |
163 | my $templates = $context->{webgen_base} . '/templates'; | |
46c573de NM |
164 | my $dir = File::Spec->catdir( $context->{release_tree}, 'html' ); |
165 | my $spec = File::Spec->catfile( $context->{docbook}, 'spec.xml' ); | |
166 | my $filter = File::Spec->catfile( $context->{docbook}, 'filter.xml' ); | |
167 | ||
be914e6c NM |
168 | mkdir($dir); |
169 | ||
46c573de NM |
170 | my @cmd = |
171 | ( $genpath, '--spec', $spec, '--filter', $filter, '--latest', $context->{trelease}, '--tmpl', $templates, '--docroot', $dir ); | |
be914e6c NM |
172 | |
173 | print "Executing ", join( ' ', @cmd ), "\n"; | |
174 | system(@cmd); | |
175 | ||
176 | # move directory into right place | |
46c573de NM |
177 | my $sourcedir = File::Spec->catdir( $context->{docbook}, 'filter.xml' ); |
178 | ||
179 | rename( | |
180 | File::Spec->catdir( $dir, sprintf( 'exim-html-%s', $context->{trelease} ) ), | |
181 | File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-html-%s', $context->{release} ) ) | |
182 | ); | |
183 | } | |
184 | ||
185 | # ------------------------------------------------------------------ | |
186 | ||
187 | sub copy_docbook_files { | |
188 | my $context = shift; | |
189 | ||
190 | # where the generated docbook files can be found | |
191 | my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' ); | |
192 | ||
193 | # where the website docbook source dir is - push files to here | |
194 | my $webdir = File::Spec->catdir( $context->{webgen_base}, 'docbook', $context->{trelease} ); | |
195 | mkpath( $webdir, { verbose => ( $verbose || $debug ) } ); | |
196 | ||
197 | foreach my $file ( 'spec.xml', 'filter.xml' ) { | |
198 | my $from = File::Spec->catfile( $docdir, $file ); | |
199 | my $to = File::Spec->catfile( $context->{docbook}, $file ); | |
200 | my $webto = File::Spec->catfile( $webdir, $file ); | |
201 | copy( $from, $to ); | |
202 | copy( $from, $webto ); | |
203 | } | |
be914e6c NM |
204 | } |
205 | ||
206 | # ------------------------------------------------------------------ | |
207 | ||
8a483da6 | 208 | sub build_documentation { |
be914e6c NM |
209 | my $context = shift; |
210 | ||
46c573de NM |
211 | my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' ); |
212 | system("cd '$docdir' && ./OS-Fixups && make everything") == 0 | |
8a483da6 | 213 | || croak "Doc build failed"; |
be914e6c | 214 | |
46c573de | 215 | copy_docbook_files($context); |
be914e6c | 216 | build_html_documentation($context); |
8a483da6 NM |
217 | } |
218 | ||
219 | # ------------------------------------------------------------------ | |
220 | ||
221 | sub move_text_docs_into_pkg { | |
222 | my $context = shift; | |
223 | ||
821bc55f PP |
224 | my $old_docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' ); |
225 | my $old_txtdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-txt' ); | |
46c573de | 226 | my $new_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc' ); |
8a483da6 NM |
227 | mkpath( $new_docdir, { verbose => ( $verbose || $debug ) } ); |
228 | ||
229 | # move generated documents from docbook stuff | |
230 | foreach my $file (qw/exim.8 spec.txt filter.txt/) { | |
231 | move( File::Spec->catfile( $old_docdir, $file ), File::Spec->catfile( $new_docdir, $file ) ); | |
232 | } | |
233 | ||
234 | # move text documents across | |
821bc55f | 235 | foreach my $file ( glob( File::Spec->catfile( $old_txtdir, '*' ) ) ) { |
8a483da6 NM |
236 | |
237 | # skip a few we dont want | |
238 | my $fn = ( File::Spec->splitpath($file) )[2]; | |
239 | next | |
240 | if ( ( $fn eq 'ABOUT' ) | |
241 | || ( $fn eq 'ChangeLog.0' ) | |
242 | || ( $fn eq 'test-harness.txt' ) ); | |
243 | move( $file, File::Spec->catfile( $new_docdir, $fn ) ); | |
244 | } | |
245 | } | |
246 | ||
247 | # ------------------------------------------------------------------ | |
248 | ||
249 | sub build_pspdfinfo_directory { | |
250 | my $context = shift; | |
251 | ||
252 | ##foreach my $format (qw/pdf postscript texinfo info/) { | |
253 | foreach my $format (qw/pdf postscript/) { | |
46c573de | 254 | my $target = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s-%s', $format, $context->{release} ), 'doc' ); |
8a483da6 NM |
255 | mkpath( $target, { verbose => ( $verbose || $debug ) } ); |
256 | ||
257 | # move documents across | |
258 | foreach my $file ( | |
259 | glob( | |
260 | File::Spec->catfile( | |
46c573de NM |
261 | $context->{release_tree}, |
262 | 'doc', | |
263 | 'doc-docbook', | |
8a483da6 NM |
264 | ( |
265 | ( $format eq 'postscript' ) | |
266 | ? '*.ps' | |
267 | : ( '*.' . $format ) | |
268 | ) | |
269 | ) | |
270 | ) | |
271 | ) | |
272 | { | |
46c573de | 273 | move( $file, File::Spec->catfile( $target, ( File::Spec->splitpath($file) )[2] ) ); |
8a483da6 NM |
274 | } |
275 | } | |
276 | } | |
277 | ||
278 | # ------------------------------------------------------------------ | |
279 | ||
8a483da6 NM |
280 | sub build_main_package_directory { |
281 | my $context = shift; | |
282 | ||
46c573de NM |
283 | # build the exim package directory path |
284 | $context->{eximpkgdir} = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s', $context->{release} ) ); | |
285 | ||
8a483da6 | 286 | # initially we move the exim-src directory to the new directory name |
46c573de NM |
287 | rename( File::Spec->catdir( $context->{release_tree}, 'src' ), $context->{eximpkgdir} ) |
288 | || croak "Rename of src dir failed - $!"; | |
8a483da6 NM |
289 | |
290 | # add Local subdirectory | |
46c573de | 291 | mkpath( File::Spec->catdir( $context->{eximpkgdir}, 'Local' ), { verbose => ( $verbose || $debug ) } ); |
8a483da6 NM |
292 | |
293 | # now add the text docs | |
294 | move_text_docs_into_pkg($context); | |
295 | } | |
296 | ||
297 | # ------------------------------------------------------------------ | |
298 | ||
299 | sub build_package_directories { | |
300 | my $context = shift; | |
301 | ||
302 | build_main_package_directory($context); | |
303 | build_pspdfinfo_directory($context); | |
8a483da6 NM |
304 | } |
305 | ||
306 | # ------------------------------------------------------------------ | |
307 | ||
46c573de NM |
308 | sub do_cleanup { |
309 | my $context = shift; | |
310 | ||
311 | print "Cleaning up\n" if ($verbose); | |
312 | rmtree( $context->{release_tree}, { verbose => $debug } ); | |
313 | rmtree( $context->{docbook}, { verbose => $debug } ); | |
314 | rmtree( $context->{pkgdirs}, { verbose => $debug } ); | |
315 | } | |
316 | ||
317 | # ------------------------------------------------------------------ | |
318 | ||
8a483da6 NM |
319 | sub create_tar_files { |
320 | my $context = shift; | |
321 | ||
46c573de NM |
322 | my $pkgs = $context->{pkgs}; |
323 | my $pkgdirs = $context->{pkgdirs}; | |
324 | foreach my $dir ( glob( File::Spec->catdir( $pkgdirs, ( 'exim*-' . $context->{release} ) ) ) ) { | |
325 | my $dirname = ( File::Spec->splitdir($dir) )[-1]; | |
326 | system("tar cfz ${pkgs}/${dirname}.tar.gz -C ${pkgdirs} ${dirname}"); | |
327 | system("tar cfj ${pkgs}/${dirname}.tar.bz2 -C ${pkgdirs} ${dirname}"); | |
8a483da6 NM |
328 | } |
329 | } | |
330 | ||
331 | # ------------------------------------------------------------------ | |
332 | { | |
333 | my $man; | |
334 | my $help; | |
335 | my $context = { | |
be914e6c NM |
336 | pkgname => 'exim', |
337 | orig_dir => File::Spec->curdir(), | |
338 | tmp_dir => File::Temp->newdir(), | |
339 | webgen_base => "$FindBin::Bin/../../../exim-website", | |
8a483da6 NM |
340 | }; |
341 | my $delete; | |
46c573de | 342 | my $cleanup = 1; |
8f29c950 | 343 | ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'}; |
8a483da6 NM |
344 | |
345 | unless ( | |
346 | GetOptions( | |
be914e6c NM |
347 | 'directory=s' => \$context->{directory}, |
348 | 'webgen_base=s' => \$context->{webgen_base}, | |
349 | 'verbose!' => \$verbose, | |
350 | 'debug!' => \$debug, | |
351 | 'help|?' => \$help, | |
352 | 'man!' => \$man, | |
353 | 'delete!' => \$delete, | |
46c573de | 354 | 'cleanup!' => \$cleanup, |
8a483da6 NM |
355 | ) |
356 | ) | |
357 | { | |
358 | pod2usage( -exitval => 1, -verbose => 0 ); | |
359 | } | |
360 | pod2usage(0) if $help; | |
361 | pod2usage( -verbose => 2 ) if $man; | |
362 | ||
be914e6c NM |
363 | get_and_check_version( shift, $context ); |
364 | $context->{tag} = build_tag($context); | |
8a483da6 | 365 | deal_with_working_directory( $context, $delete ); |
8f29c950 | 366 | export_git_tree($context); |
8a483da6 | 367 | chdir( $context->{directory} ) || die; |
8f29c950 | 368 | unpack_tree($context); |
ba41f854 | 369 | adjust_version_extension($context); |
8a483da6 NM |
370 | build_documentation($context); |
371 | build_package_directories($context); | |
372 | create_tar_files($context); | |
46c573de | 373 | do_cleanup($context) if ($cleanup); |
8a483da6 NM |
374 | } |
375 | ||
376 | 1; | |
377 | ||
378 | __END__ | |
379 | ||
380 | =head1 NAME | |
381 | ||
382 | mk_exim_release.pl - Build an exim release | |
383 | ||
384 | =head1 SYNOPSIS | |
385 | ||
386 | mk_exim_release.pl [options] version | |
387 | ||
388 | Options: | |
389 | --debug force debug mode (SQL Trace) | |
390 | --verbose force verbose mode | |
391 | --help display this help and exits | |
392 | --man displays man page | |
393 | --directory=dir dir to package | |
8a483da6 NM |
394 | --delete Delete packaging directory at start |
395 | ||
396 | =head1 OPTIONS | |
397 | ||
398 | =over 4 | |
399 | ||
400 | =item B<--debug> | |
401 | ||
402 | Forces debug mode cause all SQL statements generated by L<DBIx::Class> | |
403 | to be output. | |
404 | ||
405 | =item B<--verbose> | |
406 | ||
407 | Force verbose mode - currently this has no effect | |
408 | ||
409 | =item B<--help> | |
410 | ||
411 | Display help and exits | |
412 | ||
413 | =item B<--man> | |
414 | ||
415 | Display man page | |
416 | ||
417 | =back | |
418 | ||
419 | =head1 DESCRIPTION | |
420 | ||
421 | Builds an exim release. | |
422 | ||
8f29c950 NM |
423 | Starting in a populated git repo that has already been tagged for |
424 | release, build docs, build packages etc. | |
8a483da6 NM |
425 | |
426 | Parameter is the version number to build as - ie 4.72 4.72RC1 etc | |
427 | ||
428 | =head1 AUTHOR | |
429 | ||
430 | Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk> | |
431 | ||
432 | =head1 COPYRIGHT | |
433 | ||
8f29c950 | 434 | Copyright 2010 Exim Maintainers. All rights reserved. |
8a483da6 NM |
435 | |
436 | =cut |