2 #############################################################################
4 ## Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
5 ## Contact: http://www.qt-project.org/
7 ## This file is part of the build configuration tools of the Qt Toolkit.
9 ## $QT_BEGIN_LICENSE:LGPL$
10 ## GNU Lesser General Public License Usage
11 ## This file may be used under the terms of the GNU Lesser General Public
12 ## License version 2.1 as published by the Free Software Foundation and
13 ## appearing in the file LICENSE.LGPL included in the packaging of this
14 ## file. Please review the following information to ensure the GNU Lesser
15 ## General Public License version 2.1 requirements will be met:
16 ## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ## In addition, as a special exception, Nokia gives you certain additional
19 ## rights. These rights are described in the Nokia Qt LGPL Exception
20 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ## GNU General Public License Usage
23 ## Alternatively, this file may be used under the terms of the GNU General
24 ## Public License version 3.0 as published by the Free Software Foundation
25 ## and appearing in the file LICENSE.GPL included in the packaging of this
26 ## file. Please review the following information to ensure the GNU General
27 ## Public License version 3.0 requirements will be met:
28 ## http://www.gnu.org/copyleft/gpl.html.
31 ## Alternatively, this file may be used in accordance with the terms and
32 ## conditions contained in a signed written agreement between you and Nokia.
41 #############################################################################
44 # Runs any module configuration tests
46 # Called (currently) from syncqt, and expects a few arguments
48 # configtests $basedir $out_basedir $qtbasedir $quietmode
54 # use packages -------------------------------------------------------
56 use File::Path 'mkpath';
57 use File::Spec::Functions qw/ :ALL /;
58 use File::Temp qw/ :POSIX /;
63 # Which file to look for the %configtests variable in
64 my $configTestSource = "sync.profile";
68 warn " $0 <module base directory> <module output directory> <QtBase directory> <generator spec>\n";
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];
80 my $absOutDir = abs_path($out_basedir);
81 my $qmakeCachePath = catfile($absOutDir, '.qmake.cache');
82 my $configLogPath = catfile($absOutDir, 'config.log');
84 my $QMAKE = catfile($qtbasedir, "bin", ($^O =~ /win32/i) ? 'qmake.exe' : 'qmake');
86 # try the qmake from the path (e.g. this is a shadow build)
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?
95 } elsif ($generator =~ /MINGW/i) {
96 $MAKE = 'mingw32-make';
97 } elsif ($generator =~ /MSVC.NET|MSBUILD/i) {
100 # Unhandled (at least): BMAKE, GBUILD, SYMBIAN_ABLD, SYMBIAN_SBSV2
101 warn "Unrecognized generator spec ($generator) - assuming '$MAKE'\n";
104 ######################################################################
105 # Syntax: fileContents(filename)
106 # Params: filename, string, filename of file to return contents
108 # Purpose: Get the contents of a file.
109 # Returns: String with contents of the file, or empty string if file
111 # Warning: Dies if it does exist but script cannot get read access.
112 ######################################################################
115 my $filecontents = "";
117 open(I, "< $filename") || die "Could not open $filename for reading, read block?";
123 return $filecontents;
126 ######################################################################
127 # Syntax: loadConfigTests()
129 # Purpose: Loads the config tests from the source basedir into %configtests.
131 ######################################################################
132 sub loadConfigTests {
133 my $configprofile = catfile($basedir, $configTestSource);
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
141 ######################################################################
142 # Syntax: hashesAreDifferent
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 {
151 if (keys %a != keys %b) {
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};
169 ######################################################################
170 # Syntax: executeLoggedCommand()
171 # Params: path to executable, arguments
173 # This function is equivalent to system(), except that the command
174 # details and output is placed in the configure log (only).
176 # Purpose: run a command and log the output
177 # Returns: exit code and output.
178 ######################################################################
179 sub executeLoggedCommand {
180 my (@command_with_args) = @_;
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: $!";
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: $!";
191 print "+ @command_with_args\n";
192 my $exitCode = system(@command_with_args) >> 8;
197 open(STDOUT, '>&', $save_stdout) || die "restoring STDOUT: $!";
198 open(STDERR, '>&', $save_stderr) || die "restoring STDERR: $!";
200 # Append output to config log and return it.
201 my ($tmpFile, $configLog);
203 open($tmpFile, '<', $tmpName) || die "open $tmpName: $!";
204 open($configLog, '>>', $configLogPath) || die "open $configLogPath: $!";
205 while (my $line = <$tmpFile>) {
206 print $configLog $line;
212 return ($exitCode, $out);
215 ######################################################################
216 # Syntax: executeTest()
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
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 ######################################################################
234 open($fh, '>>', $configLogPath) || die "open $configLogPath: $!";
235 print $fh 'executing config test "',$testName, "\":\n";
239 my $oldWorkingDir = getcwd();
242 my @QMAKEARGS = ('CONFIG-=debug_and_release', 'CONFIG-=app_bundle');
244 my $testOutDir = catdir($absOutDir, 'config.tests', $testName);
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);
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";
256 chdir $testOutDir or die "\nUnable to change to config test directory ($testOutDir): $!\n";
258 push (@QMAKEARGS, catdir($basedir, 'config.tests', $testName));
261 # First remove existing stuff (XXX this probably needs generator specific code, but hopefully
262 # the target removal below will suffice)
264 executeLoggedCommand($MAKE, 'distclean');
267 # and any targets that we might find that weren't distcleaned
268 unlink $testOutFile1, $testOutFile2;
271 my ($qmakeExitCode, $qmakeOutput) = executeLoggedCommand($QMAKE, @QMAKEARGS);
272 if ($qmakeExitCode == 0) {
273 my ($makeExitCode, $makeOutput) = executeLoggedCommand($MAKE);
275 # If make prints "blah blah blah\nSkipped." we consider this a skipped test
276 if ($makeOutput !~ qr(^Skipped\.$)ms) {
277 # Check the test exists (can't reliably execute, especially for cross compilation)
278 if ($makeExitCode == 0 and (-e $testOutFile1 or -e $testOutFile2)) {
287 open($fh, '>>', $configLogPath) || die "open $configLogPath: $!";
288 print $fh 'config test "',$testName, '" completed with result ',$ret, "\n";
291 chdir $oldWorkingDir or die "\nUnable to restore working directory: $!\n";
295 # Remove existing config.log
296 if (-e $configLogPath) {
297 unlink($configLogPath) || die "unlink $configLogPath: $!";
300 # Now run configuration tests
301 # %configtests is a map from config test name to a map of parameters
305 # "simple" => {fatal => 1, message => "Missing required 'simple' component\n"},
306 # "failed" => {message => "You need to install the FAILED sdk for this to work\n"}
309 # Parameters and their defaults:
310 # - fatal [false] - whether failing this test should abort everything
311 # - message [""] - A special message to display if this test fails
315 # Only do this step for modules that have config tests
316 # (qtbase doesn't). We try to preserve existing contents (and furthermore
317 # only write to .qmake.cache if the tests change)
318 if (abs_path($out_basedir) ne abs_path($qtbasedir)) {
319 # Read any existing content
320 my $existingContents = fileContents($qmakeCachePath);
323 my @fatalTestsEncountered;
325 # Parse the existing results so we can check if we change them
326 while ($existingContents =~ /^config_test_(.*) = (yes|no)$/gm) {
327 $oldTestResults{$1} = $2;
330 # Get the longest length test name so we can pretty print
331 use List::Util qw(max);
332 my $maxNameLength = max map { length $_ } keys %configtests;
337 # Remove existing config.log
338 if (-e $configLogPath) {
339 unlink($configLogPath) || die "unlink $configLogPath: $!";
342 # Now run the configuration tests
343 print "Configuration tests:\n" if (%configtests);
345 while ((my $testName, my $testParameters) = each %configtests) {
346 printf " % *s: ", $maxNameLength, $testName; # right aligned, yes/no lines up
348 my $fatalTest = $testParameters->{"fatal"};
349 my $message = $testParameters->{"message"};
351 my $testResult = executeTest($testName);
352 my @testResultStrings = ("no\n","yes\n","skipped\n");
354 $newTestResults{$testName} = (($testResult == 1) ? "yes" : "no"); # skipped = no
356 if ($testResult == 0) {
359 print "no (fatal)\n";
360 # Report the fatality at the end, too
361 push (@fatalTestsEncountered, $testName);
365 if (defined($message)) {
367 print "\n" unless chop $message eq "\n";
371 print $testResultStrings[$testResult];
375 # Check if the test results are different
376 if (hashesAreDifferent(\%oldTestResults, \%newTestResults)) {
377 # Generate the new contents
378 my $newContents = $existingContents;
380 # Strip out any existing config test results
381 $newContents =~ s/^config_test_[^\$]*$//gm;
382 $newContents =~ s/^# Compile time test results[^\$]*$//gm;
384 # Add any remaining content and make sure we start on a new line
385 if ($newContents and chop $newContents ne '\n') {
386 $newContents = $newContents . "\n";
390 if (%newTestResults) {
391 $newContents = $newContents . '# Compile time test results ('.(localtime).")\n";
394 while ((my $testName, my $testResult) = each %newTestResults) {
395 $newContents = $newContents . "config_test_$testName = $testResult\n";
400 $newContents =~ s/^[\s]*$//gms;
403 open my $cacheFileHandle, ">$qmakeCachePath" or die "Unable to open $qmakeCachePath for writing: $!\n";
405 print $cacheFileHandle $newContents;
407 close $cacheFileHandle or die "Unable to close $qmakeCachePath: $!\n";
410 # Now see if we have to die
411 if (@fatalTestsEncountered) {
412 if ($#fatalTestsEncountered == 0) {
413 warn "Mandatory configuration test (".$fatalTestsEncountered[0].") failed.\n\n";
415 warn "Mandatory configuration tests (". join (", ", @fatalTestsEncountered) . ") failed.\n\n";