2 # Copyright (c) The Exim Maintainers 2016-2018
9 use File::Path qw(make_path remove_tree);
16 use if $ENV{DEBUG} => 'Smart::Comments';
24 # MAJOR.MINOR[.SECURITY[.FIXES]][-RCX]
26 my $version_pattern = qr/
30 \.(?<minor>\d\d) # .90
31 (?:\.(?<security>\d+) # .0
32 (?:\.(?<fixes>)\d+)?)? # .22
33 ) # target-release ->|
34 (?:-(?<rc>RC\d+)?)? # -RC1
38 my $quick_version_pattern = qr/
42 \.(?<minor>\d\d) # .90
43 (?:\.(?<security>\d+) # .0
44 (?:\.(?<fixes>)\d+)?)? # .22
46 (?:-(?<quick>\d+-g[[:xdigit:]]+))? # -3-gdeadbef
50 # ------------------------------------------------------------------
53 use strict; # not strictly necessary yet, until in an own package
54 use warnings; # not strictly necessary yet, ...
55 use File::Spec::Functions qw'splitpath catfile catdir splitdir';
56 use File::Path qw'make_path remove_tree';
63 sub TIESCALAR { bless do {\my $x} }
67 tie my $cwd => 'PWD' or die; # this returns the current dir now, dynamically
71 return bless { @_ } => $class;
76 my $version = shift // 'HEAD';
80 # release => 4.92-RC4 | 4.92-27-gabcdef
81 # target_release|last_tag => 4.92 | 4.92
88 # rc|quick => RC4 | 27-gabcdef
93 # release => 4.92-RC4 | 4.92-27-gabcdef-dirty
94 # target_release|last_tag => 4.92 | 4.92
101 # rc|quick => RC4 | 27-gabcdef-dirty
104 if ($context->{quick}) {
105 # Try to find suitable version description
106 chomp(my $describe = do { # we wrap it into a open() to avoid hassle with
107 open(my $fh, '-|', # strange version descriptions
108 'git', describe => $version) or die;
111 $describe =~ /$quick_version_pattern/;
113 %{$context->{v}} = %+;
114 ($context->{commit}) = $version // ($context->{v}{quick} =~ /g([[:xdigit:]]+)/);
117 croak "The given version number does not look right - $version"
118 if not $version =~ /$version_pattern/;
119 %{$context->{v}} = %+;
121 # find a valid vcs tag matching the version
122 my $pattern = "$context->{pkgname}-$context->{v}{release}" =~ s/[-_.]/[-_.]/gr;
123 chomp(my @tags = qx{git tag --list '$pattern'});
125 croak "The given version is ambigous, tags: @tags\n" if @tags > 1;
126 croak "The given version does not exist (no such tag: exim-$version)\n" if @tags == 0;
128 $context->{commit} = $tags[0];
129 # target_release: the release we aim to reach with release candidates
130 # FIXME: re-construct from the parsed version number
133 die "$ME: This script doesn't work for versions prior 4.92-RCx. "
134 ."Please checkout an older version.\n"
135 if $context->{v}{major} < 4
136 or $context->{v}{major} == 4 && $context->{v}{minor} < 92;
143 # We prefer gtar to tar if gtar exists in $PATH
144 sub override_tar_cmd {
146 my $tar = $context->{tar_cmd};
148 return unless $tar eq 'tar';
150 foreach my $d (File::Spec->path()) {
151 my $p = catfile($d, 'gtar');
153 $context->{tar_cmd} = $p;
154 print "Switched tar command to: $p\n" if $verbose;
160 sub prepare_working_directory {
162 my $workspace = $context->{workspace};
164 if (not defined $workspace) {
165 $workspace = $context->{workspace} = File::Temp->newdir(File::Spec->tmpdir . '/exim-packaging-XXXX');
168 # ensure the working directory is not in place
170 if ($context->{delete}) {
171 print "Deleting existing $workspace\n" if $verbose;
172 remove_tree $workspace, { verbose => $verbose || $debug };
175 croak "Working directory $workspace exists" if -e $workspace;
179 # create base directory
180 make_path( $context->{directory}, { verbose => $verbose || $debug } );
183 # Set(!) and create subdirectories
184 foreach (qw(vcs_export pkg_tars pkg_trees tmp)) { # {dookbook}
186 $context->{d}{$_} = catdir($workspace, $_),
187 { verbose => $verbose || $debug });
191 sub export_git_tree {
195 my $archive_file = $context->{tmp_archive_file} = sprintf'%s/%s-%s.tar', $context->{d}{tmp}, $context->{pkgname}, $context->{v}{release};
197 my @cmd = ( 'git', 'archive', '--format=tar', "--output=$archive_file", $context->{commit} );
200 print "[$cwd] Running: @cmd\n" if $verbose;
201 0 == system @cmd or croak "Export failed";
205 # TODO: Why can't we combine the export_git_tree with the
206 # unpack_tree function?
210 die "Cannot see archive file\n" unless -f $context->{tmp_archive_file};
212 xf => $context->{tmp_archive_file},
213 -C => $context->{d}{vcs_export} );
216 print "[$cwd] Running: @cmd\n" if $verbose;
217 system @cmd and croak "Unpack failed\n";
221 sub make_version_script {
224 #my $variant = substr( $context->{v}{release}, length($context->{v}{target_release}) );
225 #if ( $context->{v}{release} ne $context->{v}{target_release} . $variant ) {
226 # die "Broken version numbering, I'm buggy";
231 if (not my $pid = fork // die "$ME: Cannot fork: $!\n") {
233 my $source_tree = catdir($context->{d}{vcs_export}, 'src', 'src');
236 chdir $source_tree or die "chdir $source_tree: $!\n";
238 croak "WARNING: version.sh already exists - leaving it in place\n"
241 # Currently (25. Feb. 2016) the mk_exim_release.pl up to now can't
242 # deal with security releases.!? So we need a current
243 # mk_exim_release.pl. But if we use a current (master), the
244 # reversion script returns wrong version info (it's running inside
245 # the Git tree and uses git --describe, which always returns the
246 # current version of master.) I do not want to change the old
247 # reversion scripts (in 4.86.1, 4.85.1).
249 # Thus we've to provide the version.sh, based on the info we have
250 # about the release. If reversion finds this, it doesn't try to find
251 # it's own way to get a valid version number from the git.
253 # 4.89 series: the logic here did not handle _RC<N> thus breaking RC
254 # status in versions. nb: rc in context should be same as $variant
257 #my $stamp = $context->{minor} ? '_'.$context->{minor} : '';
258 #$stamp .= $context->{rc} if $context->{rc};
259 my $release = $context->{v}{rc} ? $context->{v}{target_release}
260 : $context->{v}{last_tag};
263 $context->{v}{rc} ? $context->{v}{rc}
264 : $context->{v}{quick} ? $context->{v}{quick}
267 print "[$cwd] create version.sh\n" if $verbose;
268 open(my $v, '>', 'version.sh') or die "Can't open version.sh for writing: $!\n";
270 # initial version automatically generated by $0
271 EXIM_RELEASE_VERSION=$release
272 EXIM_VARIANT_VERSION=$variant
273 EXIM_COMPILE_NUMBER=0
274 # echo "[[[ \$EXIM_RELEASE_VERSION | \$EXIM_VARIANT_VERSION | \$EXIM_COMPILE_NUMBER ]]]"
276 close $v or die "$0: Can not close $source_tree/version.h: $!\n";
277 unlink 'version.h' or die "$ME: Can not unlink $source_tree/version.h: $!\n"
280 # Later, if we get the reversion script fixed, we can call it again.
281 # For now (25. Feb. 2016) we'll leave it unused.
282 #my @cmd = ('../scripts/reversion', 'release', $context->{commit});
284 my @cmd = ('../scripts/reversion', 'release');
285 print "[$cwd] Running: @cmd\n" if $verbose;
286 system(@cmd) and croak "reversion failed";
288 die "$ME: failed to create version.sh"
289 unless -f 'version.sh';
294 $pid == waitpid($pid, 0) or die "$0: waidpid: $!\n";
299 sub build_documentation {
301 my $docdir = catdir $context->{d}{vcs_export}, 'doc', 'doc-docbook';
303 # documentation building does a chdir, so we'll do it in a
305 if (not my $pid = fork // die "$ME: Can't fork: $!\n") {
306 chdir $docdir or die "$ME: Can't chdir to $docdir: $!\n";
307 system('./OS-Fixups') == 0 or exit $?;
308 exec $context->{make_cmd},
309 "EXIM_VER=$context->{v}{release}", 'everything'
310 or die "$ME: [$cwd] Cannot exec $context->{make_cmd}: $!\n";
317 $context->copy_docbook_files;
320 sub copy_docbook_files {
323 # where the generated docbook files can be found
324 my $docdir = catdir $context->{d}{vcs_export}, 'doc', 'doc-docbook';
326 foreach ('spec.xml', 'filter.xml') {
327 my $from = catfile $docdir, $_;
328 my $to = catdir $context->{d}{tmp}; # {dookbook}
329 copy $from => $to or die $@;
333 sub build_html_documentation {
336 # where the website docbook source dir is - push the generated
339 my $webdir = catdir $context->{website_base}, 'docbook', $context->{v}{target_release};
340 make_path $webdir, { verbose => $verbose || $debug };
341 copy catfile($context->{d}{vcs_export}, 'doc', 'doc-docbook', $_)
343 for 'spec.xml', 'filter.xml';
346 my $gen = catfile $context->{website_base}, 'script/gen';
347 my $outdir = catdir $context->{d}{pkg_trees}, "exim-html-$context->{v}{release}";
349 make_path $outdir, { verbose => $verbose || $debug };
353 '--spec' => catfile($context->{d}{tmp}, 'spec.xml'), # {dookbook}
354 '--filter' => catfile($context->{d}{tmp}, 'filter.xml'), # {dookbok}
355 '--latest' => $context->{v}{target_release},
356 '--docroot' => $outdir,
358 ($verbose || $debug ? '--verbose' : ()),
361 print "[$cwd] Executing @cmd\n";
362 0 == system @cmd or exit $? >> 8;
368 foreach my $tar (glob "$context->{d}{pkg_tars}/*") {
370 '--quiet', '--batch',
371 defined $context->{gpg}{key}
372 ? ('--local-user' => $context->{gpg}{key})
374 '--detach-sig', '--armor', $tar;
380 make_path $context->{OUTDIR}, { verbose => $verbose || $debug };
381 move $_ => $context->{OUTDIR} or die $@
382 for glob "$context->{d}{pkg_tars}/*";
385 sub build_src_package_directory {
388 # build the exim package directory path
389 $context->{d}{src} = catdir $context->{d}{pkg_trees}, "exim-$context->{v}{release}";
391 # initially we move the exim-src directory to the new directory name
393 catdir( $context->{d}{vcs_export}, 'src')
394 => $context->{d}{src}
395 or croak "Move of src dir failed - $!";
397 # add Local subdirectory
398 make_path( catdir( $context->{d}{src}, 'Local' ), { verbose => $verbose || $debug } );
400 # now add the text docs
401 $context->move_text_docs_into_pkg;
404 sub build_doc_packages_directory {
407 ##foreach my $format (qw/pdf postscript texinfo info/) {
408 foreach my $format (qw/pdf postscript/) {
409 my $target = catdir $context->{d}{pkg_trees}, "exim-$format-$context->{v}{release}", 'doc';
410 make_path( $target, { verbose => $verbose || $debug } );
412 # move documents across
416 $context->{d}{vcs_export},
420 ( $format eq 'postscript' )
428 move( $file, catfile( $target, ( splitpath($file) )[2] ) );
433 sub move_text_docs_into_pkg {
436 my $old_docdir = catdir( $context->{d}{vcs_export}, 'doc', 'doc-docbook' );
437 my $old_txtdir = catdir( $context->{d}{vcs_export}, 'doc', 'doc-txt' );
438 my $new_docdir = catdir( $context->{d}{src}, 'doc' );
439 make_path( $new_docdir, { verbose => $verbose || $debug } );
441 # move generated documents from docbook stuff
442 foreach my $file (qw/exim.8 spec.txt filter.txt/) {
443 die "Empty file \"$file\"\n" if -z catfile( $old_docdir, $file );
444 move( catfile( $old_docdir, $file ), catfile( $new_docdir, $file ) );
447 # move text documents across
448 foreach my $file ( glob( catfile( $old_txtdir, '*' ) ) ) {
450 # skip a few we dont want
451 my $fn = ( splitpath($file) )[2];
453 if ( ( $fn eq 'ABOUT' )
454 || ( $fn eq 'ChangeLog.0' )
455 || ( $fn eq 'test-harness.txt' )
456 # Debian issue re licensing of RFCs
457 || ( $fn =~ /^draft-ietf-.*/ )
458 || ( $fn =~ /^rfc.*/ )
460 move( $file, catfile( $new_docdir, $fn ) );
464 sub create_tar_files {
467 my $pkg_tars = $context->{d}{pkg_tars};
468 my $pkg_trees = $context->{d}{pkg_trees};
469 my $tar = $context->{tar_cmd};
471 foreach my $c (keys %{ $context->{compressors} }) {
472 print "Compression: $c\t$context->{compressors}{$c}\n";
476 # We ideally do not want local system user information in release tarballs;
477 # those are artifacts of use of tar for backups and have no place in
478 # software release packaging; if someone extracts as root, then they should
479 # get sane file ownerships.
481 '--owner' => $context->{tar_perms}{user},
482 '--group' => $context->{tar_perms}{group},
483 # on this GNU tar, --numeric-owner works during creation too
485 ) if qx/tar --help 2>&1/ =~ /^\s*--owner=/m;
487 # See also environment variables set in main, tuning compression levels
491 foreach my $dir ( glob( catdir( $pkg_trees, ( 'exim*-' . $context->{v}{release} ) ) ) ) {
492 my $dirname = ( splitdir($dir) )[-1];
493 foreach my $comp (keys %{$context->{compressors}}) {
494 my %compressor = %{$context->{compressors}{$comp}};
495 next unless $compressor{use};
497 my $basename = "$dirname.tar.$compressor{extension}";
498 my $outfile = catfile $pkg_tars, $basename;
500 print "Creating: $outfile\n" if $verbose || $debug;
504 @ownership, -C => $pkg_trees, $dirname)
507 # calculate size and md5sum
508 $size{$basename} = -s $outfile;
509 $sha256{$basename} = do {
510 my $sha = Digest::SHA->new(256);
511 $sha->addfile($outfile);
517 # write the sizes file
518 if ($context->{sizes}) {
519 open my $sizes, '>', $_ = catfile $pkg_tars, 'sizes.txt'
520 or die "$ME: Can't open `$_': $!\n";
522 print $sizes join "\n",
523 (map { "SIZE($_) = $size{$_}" } sort keys %size),
524 (map { "SHA256($_) = $sha256{$_}" } sort keys %sha256);
526 close($sizes) or die "$ME: Can't close $_: $!\n";
533 print "Cleaning up\n" if $verbose;
534 remove_tree $context->{d}{tmp}, { verbose => $verbose || $debug };
539 # Check, if tar understands --use-compress-program and use this, as
540 # at least gzip deprecated passing options via the environment.
542 my ($compressor, $fallback) = @_;
543 state $use_compress_option =
544 0 == system("tar c -f /dev/null -C / --use-compress-program=cat dev/null 2>/dev/null");
545 return $use_compress_option
546 ? "--use-compress-program=$compressor"
547 : ref $fallback eq ref sub {} ? $fallback->() : $fallback;
552 # some of these settings are useful only if we're in the
553 # exim-projekt-root, but the check, if we're, is deferred
554 my $context = Context->new(
556 website_base => abs_path('../exim-website'),
562 make_cmd => 'make', # for 'make'ing the docs
565 gzip => { use => 1, extension => 'gz', flags => compressor('gzip -9', sub { $ENV{GZIP} = '-9'; '--gzip' }) },
566 bzip2 => { use => 1, extension => 'bz2', flags => compressor('bzip2 -9', sub { $ENV{BZIP2} = '-9'; '--bzip2' }) },
567 xz => { use => 1, extension => 'xz', flags => compressor('xz -9', sub { $ENV{XZ_OPT} = '-9'; '--xz' }) },
568 lzip => { use => 0, extension => 'lz', flags => compressor('lzip -9', '--lzip') },
581 ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'};
585 qw(workspace|tmp=s website_base|webgen_base=s tar_cmd|tar-cmd=s make_cmd|make-cmd=s
586 docs|build-docs! web|build-web! sizes!
587 delete! cleanup! quick|quick-release! minimal),
588 'sign!' => \$context->{gpg}{sign},
589 'key=s' => \$context->{gpg}{key},
590 'verbose!' => \$verbose,
591 'compressors=s@' => sub {
592 die "$0: can't parse compressors string `$_[1]'\n" unless $_[1] =~ /^[+=-]?\w+(?:[+=-]\w+)*$/;
593 while ($_[1] =~ /(?<act>[+=-])?(?<name>\w+)\b/g) {
594 die "$0: Unknown compressor $+{name}"
595 unless $context->{compressors}{$+{name}};
596 if (not defined $+{act} or $+{act} eq '=') {
598 for values %{$context->{compressors}};
599 $context->{compressors}{$+{name}}{use}++;
601 elsif ($+{act} eq '+') { $context->{compressors}{$+{name}}{use}++; }
602 elsif ($+{act} eq '-') { $context->{compressors}{$+{name}}{use}--; }
605 'debug:s' => \$debug,
606 'quick' => sub { $context->{web}--; $context->{quick} = 1 },
607 'help|?' => sub { pod2usage(-verbose => 1, -exit => 0) },
608 'man!' => sub { pod2usage(-verbose => 2, -exit => 0, -noperldoc => system('perldoc -V >/dev/null 2>&1')) },
609 ) and (@ARGV == 2 or ($context->{quick} and @ARGV >= 1))
612 -f '.exim-project-root'
613 or die "$ME: please call this script from the root of the Exim project sources\n";
615 $context->{OUTDIR} = pop @ARGV;
617 if ($context->{gpg}{sign}) {
618 $context->{gpg}{key} //= do { chomp($_ = qx/git config user.signingkey/); $_ }
621 warn "$ME: No GPG key, using default\n";
627 warn "$ME: changed umask to 022\n" if umask(022) != 022;
629 $context->check_version(shift); # may be undef for a quick release
631 if ($debug//'' eq 'version') {
632 for (sort keys %{$context->{v}}) {
633 print "version $_: $context->{v}{$_}\n";
635 print "git commit: $context->{commit}\n";
638 $context->override_tar_cmd;
639 $context->prepare_working_directory;
640 $context->export_git_tree;
641 $context->unpack_tree;
642 $context->make_version_script;
644 $context->build_documentation if $context->{docs};
645 $context->build_html_documentation if $context->{docs} && $context->{web};
647 $context->build_src_package_directory;
648 $context->build_doc_packages_directory if $context->{docs};
650 $context->create_tar_files;
651 $context->sign if $context->{gpg}{sign};
652 $context->move_to_outdir;
653 $context->do_cleanup if $context->{cleanup};
664 mk_exim_release - Build an exim release
668 mk_exim_release [options] version PKG-DIRECTORY
669 mk_exim_release [options] --quick [version] PKG-DIRECTORY
673 B<mk_exim_release> builds an exim release.
675 Starting in a populated git repo that has already been tagged for
676 release it builds docs, packages etc. Parameter is the version number
677 to build as - ie 4.72 4.72-RC1, 4.86.1, etc, without any prefix.
679 This scripts expects to find a tag "exim-<version>".
681 After creating the release files, they should be signed. There is another
682 helper for creating the signatures:
683 F<release-process/scripts/sign_exim_packages>.
685 Call B<mk_exim_release> about like this:
687 release-process/scripts/mk_exim_release 4.99 OUT-DIR
694 =item B<--[no]cleanup>
696 Do (or do not) cleanup the tmp directory at exit (default: do cleanup)
698 =item B<--compressors> [I<action>]I<compressor[I<action>$<compressor>]...
700 A list of compressors to use. Currently the default list is
701 B<gzip>, B<xz>, and B<bzip2>, with B<lzip> optionally to be enabled.
703 I<action> can be "+" (add), "-" (remove), and "=" (set).
705 =item B<--debug[=I<item>]>
707 Forces debug mode. If (default: no debug info)
711 =item item: B<version>
713 Output the parsed/found version number and exit.
717 =item B<--[no]delete>
719 Delete a pre-existing tmp- and package-directory at start. (default: don't delete)
723 Do (not) build the documentation. This needs C<gnu-make> (default: build the docs)
727 Display short help and exit cleanly. (default: don't do that)
729 =item B<--key> I<GPG key>
731 Use this GPG key for signing. If nothing is specified the first one of this list
736 =item - git config user.signingkey
738 =item - environment C<EXIM_KEY>
740 =item - default GPG key
744 =item B<--make-cmd> I<cmd>
746 Force the use of a specific C<make> command. This may be necessary if C<make> is not
747 C<gmake>. This is necessary to build the docs. (default: C<make>)
751 Display man page and exit cleanly. (default: don't do that)
755 Create a quick release. The I<version> mandatory argument needs to be a git commit-ish.
756 (try I<master> or I<HEAD> or similar). This mode switches off the
757 website creation (which can be enabled by B<--web> again).
761 Sign the created archive files (and the sizes.txt). (default: sign)
765 Write the sizes information to F<sizes.txt>. (default: write sizes)
767 =item B<--tar-cmd> I<cmd>
769 Use to override the path to the C<tar> command. Need GNU tar in case
770 I<lzip> is selected. (default: C<gtar>, if not found, use C<tar>).
772 =item B<--tmpdir> I<dir>
774 Change the name of the tmp directory (default: temporary directory)
778 Force verbose mode. (default: no verbosity)
782 Control the creation of the website. For creation of the website, the F<../exim-website>
783 (but see the B<website-base> option) directory must exist. (default: create the website, except when
786 =item B<--website-base> I<dir>
788 Base directory for the web site generation (default: F<../exim-website>)
790 =item B<-workspace>|B<--tmp> I<directory>
792 During release gerneration temporary storage is necessary. (default: F<exim-packaging-XXXX>
793 under your system's default temporary directory (typically this is F</tmp>)).
799 Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>,
800 some changes by Heiko Schlittermann <hs@schlittermann.de>
804 Copyright 2010-2016 Exim Maintainers. All rights reserved.