extract gnulib commit number too
[pspp] / build-pspp
1 #! /usr/bin/env perl
2
3 use Getopt::Long qw(:config bundling no_ignore_case);
4 use POSIX;
5
6 use strict;
7 use warnings;
8
9 my $help = 0;
10 GetOptions ("h|help" => \$help);
11
12 usage () if $help;
13
14 die "$0: exactly one or two nonoption arguments are required\n"
15   if @ARGV != 1 && @ARGV != 2;
16
17 my $buildslave = `hostname`;
18 chomp $buildslave;
19
20 # Select build number.
21 my $buildnumber = POSIX::strftime("%Y%m%d%H%M%S", localtime);
22 print "\tBUILDNUMBER=$buildnumber\n";
23
24 # Create build directory.
25 my $builddir = "builds/$buildnumber";
26 print "Create $builddir\n";
27 mkdir "builds" or die "builds: mkdir: $!\n" if ! -d "builds";
28 mkdir $builddir or die "$builddir: mkdir: $!\n";
29
30 if (@ARGV == 2) {
31     my ($repo, $branch) = @ARGV;
32
33     # Fetch branch
34     print "Fetch $repo, branch $branch\n";
35     run ("git fetch $repo +$branch:buildtmp/$$/pspp");
36
37     # Get revision number.
38     my $revision = `git rev-parse buildtmp/$$/pspp`;
39     chomp $revision;
40     print "\tREVISION=$revision\n";
41     my $abbrev_revision = substr ($revision, 0, 6);
42
43     # Extract source.
44     print "Extract branch into $builddir/pspp$buildnumber\n";
45     run ("git archive --format=tar --prefix=pspp$buildnumber/ buildtmp/$$/pspp | (cd $builddir && tar xf -)");
46
47     # Extract version number.
48     print "Extract version number\n";
49     my $trace = `cd $builddir/pspp$buildnumber && autoconf -t AC_INIT`;
50     chomp $trace;
51     my ($file, $line, $macro, $package, $version, @rest) = split (':', $trace);
52     print "\tVERSION=$version\n";
53
54     # Append -g012345 to AC_INIT version number.
55     my $fullname = "$builddir/pspp$buildnumber/$file";
56     open (OLDFILE, '<', $fullname)
57       or die "opening $fullname failed: $!\n";
58     open (NEWFILE, '>', "$fullname.new")
59       or die "creating $fullname.new failed: $!\n";
60     while (<OLDFILE>) {
61         if ($. != $line) {
62             print NEWFILE $_;
63         } else {
64             print NEWFILE "AC_INIT([[$package]]";
65             print NEWFILE ", [[$version-g$abbrev_revision]]";
66             print NEWFILE ", [[$_]]" foreach @rest;
67             print NEWFILE ")\n";
68         }
69     }
70     close (NEWFILE);
71     close (OLDFILE);
72     rename ("$fullname.new", $fullname)
73       or die "rename $fullname.new to $fullname failed: $!\n";
74
75     # Add note to beginning of NEWS (otherwise "make dist" fails).
76     $fullname = "$builddir/pspp$buildnumber/NEWS";
77     open (OLDFILE, '<', $fullname)
78       or die "opening $fullname failed: $!\n";
79     open (NEWFILE, '>', "$fullname.new")
80       or die "creating $fullname.new failed: $!\n";
81     my $found_changes = 0;
82     while (<OLDFILE>) {
83         if (!$found_changes && /^Changes/) {
84             $found_changes = 1;
85             print NEWFILE <<EOF;
86 Changes from $version to $version-g$abbrev_revision:
87
88  * Built automatically from commit $revision
89    in branch $branch on build slave $buildslave
90
91 EOF
92         }
93         print NEWFILE $_;
94     }
95     close (NEWFILE);
96     close (OLDFILE);
97     rename ("$fullname.new", $fullname)
98       or die "rename $fullname.new to $fullname failed: $!\n";
99
100     # Get Gnulib commit number.
101     my $gnulib_commit;
102     $fullname = "$builddir/pspp$buildnumber/README.Git";
103     open (README_GIT, '<', $fullname)
104       or die "opening $fullname failed: $!\n";
105     while (<README_GIT>) {
106         ($gnulib_commit) = /^\s+commit ([0-9a-fA-F]{8,})/ and last;
107     }
108     die "$fullname does not specify a Git commit number\n"
109       if !defined ($gnulib_commit);
110     print "\tGNULIB_REVISION=$gnulib_commit\n";
111 }
112
113 sub usage {
114     print <<EOF;
115 $0, for building and testing PSPP
116 usage: $0 [OPTIONS] [TARBALL | REPO REFSPEC]
117 where TARBALL is the name of a tarball produced by "make dist"
118    or REPO and REFSPEC are a Git repo and refspec (e.g. branch) to clone.
119
120 Options:
121   --help            Print this usage message and exit
122 EOF
123     exit(0);
124 }
125
126 sub run {
127     my ($command) = @_;
128     return if system ($command) == 0;
129     if ($? == -1) {
130         print "$command: failed to execute: $!\n";
131     } elsif ($? & 127) {
132         printf "%s: child died with signal %d, %s coredump\n",
133           $command, ($? & 127),  ($? & 128) ? 'with' : 'without';
134     } else {
135         printf "%s: child exited with value %d\n", $command, $? >> 8;
136     }
137     exit 1;
138 }
139
140 # Clone source
141 # Add build number
142 # Tag build
143 # Clone gnulib at correct commit number
144 # Run gnulib-tool.
145 # Run configure
146 # Make dist
147
148 # Unpack dist
149 # Run configure
150 # Check
151 # Install
152 # Make binary dist
153 # Build mingw32 installer
154 # Other distcheck stuff?