make sure git pull is actually done!
[platform/upstream/curl.git] / tests / testcurl.pl
1 #!/usr/bin/env perl
2 #***************************************************************************
3 #                                  _   _ ____  _
4 #  Project                     ___| | | |  _ \| |
5 #                             / __| | | | |_) | |
6 #                            | (__| |_| |  _ <| |___
7 #                             \___|\___/|_| \_\_____|
8 #
9 # Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
10 #
11 # This software is licensed as described in the file COPYING, which
12 # you should have received as part of this distribution. The terms
13 # are also available at http://curl.haxx.se/docs/copyright.html.
14 #
15 # You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 # copies of the Software, and permit persons to whom the Software is
17 # furnished to do so, under the terms of the COPYING file.
18 #
19 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 # KIND, either express or implied.
21 #
22 ###########################################################################
23
24 ###########################
25 #  What is This Script?
26 ###########################
27
28 # testcurl.pl is the master script to use for automatic testing of curl
29 # directly off its source repository.
30 # This is written for the purpose of being run from a crontab job or similar
31 # at a regular interval. The output is suitable to be mailed to
32 # curl-autocompile@haxx.se to be dealt with automatically (make sure the
33 # subject includes the word "autobuild" as the mail gets silently discarded
34 # otherwise).  The most current build status (with a resonable backlog) will
35 # be published on the curl site, at http://curl.haxx.se/auto/
36
37 # USAGE:
38 # testcurl.pl [options] [curl-daily-name] > output
39
40 # Options:
41 #
42 # --configure=[options]    Configure options
43 # --crosscompile           This is a crosscompile
44 # --desc=[desc]            Description of your test system
45 # --email=[email]          Set email address to report as
46 # --extvercmd=[command]    Command to use for displaying version with cross compiles.
47 # --mktarball=[command]    Command to run after completed test
48 # --name=[name]            Set name to report as
49 # --nocvsup                Don't pull from git even though it is a git tree
50 # --nogitpull              Don't pull from git even though it is a git tree
51 # --nobuildconf            Don't run buildconf
52 # --runtestopts=[options]  Options to pass to runtests.pl
53 # --setup=[file name]      File name to read setup from (deprecated)
54 # --target=[your os]       Specify your target environment.
55 #
56 # if [curl-daily-name] is omitted, a 'curl' git directory is assumed.
57 #
58
59 use strict;
60
61 use Cwd;
62
63 # Turn on warnings (equivalent to -w, which can't be used with /usr/bin/env)
64 #BEGIN { $^W = 1; }
65
66 use vars qw($version $fixed $infixed $CURLDIR $git $pwd $build $buildlog
67             $buildlogname $configurebuild $targetos $confsuffix $binext
68             $libext);
69
70 use vars qw($name $email $desc $confopts $runtestopts $setupfile $mktarball
71             $extvercmd $nogitpull $nobuildconf $crosscompile
72             $timestamp);
73
74 # version of this script
75 $version='2010-03-24';
76 $fixed=0;
77
78 # Determine if we're running from git or a canned copy of curl,
79 # or if we got a specific target option or setup file option.
80 $CURLDIR="curl";
81 $git=1;
82 $setupfile = 'setup';
83 while ($ARGV[0]) {
84   if ($ARGV[0] =~ /--target=/) {
85     $targetos = (split(/=/, shift @ARGV))[1];
86   }
87   elsif ($ARGV[0] =~ /--setup=/) {
88     $setupfile = (split(/=/, shift @ARGV))[1];
89   }
90   elsif ($ARGV[0] =~ /--extvercmd=/) {
91     $extvercmd = (split(/=/, shift @ARGV))[1];
92   }
93   elsif ($ARGV[0] =~ /--mktarball=/) {
94     $mktarball = (split(/=/, shift @ARGV))[1];
95   }
96   elsif ($ARGV[0] =~ /--name=/) {
97     $name = (split(/=/, shift @ARGV))[1];
98   }
99   elsif ($ARGV[0] =~ /--email=/) {
100     $email = (split(/=/, shift @ARGV))[1];
101   }
102   elsif ($ARGV[0] =~ /--desc=/) {
103     $desc = (split(/=/, shift @ARGV))[1];
104   }
105   elsif ($ARGV[0] =~ /--configure=/) {
106     $confopts = (split(/=/, shift @ARGV))[1];
107   }
108   elsif (($ARGV[0] eq "--nocvsup") || ($ARGV[0] eq "--nogitpull")) {
109     $nogitpull=1;
110     shift @ARGV;
111   }
112   elsif ($ARGV[0] =~ /--nobuildconf/) {
113     $nobuildconf=1;
114     shift @ARGV;
115   }
116   elsif ($ARGV[0] =~ /--crosscompile/) {
117     $crosscompile=1;
118     shift @ARGV;
119   }
120   elsif ($ARGV[0] =~ /--runtestopts=/) {
121     $runtestopts = (split(/=/, shift @ARGV, 2))[1];
122   }
123   else {
124     $CURLDIR=shift @ARGV;
125     $git=0; # a given dir, assume not using git
126   }
127 }
128
129 # Do the platform-specific stuff here
130 $configurebuild = 1;
131 $confsuffix = '';
132 $binext = '';
133 $libext = '.la'; # .la since both libcurl and libcares are made with libtool
134 if ($^O eq 'MSWin32' || $targetos) {
135   if (!$targetos) {
136     # If no target defined on Win32 lets assume vc
137     $targetos = 'vc';
138   }
139   if ($targetos =~ /vc/ || $targetos =~ /borland/) {
140     $binext = '.exe';
141     $libext = '.lib';
142   }
143   elsif ($targetos =~ /mingw/) {
144     $binext = '.exe';
145     if ($^O eq 'MSWin32') {
146       $libext = '.a';
147     }
148   }
149   elsif ($targetos =~ /netware/) {
150     $configurebuild = 0;
151     $binext = '.nlm';
152     if ($^O eq 'MSWin32') {
153       $libext = '.lib';
154     }
155     else {
156       $libext = '.a';
157     }
158   }
159 }
160
161 if (($^O eq 'MSWin32') &&
162     ($targetos =~ /vc/ || $targetos =~ /mingw32/ || $targetos =~ /borland/)) {
163
164   # Set these things only when building ON Windows and for Win32 platform.
165   # FOR Windows since we might be cross-compiling on another system. Non-
166   # Windows builds still default to configure-style builds with no confsuffix.
167
168   $configurebuild = 0;
169   $confsuffix = '-win32';
170 }
171
172 $ENV{LC_ALL}="C" if (($ENV{LC_ALL}) && ($ENV{LC_ALL} !~ /^C$/));
173 $ENV{LC_CTYPE}="C" if (($ENV{LC_CTYPE}) && ($ENV{LC_CTYPE} !~ /^C$/));
174 $ENV{LANG}="C";
175
176 sub rmtree($) {
177     my $target = $_[0];
178     if ($^O eq 'MSWin32') {
179       foreach (glob($target)) {
180         s:/:\\:g;
181         system("rd /s /q $_");
182       }
183     } else {
184       system("rm -rf $target");
185     }
186 }
187
188 sub grepfile($$) {
189     my ($target, $fn) = @_;
190     open(F, $fn) or die;
191     while (<F>) {
192       if (/$target/) {
193         close(F);
194         return 1;
195       }
196     }
197     close(F);
198     return 0;
199 }
200
201 sub logit($) {
202     my $text=$_[0];
203     if ($text) {
204       print "testcurl: $text\n";
205     }
206 }
207
208 sub logit_spaced($) {
209     my $text=$_[0];
210     if ($text) {
211       print "\ntestcurl: $text\n\n";
212     }
213 }
214
215 sub mydie($){
216     my $text=$_[0];
217     logit "$text";
218     chdir $pwd; # cd back to the original root dir
219
220     if ($pwd && $build) {
221       # we have a build directory name, remove the dir
222       logit "removing the $build dir";
223       rmtree "$pwd/$build";
224     }
225     if (-r $buildlog) {
226       # we have a build log output file left, remove it
227       logit "removing the $buildlogname file";
228       unlink "$buildlog";
229     }
230     logit "ENDING HERE"; # last line logged!
231     exit 1;
232 }
233
234 sub get_host_triplet {
235   my $triplet;
236   my $configfile = "$pwd/$build/lib/curl_config.h";
237
238   if(-f $configfile && -s $configfile && open(LIBCONFIGH, "<$configfile")) {
239     while(<LIBCONFIGH>) {
240       if($_ =~ /^\#define\s+OS\s+"*([^"][^"]*)"*\s*/) {
241         $triplet = $1;
242         last;
243       }
244     }
245     close(LIBCONFIGH);
246   }
247   return $triplet;
248 }
249
250 if (open(F, "$setupfile")) {
251   while (<F>) {
252     if (/(\w+)=(.*)/) {
253       eval "\$$1=$2;";
254     }
255   }
256   close(F);
257   $infixed=$fixed;
258 } else {
259   $infixed=0;    # so that "additional args to configure" works properly first time...
260 }
261
262 if (!$name) {
263   print "please enter your name\n";
264   $name = <>;
265   chomp $name;
266   $fixed=1;
267 }
268
269 if (!$email) {
270   print "please enter your contact email address\n";
271   $email = <>;
272   chomp $email;
273   $fixed=2;
274 }
275
276 if (!$desc) {
277   print "please enter a one line system description\n";
278   $desc = <>;
279   chomp $desc;
280   $fixed=3;
281 }
282
283 if (!$confopts) {
284   if ($infixed < 4) {
285     print "please enter your additional arguments to configure\n";
286     print "examples: --with-ssl --enable-debug --enable-ipv6 --with-krb4\n";
287     $confopts = <>;
288     chomp $confopts;
289   }
290 }
291
292
293 if ($fixed < 4) {
294     $fixed=4;
295     open(F, ">$setupfile") or die;
296     print F "name='$name'\n";
297     print F "email='$email'\n";
298     print F "desc='$desc'\n";
299     print F "confopts='$confopts'\n";
300     print F "fixed='$fixed'\n";
301     close(F);
302 }
303
304 # Enable picky compiler warnings unless explicitly disabled
305 if (($confopts !~ /--enable-debug/) &&
306     ($confopts !~ /--enable-warnings/) &&
307     ($confopts !~ /--disable-warnings/)) {
308   $confopts .= " --enable-warnings";
309 }
310
311 my $str1066os = 'o' x 1066;
312
313 # Set timestamp to the UTC this script is running. Its value might
314 # be changed later in the script to the value present in curlver.h
315 $timestamp = scalar(gmtime)." UTC";
316
317 logit "STARTING HERE"; # first line logged, for scripts to trigger on
318 logit 'TRANSFER CONTROL ==== 1120 CHAR LINE' . $str1066os . 'LINE_END';
319 logit "NAME = $name";
320 logit "EMAIL = $email";
321 logit "DESC = $desc";
322 logit "CONFOPTS = $confopts";
323 logit "CPPFLAGS = ".$ENV{CPPFLAGS};
324 logit "CFLAGS = ".$ENV{CFLAGS};
325 logit "LDFLAGS = ".$ENV{LDFLAGS};
326 logit "CC = ".$ENV{CC};
327 logit "MAKEFLAGS = ".$ENV{MAKEFLAGS};
328 logit "PKG_CONFIG_PATH = ".$ENV{PKG_CONFIG_PATH};
329 logit "target = ".$targetos;
330 logit "version = $version"; # script version
331 logit "date = $timestamp";  # When the test build starts
332
333 $str1066os = undef;
334
335 # Make $pwd to become the path without newline. We'll use that in order to cut
336 # off that path from all possible logs and error messages etc.
337 $pwd = getcwd();
338
339 if (-d $CURLDIR) {
340   if ($git && -d "$CURLDIR/.git") {
341     logit "$CURLDIR is verified to be a fine git source dir";
342     # remove the generated sources to force them to be re-generated each
343     # time we run this test
344     unlink "$CURLDIR/src/hugehelp.c";
345   } elsif (!$git && -f "$CURLDIR/tests/testcurl.pl") {
346     logit "$CURLDIR is verified to be a fine daily source dir"
347   } else {
348     mydie "$CURLDIR is not a daily source dir or checked out from git!"
349   }
350 }
351 $build="build-$$";
352 $buildlogname="buildlog-$$";
353 $buildlog="$pwd/$buildlogname";
354
355 # remove any previous left-overs
356 rmtree "build-*";
357 rmtree "buildlog-*";
358
359 # this is to remove old build logs that ended up in the wrong dir
360 foreach (glob("$CURLDIR/buildlog-*")) { unlink $_; }
361
362 # create a dir to build in
363 mkdir $build, 0777;
364
365 if (-d $build) {
366   logit "build dir $build was created fine";
367 } else {
368   mydie "failed to create dir $build";
369 }
370
371 # get in the curl source tree root
372 chdir $CURLDIR;
373
374 sub gitpull() {
375     # update quietly to the latest git
376     if($nogitpull) {
377         logit "Skipping git pull (--nogitpull)";
378         return 1;
379     }
380     else {
381         logit "run git pull";
382         system("git pull 2>&1");
383     }
384
385     my $stat=$?;
386
387     return $stat;
388 }
389
390 # Do the git thing, or not...
391 if ($git) {
392
393   my $cvsstat = gitpull();
394
395   if ($cvsstat != 0) {
396     # update failure is not lethal
397     logit "failed to update from git ($cvsstat), continue anyway";
398   }
399   elsif (!$nogitpull) {
400     # Set timestamp to the UTC the git update took place.
401     $timestamp = scalar(gmtime)." UTC";
402   }
403
404   # get the last 5 commits for show (even if no pull was made)
405   my @commits=`git log --pretty=oneline --abbrev-commit -5`;
406   logit "The most recent git commits:";
407   for my $l (@commits) {
408       chomp ($l);
409       logit "  $l";
410   }
411
412   if($nobuildconf) {
413       logit "told to not run buildconf";
414   }
415   elsif ($configurebuild) {
416     # remove possible left-overs from the past
417     unlink "configure";
418     unlink "autom4te.cache";
419
420     # generate the build files
421     logit "invoke buildconf, but filter off aclocal underquoted definition warnings";
422     open(F, "./buildconf 2>&1 |") or die;
423     open(LOG, ">$buildlog") or die;
424     while (<F>) {
425       next if /warning: underquoted definition of/;
426       print;
427       print LOG;
428     }
429     close(F);
430     close(LOG);
431
432     if (grepfile("^buildconf: OK", $buildlog)) {
433       logit "buildconf was successful";
434     }
435     else {
436       mydie "buildconf was NOT successful";
437     }
438   }
439   else {
440     logit "buildconf was successful (dummy message)";
441   }
442 }
443
444 # Set timestamp to the one in curlver.h if this isn't a git test build.
445 if ((-f "include/curl/curlver.h") &&
446     (open(F, "<include/curl/curlver.h"))) {
447   while (<F>) {
448     chomp;
449     if ($_ =~ /^\#define\s+LIBCURL_TIMESTAMP\s+\"(.+)\".*$/) {
450       my $stampstring = $1;
451       if ($stampstring !~ /DEV/) {
452           $stampstring =~ s/\s+UTC//;
453           $timestamp = $stampstring." UTC";
454       }
455       last;
456     }
457   }
458   close(F);
459 }
460
461 # Show timestamp we are using for this test build.
462 logit "timestamp = $timestamp";
463
464 if ($configurebuild) {
465   if (-f "configure") {
466     logit "configure created (at least it exists)";
467   } else {
468     mydie "no configure created/found";
469   }
470 } else {
471   logit "configure created (dummy message)"; # dummy message to feign success
472 }
473
474 sub findinpath {
475   my $c;
476   my $e;
477   my $x = ($^O eq 'MSWin32') ? '.exe' : '';
478   my $s = ($^O eq 'MSWin32') ? ';' : ':';
479   my $p=$ENV{'PATH'};
480   my @pa = split($s, $p);
481   for $c (@_) {
482     for $e (@pa) {
483       if( -x "$e/$c$x") {
484         return $c;
485       }
486     }
487   }
488 }
489
490 my $make = findinpath("gmake", "make", "nmake");
491 if(!$make) {
492     mydie "Couldn't find make in the PATH";
493 }
494 # force to 'nmake' for VC builds
495 $make = "nmake" if ($targetos =~ /vc/);
496 logit "going with $make as make";
497
498 # change to build dir
499 chdir "$pwd/$build";
500
501 if ($configurebuild) {
502   # run configure script
503   print `../$CURLDIR/configure $confopts 2>&1`;
504
505   if (-f "lib/Makefile") {
506     logit "configure seems to have finished fine";
507   } else {
508     mydie "configure didn't work";
509   }
510 } else {
511   logit "copying files to build dir ...";
512   if (($^O eq 'MSWin32') && ($targetos !~ /netware/)) {
513     system("xcopy /s /q ..\\$CURLDIR .");
514     system("buildconf.bat");
515   }
516   elsif ($targetos =~ /netware/) {
517     system("cp -afr ../$CURLDIR/* .");
518     system("cp -af ../$CURLDIR/Makefile.dist Makefile");
519     system("$make -i -C lib -f Makefile.netware prebuild");
520     system("$make -i -C src -f Makefile.netware prebuild");
521     if (-d "../$CURLDIR/ares") {
522       system("$make -i -C ares -f Makefile.netware prebuild");
523     }
524   }
525   elsif ($^O eq 'linux') {
526     system("cp -afr ../$CURLDIR/* .");
527     system("cp -af ../$CURLDIR/Makefile.dist Makefile");
528     system("cp -af ../$CURLDIR/include/curl/curlbuild.h.dist ./include/curl/curlbuild.h");
529     system("$make -i -C lib -f Makefile.$targetos prebuild");
530     system("$make -i -C src -f Makefile.$targetos prebuild");
531     if (-d "../$CURLDIR/ares") {
532       system("cp -af ../$CURLDIR/ares/ares_build.h.dist ./ares/ares_build.h");
533       system("$make -i -C ares -f Makefile.$targetos prebuild");
534     }
535   }
536 }
537
538 if(-f "./libcurl.pc") {
539   logit_spaced "display libcurl.pc";
540   if(open(F, "<./libcurl.pc")) {
541     while(<F>) {
542       my $ll = $_;
543       print $ll if(($ll !~ /^ *#/) && ($ll !~ /^ *$/));
544     }
545     close(F);
546   }
547 }
548
549 if(-f "./include/curl/curlbuild.h") {
550   logit_spaced "display include/curl/curlbuild.h";
551   if(open(F, "<./include/curl/curlbuild.h")) {
552     while(<F>) {
553       my $ll = $_;
554       print $ll if(($ll =~ /^ *# *define *CURL_/) && ($ll !~ /__CURL_CURLBUILD_H/));
555     }
556     close(F);
557   }
558 }
559 else {
560   mydie "no curlbuild.h created/found";
561 }
562
563 logit_spaced "display lib/curl_config$confsuffix.h";
564 open(F, "lib/curl_config$confsuffix.h") or die "lib/curl_config$confsuffix.h: $!";
565 while (<F>) {
566   print if /^ *#/;
567 }
568 close(F);
569
570 if (grepfile("define USE_ARES", "lib/curl_config$confsuffix.h")) {
571   print "\n";
572   logit "setup to build ares";
573
574   if(-f "./ares/libcares.pc") {
575     logit_spaced  "display ares/libcares.pc";
576     if(open(F, "<./ares/libcares.pc")) {
577       while(<F>) {
578         my $ll = $_;
579         print $ll if(($ll !~ /^ *#/) && ($ll !~ /^ *$/));
580       }
581       close(F);
582     }
583   }
584
585   if(-f "./ares/ares_build.h") {
586     logit_spaced "display ares/ares_build.h";
587     if(open(F, "<./ares/ares_build.h")) {
588       while(<F>) {
589         my $ll = $_;
590         print $ll if(($ll =~ /^ *# *define *CARES_/) && ($ll !~ /__CARES_BUILD_H/));
591       }
592       close(F);
593     }
594   }
595   else {
596     mydie "no ares_build.h created/found";
597   }
598
599   logit_spaced "display ares/ares_config$confsuffix.h";
600   if(open(F, "ares/ares_config$confsuffix.h")) {
601       while (<F>) {
602           print if /^ *#/;
603       }
604       close(F);
605   }
606
607   print "\n";
608   logit "build ares";
609   chdir "ares";
610
611   if ($targetos && !$configurebuild) {
612       logit "$make -f Makefile.$targetos";
613       open(F, "$make -f Makefile.$targetos 2>&1 |") or die;
614   }
615   else {
616       logit "$make";
617       open(F, "$make 2>&1 |") or die;
618   }
619   while (<F>) {
620     s/$pwd//g;
621     print;
622   }
623   close(F);
624
625   if (-f "libcares$libext") {
626     logit "ares is now built successfully (libcares$libext)";
627   } else {
628     mydie "ares build failed (libcares$libext)";
629   }
630
631   # cd back to the curl build dir
632   chdir "$pwd/$build";
633 }
634
635 my $mkcmd = "$make -i" . ($targetos && !$configurebuild ? " $targetos" : "");
636 logit "$mkcmd";
637 open(F, "$mkcmd 2>&1 |") or die;
638 while (<F>) {
639   s/$pwd//g;
640   print;
641 }
642 close(F);
643
644 if (-f "lib/libcurl$libext") {
645   logit "libcurl was created fine (libcurl$libext)";
646 }
647 else {
648   mydie "libcurl was not created (libcurl$libext)";
649 }
650
651 if (-f "src/curl$binext") {
652   logit "curl was created fine (curl$binext)";
653 }
654 else {
655   mydie "curl was not created (curl$binext)";
656 }
657
658 if (!$crosscompile || (($extvercmd ne '') && (-x $extvercmd))) {
659   logit "display curl${binext} --version output";
660   my $cmd = ($extvercmd ne '' ? $extvercmd.' ' : '')."./src/curl${binext} --version|";
661   open(F, $cmd);
662   while(<F>) {
663     # strip CR from output on non-win32 platforms (wine on Linux)
664     s/\r// if ($^O ne 'MSWin32');
665     print;
666   }
667   close(F);
668 }
669
670 if ($configurebuild && !$crosscompile) {
671   my $o;
672   if($runtestopts) {
673       $o = "TEST_F=\"$runtestopts\" ";
674   }
675   logit "$make -k ${o}test-full";
676   open(F, "$make -k ${o}test-full 2>&1 |") or die;
677   open(LOG, ">$buildlog") or die;
678   while (<F>) {
679     s/$pwd//g;
680     print;
681     print LOG;
682   }
683   close(F);
684   close(LOG);
685
686   if (grepfile("^TEST", $buildlog)) {
687     logit "tests were run";
688   } else {
689     mydie "test suite failure";
690   }
691
692   if (grepfile("^TESTFAIL:", $buildlog)) {
693     logit "the tests were not successful";
694   } else {
695     logit "the tests were successful!";
696   }
697 }
698 else {
699   if($crosscompile) {
700     # build test harness programs for selected cross-compiles
701     my $host_triplet = get_host_triplet();
702     if($host_triplet =~ /([^-]+)-([^-]+)-mingw(.*)/) {
703       chdir "$pwd/$build/tests";
704       logit_spaced "build test harness";
705       open(F, "$make -i 2>&1 |") or die;
706       open(LOG, ">$buildlog") or die;
707       while (<F>) {
708         s/$pwd//g;
709         print;
710         print LOG;
711       }
712       close(F);
713       close(LOG);
714       chdir "$pwd/$build";
715     }
716     logit_spaced "cross-compiling, can't run tests";
717   }
718   # dummy message to feign success
719   print "TESTDONE: 1 tests out of 0 (dummy message)\n";
720 }
721
722 # create a tarball if we got that option.
723 if (($mktarball ne '') && (-x $mktarball)) {
724   system($mktarball);
725 }
726
727 # mydie to cleanup
728 mydie "ending nicely";