Fixed qtmodule-configtests on Windows.
[profile/ivi/qtbase.git] / bin / qtmodule-configtests
1 #!/usr/bin/perl
2 #############################################################################
3 ##
4 ## Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
5 ## All rights reserved.
6 ## Contact: http://www.qt-project.org/
7 ##
8 ## This file is part of the build configuration tools of the Qt Toolkit.
9 ##
10 ## $QT_BEGIN_LICENSE:LGPL$
11 ## GNU Lesser General Public License Usage
12 ## This file may be used under the terms of the GNU Lesser General Public
13 ## License version 2.1 as published by the Free Software Foundation and
14 ## appearing in the file LICENSE.LGPL included in the packaging of this
15 ## file. Please review the following information to ensure the GNU Lesser
16 ## General Public License version 2.1 requirements will be met:
17 ## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ##
19 ## In addition, as a special exception, Nokia gives you certain additional
20 ## rights. These rights are described in the Nokia Qt LGPL Exception
21 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ##
23 ## GNU General Public License Usage
24 ## Alternatively, this file may be used under the terms of the GNU General
25 ## Public License version 3.0 as published by the Free Software Foundation
26 ## and appearing in the file LICENSE.GPL included in the packaging of this
27 ## file. Please review the following information to ensure the GNU General
28 ## Public License version 3.0 requirements will be met:
29 ## http://www.gnu.org/copyleft/gpl.html.
30 ##
31 ## Other Usage
32 ## Alternatively, this file may be used in accordance with the terms and
33 ## conditions contained in a signed written agreement between you and Nokia.
34 ##
35 ##
36 ##
37 ##
38 ##
39 ## $QT_END_LICENSE$
40 ##
41 #############################################################################
42
43 #
44 # Runs any module configuration tests
45 #
46 # Called (currently) from syncqt, and expects a few arguments
47 #
48 # configtests $basedir $out_basedir $qtbasedir $quietmode
49 #
50
51 use strict;
52 use warnings;
53
54 # use packages -------------------------------------------------------
55 use File::Basename;
56 use File::Path 'mkpath';
57 use File::Spec::Functions qw/ :ALL /;
58 use File::Temp qw/ :POSIX /;
59 use Cwd;
60 use Cwd 'abs_path';
61 use Config;
62
63 # Which file to look for the %configtests variable in
64 my $configTestSource = "sync.profile";
65
66 if ($#ARGV < 3) {
67     warn "Usage:\n";
68     warn "  $0 <module base directory> <module output directory> <QtBase directory> <generator spec>\n";
69     exit 1;
70 }
71
72 # These might be needed in sync.profile
73 our $basedir = $ARGV[0];
74 our $out_basedir = $ARGV[1];
75 our $qtbasedir = $ARGV[2];
76 my $generator = $ARGV[3];
77
78 our %configtests;
79
80 my $absOutDir = abs_path($out_basedir);
81 my $qmakeCachePath = catfile($absOutDir, '.qmake.cache');
82 my $configLogPath = catfile($absOutDir, 'config.log');
83
84 my $QMAKE = catfile($qtbasedir, "bin", ($^O =~ /win32/i) ? 'qmake.exe' : 'qmake');
85 if (!-x $QMAKE) {
86     # try the qmake from the path (e.g. this is a shadow build)
87     $QMAKE = 'qmake';
88 }
89
90 # Need to use the right make
91 # SYMBIAN_UNIX/MINGW should fall back to the non SYMBIAN ones
92 my $MAKE = 'make'; # default, only works on unix
93 if ($generator =~ /UNIX|XCODE/i) { # XCODE = make?
94     $MAKE = 'make';
95 } elsif ($generator =~ /MINGW/i) {
96     $MAKE = 'mingw32-make';
97 } elsif ($generator =~ /MSVC.NET|MSBUILD/i) {
98     $MAKE = 'nmake';
99 } else {
100     # Unhandled (at least): BMAKE, GBUILD, SYMBIAN_ABLD, SYMBIAN_SBSV2
101     warn "Unrecognized generator spec ($generator) - assuming '$MAKE'\n";
102 }
103
104 ######################################################################
105 # Syntax:  fileContents(filename)
106 # Params:  filename, string, filename of file to return contents
107 #
108 # Purpose: Get the contents of a file.
109 # Returns: String with contents of the file, or empty string if file
110 #          doens't exist.
111 # Warning: Dies if it does exist but script cannot get read access.
112 ######################################################################
113 sub fileContents {
114     my ($filename) = @_;
115     my $filecontents = "";
116     if (-e $filename) {
117         open(I, "< $filename") || die "Could not open $filename for reading, read block?";
118         local $/;
119         binmode I;
120         $filecontents = <I>;
121         close I;
122     }
123     return $filecontents;
124 }
125
126 ######################################################################
127 # Syntax:  loadConfigTests()
128 #
129 # Purpose: Loads the config tests from the source basedir into %configtests.
130 # Returns: Nothing
131 ######################################################################
132 sub loadConfigTests {
133     my $configprofile = catfile($basedir, $configTestSource);
134     my $result;
135     unless ($result = do $configprofile) {
136         die "configtests couldn't parse $configprofile: $@\n" if $@;
137         # We don't check for non null output, since that is valid
138     }
139 }
140
141 ######################################################################
142 # Syntax:  hashesAreDifferent
143 #
144 # Purpose: Compares two hashes. (must have same key=value for everything)
145 # Returns: 0 if they are the same, 1 otherwise
146 ######################################################################
147 sub hashesAreDifferent {
148     my %a = %{$_[0]};
149     my %b = %{$_[1]};
150
151     if (keys %a != keys %b) {
152         return 1;
153     }
154
155     my %cmp = map { $_ => 1 } keys %a;
156     for my $key (keys %b) {
157         last unless exists $cmp{$key};
158         last unless $a{$key} eq $b{$key};
159         delete $cmp{$key};
160     }
161     if (%cmp) {
162         return 1;
163     } else {
164         return 0;
165     }
166 }
167
168
169 ######################################################################
170 # Syntax:  executeLoggedCommand()
171 # Params:  path to executable, arguments
172 #
173 # This function is equivalent to system(), except that the command
174 # details and output is placed in the configure log (only).
175 #
176 # Purpose: run a command and log the output
177 # Returns: exit code and output.
178 ######################################################################
179 sub executeLoggedCommand {
180     my (@command_with_args) = @_;
181
182     # Redirect all stdout, stderr into the config.log
183     my ($save_stdout, $save_stderr);
184     open($save_stdout, '>&', STDOUT) || die "save STDOUT: $!";
185     open($save_stderr, '>&', STDERR) || die "save STDERR: $!";
186
187     my $tmpName = File::Temp::tempnam(File::Spec->tmpdir(), 'log');
188     open(STDOUT, '>', $tmpName) || die "open $tmpName: $!";
189     open(STDERR, '>&', STDOUT) || die "redirect STDERR to STDOUT: $!";
190
191     print "+ @command_with_args\n";
192     my $exitCode = system(@command_with_args) >> 8;
193
194     # Put them back.
195     close(STDOUT);
196     close(STDERR);
197     open(STDOUT, '>&', $save_stdout) || die "restoring STDOUT: $!";
198     open(STDERR, '>&', $save_stderr) || die "restoring STDERR: $!";
199
200     # Append output to config log and return it.
201     my ($tmpFile, $configLog);
202     my $out = '';
203     open($tmpFile, '<', $tmpName) || die "open $tmpName: $!";
204     open($configLog, '>>', $configLogPath) || die "open $configLogPath: $!";
205     while (my $line = <$tmpFile>) {
206         print $configLog $line;
207         $out .= $line;
208     }
209     close($tmpFile);
210     close($configLog);
211     unlink($tmpName);
212     return ($exitCode, $out);
213 }
214
215 ######################################################################
216 # Syntax:  executeTest()
217 # Params: testName
218 #
219 # The testName variable controls the actual config test run - the
220 # source is assumed to be in $basedir/config.tests/$testName, and
221 # when 'qmake; make clean; make' is run, is expected to produce a file
222 # $out_basedir/config.tests/$testName/$testName.  If this test passes,
223 # then 'config_test_$testName = yes' will be written to $out_basedir/.qmake.cache
224 #
225 # Purpose: Runs a configuration time test.
226 # Returns: 0 if the test fails, 1 if it passes, 2 if the test is skipped
227 #          (e.g. .pro file has requires(x) and x is not satisfied)
228 ######################################################################
229 sub executeTest {
230     my ($testName) = @_;
231
232     {
233         my $fh;
234         open($fh, '>>', $configLogPath) || die "open $configLogPath: $!";
235         print $fh 'executing config test "',$testName, "\":\n";
236         close($fh);
237     }
238
239     my $oldWorkingDir = getcwd();
240     my $ret = 0;
241
242     my @QMAKEARGS = ('CONFIG-=debug_and_release', 'CONFIG-=app_bundle');
243
244     my $testOutDir = catdir($absOutDir, 'config.tests', $testName);
245
246     # Since we might be cross compiling, look for barename (Linux) and .exe (Win32/Symbian)
247     my $testOutFile1 = catfile($testOutDir, "$testName.exe");
248     my $testOutFile2 = catfile($testOutDir, $testName);
249
250     if (abs_path($basedir) eq abs_path($out_basedir)) {
251         chdir $testOutDir or die "\nUnable to change to config test directory ($testOutDir): $!\n";
252     } else { # shadow build
253         if (! -e $testOutDir) {
254             mkpath $testOutDir or die "\nUnable to create shadow build config test directory ($testOutDir): $!\n";
255         }
256         chdir $testOutDir or die "\nUnable to change to config test directory ($testOutDir): $!\n";
257
258         push (@QMAKEARGS, catdir($basedir, 'config.tests', $testName));
259     }
260
261     # First remove existing stuff (XXX this probably needs generator specific code, but hopefully
262     # the target removal below will suffice)
263     if (-e "Makefile") {
264         executeLoggedCommand($MAKE, 'distclean');
265     }
266
267     # and any targets that we might find that weren't distcleaned
268     unlink $testOutFile1, $testOutFile2;
269
270     # Run qmake && make
271     executeLoggedCommand($QMAKE, @QMAKEARGS);
272     my ($makeExitCode, $makeOutput) = executeLoggedCommand($MAKE);
273
274     # If make prints "blah blah blah\nSkipped." we consider this a skipped test
275     if ($makeOutput !~ qr(^Skipped\.$)ms) {
276         # Check the test exists (can't reliably execute, especially for cross compilation)
277         if (-e $testOutFile1 or -e $testOutFile2) {
278             $ret = 1;
279         }
280     } else {
281         $ret = 2;
282     }
283
284     my $fh;
285     open($fh, '>>', $configLogPath) || die "open $configLogPath: $!";
286     print $fh 'config test "',$testName, '" completed with result ',$ret, "\n";
287     close($fh);
288
289     chdir $oldWorkingDir or die "\nUnable to restore working directory: $!\n";
290     return $ret;
291 }
292
293 # Remove existing config.log
294 if (-e $configLogPath) {
295     unlink($configLogPath) || die "unlink $configLogPath: $!";
296 }
297
298 # Now run configuration tests
299 # %configtests is a map from config test name to a map of parameters
300 # e.g:
301 #
302 # %configtests = (
303 #    "simple" => {fatal => 1, message => "Missing required 'simple' component\n"},
304 #    "failed" => {message => "You need to install the FAILED sdk for this to work\n"}
305 # );
306 #
307 # Parameters and their defaults:
308 #  - fatal [false] - whether failing this test should abort everything
309 #  - message [""] - A special message to display if this test fails
310 #
311 loadConfigTests();
312
313 # Only do this step for modules that have config tests
314 # (qtbase doesn't). We try to preserve existing contents (and furthermore
315 # only write to .qmake.cache if the tests change)
316 if (abs_path($out_basedir) ne abs_path($qtbasedir)) {
317     # Read any existing content
318     my $existingContents = fileContents($qmakeCachePath);
319     my %oldTestResults;
320     my %newTestResults;
321     my @fatalTestsEncountered;
322
323     # Parse the existing results so we can check if we change them
324     while ($existingContents =~ /^config_test_(.*) = (yes|no)$/gm) {
325         $oldTestResults{$1} = $2;
326     }
327
328     # Get the longest length test name so we can pretty print
329     use List::Util qw(max);
330     my $maxNameLength = max map { length $_ } keys %configtests;
331
332     # Turn off buffering
333     $| = 1;
334
335     # Remove existing config.log
336     if (-e $configLogPath) {
337         unlink($configLogPath) || die "unlink $configLogPath: $!";
338     }
339
340     # Now run the configuration tests
341     print "Configuration tests:\n" if (%configtests);
342
343     while ((my $testName, my $testParameters) = each %configtests) {
344         printf "  % *s: ", $maxNameLength, $testName; # right aligned, yes/no lines up
345
346         my $fatalTest = $testParameters->{"fatal"};
347         my $message = $testParameters->{"message"};
348
349         my $testResult = executeTest($testName);
350         my @testResultStrings = ("no\n","yes\n","skipped\n");
351
352         $newTestResults{$testName} = (($testResult == 1) ? "yes" : "no"); # skipped = no
353
354         if ($testResult == 0) {
355             # Failed test
356             if ($fatalTest) {
357                 print "no (fatal)\n";
358                 # Report the fatality at the end, too
359                 push (@fatalTestsEncountered, $testName);
360             } else {
361                 print "no\n";
362             }
363             if (defined($message)) {
364                 print $message;
365                 print "\n" unless chop $message eq "\n";
366             }
367         } else {
368             # yes or skipped
369             print $testResultStrings[$testResult];
370         }
371     }
372
373     # Check if the test results are different
374     if (hashesAreDifferent(\%oldTestResults, \%newTestResults)) {
375         # Generate the new contents
376         my $newContents = $existingContents;
377
378         # Strip out any existing config test results
379         $newContents =~ s/^config_test_[^\$]*$//gm;
380         $newContents =~ s/^# Compile time test results[^\$]*$//gm;
381
382         # Add any remaining content and make sure we start on a new line
383         if ($newContents and chop $newContents ne '\n') {
384             $newContents = $newContents . "\n";
385         }
386
387         # Results and header
388         if (%newTestResults) {
389             $newContents = $newContents . '# Compile time test results ('.(localtime).")\n";
390
391             # Results
392             while ((my $testName, my $testResult) = each %newTestResults) {
393                 $newContents = $newContents . "config_test_$testName = $testResult\n";
394             }
395         }
396
397         # Remove blank lines
398         $newContents =~ s/^[\s]*$//gms;
399
400         # and open the file
401         open my $cacheFileHandle, ">$qmakeCachePath" or die "Unable to open $qmakeCachePath for writing: $!\n";
402
403         print $cacheFileHandle $newContents;
404
405         close $cacheFileHandle or die "Unable to close $qmakeCachePath: $!\n";
406     }
407
408     # Now see if we have to die
409     if (@fatalTestsEncountered) {
410         if ($#fatalTestsEncountered == 0) {
411             warn "Mandatory configuration test (".$fatalTestsEncountered[0].") failed.\n\n";
412         } else {
413             warn "Mandatory configuration tests (". join (", ", @fatalTestsEncountered) . ") failed.\n\n";
414         }
415         exit -1;
416     }
417 }
418
419 exit 0;