3 # Copyright (C) 2011,2012,2013,2015 Olly Betts
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to
7 # deal in the Software without restriction, including without limitation the
8 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 # sell copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 my $MAXLOADPERCPU = 1.5;
40 if (scalar @ARGV && $ARGV[0] eq '--force') {
46 my $work = "/home/olly/tmp/xapian-git-snapshot";
47 # Add ccache's directory first so we'll use it if it is installed.
48 $ENV{PATH
} = "/usr/lib/ccache:/home/olly/install/bin:/usr/bin:/bin";
49 my $repourl = '/home/xapian-git/xapian/';
50 my $webbase = '/srv/www';
52 # Create the work directory first, since we need it to exist so we can create
54 mkpath
($work, 0, 0755);
55 chdir $work or die $!;
57 # Prevent multiple instances of this script from running at once.
58 # Use the same lockfile that fetch does.
59 open LOCK
, '>', 'flockme' or die "Couldn't open 'flockme' for writing: $!\n";
60 unless (flock LOCK
, LOCK_EX
|LOCK_NB
) {
61 # If we're building a tagged release, we want to wait rather than exit.
62 unless (scalar @ARGV && $ARGV[0] =~ m!^tags/! && flock LOCK
, LOCK_EX
) {
63 # Work directory already in use. Don't print anything unless STDOUT
64 # is a tty - the cron job will send it as mail, which we don't really
66 print STDERR
"'flockme' is already locked, can't build '$ARGV[0]' right now\n"
72 # Check the load average AFTER getting the lock, since we generate output if
73 # the load is too high, and it will probably be too high if we're already
75 my $HOSTNAME = Sys
::Hostname
::hostname
();
76 # Check the load average isn't too high.
78 if (-e
"/var/run/dobackup") {
79 print STDERR
"$HOSTNAME: Backup running (/var/run/dobackup exists)\n"
83 if (((`uptime 2>/dev/null`)[0] =~ /.*: (\d+(?:\.\d+)?),/) &&
84 ($1 > $MAXLOADPERCPU)) {
86 # `getconf _NPROCESSORS_ONLN` on linux gives e.g. 2
87 # `sysctl hw.ncpu` on openbsd (and prob. freebsd & darwin) gives e.g. hw.ncpu=2
88 # `psrinfo|grep -c on-line` on Solaris or OSF/1 gives e.g. 2
90 # Works on Linux, at least back to kernel 2.2.26.
91 $ncpu ||= run_command
("getconf _NPROCESSORS_ONLN 2>/dev/null|grep -v '[^0-9]'");
92 # Works on OpenBSD (and apparently FreeBSD and Darwin).
93 $ncpu ||= run_command
("sysctl hw.ncpu 2>/dev/null|sed 's/.*=//'");
94 # Works on Solaris and OSF/1.
95 $ncpu ||= run_command
("PATH=/usr/sbin:\$PATH psrinfo 2>/dev/null|grep -c on-line");
96 # Works on Linux, just in case the getconf version doesn't. Different
97 # architectures have different formats for /proc/cpuinfo so this won't
98 # work as widely as getconf _NPROCESSORS_ONLN will.
99 $ncpu ||= run_command
("grep -c processor /proc/cpuinfo 2>/dev/null");
101 if ($loadavg > $ncpu * $MAXLOADPERCPU) {
103 print STDERR
"$HOSTNAME: High load average: $loadavg ($ncpu CPUs)\n";
109 # If no tags specified, default to those which there are directories for.
110 # Don't bother to build tags/* since those (generally) don't change.
111 if (scalar @ARGV == 0) {
112 @ARGV = grep /\/[A
-Za
-z0
-9][-A
-Za
-z0
-9_
.]*$/ && -d
$_,
113 glob('branches/[A-Za-z0-9]*');
114 -d
"trunk" && unshift @ARGV, "trunk";
115 -d
"master" && unshift @ARGV, "master";
118 # Or if there are no directories, default to the trunk.
119 if (scalar @ARGV == 0) {
124 foreach my $tag (@ARGV) {
125 chdir $work or die $!;
126 # Restrict tags to sane characters.
127 next if $tag !~ /^[-A-Za-z0-9_.\/]+$/;
129 print "*** No directory for '$tag'\n";
137 my $logfile = "$tag/snapshot.log";
139 my $co_dir = "$tag/xapian";
140 if (! -d
"$co_dir/.git") {
141 system "chmod", "-R", "+w", $co_dir if -d
$co_dir;
142 system "rm", "-rf", $co_dir;
143 # Check out the tag into a directory of the same name.
145 $branch =~ s!^branches/!svn/! || $branch =~ s!^tags/!! ||
146 ($branch =~ s/^trunk$/master/);
147 open CMD
, "git clone --branch \Q$branch\E \Q$repourl\E \Q$co_dir\E 2>&1|" or die $!;
148 $log = join '', <CMD
>;
149 close CMD
or do { print $log; die $!; };
150 chdir $co_dir or die $!;
152 # Revert any local changes.
153 chdir $co_dir or die $!;
154 $log = "git reset --hard:\n".`git reset --hard 2>&1`."git pull --ff-only:\n";
155 open CMD
, "git pull --ff-only 2>&1|" or die $!;
159 if ($changed && !$force) {
160 if (/^Already up-to-date/) {
167 chdir $old_cwd or die $!;
168 $ENV{'VERBOSE'} and print "No changes\n";
172 my ($revision) = `git describe --always`;
174 my ($revcount) = ($revision =~ /-([0-9]+)-/);
175 if (!defined $revcount) {
176 # Workaround until we have git tags - count commits since 1.3.1.
177 $revcount = scalar(@
{[`git log --format='%H' 1daf0071342d883ca308762f269a63f6ec5df981..`]});
178 $revision = "v1.3.1-$revcount-g$revision";
181 chdir $old_cwd or die $!;
183 # Don't repeat a build for the same revision.
184 next if -f
"$logfile.$revision";
186 open LOG
, ">", "$logfile.$revision" or die $!;
187 # Flush output after every print.
188 my $old_fh = select(LOG
);
195 if ($tag !~ m!^tags/!) {
196 # Modify configure.ac files to insert $revision into version string.
197 foreach my $configure_ac
198 (glob("\Q$co_dir\E/xapian*/configure.ac"),
199 glob("\Q$co_dir\E/xapian*/*/configure.ac")) {
200 open OUT
, ">", "tmp.out" or die $!;
201 open IN
, "<", $configure_ac or die $!;
203 s/(^(?:AC_INIT\([^,]*|m4_define\(\[project_version\]),.*?[0-9])(\s*[),\]])/$1_git$revcount$2/g;
208 rename "tmp.out", $configure_ac or die $!;
210 if (-f
"$co_dir/search-xapian/Makefile.PL") {
212 my $fnm = "$co_dir/search-xapian/Xapian.pm";
213 open OUT
, ">", "tmp.out" or die $!;
214 open IN
, "<", $fnm or die $!;
216 if (s/^(our \$VERSION = ')(\d+\.\d+\.\d+\.)\d+(.*)/$1$2$revcount$3 # git snapshot/) {
217 $snap_version = $2 . $revcount;
223 rename "tmp.out", $fnm or die $!;
225 $fnm = "$co_dir/search-xapian/README";
226 open OUT
, ">", "tmp.out" or die $!;
227 open IN
, "<", $fnm or die $!;
229 s/(\d+\.\d+\.\d+\.)\d+/$1$revcount (git snapshot)/;
236 rename "tmp.out", $fnm or die $!;
238 $fnm = "$co_dir/search-xapian/Changes";
239 open OUT
, ">", "tmp.out" or die $!;
240 open IN
, "<", $fnm or die $!;
245 print OUT
$snap_version.strftime
(" %a %b %e %H:%M:%S %Z %Y\n",gmtime());
246 print OUT
"\t- git snapshot of revision $revision.\n\n";
252 rename "tmp.out", $fnm or die $!;
256 system "chmod", "-R", "+w", "$tag/build" if -d
"$tag/build";
257 system "rm", "-rf", "$tag/build";
258 mkpath
("$tag/build", 0, 0755) or die $!;
259 chdir "$tag/build" or die $!;
261 # Note the current time so we can find sources which weren't used during
262 # the build. Sleep for a couple of seconds first to avoid needing to
263 # worry about timestamps equal to $timestamp.
266 # Skip xapian-letor for now, as atreus lacks libsvm-dev, or the ability
267 # to install a new enough one from a package.
268 open TOUCH
, '>>', '../xapian/xapian-letor/.nobootstrap' and close TOUCH
;
270 open F
, '<', '../xapian/bootstrap' or die $!;
272 my $timestamp = (stat F
)[8];
275 $log = `../xapian/bootstrap 2>&1`;
278 print "*** bootstrap failed for '$tag':";
285 # On atreus, javac keeps going into memory eating loops, so don't try to
286 # build the java bindings.
287 $log = `../xapian/configure --enable-quiet --enable-maintainer-mode --without-java 2>&1`;
290 print "*** configure failed for '$tag':";
297 $log = `make -s 2>&1`;
300 print "*** make failed for '$tag':";
307 my %unused_files = ();
308 sub check_unused_files_from_build
{
309 return if $File::Find
::name
eq '../xapian';
310 my $f = substr($File::Find
::name
, length('../xapian/'));
311 if ($_ eq 'autom4te.cache' ||
313 $f eq 'search-xapian/blib' ||
315 $f eq 'xapian-applications/omega/common' ||
316 $f eq 'xapian-data' || # FIXME: make check should use this
317 $f eq 'xapian-maintainer-tools' ||
321 /^Search-Xapian-\d+\.\d+\.\d+\.\d+$/) {
322 if (-d
$File::Find
::name
) {
323 # Don't descend into these subdirectories.
324 $File::Find
::prune
= 1;
328 return unless -f
$File::Find
::name
and (stat _
)[8] < $timestamp;
329 return if $_ eq '.gitignore';
330 return if $_ eq 'config.h.in~';
331 return if $_ eq 'NEWS.SKELETON';
332 return if $f eq 'README';
333 return if $f eq 'Vagrantfile';
334 return if $f eq '.travis.yml';
335 return if /^Search-Xapian-\d+\.\d+\.\d+\.\d+\.tar\..z$/;
337 print "Unused during make: $f\n";
339 find
(\
&check_unused_files_from_build
, '../xapian');
341 my $lib = (<xapian
-core
/.libs/libxapian
*.so
>)[0];
342 my $unstripped_so = -s
$lib;
343 $log = `strip \Q$lib\E`;
346 my $stripped_so = -s
$lib;
348 open SIZELOG
, ">>/home/olly/xapian-autobuild-stats.log";
349 print SIZELOG
"$tag\trev=$revision\tunstripped_so=$unstripped_so\tstripped_so=$stripped_so\n";
352 $log = `make -s distcheck VALGRIND= 2>&1`;
355 print "*** make distcheck failed for '$tag':";
362 # This finds files we don't ship or use to get to what we ship:
363 find
(\
&check_unused_files_from_build
, '../xapian');
365 # MSVC build files aren't up to date, so don't snapshot them for now.
367 # Snapshots of MSVC build files.
368 my $win32dir = "win32msvc_$revision";
370 symlink "../xapian/xapian-maintainer-tools/win32msvc", $win32dir;
371 $log = `tar --dereference --exclude .git -acvf \Q$win32dir\E.tar.xz \Q$win32dir\E 2>&1`;
375 print "*** tar win32msvc failed for '$tag':";
382 my $d = "$webbase/oligarchy.co.uk/xapian/$tag";
383 if ($tag =~ m!^tags/v?([\d.]+)$!) {
384 $d = "$webbase/oligarchy.co.uk/xapian/$1";
387 mkpath
($d, 0, 0755) or die $!;
388 open HTACCESS
, ">", "$d/.htaccess" or die $!;
389 print HTACCESS
"IndexOptions NameWidth=*\n";
390 close HTACCESS
or die $!;
393 # Delete snapshots more than a week old, but leave at least one.
394 my $num_of_days_to_keep = 7;
395 my @o = glob "$d/old/*.tar.?z";
397 @o = grep {-M
$_ > $num_of_days_to_keep} @o;
401 mkdir "$d/old", 0755 or die $!;
402 open HTACCESS
, ">", "$d/old/.htaccess" or die $!;
403 print HTACCESS
"IndexOptions NameWidth=*\n";
404 close HTACCESS
or die $!;
406 for (glob "$d/*.tar.?z") {
407 my ($leaf) = m!([^/]*)$!;
408 rename $_, "$d/old/$leaf";
411 for (glob("*/*.tar.?z"), glob("xapian-applications/*/*.tar.?z"), glob("*.tar.?z")) {
412 print LOG
"Moving '$_' to '$d'\n";
413 system("mv", $_, $d);
415 print LOG
"Failed with exit code $?\n";
420 for (glob("search-xapian/*.tar.?z")) {
421 print LOG2
"Moving '$_' to '$d'\n";
422 system("mv", $_, $d);
424 print LOG2
"Failed with exit code $?\n";
430 system("rm -rf xapian/search-xapian/Search-Xapian-*");
432 # Expire logs more than 10 days old
433 unlink grep {-M
$_ > 10} glob 'snapshot.log.*';
436 system("/home/olly/bin/plot-sizes");