Start working on PSPP build script.
[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     
101 }
102
103 sub usage {
104     print <<EOF;
105 $0, for building and testing PSPP
106 usage: $0 [OPTIONS] [TARBALL | REPO REFSPEC]
107 where TARBALL is the name of a tarball produced by "make dist"
108    or REPO and REFSPEC are a Git repo and refspec (e.g. branch) to clone.
109
110 Options:
111   --help            Print this usage message and exit
112 EOF
113     exit(0);
114 }
115
116 sub run {
117     my ($command) = @_;
118     return if system ($command) == 0;
119     if ($? == -1) {
120         print "$command: failed to execute: $!\n";
121     } elsif ($? & 127) {
122         printf "%s: child died with signal %d, %s coredump\n",
123           $command, ($? & 127),  ($? & 128) ? 'with' : 'without';
124     } else {
125         printf "%s: child exited with value %d\n", $command, $? >> 8;
126     }
127     exit 1;
128 }
129
130 # Clone source
131 # Add build number
132 # Tag build
133 # Clone gnulib at correct commit number
134 # Run gnulib-tool.
135 # Run configure
136 # Make dist
137
138 # Unpack dist
139 # Run configure
140 # Check
141 # Install
142 # Make binary dist
143 # Build mingw32 installer
144 # Other distcheck stuff?