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