Several corrections & changes to what has been stated in revision 1.45
[platform/upstream/curl.git] / tests / testcurl.pl
1 #!/usr/bin/env perl
2 #***************************************************************************
3 #                                  _   _ ____  _
4 #  Project                     ___| | | |  _ \| |
5 #                             / __| | | | |_) | |
6 #                            | (__| |_| |  _ <| |___
7 #                             \___|\___/|_| \_\_____|
8 #
9 # Copyright (C) 1998 - 2007, 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 # $Id$
23 ###########################################################################
24
25 ###########################
26 #  What is This Script?
27 ###########################
28
29 # testcurl.pl is the master script to use for automatic testing of CVS-curl.
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 # --mktarball=[command]    Command to run after completed test
47 # --name=[name]            Set name to report as
48 # --nocvsup                Don't update from CVS even though it is a CVS tree
49 # --nobuildconf            Don't run buildconf
50 # --runtestopts=[options]  Options to pass to runtests.pl
51 # --setup=[file name]      File name to read setup from (deprecated)
52 # --target=[your os]       Specify your target environment.
53 #
54 # if [curl-daily-name] is omitted, a 'curl' CVS directory is assumed.
55 #
56
57 use strict;
58
59 use Cwd;
60
61 # Turn on warnings (equivalent to -w, which can't be used with /usr/bin/env)
62 #BEGIN { $^W = 1; }
63
64 use vars qw($version $fixed $infixed $CURLDIR $CVS $pwd $build $buildlog
65             $buildlogname $configurebuild $targetos $confsuffix $binext
66             $libext);
67 use vars qw($name $email $desc $confopts $runtestopts $setupfile $mktarball
68             $nocvsup $nobuildconf $crosscompile $timestamp);
69
70 # version of this script
71 $version='$Revision$';
72 $fixed=0;
73
74 # Determine if we're running from CVS or a canned copy of curl,
75 # or if we got a specific target option or setup file option.
76 $CURLDIR="curl";
77 $CVS=1;
78 $setupfile = 'setup';
79 while ($ARGV[0]) {
80   if ($ARGV[0] =~ /--target=/) {
81     $targetos = (split(/=/, shift @ARGV))[1];
82   }
83   elsif ($ARGV[0] =~ /--setup=/) {
84     $setupfile = (split(/=/, shift @ARGV))[1];
85   }
86   elsif ($ARGV[0] =~ /--mktarball=/) {
87     $mktarball = (split(/=/, shift @ARGV))[1];
88   }
89   elsif ($ARGV[0] =~ /--name=/) {
90     $name = (split(/=/, shift @ARGV))[1];
91   }
92   elsif ($ARGV[0] =~ /--email=/) {
93     $email = (split(/=/, shift @ARGV))[1];
94   }
95   elsif ($ARGV[0] =~ /--desc=/) {
96     $desc = (split(/=/, shift @ARGV))[1];
97   }
98   elsif ($ARGV[0] =~ /--configure=/) {
99     $confopts = (split(/=/, shift @ARGV))[1];
100   }
101   elsif ($ARGV[0] =~ /--nocvsup/) {
102     $nocvsup=1;
103     shift @ARGV;
104   }
105   elsif ($ARGV[0] =~ /--nobuildconf/) {
106     $nobuildconf=1;
107     shift @ARGV;
108   }
109   elsif ($ARGV[0] =~ /--crosscompile/) {
110     $crosscompile=1;
111     shift @ARGV;
112   }
113   elsif ($ARGV[0] =~ /--runtestopts=/) {
114     $runtestopts = (split(/=/, shift @ARGV, 2))[1];
115   }
116   else {
117     $CURLDIR=shift @ARGV;
118     $CVS=0;
119   }
120 }
121
122 # Do the platform-specific stuff here
123 $configurebuild = 1;
124 $confsuffix = '';
125 $binext = '';
126 $libext = '.la'; # .la since both libcurl and libcares are made with libtool
127 if ($^O eq 'MSWin32' || $targetos) {
128   if (!$targetos) {
129     # If no target defined on Win32 lets assume vc
130     $targetos = 'vc';
131   }
132   if ($targetos =~ /vc/ || $targetos =~ /borland/) {
133     $binext = '.exe';
134     $libext = '.lib';
135   }
136   elsif ($targetos =~ /mingw32/) {
137     $binext = '.exe';
138     if ($^O eq 'MSWin32') {
139       $libext = '.a';
140     }
141   }
142   elsif ($targetos =~ /netware/) {
143     $configurebuild = 0;
144     $binext = '.nlm';
145     $libext = '.lib';
146   }
147 }
148
149 if (($^O eq 'MSWin32') &&
150     ($targetos =~ /vc/ || $targetos =~ /mingw32/ || $targetos =~ /borland/)) {
151
152   # Set these things only when building ON Windows and for Win32 platform.
153   # FOR Windows since we might be cross-compiling on another system. Non-
154   # Windows builds still default to configure-style builds with no confsuffix.
155
156   $configurebuild = 0;
157   $confsuffix = '-win32';
158 }
159
160
161 $ENV{LANG}="C";
162
163 sub rmtree($) {
164     my $target = $_[0];
165     if ($^O eq 'MSWin32') {
166       foreach (glob($target)) {
167         s:/:\\:g;
168         system("rd /s /q $_");
169       }
170     } else {
171       system("rm -rf $target");
172     }
173 }
174
175 sub grepfile($$) {
176     my ($target, $fn) = @_;
177     open(F, $fn) or die;
178     while (<F>) {
179       if (/$target/) {
180         close(F);
181         return 1;
182       }
183     }
184     close(F);
185     return 0;
186 }
187
188 sub logit($) {
189     my $text=$_[0];
190     if ($text) {
191       print "testcurl: $text\n";
192     }
193 }
194
195 sub mydie($){
196     my $text=$_[0];
197     logit "$text";
198     chdir $pwd; # cd back to the original root dir
199
200     if ($pwd && $build) {
201       # we have a build directory name, remove the dir
202       logit "removing the $build dir";
203       rmtree "$pwd/$build";
204     }
205     if (-r $buildlog) {
206       # we have a build log output file left, remove it
207       logit "removing the $buildlogname file";
208       unlink "$buildlog";
209     }
210     logit "ENDING HERE"; # last line logged!
211     exit 1;
212 }
213
214 if (open(F, "$setupfile")) {
215   while (<F>) {
216     if (/(\w+)=(.*)/) {
217       eval "\$$1=$2;";
218     }
219   }
220   close(F);
221   $infixed=$fixed;
222 } else {
223   $infixed=0;    # so that "additional args to configure" works properly first time...
224 }
225
226 if (!$name) {
227   print "please enter your name\n";
228   $name = <>;
229   chomp $name;
230   $fixed=1;
231 }
232
233 if (!$email) {
234   print "please enter your contact email address\n";
235   $email = <>;
236   chomp $email;
237   $fixed=2;
238 }
239
240 if (!$desc) {
241   print "please enter a one line system description\n";
242   $desc = <>;
243   chomp $desc;
244   $fixed=3;
245 }
246
247 if (!$confopts) {
248   if ($infixed < 4) {
249     print "please enter your additional arguments to configure\n";
250     print "examples: --with-ssl --enable-debug --enable-ipv6 --with-krb4\n";
251     $confopts = <>;
252     chomp $confopts;
253   }
254 }
255
256
257 if ($fixed < 4) {
258     $fixed=4;
259     open(F, ">$setupfile") or die;
260     print F "name='$name'\n";
261     print F "email='$email'\n";
262     print F "desc='$desc'\n";
263     print F "confopts='$confopts'\n";
264     print F "fixed='$fixed'\n";
265     close(F);
266 }
267
268 # Set timestamp to the UTC this script is running. Its value might
269 # be changed later in the script to the value present in curlver.h
270 $timestamp = scalar(gmtime)." UTC";
271
272 logit "STARTING HERE"; # first line logged, for scripts to trigger on
273 logit "NAME = $name";
274 logit "EMAIL = $email";
275 logit "DESC = $desc";
276 logit "CONFOPTS = $confopts";
277 logit "CPPFLAGS = ".$ENV{CPPFLAGS};
278 logit "CFLAGS = ".$ENV{CFLAGS};
279 logit "LDFLAGS = ".$ENV{LDFLAGS};
280 logit "CC = ".$ENV{CC};
281 logit "target = ".$targetos;
282 logit "version = $version"; # script version
283 logit "date = $timestamp";  # When the test build starts
284
285 # Make $pwd to become the path without newline. We'll use that in order to cut
286 # off that path from all possible logs and error messages etc.
287 $pwd = cwd();
288
289 if (-d $CURLDIR) {
290   if ($CVS && -d "$CURLDIR/CVS") {
291     logit "$CURLDIR is verified to be a fine source dir";
292     # remove the generated sources to force them to be re-generated each
293     # time we run this test
294     unlink "$CURLDIR/src/hugehelp.c";
295   } elsif (!$CVS && -f "$CURLDIR/tests/testcurl.pl") {
296     logit "$CURLDIR is verified to be a fine daily source dir"
297   } else {
298     mydie "$CURLDIR is not a daily source dir or checked out from CVS!"
299   }
300 }
301 $build="build-$$";
302 $buildlogname="buildlog-$$";
303 $buildlog="$pwd/$buildlogname";
304
305 # remove any previous left-overs
306 rmtree "build-*";
307 rmtree "buildlog-*";
308
309 # this is to remove old build logs that ended up in the wrong dir
310 foreach (glob("$CURLDIR/buildlog-*")) { unlink $_; }
311
312 # create a dir to build in
313 mkdir $build, 0777;
314
315 if (-d $build) {
316   logit "build dir $build was created fine";
317 } else {
318   mydie "failed to create dir $build";
319 }
320
321 # get in the curl source tree root
322 chdir $CURLDIR;
323
324 # Do the CVS thing, or not...
325 if ($CVS) {
326
327   # this is a temporary fix to make things work again, remove later
328   logit "remove ares/aclocal.m4";
329   unlink "ares/aclocal.m4";
330
331   logit "update from CVS";
332   my $cvsstat;
333
334   sub cvsup() {
335     # update quietly to the latest CVS
336     logit "run cvs up";
337     if($nocvsup) {
338         logit "Skipping CVS update (--nocvsup)";
339         return 1;
340     }
341     else {
342         system("cvs -Q up -dP 2>&1");
343     }
344
345     $cvsstat=$?;
346
347     # return !RETURNVALUE so that errors return 0 while goodness
348     # returns 1
349     return !$cvsstat;
350   }
351
352   my $att=0;
353   while (!cvsup()) {
354     $att++;
355     logit "failed CVS update attempt number $att.";
356     if ($att > 10) {
357       $cvsstat=111;
358       last; # get out of the loop
359     }
360     sleep 5;
361   }
362
363   if ($cvsstat != 0) {
364     mydie "failed to update from CVS ($cvsstat), exiting";
365   }
366   elsif (!$nocvsup) {
367     # Set timestamp to the UTC the CVS update took place.
368     $timestamp = scalar(gmtime)." UTC";
369   }
370
371   if($nobuildconf) {
372       logit "told to not run buildconf";
373   }
374   elsif ($configurebuild) {
375     # remove possible left-overs from the past
376     unlink "configure";
377     unlink "autom4te.cache";
378
379     # generate the build files
380     logit "invoke buildconf, but filter off the silly aclocal warnings";
381     open(F, "./buildconf 2>&1 |") or die;
382     open(LOG, ">$buildlog") or die;
383     while (<F>) {
384       next if /warning: underquoted definition of/;
385       print;
386       print LOG;
387     }
388     close(F);
389     close(LOG);
390
391     if (grepfile("^buildconf: OK", $buildlog)) {
392       logit "buildconf was successful";
393     }
394     else {
395       mydie "buildconf was NOT successful";
396     }
397   }
398   else {
399     logit "buildconf was successful (dummy message)";
400   }
401 }
402
403 # Set timestamp to the one in curlver.h if this isn't a CVS test build.
404 if ((-f "$CURLDIR/include/curl/curlver.h") &&
405     (grepfile("define LIBCURL_TIMESTAMP",
406               "$CURLDIR/include/curl/curlver.h")) &&
407     (open(F, "<$CURLDIR/include/curl/curlver.h"))) {
408   while (<F>) {
409     chomp;
410     if ($_ =~ /^\#define\s+LIBCURL_TIMESTAMP\s+\"(.+)\".*$/) {
411       my $stampstring = $1;
412       if ($stampstring !~ /CVS/) {
413           $stampstring =~ s/\s+UTC//;
414           $timestamp = $stampstring." UTC";
415       }
416       last;
417     }
418   }
419   close(F);
420 }
421
422 # Show timestamp we are using for this test build.
423 logit "timestamp = $timestamp";
424
425 if ($configurebuild) {
426   if (-f "configure") {
427     logit "configure created (at least it exists)";
428   } else {
429     mydie "no configure created/found";
430   }
431 } else {
432   logit "configure created (dummy message)"; # dummy message to feign success
433 }
434
435 sub findinpath {
436   my $c;
437   my $e;
438   my $x='';
439   $x='.exe' if ($^O eq 'MSWin32');
440   my $s=':';
441   $s=';' if ($^O eq 'MSWin32');
442   my $p=$ENV{'PATH'};
443   my @pa = split($s, $p);
444   for $c (@_) {
445     for $e (@pa) {
446       if( -x "$e/$c$x") {
447         return $c;
448       }
449     }
450   }
451 }
452
453 my $make = findinpath("gmake", "make", "nmake");
454 if(!$make) {
455     mydie "Couldn't find make in the PATH";
456 }
457 logit "going with $make as make";
458
459 # change to build dir
460 chdir "$pwd/$build";
461
462 if ($configurebuild) {
463   # run configure script
464   print `../$CURLDIR/configure $confopts 2>&1`;
465
466   if (-f "lib/Makefile") {
467     logit "configure seems to have finished fine";
468   } else {
469     mydie "configure didn't work";
470   }
471 } else {
472   if (($^O eq 'MSWin32') && ($targetos !~ /netware/)) {
473     system("xcopy /s /q ..\\$CURLDIR .");
474     system("buildconf.bat");
475   }
476   elsif (($^O eq 'linux') || ($targetos =~ /netware/)) {
477     system("cp -afr ../$CURLDIR/* ."); 
478     system("cp -af ../$CURLDIR/Makefile.dist Makefile"); 
479     system("$make -i -C lib -f Makefile.$targetos prebuild");
480     system("$make -i -C src -f Makefile.$targetos prebuild");
481   }
482 }
483
484 logit "display lib/config$confsuffix.h";
485 open(F, "lib/config$confsuffix.h") or die "lib/config$confsuffix.h: $!";
486 while (<F>) {
487   print if /^ *#/;
488 }
489 close(F);
490
491 if (grepfile("define USE_ARES", "lib/config$confsuffix.h")) {
492   logit "setup to build ares";
493
494   logit "display ares/config$confsuffix.h";
495   if(open(F, "ares/config$confsuffix.h")) {
496       while (<F>) {
497           print if /^ *#/;
498       }
499       close(F);
500   }
501
502   logit "build ares";
503   chdir "ares";
504
505   if ($targetos && !$configurebuild) {
506       logit "$make -f Makefile.$targetos";
507       open(F, "$make -f Makefile.$targetos 2>&1 |") or die;
508   }
509   else {
510       logit "$make";
511       open(F, "$make 2>&1 |") or die;
512   }
513   while (<F>) {
514     s/$pwd//g;
515     print;
516   }
517   close(F);
518
519   if (-f "libcares$libext") {
520     logit "ares is now built successfully (libcares$libext)";
521   } else {
522     logit "ares build failed (libares$libext)";
523   }
524
525   # cd back to the curl build dir
526   chdir "$pwd/$build";
527 }
528
529 if ($configurebuild) {
530   logit "$make -i";
531   open(F, "$make -i 2>&1 |") or die;
532   while (<F>) {
533     s/$pwd//g;
534     print;
535   }
536   close(F);
537 }
538 else {
539   logit "$make -i $targetos";
540   if ($^O eq 'MSWin32') {
541     if ($targetos =~ /vc/) {
542       open(F, "nmake -i $targetos|") or die;
543     }
544     else {
545       open(F, "$make -i $targetos |") or die;
546     }
547   }
548   else {
549     open(F, "$make -i $targetos 2>&1 |") or die;
550   }
551   while (<F>) {
552     s/$pwd//g;
553     print;
554   }
555   close(F);
556 }
557
558 if (-f "lib/libcurl$libext") {
559   logit "libcurl was created fine (libcurl$libext)";
560 }
561 else {
562   mydie "libcurl was not created (libcurl$libext)";
563 }
564
565 if (-f "src/curl$binext") {
566   logit "curl was created fine (curl$binext)";
567 }
568 else {
569   mydie "curl was not created (curl$binext)";
570 }
571
572 if ($targetos =~ /netware/) {
573   if (-f '../../curlver') {
574     system('../../curlver');
575   }
576 }
577 elsif(!$crosscompile) {
578   logit "display curl$binext --version output";
579   open(F, "./src/curl$binext --version|");
580   while(<F>) {
581       print;
582       print LOG;
583   }
584   close(F);
585 }
586
587 if ($configurebuild && !$crosscompile) {
588   my $o;
589   if($runtestopts) {
590       $o = "TEST_F=\"$runtestopts\" ";
591   }
592   logit "$make -k ${o}test-full";
593   open(F, "$make -k ${o}test-full 2>&1 |") or die;
594   open(LOG, ">$buildlog") or die;
595   while (<F>) {
596     s/$pwd//g;
597     print;
598     print LOG;
599   }
600   close(F);
601   close(LOG);
602
603   if (grepfile("^TEST", $buildlog)) {
604     logit "tests were run";
605   } else {
606     mydie "test suite failure";
607   }
608
609   if (grepfile("^TESTFAIL:", $buildlog)) {
610     logit "the tests were not successful";
611   } else {
612     logit "the tests were successful!";
613   }
614 } else {
615   # dummy message to feign success
616   if($crosscompile) {
617     logit "cross-compiling, can't run tests";
618   }
619   print "TESTDONE: 1 tests out of 0 (dummy message)\n";
620 }
621
622 # create a tarball if we got that option.
623 if (($mktarball ne '') && (-f $mktarball)) {
624   system($mktarball);
625 }
626
627 # mydie to cleanup
628 mydie "ending nicely";